-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_changed_files.py
executable file
·59 lines (46 loc) · 2 KB
/
copy_changed_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import sys
import os
import time
import signal
import shutil
from datetime import datetime
def exitHandler(signal, frame):
print '\nThanks for remote work! Bye!'
sys.exit(0)
if len(sys.argv) != 3:
print 'Wrong number of arguments, usage: ./' + os.path.basename(__file__) + ' /source/directory /destination/directory'
sys.exit(0)
sourceDir = sys.argv[1]
destDir = sys.argv[2]
signal.signal(signal.SIGINT, exitHandler)
excludeFileSuffix = ['.kate-swp','.swp', '___'] # list of excluded files suffixes
excludeDirs = ['.git', '.kdev4', '.idea'] # list of excluded directiories
# get list of files and their modificaton time
filesMap = {}
def directoryTreeWalker(firstRun):
"""Go through directiories and check for modified files"""
for root, dirs,files in os.walk(sourceDir):
for directory in excludeDirs:
if directory in dirs:
dirs.remove(directory) # don't visit excluded directory
for fname in files:
path = os.path.join(root, fname)
if any(fname.endswith(end) for end in excludeFileSuffix):
continue
mtime = os.stat(path).st_mtime
if firstRun: # first time remember all files last modificaton time
filesMap[path] = mtime
else: # next runs check if any file modified and copy
if path not in filesMap or mtime > filesMap[path]:
filesMap[path] = mtime
relativeLoc = os.path.relpath(path, sourceDir)
destFile = os.path.join(destDir, relativeLoc)
if not os.path.exists(os.path.dirname(destFile)):
os.makedirs(os.path.dirname(destFile))
shutil.copy(path, destFile)
print('Copied: %s > %s %s'%(path, destFile, datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S")))
directoryTreeWalker(True)
while True:
directoryTreeWalker(False)
time.sleep(0.7)