Getting SVN revision number into a program automatically

后端 未结 11 1493
孤城傲影
孤城傲影 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: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
    

提交回复
热议问题