Repository navigation
Expand file tree
/
Copy pathproject.py
More file actions
executable file
·249 lines (216 loc) · 7.85 KB
/
Copy pathproject.py
File metadata and controls
executable file
·249 lines (216 loc) · 7.85 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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#############################################################
# #
# Copyright @ 2013 - 2018 Dashingsoft corp. #
# All rights reserved. #
# #
# pyarmor #
# #
# Version: 3.4.0 - #
# #
#############################################################
#
#
# @File: project.py
#
# @Author: Jondy Zhao(jondy.zhao@gmail.com)
#
# @Create Date: 2018/01/15
#
# @Description:
#
# Define project object.
#
# @Change Log:
# 1.0.0: Initial.
# 1.0.1: Add title
# 1.0.2: Add disable_restrict_mode
# 1.1.0: Add cross_protection, obf_code, obf_mod, wrap_mode, plugins
# 1.2.0: Add platform
# 1.2.1: Add advanced_mode
# 1.2.2: Remove disable_restrice_mode, add restrict_mode
# 1.2.3: Add package_runtime
# 1.2.4: Add enable_suffix, remove obf_module_mode and obf_code_mode
#
# 2.0: Add license_file, bootstrap_code
# Remove attribute capsule
# 2.1: Add mixins
#
import os
import time
from glob import glob
from io import StringIO
from json import dump as json_dump, load as json_load
from config import config_filename, default_output_path, \
default_manifest_template
class Project(dict):
VERSION = 2, 1
OBF_MODULE_MODE = 'none', 'des', 'aes'
OBF_CODE_MODE = 'none', 'fast', 'aes', 'wrap'
DEFAULT_VALUE = \
('version', '.'.join([str(x) for x in VERSION])), \
('name', None), \
('title', None), \
('src', None), \
('is_package', None), \
('manifest', default_manifest_template), \
('entry', None), \
('output', default_output_path), \
('runtime_path', None), \
('restrict_mode', 1), \
('obf_code', 1), \
('obf_mod', 2), \
('wrap_mode', 1), \
('advanced_mode', 0), \
('bootstrap_code', 1), \
('cross_protection', 1), \
('mixins', None), \
('plugins', None), \
('platform', None), \
('package_runtime', 1), \
('enable_suffix', 0), \
('license_file', None), \
('build_time', 0.)
def __init__(self, *args, **kwargs):
self._path = ''
for k, v in Project.DEFAULT_VALUE:
kwargs.setdefault(k, v)
super(Project, self).__init__(*args, **kwargs)
def _format_path(self, path):
return os.path.normpath(path if os.path.isabs(path)
else os.path.join(self._path, path))
def __getattr__(self, name):
if name in ('src', 'output'):
return self._format_path(self[name])
elif name == 'license_file':
v = self[name] if name in self else None
return v if v in ('no', 'outer') \
else self._format_path(v) if v else None
if name in self:
return self[name]
raise AttributeError(name)
def _update(self, kwargs):
result = []
for name in dict(Project.DEFAULT_VALUE).keys():
value = kwargs.get(name)
if value is not None:
self[name] = value
result.append(name)
self['build_time'] = 0.
return result
def _check(self, path):
assert os.path.exists(os.path.normpath(path)), \
'Project path %s does not exists' % path
assert os.path.exists(self.src), \
'The src of this project is not found: %s' % self.src
assert os.path.isabs(self.src), \
'The src of this project is not absolute path'
assert self.src != self.output, \
'The output path can not be same as src in the project'
assert self.license_file is None \
or self.license_file == 'outer' \
or self.license_file.endswith('license.lic'), \
'Invalid license file'
if self.restrict_mode == 0 and self.license_file is not None:
raise RuntimeError('"--restrict 0" is ignored by license file'
'"%s", set it to 1 if outer license is used'
' and make sure this license is generated'
' with option "--disable-restrict-mode"'
% self.license_file)
def _dump(self, filename):
with open(filename, 'w') as f:
json_dump(self, f, indent=2)
def _load(self, filename):
with open(filename, 'r') as f:
obj = json_load(f)
self.update(obj)
self._check(os.path.dirname(filename))
def _project_filename(self, path):
return path if path and os.path.isfile(path) else \
os.path.join(path, config_filename)
def open(self, path):
filename = self._project_filename(path)
self._path = os.path.abspath(os.path.dirname(filename))
self._load(filename)
def save(self, path):
filename = self._project_filename(path)
self._dump(filename)
def check(self):
self._check(self._path)
@classmethod
def map_obfuscate_mode(cls, mode, comode):
m = Project.OBF_MODULE_MODE.index(mode)
c = Project.OBF_CODE_MODE.index(comode)
if comode == 'wrap':
return 13 + m
else:
return 7 + (1 - m) * 3 + c
def get_obfuscate_mode(self, mode=None, comode=None):
if mode is None:
mode = self.obf_module_mode
if comode is None:
comode = self.obf_code_mode
return Project.map_obfuscate_mode(mode, comode)
def get_build_files(self, force=False, excludes=[]):
mlist = self.manifest.split(',') + excludes
files = self.build_manifest(mlist, self.src)
if force:
return files
results = []
buildtime = self.get('build_time', 1.)
for x in files:
if os.path.getmtime(os.path.join(self.src, x)) > buildtime:
results.append(x)
return results
@classmethod
def build_manifest(cls, manifest, path=None):
from distutils.filelist import FileList
from distutils.text_file import TextFile
infile = StringIO()
infile.write('\n'.join(manifest))
infile.seek(0)
template = TextFile(file=infile,
strip_comments=1,
skip_blanks=1,
join_lines=1,
lstrip_ws=1,
rstrip_ws=1,
collapse_join=1)
lines = template.readlines()
filelist = FileList()
try:
if path is not None and not path == os.getcwd():
oldpath = os.getcwd()
os.chdir(path)
else:
oldpath = None
for line in lines:
filelist.process_template_line(line)
finally:
if oldpath is not None:
os.chdir(oldpath)
return set(filelist.files)
@classmethod
def build_globfiles(cls, patterns, path=''):
files = []
n = len(path) + 1
for x in patterns:
for name in glob(os.path.join(path, x)):
files.append(name[n:])
return set(files)
def info(self):
lines = []
for k, v in Project.DEFAULT_VALUE:
if k == 'build_time':
v = time.asctime(time.gmtime(self[k]))
else:
v = str(self[k])
n = 50
if len(v) > n:
v = v[:n] + '\n%24s' % ' ' + v[n:]
lines.append('%22s: %s' % (k, v))
return '\n'.join(lines)
if __name__ == '__main__':
project = Project()