forked from ros/rosdistro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_rosdep_yaml.py
executable file
·55 lines (45 loc) · 1.54 KB
/
clean_rosdep_yaml.py
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
#!/usr/bin/env python
import yaml
import argparse
import re
dont_bracket = ['uri', 'md5sum']
def paddify(s, l):
a = s.split('\n')
buf = ''
pad = ' ' * l
for i, r in enumerate(a[:-1]):
buf += "%s%s\n" % (pad, r)
return buf
def quote_if_necessary(s):
if type(s) is list:
return [quote_if_necessary(a) for a in s]
return re.search('{a: (.*)}\n', yaml.dump({'a': s})).group(1)
def prn(n, nm, lvl):
pad = ' ' * lvl
if isinstance(n, list):
return "%s%s: [%s]\n" % (pad, nm, ', '.join(quote_if_necessary(n)))
elif n is None:
return "%s%s:\n" % (pad, nm)
elif isinstance(n, str):
if len(n.split('\n')) > 1:
return "%s%s: |\n%s" % (pad, nm, paddify(n, lvl+1))
else:
if nm in dont_bracket:
return "%s%s: %s\n" % (pad, nm, quote_if_necessary(n))
return "%s%s: [%s]\n" % (pad, nm, ', '.join(quote_if_necessary(n.split())))
buf = "%s%s:\n" % (pad, nm)
for a in sorted(n.keys()):
buf += prn(n[a], a, lvl+1)
return buf
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Cleans a rosdep YAML file to a correct format')
parser.add_argument('infile', help='input rosdep YAML file')
parser.add_argument('outfile', help='output YAML file to be written')
args = parser.parse_args()
with open(args.infile) as f:
iny = yaml.load(f.read())
buf = ''
for a in sorted(iny):
buf += prn(iny[a], a, 0)
with open(args.outfile, 'w') as f:
f.write(buf)