|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | +packagesByCategory = defaultdict(str) |
| 8 | + |
| 9 | + |
| 10 | +def sort_write_wiki_content(file_path): |
| 11 | + """ |
| 12 | + Retrieves rows pertaining to the key category, as a result of |
| 13 | + iterating over packagesByCategory. |
| 14 | + Writes to the Markdown formatted string: |
| 15 | + - header with the category name |
| 16 | + - table with two columns: Package and Description of each of the packages |
| 17 | + Args: |
| 18 | + file_path: path of the wiki file |
| 19 | + """ |
| 20 | + wikiContent = "" |
| 21 | + for category in packagesByCategory.keys(): |
| 22 | + wikiContent += "## " + category + "\n\n" |
| 23 | + wikiContent += "| Package | Description |\n" |
| 24 | + wikiContent += "|---|---|\n" |
| 25 | + wikiContent += packagesByCategory[category] + "\n\n" |
| 26 | + with open(file_path, "w", encoding="utf-8") as f: |
| 27 | + f.write(wikiContent) |
| 28 | + |
| 29 | + |
| 30 | +def find_element_text(parent, tag_name): |
| 31 | + """Retrieves the text from a xml node, it validates if the tag exists |
| 32 | + Args: |
| 33 | + parent: parent tree to search |
| 34 | + tag_name: tag to search the text |
| 35 | + Returns: |
| 36 | + the tag text value |
| 37 | + """ |
| 38 | + tag = parent.find(f"{{*}}{tag_name}") |
| 39 | + if tag is None: |
| 40 | + return None |
| 41 | + return tag.text |
| 42 | + |
| 43 | + |
| 44 | +def process_packages_directory(packages_dir): |
| 45 | + """Parses all the nuspec files in the 'packages' directory structure. |
| 46 | + It saves into packagesByCategory[category] a line containing the package |
| 47 | + name and the description separated by | |
| 48 | + Args: |
| 49 | + packages: directory of packages |
| 50 | + """ |
| 51 | + if not os.path.isdir(packages_dir): |
| 52 | + raise FileNotFoundError(f"Packages directory not found: {packages_dir}") |
| 53 | + |
| 54 | + for nuspec_path in pathlib.Path(packages_dir).glob("**/*.nuspec"): |
| 55 | + nuspec_tree = ET.parse(nuspec_path) |
| 56 | + nuspec_metadata = nuspec_tree.find("{*}metadata") |
| 57 | + if nuspec_metadata is not None: |
| 58 | + category = find_element_text(nuspec_metadata, "tags") |
| 59 | + # we are only interested in the packages with a category |
| 60 | + # some packages that assist with the istallation (such as common.vm) do |
| 61 | + # not contain a category |
| 62 | + if category is None: |
| 63 | + continue |
| 64 | + else: |
| 65 | + description = find_element_text(nuspec_metadata, "description") |
| 66 | + package = find_element_text(nuspec_metadata, "id") |
| 67 | + """packagesByCategory is a dictionary of str where each package and description |
| 68 | + are appended formatted in Markdown""" |
| 69 | + packagesByCategory[category] += "|" + package + "|" + description + "|\n" |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + |
| 74 | + parser = argparse.ArgumentParser(description="Create wiki of packages.") |
| 75 | + parser.add_argument("--packages", type=str, required=False, default="packages", help="Path to packages") |
| 76 | + parser.add_argument("--wiki", type=str, required=False, default="wiki/Packages.md", help="Path to the wiki") |
| 77 | + args = parser.parse_args() |
| 78 | + process_packages_directory(args.packages) |
| 79 | + sort_write_wiki_content(args.wiki) |
0 commit comments