forked from dashingsoft/pyarmor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_project.py
333 lines (288 loc) · 8.92 KB
/
_project.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from __future__ import print_function
import logging
import json
import os
import shutil
import sys
from zipfile import ZipFile
project_data_path = 'projects'
project_capsule_name = 'capsule'
project_config_name = 'config.json'
project_index_name = 'index.json'
path = os.path.dirname(os.path.abspath(sys.argv[0]))
rootdir = os.path.normpath(os.path.join(path, '..', 'src'))
sys.rootdir = rootdir
sys.path.append(rootdir)
from config import version
from pyarmor import (_get_registration_code, _import_pytransform,
do_capsule, do_encrypt, do_license)
import pyarmor
pyarmor.pytransform = _import_pytransform()
def _check_project_index():
filename = os.path.join(project_data_path, project_index_name)
if not os.path.exists(filename):
with open(filename, 'w') as fp:
json.dump(dict(counter=0, projects={}), fp)
return filename
def _create_default_project(name):
'''
>>> a = _create_default_project('a')
>>> b = _create_default_project('b')
>>> a['name']
'a'
>>> a['scripts'].append('a1')
>>> a['scripts']
['a1']
>>> b['name']
'b'
>>> b['scripts']
[]
'''
return {
'name': name,
'title': '',
'description': '',
'path': '',
'scripts': [],
'files': ['include *.py'],
'licenses': [],
'output': '',
'clean': 0,
'capsule': '',
'target': '',
'default_license': '',
}
def newProject(args=None):
'''
>>> p = newProject()
>>> p['message']
'Project has been created'
'''
filename = _check_project_index()
with open(filename, 'r') as fp:
pindexes = json.load(fp)
counter = pindexes['counter'] + 1
name = 'project-%d' % counter
path = os.path.join(project_data_path, name)
if os.path.exists(path):
logging.warning('Project path %s has been exists', path)
else:
logging.info('Make project path %s', path)
os.mkdir(path)
capsule = os.path.join(path, project_capsule_name + '.zip')
if not os.path.exists(capsule):
argv = ['-O', path, project_capsule_name]
do_capsule(argv)
data = _create_default_project(name)
data['title'] = 'Project %d' % counter
data['path'] = os.path.abspath(os.getcwd())
data['capsule'] = capsule
config = os.path.join(path, project_config_name)
with open(config, 'w') as fp:
json.dump(data, fp)
pindexes['projects'][name] = config
pindexes['counter'] = counter
with open(filename, 'w') as fp:
json.dump(pindexes, fp)
return dict(project=data, message='Project has been created')
def updateProject(args):
'''
>>> p = newProject()['project']
>>> p['title'] = 'MyProject'
>>> updateProject(p)
'Update project OK'
'''
name = args['name']
config = os.path.join(project_data_path, name, project_config_name)
with open(config, 'w') as fp:
json.dump(args, fp)
return 'Update project OK'
def buildProject(args):
'''
>>> p = newProject()['project']
>>> p['title'] = 'My Project'
>>> p['scripts'] = ''
>>> p['files'] = 'include *.py'
>>> p['path'] = ''
>>> p['output'] = os.path.join('projects', 'build')
>>> buildProject(p)
'Encrypt project OK.'
>>> a = newLicense(p)
>>> p['default_license'] = a['filename']
>>> buildProject(p)
'Encrypt project OK.'
'''
name = args['name']
path = args['path'].strip()
output = args['output'].strip()
scripts = args['scripts'].split()
files = args['files'].splitlines()
capsule = args['capsule']
target = args['target'].strip()
default_license = args.get('default_license', None)
if path == '':
path = os.getcwd()
if output == '':
output = path
argv = ['-O', output, '-s', path, '-C', capsule]
if target:
argv.extend(['-p', target])
for wrapper in scripts:
argv.extend(['-m', wrapper])
manifest = os.path.join(project_data_path, name, 'MANIFEST');
argv.extend(['--manifest', manifest])
template = os.path.join(project_data_path, name, 'MANIFEST.in')
with open(template, 'w') as fp:
fp.write('\n'.join(files))
argv.append('@' + template)
do_encrypt(argv)
if not default_license == '':
licfile = os.path.join(output, 'license.lic')
logging.info('Copy %s to %s', default_license, licfile)
shutil.copyfile(default_license, licfile)
if args['clean'] == 1:
with open(manifest) as f:
filelist = f.read().splitlines()
backup = os.path.join(project_data_path, name, 'backup.zip')
myzip = ZipFile(backup, 'w')
logging.info('Backup source files to %s', backup)
try:
for filename in filelist:
myzip.write(filename)
finally:
myzip.close()
for filename in filelist:
logging.info('Remove source file %s', filename)
os.remove(filename)
return 'Encrypt project OK.'
def removeProject(args):
'''
>>> p1 = newProject()['project']
>>> m = removeProject(p1)
>>> m == 'Remove project %s OK' % p1['name']
True
'''
filename = _check_project_index()
with open(filename, 'r') as fp:
pindexes = json.load(fp)
name = args['name']
try:
pindexes['projects'].pop(name)
except KeyError:
pass
with open(filename, 'w') as fp:
json.dump(pindexes, fp)
shutil.rmtree(os.path.join(project_data_path, name))
return 'Remove project %s OK' % name
def queryProject(args=None):
'''
>>> r = queryProject()
>>> len(r) > 1
True
'''
if args is not None and args.get('name') is not None:
name = args.get('name')
config = os.path.join(project_data_path, name, project_config_name)
with open(config, 'r') as fp:
data = json.load(fp)
return dict(project=data, message='Got project %s' % name)
filename = _check_project_index()
with open(filename, 'r') as fp:
pindexes = json.load(fp)
result = []
for name, filename in pindexes['projects'].items():
try:
with open(filename, 'r') as fp:
data = json.load(fp)
item = dict(name=name, title=data['title'])
except Exception:
item = dict(name=name, title='* Something is wrong with this project')
result.append(item)
return result
def queryVersion(args=None):
'''
>>> r = queryVersion()
>>> r['version'][0] == '3'
True
>>> r['rcode'] == ''
True
'''
rcode = _get_registration_code()
return dict(version=version, rcode=rcode)
def newLicense(args):
'''
>>> p = newProject()['project']
>>> p['hdinfo'] = 'hdsioa-2abc'
>>> a1 = newLicense(p)
>>> p['expired'] = '2017-11-20'
>>> a2 = newLicense(p)
'''
name = args['name']
capsule = os.path.join(project_data_path, name, project_capsule_name + '.zip')
for i in range(1024):
output = os.path.join(project_data_path, name, 'license-%d.lic' % i)
if not os.path.exists(output):
break
argv = ['-C', capsule, '-O', output]
title = ''
try:
value = args.pop('hdinfo')
argv.extend(['-B', value])
title += 'Bind to %s.' % value
except KeyError:
pass
try:
value = args.pop('expired')
argv.extend(['-e', value])
title += 'Expired on %s.' % value
except KeyError:
pass
try:
value = args.pop('rcode')
argv.append(value)
title = 'Code: %s' % value
except KeyError:
pass
if title == '':
title = 'Default license'
do_license(argv)
config = os.path.join(project_data_path, name, project_config_name)
with open(config, 'r') as fp:
data = json.load(fp)
with open(config, 'w') as fp:
data['licenses'].append(dict(title=title, filename=output))
json.dump(data, fp)
return dict(title=title, filename=output)
def removeLicense(args):
'''
>>> p = newProject()['project']
>>> p['rcode'] = 'my-customer-a'
>>> a = newLicense(p)
>>> a['name'] = p['name']
>>> m = removeLicense(a)
>>> m == 'Remove license "Code: my-customer-a" OK.'
True
'''
name = args['name']
config = os.path.join(project_data_path, name, project_config_name)
with open(config, 'r') as fp:
data = json.load(fp)
licenses = data['licenses']
index = args.get('index')
if index is None:
filename = args['filename']
for index in range(len(licenses)):
if licenses[index]['filename'] == filename:
break
else:
raise RuntimeError('No license %s found', filename)
lic = licenses.pop(index)
with open(config, 'w') as fp:
json.dump(data, fp)
title = lic['title'];
filename = lic['filename'];
os.remove(filename)
return 'Remove license "%s" OK.' % title
if __name__ == '__main__':
import doctest
doctest.testmod()