Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS 

Cloning a node with REXML

This code recursively copies the contents of the XML node to the new node, rather than performing a shallow element copy (e.g. node_copy = doc.root.clone).

   1  node_copy = Document.new(doc.root.to_s)


Reference: rexml: Ruby Standard Library Documentation [ruby-doc.org]

MYSQL - column copy with string replacement

MySql snippet to copy a column in a table to another column, with some string manipulation.
Create new_col before executing this segment.

   1  
   2  UPDATE table SET table.new_col=REPLACE(table.old_col,'str2replace','replace_with_str');

Copy Public Key To Host In One Line

   1  
   2  ssh username@host "echo `cat ~/.ssh/id_dsa.pub` >> ~/.ssh/authorized_keys"

Java filecopy using NIO

   1  
   2      public static void fileCopy( File in, File out )
   3              throws IOException
   4      {
   5          FileChannel inChannel = new FileInputStream( in ).getChannel();
   6          FileChannel outChannel = new FileOutputStream( out ).getChannel();
   7          try
   8          {
   9  //          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows
  10  
  11              // magic number for Windows, 64Mb - 32Kb)
  12              int maxCount = (64 * 1024 * 1024) - (32 * 1024);
  13              long size = inChannel.size();
  14              long position = 0;
  15              while ( position < size )
  16              {
  17                 position += inChannel.transferTo( position, maxCount, outChannel );
  18              }
  19          }
  20          finally
  21          {
  22              if ( inChannel != null )
  23              {
  24                 inChannel.close();
  25              }
  26              if ( outChannel != null )
  27              {
  28                  outChannel.close();
  29              }
  30          }
  31      }

Simple S3 utils - copy bucket to bucket

Some simple S3 utils in Ruby

Specifically written to copy one bucket to another (for testing on production on staging)

PLEASE BE CAREFUL WITH THIS - THERE IS CODE THAT DELETES ALL CONTENTS OF A BUCKET

Example:
   1  
   2  a = AmazoneS3Asset.new
   3  a.copy_over_bucket("myapp_production", "myapp_production")


   1  
   2  require 'aws/s3'
   3  require 'mechanize'
   4  
   5  class AmazonS3Asset
   6    
   7    include AWS::S3
   8    S3ID = "your s3 id"
   9    S3KEY = "your s3 key"
  10    
  11    def initialize
  12      puts "connecting..."
  13      AWS::S3::Base.establish_connection!(
  14        :access_key_id     => S3ID,
  15        :secret_access_key => S3KEY
  16      )
  17    end
  18  
  19    def delete_key(bucket, key)
  20      if exists?(bucket, key) 
  21        S3Object.delete key, bucket
  22      end
  23    end
  24    
  25    def empty_bucket(bucket)
  26      bucket_keys(bucket).each do |k|
  27        puts "deleting #{k}"
  28        delete_key(bucket,k)
  29      end
  30    end
  31    
  32    def bucket_keys(bucket)
  33      b = Bucket.find(bucket)
  34      b.objects.collect {|o| o.key}
  35    end
  36  
  37    def copy_over_bucket(from_bucket, to_bucket)
  38      puts "Replacing #{to_bucket} with contents of #{from_bucket}"
  39      #delete to_bucket
  40      empty_bucket(to_bucket)
  41      bucket_keys(from_bucket).each do |k|
  42        copy_between_buckets(from_bucket, to_bucket, k)
  43      end
  44    end
  45    
  46    def copy_between_buckets(from_bucket, to_bucket, from_key, to_key = nil)
  47      if exists?(from_bucket, from_key)
  48        to_key = from_key if to_key.nil?
  49        puts "Copying #{from_bucket}.#{from_key} to #{to_bucket}.#{to_key}"
  50        url = "http://s3.amazonaws.com/#{from_bucket}/#{from_key}"
  51        filename = download(url)
  52        store_file(to_bucket,to_key,filename)
  53        File.delete(filename)
  54        return "http://s3.amazonaws.com/#{to_bucket}/#{to_key}"
  55      else
  56        puts "#{from_bucket}.#{from_key} didn't exist"
  57        return nil
  58      end
  59    end
  60  
  61    def store_file(bucket, key, filename)
  62       puts "Storing #{filename} in #{bucket}.#{key}"
  63       S3Object.store(
  64        key,
  65        File.open(filename),
  66        bucket,
  67        :access => :public_read
  68        )
  69    end
  70  
  71    def download(url, save_as = nil)
  72      if save_as.nil?
  73        Dir.mkdir("amazon_s3_temp") if !File.exists?("amazon_s3_temp")
  74        save_as = File.join("amazon_s3_temp",File.basename(url))
  75      end
  76      begin
  77        puts "Saving #{url} to #{save_as}"
  78        agent = WWW::Mechanize.new {|a| a.log = Logger.new(STDERR) }
  79        img = agent.get(url)
  80        img.save_as(save_as)
  81        return save_as
  82      rescue
  83        raise "Failed on " + url + "  " + save_as
  84      end
  85    end
  86  
  87    def exists?(bucket,key)
  88      begin
  89        res = S3Object.find key, bucket
  90      rescue 
  91        res = nil
  92      end
  93      return !res.nil?
  94    end
  95        
  96  end

copy files using rsync and ssh

source: http://www.mikerubel.org/computers/rsync_snapshots/#Abstract ; switches: -a = archive mode -e specifies the remote shell to use

   1  
   2  rsync -a -e ssh source/ username@remotemachine.com:/path/to/destination/

Using Ruby to copy a file

Using Ruby this code copies a file called 'notes.xml' to a new file called 'notes2.xml'. Refer to ruby-doc.org - Class: File http://urltea.com/1wbt

   1  
   2  require 'ftools'
   3  File.copy('notes.xml', 'notes2.xml')
   4  

SQL: Copy Data From One Table Into Another

// SQL Query to copy data from one table and insert it into another

   1  
   2  INSERT INTO TABLE2 (COL1, COL2, COL3) SELECT COL1, COL4, COL7 FROM TABLE1

Python - Copy List

// Copiare una Lista

   1  
   2  a = [1, 2, 3]
   3  b = a[:]

Copy a value

This will copy a value. Written in Brainfuck.

   1  
   2  Simple copy routine
   3  Written by Davor Babic
   4  davorb@gmailDOTcom
   5  
   6  ,[>>+<<-]>>[<+<+>>-]<.>>>++++++++++
« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS