Debian Codegraph @

scripts/import_transforms.py

raw
1 """
2 Script that imports transform schemas from asdf-standard.
3 This file can be removed once the format of the new schemas
4 is finalized.
5 """
6 import argparse
7 import glob
8 import os
9 import re
10
11 import pkg_resources
12
13 TAG_PATTERN = re.compile(r"^tag: .*?\n", re.MULTILINE | re.DOTALL)
14 ID_PATTERN = re.compile(r"^id: .*?/([^/-]+)-.*?\n", re.MULTILINE | re.DOTALL)
15 METASCHEMA_PATTERN = re.compile(r"^\$schema: .*?\n", re.MULTILINE | re.DOTALL)
16
17 # References to schemas outside of the transforms directory.
18 EXTERNAL_REF_PATTERN = re.compile(r'\$ref: "?\.\./(.*?[0-9]\.[0-9]\.[0-9])"?')
19
20 # Any $ref that hasn't been converted to an http URI by the
21 # previous regex will be a reference to another transform.
22 INTERNAL_REF_PATTERN = re.compile(r'\$ref: "?(?!http)(.*?)-[0-8]\.[0-9]\.[0-9]"?')
23
24 # These (found in the examples) will be updated to absolute tags:
25 BANG_TRANSFORM = re.compile(r"!transform/([^ \n-]*)-[^ \n]*")
26 BANG_UNIT = re.compile(r"!unit/([^ \n]*)")
27 BANG_CORE = re.compile(r"!core/([^ \n]*)")
28
29 # URI prefix (both id and tag) of new schemas.
30 URI_PREFIX = "http://asdf-format.org/schemas/transform"
31
32 # Initial version of new schemas.
33 VERSION = "2.0.0"
34
35 # For now just leave this at draft-01, but when we remove tag
36 # we'll need to bring in draft-02.
37 METASCHEMA = "http://stsci.edu/schemas/yaml-schema/draft-01"
38
39
40 def parse_args():
41 parser = argparse.ArgumentParser()
42 parser.add_argument("source_path")
43 parser.add_argument("dest_path")
44 return parser.parse_args()
45
46
47 args = parse_args()
48
49
50 paths = list(glob.glob(os.path.join(args.source_path, "*.yaml")))
51 paths_by_name = {}
52 for path in paths:
53 filename = os.path.basename(path)
54 name = filename.split("-")[0]
55 version = filename.split("-")[-1].rsplit(".", 1)[0]
56 if name not in paths_by_name:
57 paths_by_name[name] = []
58 paths_by_name[name].append((path, version))
59
60
61 latest_paths = []
62 for name, name_paths in paths_by_name.items():
63 latest_paths.append(sorted(name_paths, key=lambda p: pkg_resources.parse_version(p[-1]))[-1][0])
64
65
66 for path in latest_paths:
67 with open(path) as f:
68 content = f.read()
69
70 filename = os.path.basename(path)
71 new_filename = filename.split("-")[0] + f"-{VERSION}.yaml"
72
73 schema_name = ID_PATTERN.search(content).group(1)
74 new_uri = f"{URI_PREFIX}/{schema_name}-{VERSION}"
75
76 new_id = f"id: {new_uri}\n"
77 content = ID_PATTERN.sub(new_id, content)
78
79 new_tag = f"tag: {new_uri}\n"
80 content = TAG_PATTERN.sub(new_tag, content)
81
82 content = METASCHEMA_PATTERN.sub(f"$schema: {METASCHEMA}\n", content)
83
84 content = EXTERNAL_REF_PATTERN.sub(r"$ref: http://stsci.edu/schemas/asdf/\1", content)
85 content = INTERNAL_REF_PATTERN.sub(rf"$ref: \1-{VERSION}", content)
86
87 content = BANG_TRANSFORM.sub(rf"!<{URI_PREFIX}/\1-{VERSION}>", content)
88
89 # TODO: Update these once core schemas start using http:// URIs:
90 content = BANG_UNIT.sub(r"!<tag:stsci.edu:asdf/unit/\1>", content)
91 content = BANG_CORE.sub(r"!<tag:stsci.edu:asdf/core/\1>", content)
92
93 output_path = os.path.join(args.dest_path, new_filename)
94
95 with open(output_path, "w") as f:
96 f.write(content)