Created
October 19, 2023 12:41
-
-
Save cmutel/6049694745440a91fd1ea20dce52547e to your computer and use it in GitHub Desktop.
Fix the `minorRelease` in ecospold files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from pathlib import Path | |
from typing import Union | |
from lxml import etree, objectify | |
def fix_version( | |
filepath: Union[str, Path], | |
major_version: Union[str, int], | |
minor_version: Union[str, int], | |
) -> Path: | |
assert Path(filepath).is_file(), "Given path is not a file" | |
assert os.access(fp, os.R_OK), "Can't read given file" | |
assert os.access(fp, os.W_OK), "Can't write corrected file" | |
assert len(major_version) == 1 and int( | |
major_version | |
) < 11, "Major version must be a single number" | |
assert len(minor_version) < 3 and int( | |
minor_version | |
) < 11, "Minor version must be a single number" | |
data = objectify.parse(open(fp)).getroot() | |
if hasattr(data, "childActivityDataset"): | |
ad = getattr(data, "childActivityDataset") | |
else: | |
ad = getattr(data, "activityDataset") | |
fa = ad.administrativeInformation.fileAttributes | |
if not fa.get("majorRelease"): | |
raise ValueError(f"Can't find valid `majorRelease` in {filepath}") | |
if not fa.get("minorRelease"): | |
raise ValueError(f"Can't find valid `minorRelease` in {filepath}") | |
fa.set("majorRelease", major_version) | |
fa.set("minorRelease", minor_version) | |
with open(filepath, "wb") as f: | |
f.write(etree.tostring(data, encoding="utf8", pretty_print=True, xml_declaration=True)) | |
if __name__ == "__main__": | |
if not len(sys.argv) == 4: | |
raise ValueError("Must give filepath, major version, and minor version") | |
fp, major, minor = Path(sys.argv[1]), sys.argv[2], sys.argv[3] | |
fix_version(fp, major, minor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment