-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsc.py
51 lines (46 loc) · 1.4 KB
/
sc.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2012-10-16
@author: shell.xu
@remark: 用于将多个文件合并入一个脚本内,形成单一的上传文件
'''
import re, os, sys, getopt
from os import path
filepath = ['.',]
def findfile(filename):
for p in filepath:
f = path.join(p, filename)
if path.exists(f): return f
include_re = re.compile('from (.*) import \*')
addpath_re = re.compile('sys\.path\.append\((.*)\)')
def server_compile(infile):
for line in infile:
mi = include_re.match(line)
ma = addpath_re.match(line)
if mi is not None:
with open(findfile(mi.group(1) + '.py')) as fi:
for line in server_compile(fi):
if line.startswith('#'): continue
yield line
yield '\n'
elif ma is not None:
filepath.append(ma.group(1).strip('\''))
else: yield line
def main():
'''
-h: help
'''
optlist, args = getopt.getopt(sys.argv[1:], 'h')
optdict = dict(optlist)
if '-h' in optdict:
print '%s type output' % sys.argv[0]
print main.__doc__
return
d = os.getcwd()
if path.dirname(args[0]): os.chdir(path.dirname(args[0]))
with open(path.basename(args[0])) as fi:
data = ''.join(server_compile(fi))
os.chdir(d)
with open(args[1], 'w') as fo: fo.write(data)
if __name__ == '__main__': main()