forked from mozii/pybuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python27.py
99 lines (93 loc) · 3.58 KB
/
python27.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
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# inspired from https://raw.github.com/schmir/bbfreeze/master/bbfreeze/py.py
import sys
import os
sys.path.append(os.path.dirname(getattr(sys,'executable',sys.argv[0])) or '.')
try:
import zipextimporter
zipextimporter.install()
except:
pass
def parse_options(args, spec):
needarg = dict()
for x in spec.split():
if x.endswith('='):
needarg[x[:-1]] = True
else:
needarg[x] = False
options = []
newargs = []
i = 0
while i < len(args):
a, v = (args[i].split('=', 1) + [None])[:2]
if a in needarg:
if v is None and needarg[a]:
i += 1
try:
v = args[i]
except IndexError:
raise Exception('option %s needs an argument' % (a, ))
options.append((a, v))
if a in ('-c', '-m'):
break
else:
break
i += 1
newargs.extend(args[i:])
return options, newargs
options, args = parse_options(sys.argv[1:], '-u -h -B -V -x -c= -m=')
options = dict(options)
sys.argv = args or ['']
main = __import__('__main__')
if '-B' in options or os.getenv('PYTHONDONTWRITEBYTECODE'):
sys.dont_write_bytecode = True
if '-u' in options or os.getenv('PYTHONUNBUFFERED'):
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
if '-h' in options:
print """
usage: python [option] ... [-c cmd | file] [arg] ...
Options and arguments (and corresponding environment variables):
-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-h : print this help message and exit
-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
see man page for details on internal buffering relating to '-u'
-V : print the Python version number and exit (also --version)
-x : skip first line of source, allowing use of non-Unix forms of #!cmd
file : program read from script file
arg ...: arguments passed to program in sys.argv[1:]
""".strip()
elif '-V' in options:
sys.stdout.write('python %s\n' % sys.version.split()[0])
elif options.get('-m') is not None:
get_loader = getattr(__import__('imp'), 'get_loader', None) or getattr(__import__('pkgutil'), 'get_loader')
codeobj = get_loader(options['-m']).get_code(options['-m'])
main.__dict__ ['__file__'] = codeobj.co_filename
exec codeobj in main.__dict__
elif options.get('-c') is not None:
exec options.get('-c') in main.__dict__
elif sys.argv[0]:
if sys.argv[0].endswith('.zip'):
import zipimport
importer = zipimport.zipimporter(sys.argv[0])
sys.path.insert(0, sys.argv[0])
main.__dict__['__file__'] = os.path.join(os.path.abspath(sys.argv[0]), '__main__.py')
exec importer.get_code('__main__') in main.__dict__
else:
codeobj = None
with open(sys.argv[0], 'rb') as fp:
if '-x' in options:
fp.readline()
content = fp.read()
main.__dict__['__file__'] = os.path.abspath(sys.argv[0])
if content.startswith('\x03\xf3\r\n'):
codeobj = __import__('marshal').loads(content[8:])
else:
codeobj = compile(content, filename=sys.argv[0], mode='exec')
if codeobj:
exec codeobj in main.__dict__
else:
import code
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
code.interact(banner='Python %s on %s\n%s' % (sys.version, sys.platform, cprt))