1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 # from __future__ import nested_scopes
43 self.delete_excluded = 0
44 self.delete_from_source = 0
46 self.modify_window = 2
49 self.case_sensitivity = 0
51 self.case_sensitivity = re.I
54 def visit(cookie, dirname, names):
55 """Copy files names from sink_root + (dirname - sink_root) to target_root + (dirname - sink_root)"""
56 if os.path.split(cookie.sink_root)[
57 1]: # Should be tested with (C:\Cvs -> C:\)! (C:\Archives\MyDatas\UltraEdit -> C:\Archives\MyDatas) (Cvs -> "")! (Archives\MyDatas\UltraEdit -> Archives\MyDatas) (\Cvs -> \)! (\Archives\MyDatas\UltraEdit -> Archives\MyDatas)
58 dirname = dirname[len(cookie.sink_root) + 1:]
60 dirname = dirname[len(cookie.sink_root):]
61 target_dir = os.path.join(cookie.target_root, dirname)
62 if not os.path.isdir(target_dir):
63 makeDir(cookie, target_dir)
64 sink_dir = os.path.join(cookie.sink_root, dirname)
68 ignore = os.path.join(sink_dir, ".cvsignore")
69 if os.path.isfile(ignore):
70 filters = convertPatterns(ignore, "-")
71 filters = filters + cookie.filters
75 # filter sink files (names):
77 while name_index < len(names):
78 name = names[name_index]
79 path = os.path.join(dirname, name)
80 path = convertPath(path)
81 if os.path.isdir(os.path.join(sink_dir, name)):
83 for filter in filters:
84 if re.search(filter[1], path, cookie.case_sensitivity):
86 sink = os.path.join(sink_dir, name)
87 if cookie.delete_from_source:
88 if os.path.isfile(sink):
89 removeFile(cookie, sink)
90 elif os.path.isdir(sink):
91 removeDir(cookie, sink)
94 "Sink %s is neither a file nor a folder (skip removal)" %
96 names_excluded += [names[name_index]]
97 del (names[name_index])
98 name_index = name_index - 1
100 elif filter[0] == '+':
102 name_index = name_index + 1
104 if cookie.delete and os.path.isdir(target_dir):
105 # Delete files and folder in target not present in filtered sink.
106 for name in os.listdir(target_dir):
107 if not cookie.delete_excluded and name in names_excluded:
109 if name not in names:
110 target = os.path.join(target_dir, name)
111 if os.path.isfile(target):
112 removeFile(cookie, target)
113 elif os.path.isdir(target):
114 removeDir(cookie, target)
119 # Copy files and folder from sink to target.
120 sink = os.path.join(sink_dir, name)
122 target = os.path.join(target_dir, name)
123 if os.path.exists(target):
124 # When target already exit:
125 if os.path.isfile(sink):
126 if os.path.isfile(target):
128 if shouldUpdate(cookie, sink, target):
129 updateFile(cookie, sink, target)
130 elif os.path.isdir(target):
132 removeDir(cookie, target)
133 copyFile(cookie, sink, target)
137 "Target %s is neither a file nor folder (skip update)" %
140 elif os.path.isdir(sink):
141 if os.path.isfile(target):
143 removeFile(cookie, target)
144 makeDir(cookie, target)
148 "Sink %s is neither a file nor a folder (skip update)" %
151 elif not cookie.existing:
152 # When target dont exist:
153 if os.path.isfile(sink):
155 copyFile(cookie, sink, target)
156 elif os.path.isdir(sink):
158 makeDir(cookie, target)
161 "Sink %s is neither a file nor a folder (skip update)" %
165 def log(cookie, message):
169 except UnicodeEncodeError:
170 print message.encode("utf8")
173 def logError(message):
175 sys.stderr.write(message + "\n")
176 except UnicodeEncodeError:
177 sys.stderr.write(message.encode("utf8") + "\n")
180 def shouldUpdate(cookie, sink, target):
182 sink_st = os.stat(sink)
183 sink_sz = sink_st.st_size
184 sink_mt = sink_st.st_mtime
187 "Fail to retrieve information about sink %s (skip update)" %
192 target_st = os.stat(target)
193 target_sz = target_st.st_size
194 target_mt = target_st.st_mtime
197 "Fail to retrieve information about target %s (skip update)" %
202 return target_mt < sink_mt - cookie.modify_window
204 if cookie.ignore_time:
207 if target_sz != sink_sz:
213 return abs(target_mt - sink_mt) > cookie.modify_window
216 def copyFile(cookie, sink, target):
217 log(cookie, "copy: %s to: %s" % (sink, target))
218 if not cookie.dry_run:
220 shutil.copyfile(sink, target)
222 logError("Fail to copy %s" % sink)
227 os.utime(target, (s.st_atime, s.st_mtime))
229 logError("Fail to copy timestamp of %s" % sink)
232 def updateFile(cookie, sink, target):
233 log(cookie, "update: %s to: %s" % (sink, target))
234 if not cookie.dry_run:
235 # Read only and hidden and system files can not be overridden.
239 filemode = win32file.GetFileAttributesW(target)
240 win32file.SetFileAttributesW(
242 filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM)
244 os.chmod(target, stat.S_IWUSR)
246 # logError("Fail to allow override of %s" % target)
249 shutil.copyfile(sink, target)
253 os.utime(target, (s.st_atime, s.st_mtime))
255 # The utime api of the 2.3 version of python is not unicode
257 logError("Fail to copy timestamp of %s" % sink)
259 logError("Fail to override %s" % sink)
262 win32file.SetFileAttributesW(target, filemode)
265 def prepareRemoveFile(path):
267 filemode = win32file.GetFileAttributesW(path)
268 win32file.SetFileAttributesW(path, filemode & ~win32file.FILE_ATTRIBUTE_READONLY &
269 ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM)
271 os.chmod(path, stat.S_IWUSR)
274 def removeFile(cookie, target):
275 # Read only files could not be deleted.
276 log(cookie, "remove: %s" % target)
277 if not cookie.dry_run:
280 prepareRemoveFile(target)
282 # logError("Fail to allow removal of %s" % target)
287 logError("Fail to remove %s" % target)
290 def makeDir(cookie, target):
291 log(cookie, "make dir: %s" % target)
292 if not cookie.dry_run:
296 logError("Fail to make dir %s" % target)
299 def visitForPrepareRemoveDir(arg, dirname, names):
301 path = os.path.join(dirname, name)
302 prepareRemoveFile(path)
305 def prepareRemoveDir(path):
306 prepareRemoveFile(path)
307 os.path.walk(path, visitForPrepareRemoveDir, None)
310 def OnRemoveDirError(func, path, excinfo):
311 logError("Fail to remove %s" % path)
314 def removeDir(cookie, target):
315 # Read only directory could not be deleted.
316 log(cookie, "remove dir: %s" % target)
317 if not cookie.dry_run:
318 prepareRemoveDir(target)
320 shutil.rmtree(target, False, OnRemoveDirError)
322 logError("Fail to remove dir %s" % target)
325 def convertPath(path):
326 # Convert windows, mac path to unix version.
327 separator = os.path.normpath("/")
329 path = re.sub(re.escape(separator), "/", path)
331 # Help file, folder pattern to express that it should match the all file
337 def convertPattern(pattern, sign):
338 """Convert a rsync pattern that match against a path to a filter that match against a converted path."""
340 # Check for include vs exclude patterns.
341 if pattern[:2] == "+ ":
342 pattern = pattern[2:]
344 elif pattern[:2] == "- ":
345 pattern = pattern[2:]
348 # Express windows, mac patterns in unix patterns (rsync.py extension).
349 separator = os.path.normpath("/")
351 pattern = re.sub(re.escape(separator), "/", pattern)
353 # If pattern contains '/' it should match from the start.
355 if pattern[0] == "/":
356 pattern = pattern[1:]
360 # Convert pattern rules: ** * ? to regexp rules.
361 pattern = re.escape(pattern)
362 pattern = string.replace(pattern, "\\?", ".")
363 pattern = string.replace(pattern, "\\*\\*", ".*")
364 pattern = string.replace(pattern, "\\*", "[^/]*")
365 pattern = string.replace(pattern, "\\*", ".*")
368 # If pattern contains '/' it should match from the start.
369 pattern = "^\\/" + pattern
371 # Else the pattern should match the all file or folder name.
372 pattern = "\\/" + pattern
374 if pattern[-2:] != "\\/" and pattern[-2:] != ".*":
375 # File patterns should match also folders.
376 pattern = pattern + "\\/?"
378 # Pattern should match till the end.
379 pattern = pattern + "$"
380 return (sign, pattern)
383 def convertPatterns(path, sign):
384 """Read the files for pattern and return a vector of filters"""
388 pattern = f.readline()
391 if pattern[-1] == "\n":
392 pattern = pattern[:-1]
394 if re.match("[\t ]*$", pattern):
396 if pattern[0] == "#":
398 filters = filters + [convertPattern(pattern, sign)]
404 """Print the help string that should printed by rsync.py -h"""
405 print "usage: rsync.py [options] source target"
407 -q, --quiet decrease verbosity
408 -r, --recursive recurse into directories
409 -R, --relative use relative path names
410 -u, --update update only (don't overwrite newer files)
411 -t, --times preserve times
412 -n, --dry-run show what would have been transferred
413 --existing only update files that already exist
414 --delete delete files that don't exist on the sending side
415 --delete-excluded also delete excluded files on the receiving side
416 --delete-from-source delete excluded files on the receiving side
417 -I, --ignore-times don't exclude files that match length and time
418 --size-only only use file size when determining if a file should
420 --modify-window=NUM timestamp window (seconds) for file match (default=2)
421 --existing only update existing target files or folders
422 -C, --cvs-exclude auto ignore files in the same way CVS does
423 --exclude=PATTERN exclude files matching PATTERN
424 --exclude-from=FILE exclude patterns listed in FILE
425 --include=PATTERN don't exclude files matching PATTERN
426 --include-from=FILE don't exclude patterns listed in FILE
427 --version print version number
428 -h, --help show this help screen
430 See http://www.vdesmedt.com/~vds2212/rsync.html for informations and updates.
431 Send an email to vivian@vdesmedt.com for comments and bug reports."""
435 print "rsync.py version 2.0.1"
441 opts, args = getopt.getopt(args, "qrRntuCIh",
442 ["quiet", "recursive", "relative", "dry-run", "time", "update", "cvs-ignore",
443 "ignore-times", "help", "delete", "delete-excluded", "delete-from-source", "existing",
444 "size-only", "modify-window=", "exclude=", "exclude-from=", "include=", "include-from=",
447 if o in ["-q", "--quiet"]:
449 if o in ["-r", "--recursive"]:
451 if o in ["-R", "--relative"]:
453 elif o in ["-n", "--dry-run"]:
455 # --time is there to guaranty backward compatibility with previous buggy version.
456 elif o in ["-t", "--times", "--time"]:
458 elif o in ["-u", "--update"]:
460 elif o in ["-C", "--cvs-ignore"]:
461 cookie.cvs_ignore = 1
462 elif o in ["-I", "--ignore-time"]:
463 cookie.ignore_time = 1
464 elif o == "--delete":
466 elif o == "--delete-excluded":
468 cookie.delete_excluded = 1
469 elif o == "--delete-from-source":
470 cookie.delete_from_source = 1
471 elif o == "--size-only":
473 elif o == "--modify-window":
474 cookie.modify_window = int(v)
475 elif o == "--existing":
477 elif o == "--exclude":
478 cookie.filters = cookie.filters + [convertPattern(v, "-")]
479 elif o == "--exclude-from":
480 cookie.filters = cookie.filters + convertPatterns(v, "-")
481 elif o == "--include":
482 cookie.filters = cookie.filters + [convertPattern(v, "+")]
483 elif o == "--include-from":
484 cookie.filters = cookie.filters + convertPatterns(v, "+")
485 elif o == "--version":
488 elif o in ["-h", "--help"]:
496 # print cookie.filters
498 target_root = args[1]
499 try: # In order to allow compatibility below 2.3.
501 if "supports_unicode_filenames" in os.path.__dict__ and os.path.supports_unicode_filenames:
502 target_root = unicode(target_root, sys.getfilesystemencoding())
504 cookie.target_root = target_root
506 sinks = glob.glob(args[0])
512 try: # In order to allow compatibility below 2.3.
513 if "supports_unicode_filenames" in os.path.__dict__ and os.path.supports_unicode_filenames:
514 sink = unicode(sink, sys.getfilesystemencoding())
519 sink_drive, sink_root = os.path.splitdrive(sink)
521 if sink_root == os.path.sep:
524 sink_root, sink_name = os.path.split(sink_root)
525 sink_root = sink_drive + sink_root
526 if sink_root not in sink_families:
527 sink_families[sink_root] = []
528 sink_families[sink_root] = sink_families[sink_root] + [sink_name]
530 for sink_root in sink_families.keys():
532 cookie.sink_root = ""
534 cookie.sink_root = sink_root
536 # In order to allow compatibility below 2.1 (nested scope where used
541 lambda x: os.path.isfile(
545 sink_families[sink_root])
547 visit(cookie, sink_root, files)
549 # global y # In order to allow compatibility below 2.1 (nested scope
550 # where used before).
553 lambda x: os.path.isdir(
557 sink_families[sink_root])
558 for folder in folders:
559 folder_path = os.path.join(sink_root, folder)
560 if not cookie.recursive:
561 visit(cookie, folder_path, os.listdir(folder_path))
563 os.path.walk(folder_path, visit, cookie)
567 if __name__ == "__main__":
568 sys.exit(main(sys.argv[1:]))