-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadow.py
90 lines (69 loc) · 2.71 KB
/
shadow.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys, os, shutil, re
DEBUG = True
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
def debug(string):
if DEBUG:
print string
def make_link(source_path, dest_path, tiered=False):
debug(' linking %s%s' % (dest_path, ' [tiered]' if tiered else '',))
if DRY_RUN: return
try:
os.remove(dest_path)
except: pass
os.symlink(source_path, dest_path)
def copy_file(source_path, dest_path, tiered=False):
debug (' copying %s%s' % (dest_path, ' [tiered]' if tiered else '',))
if DRY_RUN: return
try:
os.remove(dest_path)
except: pass
shutil.copyfile(source_path, dest_path)
def split_file(file_path):
result = {}
if os.path.exists(file_path):
fp = open(file_path)
for path in fp:
result[path.strip()] = 1
fp.close()
return result
def walk_scheme(scheme, tier, operation=make_link, copy_list={}, ignore_list={}):
debug('creating links for scheme "%s"...' % (scheme,))
#dev_reg = re.compile(r'.*\.development$')
#stag_reg = re.compile(r'.*\.staging$')
#prod_reg = re.compile(r'.*\.production$')
#tier_reg = re.compile(r'.*\.'+tier+'$')
for dirname, subdirs, files in os.walk(scheme):
if len(dirname) == len(scheme): continue #don't do anything in root dir
dirname = dirname[len(scheme)+1:] #remove scheme name from paths
for filename in files:
tiered = False
source_path = os.path.join(BASE_DIR, scheme, dirname, filename)
dest_path = os.path.join('/', dirname, filename)
'''
if dev_reg.match(source_path) or stag_reg.match(source_path) or prod_reg.match(source_path):
if tier_reg.match(source_path):
tiered = True
dest_path = dest_path.replace('.'+tier, '')
else:
continue
'''
if dest_path in copy_list:
copy_file(source_path, dest_path, tiered=tiered)
elif dest_path in ignore_list:
debug(' skipping %s' % (dest_path,))
else:
operation(source_path, dest_path, tiered=tiered)
if __name__ == '__main__':
if len(sys.argv) != 3:
print "usage: python homeworld.py <scheme:home> <test|real>"
sys.exit(2)
scheme = sys.argv[1]
#tier = sys.argv[2]
DRY_RUN = sys.argv[2] != 'real'
if DRY_RUN:
debug('NOTE: This is just a test run. No links are being created or deleted!')
IGNORE_FILE = scheme + '/IGNORE'
COPY_FILE = scheme + '/COPY'
ignore_list = split_file(IGNORE_FILE)
copy_list = split_file(COPY_FILE)
walk_scheme(scheme, tier=None, copy_list=copy_list, ignore_list=ignore_list)