Orb/python/orb/comp_copy.py
author Jonathan Harrington <jonathan.harrington@nokia.com>
Wed, 11 Aug 2010 14:49:30 +0100
changeset 4 468f4c8d3d5b
permissions -rw-r--r--
Orb version 0.2.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     1
from __future__ import with_statement
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     2
import shutil
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     3
import os
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     4
from optparse import OptionParser
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     5
import time
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     6
import logging
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     7
import sys
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     8
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
     9
class ComparativeCopy(object):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    10
    
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    11
    def copy_dir(self, src_dir, dest_dir):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    12
        """Copy all files in a source directory to a destination directory
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    13
        Will create the directory if it does not exist.
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    14
        """
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    15
        if not os.path.exists(dest_dir):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    16
            try:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    17
                os.makedirs(dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    18
            except WindowsError as e:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    19
                print 'Error creating directory: %s' % e
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    20
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    21
        self.files_copied = 0
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    22
        self.files_skipped = 0
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    23
        for file in os.listdir(src_dir):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    24
            self.copy_file(os.path.join(src_dir,file),os.path.join(dest_dir,file))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    25
 
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    26
    def copy_file(self, src, dest):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    27
        """Copy a single file. If the source file does not exist or is smaller than the destination an error is logged. 
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    28
        """        
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    29
        if not os.path.exists(src):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    30
            logging.debug("File %s does not exist" % src)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    31
        elif (not os.path.exists(dest)) or self.should_copy(src, dest):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    32
            logging.debug("Copying %s to %s" % (src,dest))            
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    33
            shutil.copyfile(src,dest)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    34
            self.files_copied += 1 
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    35
        else:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    36
            logging.debug("Skipping copy of %s to %s. The source file was not larger than the destination" % (src,dest))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    37
            self.files_skipped += 1
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    38
            
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    39
    def should_copy(self, src, dest):    
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    40
        return os.path.getsize(src) > os.path.getsize(dest)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    41
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    42
def main():
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    43
    usage = "usage: %prog [options] source_directory destination_directory"
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    44
    parser = OptionParser(usage=usage)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    45
    parser.add_option("-l", type="int", default=20,
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    46
                  dest="loglevel",
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    47
                  help="set log level [default]")
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    48
    (options, args) = parser.parse_args()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    49
    if len(args) != 2:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    50
        parser.error("incorrect number of arguments")
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    51
        
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    52
    logging.basicConfig(level=options.loglevel)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    53
    logging.info("Command line was: %s" % " ".join(sys.argv))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    54
    src_dir = args[0]
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    55
    dest_dir = args[1]
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    56
    c_copy = ComparativeCopy()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    57
    startTime = time.clock()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    58
    c_copy.copy_dir(src_dir, dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    59
    timeTaken = time.clock() - startTime
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    60
    logging.info("Copied %d and skipped %d files in %.2f seconds " % ( c_copy.files_copied, c_copy.files_skipped, timeTaken))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    61
    
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    62
if __name__ == '__main__':
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    63
    main()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    64
    
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    65
import unittest
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    66
class TestComparativeCopy(unittest.TestCase):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    67
    
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    68
    def setUp(self):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    69
        self.src_dir=os.path.join(os.getcwd(),"test","tmp_src")
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    70
        self.dest_dir=os.path.join(os.getcwd(),"test","tmp_dest")
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    71
        os.makedirs(self.src_dir) 
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    72
        os.makedirs(self.dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    73
        self.c_copy=ComparativeCopy()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    74
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    75
    def tearDown(self):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    76
        shutil.rmtree(os.path.join(os.getcwd(),"test"))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    77
        
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    78
    def test_i_copy_files_that_are_larger(self):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    79
        with open(os.path.join(self.src_dir,"aFile.txt"),"w") as theFile:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    80
            theFile.write(commentedFile)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    81
        with open(os.path.join(self.dest_dir,"aFile.txt"),"w") as theFile:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    82
            theFile.write(unCommentedFile)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    83
            
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    84
        self.c_copy.copy_dir(self.src_dir, self.dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    85
        self.assertEqual(os.path.getsize(os.path.join(self.dest_dir,"aFile.txt")),197)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    86
        
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    87
    def test_i_dont_copy_files_that_are_smaller(self):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    88
        with open(os.path.join(self.src_dir,"aFile.txt"),"w") as theFile:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    89
            theFile.write(unCommentedFile)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    90
        with open(os.path.join(self.dest_dir,"aFile.txt"),"w") as theFile:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    91
            theFile.write(commentedFile)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    92
            
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    93
        self.c_copy.copy_dir(self.src_dir, self.dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    94
        self.assertEqual(os.path.getsize(os.path.join(self.dest_dir,"aFile.txt")),197)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    95
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    96
    def test_i_copy_files_that_dont_exist_in_dest(self):
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    97
        with open(os.path.join(self.src_dir,"aFile.txt"),"w") as theFile:
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    98
            theFile.write(commentedFile)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
    99
        self.c_copy.copy_dir(self.src_dir, self.dest_dir)
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   100
        self.assertTrue(os.path.exists(os.path.join(self.dest_dir,"aFile.txt")))
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   101
        
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   102
unCommentedFile="""#include <iostream>
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   103
using namespace std;
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   104
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   105
int main()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   106
{
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   107
    std::cout << "Welcome to the wonderful world of C++!!!\n";
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   108
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   109
    return 0;
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   110
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   111
}"""
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   112
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   113
commentedFile="""#include <iostream>
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   114
using namespace std;
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   115
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   116
/**
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   117
 * The main function.
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   118
 * @see testMain()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   119
 */
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   120
int main()
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   121
{
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   122
    std::cout << "Welcome to the wonderful world of C++!!!\n";
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   123
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   124
    return 0;
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   125
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   126
}"""
468f4c8d3d5b Orb version 0.2.0
Jonathan Harrington <jonathan.harrington@nokia.com>
parents:
diff changeset
   127