Changeset 18917 in josm


Ignore:
Timestamp:
2023-12-19T16:41:58+01:00 (3 years ago)
Author:
taylor.smock
Message:

since_xxx.py: Add optional parameter for changesets

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/since_xxx.py

    r12928 r18917  
    1 #!/usr/bin/python
     1#!/usr/bin/python3
    22# License: CC0
    33
    44"""
    5 Helper script to replace "@since xxx" in Javadoc by the upcoming revision number.
     5Helper script to replace "@since xxx" in Javadoc by the upcoming revision
     6number.
    67
    78Will retrieve the current revision number from the server. It runs over all
    … …  
    1314import subprocess
    1415import re
     16import sys
    1517
    1618revision = None
    1719
    18 def main():
     20
     21def main() -> None:
     22    """
     23    Do the main work of the script.
     24    :return: Nothing
     25    """
     26    args = sys.argv
    1927    svn_status = subprocess.check_output("svn status --xml".split(" "))
    20     for el in ElementTree.fromstring(svn_status).findall("./target/entry"):
    21         if  el.find('wc-status').get("item") not in ["added", "modified"]:
    22             continue
    23         path = el.get("path")
    24         if not path.endswith('.java'):
    25             continue
    26         with open(path, 'r') as f:
    27             filedata = f.read()
    28         filedata2 = re.sub("since xxx", lambda _: "since {}".format(get_revision()), filedata)
    29         if filedata != filedata2:
    30             print("replacing 'since xxx' with 'since {}' in '{}'".format(get_revision(), path))
    31             with open(path, 'w') as f:
    32                 f.write(filedata2)
     28    tree = ElementTree.fromstring(svn_status)
     29    rev = get_revision()
     30    if len(args) == 2:
     31        for change_set in tree.findall("./changelist"):
     32            if (
     33                "name" in change_set.attrib
     34                and change_set.attrib["name"] == args[1]
     35            ):
     36                for el in change_set.findall("./entry"):
     37                    write_xxx(rev, el)
     38    elif len(args) > 2:
     39        raise ValueError(
     40            "Too many args: only one changelist should be passed at a time, "
     41            "or none at all "
     42        )
     43    else:
     44        for el in tree.findall("./target/entry"):
     45            write_xxx(rev, el)
    3346
    34 def get_revision():
     47
     48def write_xxx(rev: int, el: ElementTree.Element) -> None:
     49    """
     50    Write a revision to a changed file
     51    :param rev: The revision to write
     52    :param el: The element containing the path to the file to update
     53    :return: Nothing
     54    """
     55    if el.find("wc-status").get("item") not in ["added", "modified"]:
     56        return
     57    path = el.get("path")
     58    if not path.endswith(".java"):
     59        return
     60    with open(path, "r") as f:
     61        old_text = f.read()
     62    new_text = re.sub("since xxx", lambda _: "since {}".format(rev), old_text)
     63    if old_text != new_text:
     64        print(
     65            "replacing 'since xxx' with 'since {}' in '{}'".format(rev, path)
     66        )
     67        with open(path, "w") as f:
     68            f.write(new_text)
     69
     70
     71def get_revision() -> int:
     72    """
     73    Get the next revision
     74    :return: The current revision + 1
     75    """
    3576    global revision
    3677    if revision is not None:
    3778        return revision
    3879    svn_info_local = subprocess.check_output("svn info --xml".split(" "))
    39     rep_url = ElementTree.fromstring(svn_info_local).findtext("./entry/repository/root")
    40     svn_info_server = subprocess.check_output("svn info --xml".split(" ") + [rep_url])
    41     revision = int(ElementTree.fromstring(svn_info_server).find("./entry").get("revision")) + 1
     80    rep_url = ElementTree.fromstring(svn_info_local).findtext(
     81        "./entry/repository/root"
     82    )
     83    svn_info_server = subprocess.check_output(
     84        "svn info --xml".split(" ") + [rep_url]
     85    )
     86    revision = (
     87        int(
     88            ElementTree.fromstring(svn_info_server)
     89            .find("./entry")
     90            .get("revision")
     91        )
     92        + 1
     93    )
    4294    return revision
    43    
    44 main()
     95
     96
     97if __name__ == "__main__":
     98    main()
Note: See TracChangeset for help on using the changeset viewer.