-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathfix_dm_versions.py
More file actions
executable file
·80 lines (66 loc) · 2.61 KB
/
Copy pathfix_dm_versions.py
File metadata and controls
executable file
·80 lines (66 loc) · 2.61 KB
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
#!/usr/bin/env python
import argparse
import shutil
from datetime import datetime
from lxml import etree
desc = """
Fix shed_data_manager_conf.xml
Modifies the guid and version attribute of data_manager tags:
- the version in the guid (text after the last slash) is replaced
by the data manager tool version
- the version attribute is set to the data manager tool version
By default only data managers with duplicated guid are modified
and version attributes are not added if absent.
A copy of the original file with a time stamp appended to the name
is created.
Note, if there are versions of the data manager tool that have the
same version there will still be DMs with duplicated guids. These
need to be corrected manually.
"""
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
"shed_data_manager_conf",
metavar="CONFIG_FILE",
type=str,
default="config/shed_data_manager_conf.xml",
help="an integer for the accumulator",
)
parser.add_argument(
"--all-entries", action="store_true", help="modify all entries (default only those with duplicated guid)"
)
parser.add_argument("--add-version", action="store_true", help="also add version attribute if absent")
parser.add_argument("--dry-run", action="store_true", help="do not write resulting config file")
args = parser.parse_args()
with open(args.shed_data_manager_conf) as fh:
tree = etree.parse(args.shed_data_manager_conf)
root = tree.getroot()
guid_mapping = {}
for dm in root.iter("data_manager"):
guid = dm.attrib["guid"]
if guid not in guid_mapping:
guid_mapping[guid] = [dm]
else:
guid_mapping[guid].append(dm)
for guid in guid_mapping:
if len(guid_mapping[guid]) > 1:
print(f"{guid} found {len(guid_mapping[guid])}x")
elif not args.all_entries:
continue
for dm in guid_mapping[guid]:
tool_version = dm.find("./tool/version")
tool_version = tool_version.text
new_guid = f"{guid[: guid.rfind('/')]}/{tool_version}"
dm.attrib["guid"] = new_guid
print(f"changing guid: {guid} -> {new_guid}")
if "version" in dm.attrib:
print(f"changing version: {dm.attrib['version']} -> {tool_version}")
dm.attrib["version"] = tool_version
elif args.add_version:
print(f"adding version: {tool_version}")
dm.attrib["version"] = tool_version
if not args.dry_run:
nfn = args.shed_data_manager_conf + datetime.now().isoformat()
print(f"save copy at {nfn}")
shutil.copyfile(args.shed_data_manager_conf, nfn)
print(f"saving {args.shed_data_manager_conf}")
tree.write(args.shed_data_manager_conf)