-
Notifications
You must be signed in to change notification settings - Fork 19
/
_update-metadata.py
executable file
·122 lines (95 loc) · 3.23 KB
/
_update-metadata.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
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
#!/usr/bin/env python3
# script to help with managing the series in a metadata.yaml file
# it provides four commands: list, add, remove and ensure
# - list the series in metadata.yaml;
# - add a series if it doesn't exist
# - remove a series if it does exist
# - ensure that the metadata contains the items on the args
import os
import sys
import collections
# from ruamel.yaml import YAML
import ruamel.yaml as YAML
Command = collections.namedtuple('Command', ['charm', 'cmd', 'params'])
def usage():
print("usage: {} <charm> [list | add | remove | ensure] [<series, ...]"
.format(sys.argv[0]))
def parse_args():
if len(sys.argv) < 3:
usage()
sys.exit(1)
charm = sys.argv[1]
cmd = sys.argv[2].lower()
if cmd not in ["list", "add", "remove", "ensure"]:
print("Command '{}' not recognised.".format(cmd))
usage()
sys.exit(1)
if cmd in ["add", "remove"]:
if len(sys.argv) < 4:
print("Command '{}' requires at least 1 argument".format(cmd))
usage()
sys.exit(1)
param = [sys.argv[3]]
elif cmd == "ensure":
param = sys.argv[3:]
else:
param = []
return Command(charm, cmd, param)
def run_command(cmd, yml):
if cmd.cmd == "list":
list_series(yml)
elif cmd.cmd == "add":
add_series(cmd, yml)
elif cmd.cmd == "remove":
remove_series(cmd, yml)
# NOTE(ajkavanagh) - this hasn't actually been written yet.
# elif cmd.cmd == "ensure":
# ensure_series_is(cmd, yml)
else:
print("Command?? {}".format(cmd))
sys.exit(1)
def metadata_file(cmd):
"""Find the metadata.yaml file"""
charm_dir = os.path.join("charms", cmd.charm)
if not os.path.isdir(charm_dir):
print("dir: {} doesn't exist.".format(charm_dir))
sys.exit(1)
metadata_in_root = os.path.join(charm_dir, 'metadata.yaml')
if (os.path.isfile(metadata_in_root) and
not os.path.islink(metadata_in_root)):
return metadata_in_root
metadata_in_src = os.path.join(charm_dir, 'src', 'metadata.yaml')
if os.path.isfile(metadata_in_src):
return metadata_in_src
print("Can't find metadata!")
sys.exit(1)
def load_yaml(cmd):
with open(metadata_file(cmd)) as f:
return YAML.load(f, YAML.RoundTripLoader)
def write_yaml(cmd, yml):
with open(metadata_file(cmd), "w") as f:
YAML.dump(yml, f, Dumper=YAML.RoundTripDumper)
def list_series(yml):
print("Series are: {}".format(", ".join(yml['series'])))
def add_series(cmd, yml):
series = cmd.params[0].lower()
if series in yml['series']:
print("Series '{}' already present; continuing".format(series))
return
yml['series'].append(series)
print("Adding series '{}' to metadata".format(series))
write_yaml(cmd, yml)
def remove_series(cmd, yml):
series = cmd.params[0].lower()
if series not in yml['series']:
print("Series '{}' not present; ignoring remove".format(series))
return
yml['series'].remove(series)
print("Removed series '{}' from metadata".format(series))
write_yaml(cmd, yml)
def run():
cmd = parse_args()
yml = load_yaml(cmd)
run_command(cmd, yml)
if __name__ == "__main__":
run()