|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import xml.etree.ElementTree as ET |
| 4 | + |
| 5 | +from pre_commit_hooks.exceptions import PreCommitException |
| 6 | + |
| 7 | + |
| 8 | +def tc_version_pinned(xml_content: str) -> bool: |
| 9 | + root = ET.fromstring(xml_content) |
| 10 | + |
| 11 | + return ( |
| 12 | + "TcVersionFixed" in root.attrib and root.attrib.get("TcVersionFixed") == "true" |
| 13 | + ) |
| 14 | + |
| 15 | + |
| 16 | +def get_tc_version(xml_content: str) -> str: |
| 17 | + root = ET.fromstring(xml_content) |
| 18 | + |
| 19 | + return root.attrib.get("TcVersion") |
| 20 | + |
| 21 | + |
| 22 | +def fix_tc_version(xml_content: str, new_version: str) -> str: |
| 23 | + pattern = r'(TcVersion=")([^"]*)(")' |
| 24 | + new_xml_content = re.sub(pattern, r"\g<1>" + new_version + r"\g<3>", xml_content) |
| 25 | + |
| 26 | + return new_xml_content |
| 27 | + |
| 28 | + |
| 29 | +def fix_pinned_version(xml_content: str, pin_version: bool) -> str: |
| 30 | + new_value = "true" if pin_version else "false" |
| 31 | + |
| 32 | + pattern = r'(TcVersionFixed=")([^"]*)(")' |
| 33 | + |
| 34 | + if re.search(pattern, xml_content): |
| 35 | + new_xml_content = re.sub(pattern, r"\g<1>" + new_value + r"\g<3>", xml_content) |
| 36 | + else: |
| 37 | + version_pattern = r'(TcVersion="[^"]*")' |
| 38 | + new_xml_content = re.sub( |
| 39 | + version_pattern, r'\g<1> TcVersionFixed="' + new_value + r'"', xml_content |
| 40 | + ) |
| 41 | + |
| 42 | + return new_xml_content |
| 43 | + |
| 44 | + |
| 45 | +def main(args=None): |
| 46 | + if args is None: |
| 47 | + parser = argparse.ArgumentParser() |
| 48 | + parser.add_argument( |
| 49 | + "filenames", nargs="+", help="List of tsproj filenames to process." |
| 50 | + ) |
| 51 | + parser.add_argument( |
| 52 | + "--target-version", type=str, help="Target TwinCAT version to enforce." |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--fix", |
| 56 | + action="store_true", |
| 57 | + help="Fix the versions if they do not match the target version and fix the pinned state if combined with --pinned/no-pinned.", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--reason", type=str, help="Reason for targeting a specific version." |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--pinned", |
| 64 | + action=argparse.BooleanOptionalAction, |
| 65 | + help="Check if the TwinCAT version should be pinned. Applies or removes pinning if combined with --fix.", |
| 66 | + ) |
| 67 | + |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + try: |
| 71 | + versions = {} |
| 72 | + pinned = {} |
| 73 | + for filename in args.filenames: |
| 74 | + with open(filename, "r") as file: |
| 75 | + xml_content = file.read() |
| 76 | + versions[filename] = get_tc_version(xml_content) |
| 77 | + pinned[filename] = tc_version_pinned(xml_content) |
| 78 | + |
| 79 | + itemize = "\n -" |
| 80 | + exception_message = "" |
| 81 | + if args.target_version: |
| 82 | + mismatched_files = [ |
| 83 | + fname for fname, ver in versions.items() if ver != args.target_version |
| 84 | + ] |
| 85 | + if mismatched_files: |
| 86 | + reason_msg = f"\nReason: {args.reason}" if args.reason else "" |
| 87 | + if args.fix: |
| 88 | + for filename in mismatched_files: |
| 89 | + with open(filename, "r") as file: |
| 90 | + xml_content = file.read() |
| 91 | + fixed_content = fix_tc_version(xml_content, args.target_version) |
| 92 | + with open(filename, "w") as file: |
| 93 | + file.write(fixed_content) |
| 94 | + |
| 95 | + print( |
| 96 | + f"Fixed TwinCAT versions for:{itemize}{itemize.join(mismatched_files)}{reason_msg}" |
| 97 | + ) |
| 98 | + else: |
| 99 | + exception_message += ( |
| 100 | + "The following files are not set to the targeted TwinCAT version " |
| 101 | + f"{args.target_version}:{itemize}{itemize.join(mismatched_files)}{reason_msg}" |
| 102 | + ) |
| 103 | + else: |
| 104 | + unique_versions = set(versions.values()) |
| 105 | + if len(unique_versions) > 1: |
| 106 | + exception_message += ( |
| 107 | + "Not all files have the same TwinCAT version:" |
| 108 | + f"{itemize}" |
| 109 | + + itemize.join(f"{fname}: {ver}" for fname, ver in versions.items()) |
| 110 | + ) |
| 111 | + |
| 112 | + if args.pinned is not None: |
| 113 | + mismatched_files = [ |
| 114 | + fname for fname, pin in pinned.items() if pin != args.pinned |
| 115 | + ] |
| 116 | + if mismatched_files: |
| 117 | + if args.fix: |
| 118 | + for filename in mismatched_files: |
| 119 | + with open(filename, "r") as file: |
| 120 | + xml_content = file.read() |
| 121 | + fixed_content = fix_pinned_version(xml_content, args.pinned) |
| 122 | + with open(filename, "w") as file: |
| 123 | + file.write(fixed_content) |
| 124 | + print( |
| 125 | + f"Fixed pinned state for:{itemize}{itemize.join(mismatched_files)}" |
| 126 | + ) |
| 127 | + else: |
| 128 | + should_be_pinned_message = ( |
| 129 | + "The following files should have a pinned TwinCAT version" |
| 130 | + if args.pinned |
| 131 | + else "The following files should NOT have a pinned TwinCAT version" |
| 132 | + ) |
| 133 | + exception_message += "\n\n" if len(exception_message) > 0 else "" |
| 134 | + exception_message += f"{should_be_pinned_message}{itemize}{itemize.join(mismatched_files)}" |
| 135 | + if len(exception_message) > 0: |
| 136 | + raise PreCommitException(exception_message) |
| 137 | + |
| 138 | + return 0 |
| 139 | + except Exception as exc: |
| 140 | + print(exc) |
| 141 | + return 1 |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + exit(main()) |
0 commit comments