-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcodebom
More file actions
executable file
·79 lines (71 loc) · 2.67 KB
/
codebom
File metadata and controls
executable file
·79 lines (71 loc) · 2.67 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
#!/usr/bin/env python
import sys
from codebom.parseargs import parse_args
from codebom.bom import BomError
from codebom.lint import load_linted_bom
from codebom.verify import verify_bom
from codebom.scan import scan_bom
from codebom.analyze import analyze_bom
from codebom.graph import graph_bom
from ruamel import yaml
import os.path
import io
def bail(err):
pos = err.pos
location = "{}:{}:{}".format(pos.src, pos.line, pos.col)
# Print an error message to stderr and exit the process.
sys.exit("{}: error: {}".format(location, err.msg))
def print_graph(dot, out):
"""
Print the 'dot' graph to out.name. The extension of out.name determines the format.
"""
name, ext = os.path.splitext(out.name)
fmt = ext[1:]
if name == '<stdout>':
out.write(dot.source)
out.write('\n')
elif fmt:
out.write(dot.pipe(fmt))
else:
out.write(bytearray(dot.source, 'utf-8'))
if __name__ == '__main__':
args = parse_args(sys.argv[1:])
if args.f is None:
if args.command == 'scan' and args.add:
args.f = io.StringIO(u'')
setattr(args.f, 'name', '<stdin>')
else:
sys.exit("codebom: error: '.bom.yaml' not found. Create one or point to an existing one with the '-f' option.")
try:
bom = load_linted_bom(args.f, args.f.name)
if args.command == 'verify':
is_source_dist = args.source_distribution
check_origins = args.check_origins
tgt_dir = os.path.dirname(args.o.name)
data = verify_bom(bom, tgt_dir, is_source_dist=is_source_dist, check_origins=check_origins)
args.o.write(yaml.dump(data, Dumper=yaml.RoundTripDumper))
args.o.close()
elif args.command == 'scan':
data = scan_bom(
bom,
is_source_dist=args.source_distribution,
scan_components=args.recursive,
add_declarations=args.add,
coalesce=args.coalesce,
run_in_parallel=True)
if args.add:
args.o.write(yaml.dump(data, Dumper=yaml.RoundTripDumper))
args.o.close()
elif args.command == 'analyze':
is_source_dist = args.source_distribution
messages = analyze_bom(bom, is_source_dist=is_source_dist)
if messages:
args.o.write('\n'.join(messages))
args.o.write('\n')
args.o.close()
elif args.command == 'graph':
is_source_dist = args.source_distribution
dot = graph_bom(bom, is_source_dist=is_source_dist)
print_graph(dot, args.o)
except BomError as err:
bail(err)