9 def is_file_different(a, b):
10 if os.path.getsize(a) != os.path.getsize(b):
11 # If the file size is different, the content must be different.
14 # Read the content of the files, and compare them.
21 buff_a = fa.read(BUFFER_SIZE)
22 buff_b = fb.read(BUFFER_SIZE)
25 # File is different in this block.
29 if len(buff_a) < BUFFER_SIZE:
39 def copyfile(src, dest):
40 if not os.path.exists(src):
41 raise ValueError('Source file not found')
43 # Make parent directory (if necessary)
44 destdir = os.path.dirname(dest)
45 if not os.path.exists(destdir):
49 raise ValueError('Unable to create directory ' + destdir)
50 elif not os.path.isdir(destdir):
51 raise ValueError(destdir + ' is not a directory')
53 if not os.path.exists(dest) or is_file_different(src, dest):
54 # If the destination file does not exist or the source file is
55 # different from the destination file, then we copy the file.
56 shutil.copyfile(src, dest)
60 print >> sys.stderr, 'USAGE:', sys.argv[0], '<srcfile> <destfile>'
63 srcfile = os.path.abspath(sys.argv[1])
64 destfile = os.path.abspath(sys.argv[2])
66 if srcfile == destfile:
67 print >> sys.stderr, 'WARNING: <srcfile> is equal to <destfile>'
70 copyfile(srcfile, destfile)
72 print >> sys.stderr, 'ERROR: ', e
75 if __name__ == '__main__':