Getting SVN revision number into a program automatically

后端 未结 11 1477
孤城傲影
孤城傲影 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条回答
  • Or you can do like this:

    import re,subprocess
    
    svn_info = subprocess.check_output("svn info")
    
    print (re.search(ur"Revision:\s\d+", svn_info)).group()
    

    it prints "Revision: 2874" in my project

    Or like this:

    print (subprocess.check_output("svnversion")).split(":")[0]
    

    it prints "2874" in my project

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

    There's a snippet of code in Django that allows you to do this, I'd recommend looking at it. http://code.djangoproject.com/browser/django/trunk/django/utils/version.py

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

    Check out pysvn. it exposes extensions in Python for Subversion runtime functionality.

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

    Re-answering because I had trouble finding all the information for python 3. In python 3 you can use the following:

    import re
    
    revstring = '$Revision: 123 $'
    revnumber = re.sub(r'\D', '', revstring)
    

    For SVN to replace the number, the file-keyword "Revision" has to be set. you achieve this by typing the following command into a terminal:

    svn propset svn:keywords Revision YOURFILE.py
    

    Moreover, in the subversion configuration file the property enable-auto-props has to be set to yes. This should already be the case. In Tortoise SVN this config file can be accessed by clicking the button "edit" next to "subversion configuration file" in the general settings tab.

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

    I find the when it is run section slightly ambiguous - when it is run from where ? From an SVN checkout or from released code ? All the solutions work on injecting the SVN version somehow into the Python source or somewhere in the repository in a separate version file.

    The version file is changed on fresh checkouts, so clean checkouts need to be done before distribution. So my question of where the program is being run remains.

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

    The file hooks/pre-commit in your repository is a script or program which will be executed before a successful commit. Simply make this a script which updates your file with the proper version number as it is being committed (the revision number is passed to your script as it's run). Here's a tutorial with an example of writing a pre-commit hook: http://wordaligned.org/articles/a-subversion-pre-commit-hook

    0 讨论(0)
提交回复
热议问题