|
| 1 | +"""Check <ref/> links on a stitched XML configuration file.""" |
| 2 | + |
| 3 | +from lxml import etree |
| 4 | + |
| 5 | + |
| 6 | +def check_ref_to_subdeliverable( |
| 7 | + ref: etree._Element, |
| 8 | + attrs: dict[str, str], |
| 9 | +) -> str | None: |
| 10 | + """Check reference to a subdeliverable.""" |
| 11 | + product = attrs.get('product') |
| 12 | + docset = attrs.get('docset') |
| 13 | + dc = attrs.get('dc') |
| 14 | + subdeliverable = attrs.get('subdeliverable') |
| 15 | + # Build the XPath |
| 16 | + xpath = ( # Use // to be more robust against nesting |
| 17 | + f'//product[@productid = {product!r}]/docset[@setid = {docset!r}]' |
| 18 | + f"/builddocs/language[@default = 'true' or @default = 1]" |
| 19 | + f"/deliverable[dc = '{dc}'][subdeliverable = '{subdeliverable}']" |
| 20 | + ) |
| 21 | + |
| 22 | + if not ref.getroottree().xpath(xpath): |
| 23 | + origin = ref.xpath( |
| 24 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 25 | + ) |
| 26 | + return ( |
| 27 | + f'Failed reference from {origin!r} to ' |
| 28 | + f'{product}/{docset}:{dc}#{subdeliverable}: ' |
| 29 | + 'Referenced subdeliverable does not exist.' |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def check_ref_to_deliverable(ref: etree._Element, attrs: dict[str, str]) -> str | None: |
| 34 | + """Check reference to a deliverable.""" |
| 35 | + product = attrs.get('product') |
| 36 | + docset = attrs.get('docset') |
| 37 | + dc = attrs.get('dc') |
| 38 | + |
| 39 | + # Build the XPath |
| 40 | + base_xpath = ( # Use // to be more robust against nesting |
| 41 | + f'//product[@productid = {product!r}]/docset[@setid = {docset!r}]' |
| 42 | + f"/builddocs/language[@default = 'true' or @default = 1]" |
| 43 | + f"/deliverable[dc = '{dc}']" |
| 44 | + ) |
| 45 | + xpath_has_sub = f'{base_xpath}[subdeliverable]' |
| 46 | + xpath_hasnot_sub = f'{base_xpath}[not(subdeliverable)]' |
| 47 | + tree = ref.getroottree() |
| 48 | + |
| 49 | + origin = ref.xpath( |
| 50 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 51 | + ) |
| 52 | + # A reference to a deliverable is only valid if it exists and |
| 53 | + # has no subdeliverables. |
| 54 | + if tree.xpath(xpath_hasnot_sub): |
| 55 | + return None # Valid reference found |
| 56 | + |
| 57 | + # If we are here, it's an invalid reference. We need to find out why. |
| 58 | + if tree.xpath(xpath_has_sub): |
| 59 | + return ( |
| 60 | + f'Failed reference from {origin!r} to {product}/{docset}:{dc}: ' |
| 61 | + 'Referenced deliverable has subdeliverables, ' |
| 62 | + 'you must choose a subdeliverable in your reference.' |
| 63 | + ) |
| 64 | + else: |
| 65 | + return ( |
| 66 | + f'Failed reference from {origin!r} to {product}/{docset}:{dc}: ' |
| 67 | + 'Referenced deliverable does not exist.' |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def check_ref_to_link(ref: etree._Element, attrs: dict[str, str]) -> str | None: |
| 72 | + """Check ref to an external link.""" |
| 73 | + product = attrs.get('product') |
| 74 | + docset = attrs.get('docset') |
| 75 | + link = attrs.get('link') |
| 76 | + |
| 77 | + xpath = ( # Use // to be more robust against nesting |
| 78 | + f'//product[@productid = {product!r}]/docset[@setid = {docset!r}]' |
| 79 | + f"/builddocs/language[@default = 'true' or @default = 1]" |
| 80 | + f"/external/link[@linkid = '{link}']" |
| 81 | + ) |
| 82 | + |
| 83 | + tree = ref.getroottree() |
| 84 | + if not tree.xpath(xpath): |
| 85 | + origin = ref.xpath( |
| 86 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 87 | + ) |
| 88 | + return ( |
| 89 | + f'Failed reference from {origin!r} to {product}/{docset}@{link}: ' |
| 90 | + 'Referenced external link does not exist.' |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def check_ref_to_docset(ref: etree._Element, attrs: dict[str, str]) -> str | None: |
| 95 | + """Check reference to a docset.""" |
| 96 | + product = attrs.get('product') |
| 97 | + docset = attrs.get('docset') |
| 98 | + xpath = ( # Use // to be more robust against nesting |
| 99 | + f'//product[@productid = {product!r}]/docset[@setid = {docset!r}]' |
| 100 | + ) |
| 101 | + |
| 102 | + tree = ref.getroottree() |
| 103 | + if not tree.xpath(xpath): |
| 104 | + origin = ref.xpath( |
| 105 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 106 | + ) |
| 107 | + return ( |
| 108 | + f'Failed reference from {origin!r} to {product}/{docset}: ' |
| 109 | + 'Referenced docset does not exist.' |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def check_ref_to_product(ref: etree._Element, attrs: dict[str, str]) -> str | None: |
| 114 | + """Check reference to a product.""" |
| 115 | + product = attrs.get('product') |
| 116 | + |
| 117 | + xpath = f'//product[@productid = {product!r}]' # Use // to be more robust |
| 118 | + tree = ref.getroottree() |
| 119 | + if not tree.xpath(xpath): |
| 120 | + origin = ref.xpath( |
| 121 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 122 | + ) |
| 123 | + return ( |
| 124 | + f'Failed reference from {origin!r} to {product}: ' |
| 125 | + 'Referenced product does not exist.' |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def check_stitched_references(tree: etree._ElementTree) -> list[str]: |
| 130 | + """Check <ref> elements for broken references. |
| 131 | +
|
| 132 | + This function is a Python equivalent of the |
| 133 | + ``global-check-ref-list.xsl`` stylesheet. |
| 134 | +
|
| 135 | + :param tree: The tree of the stitched XML file. |
| 136 | + :returns: A list of error messages for any broken references found. |
| 137 | + """ |
| 138 | + errors: list[str] = [] |
| 139 | + |
| 140 | + # TODO: Make it concurrent using Python's `concurrent.futures` module. |
| 141 | + # Consider using ThreadPoolExecutor or ProcessPoolExecutor to parallelize |
| 142 | + # the processing of <ref> elements. Ensure thread safety when appending |
| 143 | + # to the `errors` list, and handle exceptions raised during concurrent |
| 144 | + # execution to avoid losing error messages. |
| 145 | + for ref in tree.iter('ref'): |
| 146 | + attrs = ref.attrib |
| 147 | + product = attrs.get('product') |
| 148 | + docset = attrs.get('docset') |
| 149 | + dc = attrs.get('dc') |
| 150 | + subdeliverable = attrs.get('subdeliverable') |
| 151 | + link = attrs.get('link') |
| 152 | + |
| 153 | + result = None |
| 154 | + # Case 1: Reference to a subdeliverable |
| 155 | + if subdeliverable and dc and docset and product: |
| 156 | + result = check_ref_to_subdeliverable(ref, attrs) |
| 157 | + |
| 158 | + # Case 2: Reference to a deliverable |
| 159 | + elif dc and docset and product: |
| 160 | + result = check_ref_to_deliverable(ref, attrs) |
| 161 | + |
| 162 | + # Case 3: Reference to an external link |
| 163 | + elif link and docset and product: |
| 164 | + result = check_ref_to_link(ref, attrs) |
| 165 | + |
| 166 | + # Case 4: Reference to a docset |
| 167 | + elif docset and product: |
| 168 | + result = check_ref_to_docset(ref, attrs) |
| 169 | + |
| 170 | + # Case 5: Reference to a product |
| 171 | + elif product: |
| 172 | + result = check_ref_to_product(ref, attrs) |
| 173 | + |
| 174 | + # Case 6: Invalid <ref> element |
| 175 | + else: |
| 176 | + origin = ref.xpath( |
| 177 | + 'concat(ancestor::product/@productid, "/", ancestor::docset/@setid)' |
| 178 | + ) |
| 179 | + result = ( |
| 180 | + f'Reference failed in {origin!r}. ' |
| 181 | + 'This issue should have been caught by the RNC validation: ' |
| 182 | + 'This is a docserv-stitch bug.' |
| 183 | + ) |
| 184 | + |
| 185 | + if result: |
| 186 | + errors.append(result) |
| 187 | + |
| 188 | + return errors |
0 commit comments