Gentoo Archives: gentoo-commits

From: Matt Turner <mattst88@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:wip/mattst88 commit in: catalyst/
Date: Fri, 29 Jan 2021 23:50:55
Message-Id: 1611964082.e40f31c10a79499a822596948b68fcf6ea3f647f.mattst88@gentoo
1 commit: e40f31c10a79499a822596948b68fcf6ea3f647f
2 Author: Matt Turner <mattst88 <AT> gentoo <DOT> org>
3 AuthorDate: Mon Nov 2 17:43:34 2020 +0000
4 Commit: Matt Turner <mattst88 <AT> gentoo <DOT> org>
5 CommitDate: Fri Jan 29 23:48:02 2021 +0000
6 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=e40f31c1
7
8 catalyst: Switch spec files to TOML
9
10 Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>
11
12 catalyst/config.py | 120 -----------------------------------------------------
13 catalyst/main.py | 26 ++++--------
14 2 files changed, 9 insertions(+), 137 deletions(-)
15
16 diff --git a/catalyst/config.py b/catalyst/config.py
17 deleted file mode 100644
18 index e1963f71..00000000
19 --- a/catalyst/config.py
20 +++ /dev/null
21 @@ -1,120 +0,0 @@
22 -
23 -import re
24 -
25 -from catalyst import log
26 -from catalyst.support import CatalystError
27 -
28 -
29 -class ParserBase():
30 -
31 - filename = ""
32 - lines = None
33 - values = None
34 - key_value_separator = "="
35 - multiple_values = False
36 - empty_values = True
37 - eval_none = False
38 -
39 - def __getitem__(self, key):
40 - return self.values[key]
41 -
42 - def get_values(self):
43 - return self.values
44 -
45 - def dump(self):
46 - dump = ""
47 - for x in self.values:
48 - dump += x + " = " + repr(self.values[x]) + "\n"
49 - return dump
50 -
51 - def parse_file(self, filename):
52 - try:
53 - with open(filename, "r") as myf:
54 - self.lines = myf.readlines()
55 - except:
56 - raise CatalystError("Could not open file " + filename,
57 - print_traceback=True)
58 - self.filename = filename
59 - self.parse()
60 -
61 - def parse_lines(self, lines):
62 - self.lines = lines
63 - self.parse()
64 -
65 - def parse(self):
66 - values = {}
67 - cur_array = []
68 -
69 - trailing_comment = re.compile(r'\s*#.*$')
70 -
71 - for x, myline in enumerate(self.lines):
72 - myline = myline.strip()
73 -
74 - # Force the line to be clean
75 - # Remove Comments ( anything following # )
76 - myline = trailing_comment.sub("", myline)
77 -
78 - # Skip any blank lines
79 - if not myline:
80 - continue
81 -
82 - if self.key_value_separator in myline:
83 - # Split on the first occurence of the separator creating two strings in the array mobjs
84 - mobjs = myline.split(self.key_value_separator, 1)
85 - mobjs[1] = mobjs[1].strip().strip('"')
86 -
87 - # Start a new array using the first element of mobjs
88 - cur_array = [mobjs[0]]
89 - if mobjs[1]:
90 - # do any variable substitiution embeded in it with
91 - # the values already obtained
92 - mobjs[1] = mobjs[1] % values
93 - if self.multiple_values:
94 - # split on white space creating additional array elements
95 - subarray = mobjs[1].split()
96 - cur_array += subarray
97 - else:
98 - cur_array += [mobjs[1]]
99 -
100 - # Else add on to the last key we were working on
101 - else:
102 - if self.multiple_values:
103 - cur_array += myline.split()
104 - else:
105 - raise CatalystError("Syntax error: %s" %
106 - x, print_traceback=True)
107 -
108 - # XXX: Do we really still need this "single value is a string" behavior?
109 - if len(cur_array) == 2:
110 - values[cur_array[0]] = cur_array[1]
111 - else:
112 - values[cur_array[0]] = cur_array[1:]
113 -
114 - if not self.empty_values:
115 - # Make sure the list of keys is static since we modify inside the loop.
116 - for x in list(values.keys()):
117 - # Delete empty key pairs
118 - if not values[x]:
119 - log.warning('No value set for key "%s"; deleting', x)
120 - del values[x]
121 -
122 - if self.eval_none:
123 - # Make sure the list of keys is static since we modify inside the loop.
124 - for x in list(values.keys()):
125 - # reset None values
126 - if isinstance(values[x], str) and values[x].lower() in ['none']:
127 - log.info('None value found for key "%s"; reseting', x)
128 - values[x] = None
129 - self.values = values
130 -
131 -
132 -class SpecParser(ParserBase):
133 -
134 - key_value_separator = ':'
135 - multiple_values = True
136 - empty_values = False
137 - eval_none = True
138 -
139 - def __init__(self, filename=""):
140 - if filename:
141 - self.parse_file(filename)
142
143 diff --git a/catalyst/main.py b/catalyst/main.py
144 index 0de1040f..81495c62 100644
145 --- a/catalyst/main.py
146 +++ b/catalyst/main.py
147 @@ -13,7 +13,6 @@ from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
148 from DeComp.contents import ContentsMap
149
150 from catalyst import log
151 -import catalyst.config
152 from catalyst.context import namespace
153 from catalyst.defaults import (confdefaults, option_messages,
154 DEFAULT_CONFIG_FILE, valid_config_file_values)
155 @@ -276,11 +275,6 @@ def _main(parser, opts):
156 myconfigs = [DEFAULT_CONFIG_FILE]
157 myspecfile = opts.file
158
159 - mycmdline = list()
160 - if opts.snapshot:
161 - mycmdline.append('target: snapshot')
162 - mycmdline.append('snapshot_treeish: ' + opts.snapshot)
163 -
164 conf_values['DEBUG'] = opts.debug
165 conf_values['VERBOSE'] = opts.debug or opts.verbose
166
167 @@ -299,7 +293,7 @@ def _main(parser, opts):
168 options.append('enter-chroot')
169
170 # Make sure we have some work before moving further.
171 - if not myspecfile and not mycmdline:
172 + if not myspecfile and not opts.snapshot:
173 parser.error('please specify one of either -f or -C or -s')
174
175 # made it this far so start by outputting our version info
176 @@ -320,7 +314,6 @@ def _main(parser, opts):
177 # initialize our (de)compression definitions
178 conf_values['decompress_definitions'] = DECOMPRESS_DEFINITIONS
179 conf_values['compress_definitions'] = COMPRESS_DEFINITIONS
180 - # TODO add capability to config/spec new definitions
181
182 if "digests" in conf_values:
183 valid_digests = hashlib.algorithms_available
184 @@ -338,16 +331,15 @@ def _main(parser, opts):
185
186 if myspecfile:
187 log.notice("Processing spec file: %s", myspecfile)
188 - spec = catalyst.config.SpecParser(myspecfile)
189 - addlargs.update(spec.get_values())
190 -
191 - if mycmdline:
192 try:
193 - cmdline = catalyst.config.SpecParser()
194 - cmdline.parse_lines(mycmdline)
195 - addlargs.update(cmdline.get_values())
196 - except CatalystError:
197 - log.critical('Could not parse commandline')
198 + addlargs.update(toml.load(myspecfile))
199 + except Exception as e:
200 + log.critical('Could not find parse spec file: %s: %s',
201 + myspecfile, e)
202 +
203 + if opts.snapshot:
204 + addlargs['target'] = 'snapshot'
205 + addlargs['snapshot_treeish'] = opts.snapshot
206
207 if "target" not in addlargs:
208 raise CatalystError("Required value \"target\" not specified.")