forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spread-shellcheck
executable file
·292 lines (240 loc) · 9.81 KB
/
spread-shellcheck
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
#!/usr/bin/env python3
# Copyright (C) 2018 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import subprocess
import argparse
import itertools
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import cpu_count
from typing import Dict
import yaml
# default shell for shellcheck
SHELLCHECK_SHELL = os.getenv('SHELLCHECK_SHELL', 'bash')
# set to non-empty to ignore all errors
NO_FAIL = os.getenv('NO_FAIL')
# set to non empty to enable 'set -x'
D = os.getenv('D')
# set to non-empty to enable verbose logging
V = os.getenv('V')
# set to a number to use these many threads
N = int(os.getenv('N') or cpu_count())
# file with list of files that can fail validation
CAN_FAIL = os.getenv('CAN_FAIL')
# names of sections
SECTIONS = ['prepare', 'prepare-each', 'restore', 'restore-each',
'debug', 'debug-each', 'execute', 'repack']
def parse_arguments():
parser = argparse.ArgumentParser(description='spread shellcheck helper')
parser.add_argument('-s', '--shell', default='bash',
help='shell')
parser.add_argument('-n', '--no-errors', action='store_true',
default=False, help='ignore all errors ')
parser.add_argument('-v', '--verbose', action='store_true',
default=False, help='verbose logging')
parser.add_argument('--can-fail', default=None,
help=('file with list of files that are can fail '
'validation'))
parser.add_argument('-P', '--max-procs', default=N, type=int, metavar='N',
help='run these many shellchecks in parallel (default: %(default)s)')
parser.add_argument('paths', nargs='+', help='paths to check')
return parser.parse_args()
class ShellcheckRunError(Exception):
def __init__(self, stderr):
super().__init__()
self.stderr = stderr
class ShellcheckError(Exception):
def __init__(self, path):
super().__init__()
self.sectionerrors = {}
self.path = path
def addfailure(self, section, error):
self.sectionerrors[section] = error
def __len__(self):
return len(self.sectionerrors)
class ShellcheckFailures(Exception):
def __init__(self, failures=None):
super().__init__()
self.failures = set()
if failures:
self.failures = set(failures)
def merge(self, otherfailures):
self.failures = self.failures.union(otherfailures.failures)
def __len__(self):
return len(self.failures)
def intersection(self, other):
return self.failures.intersection(other)
def difference(self, other):
return self.failures.difference(other)
def __iter__(self):
return iter(self.failures)
def checksection(data, env: Dict[str, str]):
# spread shell snippets are executed under 'set -e' shell, make sure
# shellcheck knows about that
script_data = []
script_data.append('set -e')
for key, value in env.items():
value = str(value)
# Unpack the special "$(HOST: ...) syntax and tell shellcheck not to
# worry about the use of echo to print variable value.
if value.startswith("$(HOST:") and value.endswith(")"):
script_data.append("# shellcheck disable=SC2116")
value = "$({})".format(value[len("$(HOST:"):-1])
# XXX: poor man's shell key=value assignment with values in double
# quotes so that one value can refer to another value.
if '"' in value:
value = value.replace('"', '\"')
# converts
# FOO: "$(HOST: echo $foo)" -> FOO="$(echo $foo)"
# FOO: "$(HOST: echo \"$foo\")" -> FOO="$(echo \"$foo\")"
# FOO: "foo" -> FOO="foo"
script_data.append("{}=\"{}\"".format(key, value))
script_data.append("export {}".format(key))
script_data.append(data)
proc = subprocess.Popen("shellcheck -s {} -x -".format(SHELLCHECK_SHELL),
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=True)
stdout, _ = proc.communicate(input='\n'.join(script_data).encode('utf-8'), timeout=30)
if proc.returncode != 0:
raise ShellcheckRunError(stdout)
def checkfile(path, executor):
logging.debug("checking file %s", path)
with open(path) as inf:
data = yaml.load(inf, Loader=yaml.CSafeLoader)
errors = ShellcheckError(path)
# TODO: handle stacking of environment from other places that influence it:
# spread.yaml -> global env + backend env + suite env -> task.yaml (task
# env + variant env).
env = {}
for key, value in data.get("environment", {}).items():
if "/" in key:
# TODO: re-check with each variant's value set.
key = key.split('/', 1)[0]
env[key] = value
for section in SECTIONS:
if section not in data:
continue
try:
logging.debug("%s: checking section %s", path, section)
checksection(data[section], env)
except ShellcheckRunError as serr:
errors.addfailure(section, serr.stderr.decode('utf-8'))
if path.endswith('spread.yaml') and 'suites' in data:
# check suites
suites_sections_and_futures = []
for suite in data['suites'].keys():
for section in SECTIONS:
if section not in data['suites'][suite]:
continue
logging.debug("%s (suite %s): checking section %s", path, suite, section)
future = executor.submit(checksection, data['suites'][suite][section], env)
suites_sections_and_futures.append((suite, section, future))
for item in suites_sections_and_futures:
suite, section, future = item
try:
future.result()
except ShellcheckRunError as serr:
errors.addfailure('suites/' + suite + '/' + section,
serr.stderr.decode('utf-8'))
if errors:
raise errors
def findfiles(locations):
for loc in locations:
if os.path.isdir(loc):
for root, _, files in os.walk(loc, topdown=True):
for name in files:
if name in ['spread.yaml', 'task.yaml']:
yield os.path.join(root, name)
else:
yield loc
def check1path(path, executor):
try:
checkfile(path, executor)
except ShellcheckError as err:
return err
return None
def checkpaths(locs, executor):
# setup iterator
locations = findfiles(locs)
failed = []
for serr in executor.map(check1path, locations, itertools.repeat(executor)):
if serr is None:
continue
logging.error(('shellcheck failed for file %s in sections: '
'%s; error log follows'),
serr.path, ', '.join(serr.sectionerrors.keys()))
for section, error in serr.sectionerrors.items():
logging.error("%s: section '%s':\n%s", serr.path, section, error)
failed.append(serr.path)
if failed:
raise ShellcheckFailures(failures=failed)
def loadfilelist(flistpath):
flist = set()
with open(flistpath) as inf:
for line in inf:
if not line.startswith('#'):
flist.add(line.strip())
return flist
def main(opts):
paths = opts.paths or ['.']
failures = ShellcheckFailures()
with ThreadPoolExecutor(max_workers=opts.max_procs) as executor:
try:
checkpaths(paths, executor)
except ShellcheckFailures as sf:
failures.merge(sf)
if failures:
if opts.can_fail:
can_fail = loadfilelist(opts.can_fail)
unexpected = failures.difference(can_fail)
if unexpected:
logging.error(('validation failed for the following '
'non-whitelisted files:\n%s'),
'\n'.join([' - ' + f for f in
sorted(unexpected)]))
raise SystemExit(1)
did_not_fail = can_fail - failures.intersection(can_fail)
if did_not_fail:
logging.error(('the following files are whitelisted '
'but validated successfully:\n%s'),
'\n'.join([' - ' + f for f in
sorted(did_not_fail)]))
raise SystemExit(1)
# no unexpected failures
return
logging.error('validation failed for the following files:\n%s',
'\n'.join([' - ' + f for f in sorted(failures)]))
if NO_FAIL or opts.no_errors:
logging.warning("ignoring errors")
else:
raise SystemExit(1)
if __name__ == '__main__':
opts = parse_arguments()
if opts.verbose or D or V:
lvl = logging.DEBUG
else:
lvl = logging.INFO
logging.basicConfig(level=lvl)
if CAN_FAIL:
opts.can_fail = CAN_FAIL
if NO_FAIL:
opts.no_errors = True
if opts.max_procs == 1:
# TODO: temporary workaround for a deadlock when running with a single
# worker
opts.max_procs += 1
logging.warning('workers count bumped to 2 to workaround a deadlock')
main(opts)