forked from AdamGagorik/ffxiahbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makebin.py
174 lines (135 loc) · 4.53 KB
/
makebin.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Create wrapper scripts for running apps.
"""
import argparse
import logging
import shutil
import stat
import os
import re
def setup_logging(**kwargs):
"""
Setup python logging module.
"""
lfmt = '%(levelname)-5s: %(message)s'
dfmt = "%Y-%m-%d %H:%M:%S"
_kwargs = dict(level=logging.DEBUG, format=lfmt, datefmt=dfmt)
_kwargs.update(**kwargs)
logging.basicConfig(**_kwargs)
def log_parameter(k, v, fmt='%-14s: %s', level=logging.INFO):
logging.log(level, fmt, k, v)
class Options:
def __init__(self):
self.work = os.getcwd()
self.python = shutil.which('python3')
self.project_path = os.path.join(os.path.dirname(os.path.abspath(__file__)))
self.package_name = 'pydarkstar'
self.package_path = os.path.join(self.project_path, self.package_name)
self.apps_path = os.path.join(self.package_path, 'apps')
self.bin_path = os.path.join(self.project_path, 'bin')
self.apps_list = {
'broker': 'pydarkstar.apps.broker.run',
'seller': 'pydarkstar.apps.seller.run',
'buyer': 'pydarkstar.apps.buyer.run',
'refill': 'pydarkstar.apps.refill.run',
'clear': 'pydarkstar.apps.clear.run',
'scrub': 'pydarkstar.apps.scrub.run',
'alter': 'pydarkstar.apps.alter.run',
}
if self.python is None:
self.python = 'python'
log_parameter('work', self.work)
log_parameter('project_path', self.project_path)
log_parameter('package_name', self.package_name)
log_parameter('package_path', self.package_path)
log_parameter('python', self.python)
log_parameter('bin_path', self.bin_path)
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.parse_args()
try:
assert os.path.exists(self.work)
assert os.path.exists(self.project_path)
assert os.path.exists(self.package_path)
assert os.path.exists(self.apps_path)
except AssertionError:
logging.exception('invalid configuration')
exit(-1)
def find_apps(top, regex=r'(run|main)\.py'):
"""
Search for applications.
"""
if isinstance(regex, str):
regex = re.compile(regex)
found = []
logging.info('looking for apps...')
for root, dirs, files in os.walk(top):
for f in files:
m = regex.match(f)
if m:
p = os.path.join(root, f)
found.append(p)
found.sort(key=lambda x: x.split(os.sep))
for f in found:
logging.info('found app! %s', f)
return found
LTEMPLATE = r"""
#!/bin/bash
export PYTHONPATH=$PYTHONPATH:{path}
{python} -m {spec} $*
"""[1:-1]
WTEMPLATE = r"""
@ECHO OFF
set PYTHONPATH="%PYTHONPATH%;{path}"
{python} -m {spec} %*
"""[1:-1]
def choose_template():
"""
Choose script template based on platform.
"""
from sys import platform
log_parameter('platform', platform)
# select template based on platform
if platform in ['linux', 'linux2', 'darwin']:
return LTEMPLATE, '{}.sh'
elif platform in ['win32', 'win64']:
return WTEMPLATE, '{}.bat'
else:
raise RuntimeError('unknown platform: %s', platform)
def chmod(path):
os.chmod(path,
# user
stat.S_IRUSR | # read
stat.S_IWUSR | # write
stat.S_IXUSR | # execute
# group
stat.S_IRGRP | # read
stat.S_IWGRP | # write
stat.S_IXGRP | # execute
# other
stat.S_IROTH | # read
# stat.S_IWOTH | # write
stat.S_IXOTH # execute
)
def main():
setup_logging()
opts = Options()
# choose script template and output file name
template, ostub = choose_template()
# create bin directory
if not os.path.exists(opts.bin_path):
logging.info('mkdir -p %s', opts.bin_path)
os.makedirs(opts.bin_path)
for app in opts.apps_list:
log_parameter(app, opts.apps_list[app])
opath = os.path.join(opts.bin_path, ostub.format(app))
with open(opath, 'w') as handle:
handle.write(template.format(python=opts.python, path=os.path.dirname(opts.package_path),
spec=opts.apps_list[app]))
chmod(opath)
if __name__ == '__main__':
# noinspection PyBroadException
try:
main()
except Exception:
logging.exception('')
exit(-1)