-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaml_2_req.py
83 lines (60 loc) · 2.11 KB
/
yaml_2_req.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
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
# https://pip.pypa.io/en/stable/reference/pip_install/
# https://pyyaml.org/wiki/PyYAMLDocumentation
import argparse
import os
import re
import sys
import yaml
def main(argv=sys.argv):
p = get_argparse()
ns = p.parse_args(argv[1:])
os.makedirs(ns.req, exist_ok=True)
for yaml_filename in os.listdir(ns.yaml):
with open(os.path.join(ns.yaml, yaml_filename), 'rt') as yaml_fp:
yaml_txt = yaml_fp.read()
req_txt = yaml_to_req(yaml_txt)
req_filename = os.path.splitext(yaml_filename)[0] + '.txt'
if not ns.dry_run:
with open(os.path.join(ns.req, req_filename), 'wt') as req_fp:
req_fp.write(req_txt)
else:
print('## write to', os.path.join(ns.req, yaml_filename))
print(req_txt)
def get_argparse():
p = argparse.ArgumentParser()
p.add_argument('--yaml', type=str, default='tests', help='folder')
p.add_argument('--req', type=str, default='req', help='folder')
p.add_argument('--dry-run', default=False, action='store_true')
return p
def yaml_to_req(yaml_txt):
p = yaml.load(yaml_txt)
req_txt = ''
if 'name' in p:
req_txt += f"# name : {p['name']}\n"
if 'channels' in p:
req_txt += "# channels :\n"
for ch in p['channels']:
req_txt += f"#\t{ch}\n"
for lib in p['dependencies']:
if '>=' in lib:
lib_replaced = lib.replace('>=', ' >= ')
elif '<=' in lib:
lib_replaced = lib.replace('<=', ' <= ')
elif '==' in lib:
lib_replaced = lib.replace('==', ' == ')
elif '!=' in lib:
lib_replaced = lib.replace('!=', ' != ')
elif '~=' in lib:
lib_replaced = lib.replace('~=', ' ~= ')
elif '>' in lib:
lib_replaced = lib.replace('>', ' > ')
elif '<' in lib:
lib_replaced = lib.replace('<', ' < ')
elif '=' in lib:
lib_replaced = lib.replace('=', ' == ')
else:
lib_replaced = lib
req_txt += f"{lib_replaced}\n"
return req_txt
if "__main__" == __name__:
main(sys.argv)