-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcab.py
More file actions
125 lines (96 loc) · 3.28 KB
/
Copy pathcab.py
File metadata and controls
125 lines (96 loc) · 3.28 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os, zipfile, time, fnmatch, threading
def tmpDir():
ret = os.path.split(__file__)[0]
if ret:
ret = ret + '\\tmp'
else:
ret = os.getcwd() + '\\tmp'
try:
os.makedirs(ret)
except:
pass
return ret
def famatch(fn, matches):
for m in matches:
if fnmatch.fnmatch(fn, m):
return True
return False
def zipDir(source_dir, target_file, match=[
'*'], exclude_dirs=[], exclude_files=[]):
myZipFile = zipfile.ZipFile(target_file, 'w')
for root, dirs, files in os.walk(source_dir):
for xdir in exclude_dirs:
if xdir in dirs:
dirs.remove(xdir)
if files:
files = filter(lambda f: famatch(f, match), files)
files = filter(lambda f: not famatch(f, exclude_files), files)
for vfileName in files:
fileName = os.path.join(root, vfileName)
myZipFile.write(fileName, fileName, zipfile.ZIP_DEFLATED)
myZipFile.close()
def listFile(source_dir, match=[
'*'], exclude_dirs=[], exclude_files=[]):
fl = []
for root, dirs, files in os.walk(source_dir):
for xdir in exclude_dirs:
if xdir in dirs:
dirs.remove(xdir)
if files:
files = filter(lambda f: famatch(f, match), files)
files = filter(lambda f: not famatch(f, exclude_files), files)
for vfileName in files:
fl.append(os.path.join(root, vfileName))
return fl
def unzipFile(source_file, target_dir):
fl = {}
if not os.path.exists(target_dir):
os.mkdir(target_dir)
if target_dir[-1] not in ('/', '\\'):
target_dir += '/'
z = zipfile.ZipFile(source_file, 'r')
for fn in z.namelist():
bytes = z.read(fn)
filename = target_dir + fn
if len(bytes) == 0 and fn[-1] in ('/', '\\'):
try:
os.makedirs(filename)
except:
pass
else:
try:
os.makedirs(('/').join(filename.split('/')[:-1]))
except:
pass
else:
file(filename, 'wb+').write(bytes)
fl[filename] = len(bytes)
return fl
def restartSvr(svrName):
os.system('cmd /C net stop %s & net start %s' % (svrName, svrName))
class restartThread(threading.Thread):
def __init__(self, svrName):
threading.Thread.__init__(self)
self.svrName = svrName
def run(self):
time.sleep(2)
restartSvr(self.svrName)
def exportMysite():
import compileall
old = os.getcwd()
compileall.compile_dir(old)
zipDir(old, tmpDir() + '/mysite.zip', ['*'],
['data', '.hg', '_svn', '.svn', 'photologue', 'photos', 'lzo', 'upload', 'tmp'],
['.*', 'icdat.db', '*.swp', '*.py', '*.orig', '*.zip', 'options.txt', 'l.txt', '*.log', '*.sql', '*.7z',
'*.doc', 'tftpgui.cfg', 'oracle9'])
try:
os.removedirs(tmpDir() + '/mysite')
except:
pass
unzipFile(tmpDir() + '/mysite.zip', tmpDir() + '/mysite/')
file(tmpDir() + '/mysite/manage.py', 'w+').write(file('manage.py', 'r').read())
os.chdir(tmpDir() + '/')
zipDir('mysite/', tmpDir() + '/mysite.zip')
os.chdir(old)
if __name__ == '__main__':
exportMysite()