Getting SVN revision number into a program automatically

后端 未结 11 1478
孤城傲影
孤城傲影 2020-12-02 21:07

I have a python project under SVN, and I\'m wanting to display the version number when it is run. Is there any way of doing this (such as automatically running a short scrip

相关标签:
11条回答
  • 2020-12-02 21:50

    I'm not sure about the Python specifics, but if put the string $Revision$ into your file somewhere and you have enable-auto-props=true in your SVN config, it'll get rewritten to something like $Revision: 144$. You could then parse this in your script.

    There are a number of property keywords you can use in this way.

    This won't have any overhead, e.g. querying the SVN repo, because the string is hard-coded into your file on commit or update.

    I'm not sure how you'd parse this in Python but in PHP I'd do:

    $revString = '$Revision: 144$';
    if(preg_match('/: ([0-9]+)\$/', $revString, $matches) {
        echo 'Revision is ' . $matches[1];
    }
    
    0 讨论(0)
  • 2020-12-02 21:54

    Similar to, but a little more pythonic than the PHP answer; put this in your module's __init__.py:

    __version__ = filter(str.isdigit, "$Revision: 13 $")
    

    and make sure you add the Revision property:

    svn propset svn:keywords Revision __init__.py
    
    0 讨论(0)
  • 2020-12-02 21:54

    I do this by simply running a small script when building my project. The script just calls svn info in combination with sed to get out the bare revision data and injects that number into the revision.txt file.

    The Hudson build server makes it even easier as it sets the SVN revision number as an environment variable right before invoking your Build scripts.

    0 讨论(0)
  • 2020-12-02 21:54

    This worked for me in Python:

    Get all the svn info:

    svn_info = subprocess.check_output('svn info').decode("utf-8") 
    print(svn_info)
    

    Do your search and split off the number:

    revisionNum = re.search("Revision:\s\d+", svn_info)
    print(revisionNum.group().split(":")[1])
    
    0 讨论(0)
  • 2020-12-02 21:54

    You could do this programmatically by parsing the output from 'svn info --xml' to an xml Element:

    def get_svn_rev(dir: Union[str, Path]) -> str:
        """Get the svn revision of the given directory"""
        dir = str(dir)
        svn_info_cmd = ['svn', 'info', '--xml', dir]
        print(' '.join(svn_info_cmd))
        svn_info_process = subprocess.run(svn_info_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if svn_info_process.returncode:
            eprint(svn_info_process.stderr.decode().strip())
            raise subprocess.CalledProcessError(returncode=svn_info_process.returncode, cmd=svn_info_cmd,
                                                output=svn_info_process.stdout,
                                                stderr=svn_info_process.stderr)
        else:
            info = ElementTree.fromstring(svn_info_process.stdout.decode().strip())
            entry = info.find('entry')
            revision = entry.get('revision')
    
        return revision
    
    0 讨论(0)
提交回复
热议问题