-
Notifications
You must be signed in to change notification settings - Fork 37
/
migrate.py
73 lines (58 loc) · 2.2 KB
/
migrate.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
"""Convert to Anod newer API."""
from __future__ import annotations
from e3.anod.error import SandBoxError
from e3.anod.sandbox.action import SandBoxAction
from e3.os.fs import cd
class SandBoxMigrate(SandBoxAction):
name = "migrate"
help = "Migrate to a newer anod API version"
require_sandbox = False
def add_parsers(self) -> None:
self.parser.add_argument("api_version", help="API version")
self.parser.add_argument("spec_dir", help="Path to spec directory")
def run(self, args):
if args.api_version != "1.5":
raise SandBoxError("Only 1.5 is supported for now")
cd(args.spec_dir)
migrate_v1_5()
def migrate_v1_5() -> None:
"""Migrate to API 1.5.
Move all .yaml files in subdirs, when the name of a yaml
file matches a spec name move the yaml file in
<spec name>/config.yaml
When there is an additional suffix, move it to
<spec name>/<suffix>.yaml
Make sure to run e3-plan-checker after running this script.
"""
from glob import glob
import os
from e3.fs import mv
for f in glob("*.yaml"):
print("looking at %s" % f)
name, _ = os.path.splitext(os.path.basename(f))
if os.path.exists(name + ".anod"):
try:
os.mkdir(name)
except OSError:
pass
try:
mv(name + ".yaml", os.path.join(name, "config.yaml"))
except OSError: # defensive code
print("error for %s" % name)
elif "-" in name:
print("suffix detected in %s" % f)
prefix, suffix = name.rsplit("-", 1)
if not os.path.exists(prefix + ".anod"):
prefix, suffix2 = prefix.rsplit("-", 1)
suffix = suffix2 + "-" + suffix
try:
try:
os.mkdir(prefix)
except OSError: # defensive code
pass
mv(name + ".yaml", os.path.join(prefix, "%s.yaml" % suffix))
except Exception as er: # defensive code
print(f"error for {name}.yaml {prefix} {suffix}")
print(er)
else:
print("unknown yaml file %s.yaml" % name)