Last active
December 14, 2015 13:49
-
-
Save chenzx/5096519 to your computer and use it in GitHub Desktop.
I have copy C/C++ source files using command "cp -r /a /b",and then modified files inside /b,after that, i want to sync the changes into original /a, so i wrote this script, i found out that Python's os.path.ctime/mtime is really both shit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# | |
import os | |
import os.path | |
import sys | |
import time | |
import glob | |
FILE_EXT_FILTERS = ('.h', '.cpp', '.c') | |
def find_newest_mtime( base_dir ): | |
newest_base_mtime = 0 | |
for root, dirs, files in os.walk(base_dir, topdown=False): | |
for filename in files: | |
mtime = os.path.getmtime( os.path.join(root, filename) ) | |
if mtime>newest_base_mtime : | |
newest_base_mtime = mtime | |
def directory_sync( newer_dir, base_dir ): | |
newest_base_mtime = find_newest_mtime( base_dir ) | |
#print 'newer_dir=%s, base_dir=%s' % (newer_dir, base_dir) | |
for root, dirs, files in os.walk(newer_dir, topdown=False): | |
for filename in files: | |
newer_abspath = os.path.join(root, filename) | |
newer_relpath = os.path.join(root.replace(newer_dir, "", 1), filename) | |
if newer_relpath[0]=='/': | |
newer_relpath = newer_relpath[1:] | |
if filename.endswith( FILE_EXT_FILTERS ): | |
base_abspath = os.path.join(base_dir, newer_relpath) | |
if not os.path.exists(base_abspath): | |
print '%s +' % (newer_abspath, base_abspath) | |
else: | |
newer_mtime = os.path.getmtime(newer_abspath) | |
base_mtime = os.path.getmtime(base_abspath) | |
if newer_mtime > newest_base_mtime: | |
print '%s %f' % (newer_abspath, newer_mtime) | |
for dirname in dirs: | |
pass | |
def Main(): | |
if len(sys.argv)!=3: | |
print 'Usage: python directory_sync.py <newer_dir> <base_dir>' | |
sys.exit(0) | |
else: | |
directory_sync(sys.argv[1], sys.argv[2]) | |
if __name__ == '__main__': | |
sys.exit(Main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment