-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-here
executable file
·52 lines (45 loc) · 1.32 KB
/
copy-here
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
#!/usr/bin/python
from sys import argv
from os import getenv, listdir
from os.path import join, isdir, basename, exists
from fnmatch import fnmatch
from shutil import copy2
filters = [ basename(argv[0]), '.ssh', '.git', '.gitignore' ] + open('.gitignore').read().split()
def keep(x):
global filters
return not any(fnmatch(basename(x), f) or fnmatch(join('/', x), f) for f in filters)
def filter_(dirlist):
dirs, rest = [], []
for x in sorted(filter(keep, dirlist)):
if isdir(x):
dirs.append(x)
else:
rest.append(x)
return dirs, rest
def walk(top):
dirs, rest = filter_(listdir(top))
while True:
for y in rest:
yield y
if not dirs:
break
x = dirs.pop()
new_dirs, rest = filter_(join(x, y) for y in listdir(x))
dirs += new_dirs
def copy_here(dry_run=False):
home = getenv('HOME')
n = 0
for x in walk('.'):
y = join(home, x)
if not exists(y):
print('skip', x)
continue
if open(x).read() == open(y).read():
continue
print(x)
if not dry_run:
copy2(y, x)
n += 1
print(n, 'files copied' if not dry_run else 'files to be copied')
if __name__ == '__main__':
copy_here(len(argv) >= 2 and argv[1] == 'dry-run')