Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
306 lines (247 loc) · 10.5 KB
/
Copy pathconfig.py
File metadata and controls
306 lines (247 loc) · 10.5 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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#############################################################
# #
# Copyright @ 2023 - Dashingsoft corp. #
# All rights reserved. #
# #
# Pyarmor #
# #
# Version: 8.0.1 - #
# #
#############################################################
#
#
# @File: cli/config.py
#
# @Author: Jondy Zhao (pyarmor@163.com)
#
# @Create Date: Thu Jan 12 10:27:05 CST 2023
#
import configparser
import fnmatch
import os
from . import logger, CliError
def indent(lines, n=2):
fmt = ' ' * 2 + '%s'
return [fmt % x for x in lines]
def str_opt(k, v, n=30):
v = '\n\t'.join(v.splitlines())
return ' %s = %s%s' % (k, v[:n], '...' if len(v) > n else '')
class Configer(object):
SECTIONS = 'pyarmor', 'logging', 'finder', 'builder', 'runtime', \
'pack', 'bcc', 'mix.str', 'assert.call', 'assert.import'
def __init__(self, ctx, encoding=None):
self.ctx = ctx
self._encoding = encoding
def _read_config(self, filename):
cfg = configparser.ConfigParser(empty_lines_in_values=False)
cfg.read(filename, encoding=self._encoding)
return cfg
def list_sections(self, local=True, name=None):
lines = ['All available sections:']
cfg = self.ctx.cfg
lines.extend(indent(self.SECTIONS))
lines.extend(['', 'Global sections'])
cfg = self._read_config(self.ctx.global_config)
lines.extend(indent(cfg.sections()))
if local:
lines.extend(['', 'Local sections'])
cfg = self._read_config(self.ctx.local_config)
lines.extend(indent(cfg.sections()))
if name:
lines.extend(['', 'Private "%s" sections' % name])
cfg = self._read_config(self.ctx.get_filename(local, name))
lines.extend(indent(cfg.sections()))
return lines
def list_options(self, sect, local=True, name=None):
lines = ['Current options']
cfg = self.ctx.cfg
if cfg.has_section(sect):
lines.extend([str_opt(*x) for x in cfg.items(sect)])
lines.extend(['', 'Global options'])
cfg = self._read_config(self.ctx.global_config)
if cfg.has_section(sect):
lines.extend([str_opt(*x) for x in cfg.items(sect)])
if local:
lines.extend(['', 'Local options'])
cfg = self._read_config(self.ctx.local_config)
if cfg.has_section(sect):
lines.extend([str_opt(*x) for x in cfg.items(sect)])
if name:
lines.extend(['', 'Private "%s" options' % name])
cfg = self._read_config(self.ctx.get_filename(local, name))
if cfg.has_section(sect):
lines.extend([str_opt(*x) for x in cfg.items(sect)])
return lines
def _list_value(self, sect, opt, local=True, name=None):
clines, glines, lines, plines = self.infos
def format_value(opt):
v = cfg[sect].get(opt)
n = 1 << 30
return 'no option "%s"' % opt if v is None else str_opt(opt, v, n)
cfg = self.ctx.cfg
if cfg.has_section(sect):
clines.append(format_value(opt))
cfg = self._read_config(self.ctx.global_config)
if cfg.has_section(sect) and cfg.has_option(sect, opt):
glines.append(format_value(opt))
if local:
cfg = self._read_config(self.ctx.local_config)
if cfg.has_section(sect) and cfg.has_option(sect, opt):
lines.append(format_value(opt))
if name:
cfg = self._read_config(self.ctx.get_filename(local, name))
if cfg.has_section(sect) and cfg.has_option(sect, opt):
plines.append(format_value(opt))
return clines, glines, lines, plines
def _set_option(self, sect, opt, value, local=True, name=None):
ctx = self.ctx
filename = ctx.get_filename(local=local, name=name)
cfg = self._read_config(filename)
if not cfg.has_section(sect):
cfg.add_section(sect)
# TBD: input multiple lines
optname, optvalue = opt, value
if optvalue and optvalue[:1] in ('+', '-', '=', '^'):
op = optvalue[:1]
optvalue = optvalue.strip(op)
if op == '=':
op = None
else:
op = None
if optvalue and optvalue[:1] in ("'", '"'):
optvalue = optvalue.strip(optvalue[0])
if not optvalue:
if op is None:
self._clear(sect, optname, local, name)
return
ctxcfg = ctx.cfg
old = ctxcfg[sect].get(optname, '') if ctxcfg.has_section(sect) else ''
if op == '+':
if (optvalue + ' ').find(old + ' ') == -1:
optvalue = ('%s %s' % (old, optvalue)).strip()
elif op == '-':
optvalue = (old + ' ').replace(optvalue + ' ', '').strip()
elif op == '^':
optvalue = '\n'.join((old.splitlines() + [optvalue]))
logger.info('change option "%s" to new value "%s"', optname, optvalue)
cfg.set(sect, optname, optvalue)
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w') as f:
cfg.write(f)
self._list_value(sect, optname, local=local, name=name)
def _remove(self, section=None, options=None, local=True, name=None):
ctx = self.ctx
filename = ctx.get_filename(local=local, name=name)
if section is None:
logger.info('remove config file "%s"', filename)
if os.path.exists(filename):
os.remove(filename)
return
cfg = self._read_config(filename)
if cfg.has_section(section):
if not options:
logger.info('remove section "%s"', section)
cfg.remove_section(section)
else:
for opt in [x for x in options if cfg.has_option(section, x)]:
logger.info('remove option "%s:%s"', section, opt)
cfg.remove_option(section, opt)
if not cfg.options(section):
logger.info('remove empty section "%s"', section)
cfg.remove_section(section)
with open(filename, 'w') as f:
cfg.write(f)
def _clear(self, section=None, options=None, local=True, name=None):
ctx = self.ctx
scope = '%s%s config file' % (
'local' if local else 'global',
' "%s"' % name if name else ''
)
filename = ctx.get_filename(local=local, name=name)
if not filename:
logger.info('no %s', scope)
return
logger.info('remove %s "%s"', scope, filename)
if os.path.exists(filename):
os.remove(filename)
def _parse_opt(self, opt):
i = opt.find(':')
if i == -1:
j = opt.find('=')
pat, v = (opt, '') if j == -1 else (opt[:j], opt[j:])
cfg = self.ctx.cfg
rlist = []
for sect in self.SECTIONS:
if cfg.has_section(sect):
rlist.append((sect, [x+v for x in cfg.options(sect)
if fnmatch.fnmatch(x, pat)]))
return [x for x in rlist if x[1]]
else:
return [(opt[:i], [opt[i+1:]])]
def reset(self, options=None, local=True, name=None):
for pat in options:
for sect, opts in self._parse_opt(pat):
self._remove(sect, opts, local, name)
def run(self, options=None, local=True, name=None):
lines = []
if options and len(options) == 1 and options[0].find('=') == -1:
for sect, opts in self._parse_opt(options[0]):
title = 'Section: %s' % sect
lines.extend(['', '-' * 60, title])
self.infos = [], [], [], []
for opt in opts:
self._list_value(sect, opt, local, name)
lines.extend(['', 'Current settings'])
lines.extend(self.infos[0])
lines.extend(['', 'Global settings'])
lines.extend(self.infos[1])
lines.extend(['', 'Local settings'])
lines.extend(self.infos[2])
if name:
lines.extend(['', 'Private "%s" settings' % name])
lines.extend(self.infos[3])
elif options:
pairs = []
prev, op = None, ''
for opt in options:
i = opt.find('=')
if i > 0:
pairs.append([opt[:i], opt[i+1:]])
elif opt in ('+', '=', '-', '^'):
op = opt
elif prev:
pairs.append((prev, op + opt))
prev, op = None, ''
else:
prev = opt
if prev:
raise CliError('no value for option "%s"' % prev)
for pat, value in pairs:
sect_opts = self._parse_opt(pat)
if not sect_opts:
logger.debug('new builder option "%s"', pat)
sect_opts = [('builder', [pat])]
for sect, opts in sect_opts:
title = 'Section: %s' % sect
lines.extend(['', '-' * 60, title])
self.infos = [], [], [], []
for opt in opts:
self._set_option(sect, opt, value, local, name)
lines.extend(['', 'Current settings'])
lines.extend(self.infos[0])
lines.extend(['', 'Global settings'])
lines.extend(self.infos[1])
lines.extend(['', 'Local settings'])
lines.extend(self.infos[2])
if name:
lines.extend(['', 'Private "%s" settings' % name])
lines.extend(self.infos[3])
else:
for sect in self.SECTIONS:
title = 'Section: %s' % sect
lines.extend(['', '-' * 60, title, ''])
lines.extend(self.list_options(sect, local, name))
print('\n'.join(lines))