产生一个子进程并调用hg为了收集输出。我使用类似this。有一点缩短,在本质上(我希望我没有被缩短的基本知识引入错误,这是PY3,虽然):
def get_child_output(cmd):
"""
Run a child process, and collect the generated output.
@param cmd: Command to execute.
@type cmd: C{list} of C{str}
@return: Generated output of the command, split on whitespace.
@rtype: C{list} of C{str}
"""
return subprocess.check_output(cmd, universal_newlines = True).split()
def get_hg_version():
path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
version = ''
version_list = get_child_output(['hg', '-R', path, 'id', '-n', '-i'])
hash = version_list[0].rstrip('+')
# Get the date of the commit of the current NML version in days since January 1st 2000
ctimes = get_child_output(["hg", "-R", path, "parent", "--template='{date|hgdate} {date|shortdate}\n'"])
ctime = (int((ctimes[0].split("'"))[1]) - 946684800) // (60 * 60 * 24)
cversion = str(ctime)
# Combine the version string
version = "v{}:{} from {}".format(cversion, hash, ctimes[2].split("'", 1)[0])
return version
# Save the revision in the proper variable
__revision__ = get_hg_version()
最后:考虑不使用(只)hg id -n
输出作为版本号:这是一个只对该特定回购实例具有本地特性的值,可能会在相同回购的不同克隆之间有所不同。使用哈希和/或提交时间作为版本(以及)。
谢谢!你可以评论如何在源文件中嵌入从get_hg_version()获得的__revision__值吗?所以,当我想要发布项目文件进行部署时,我希望项目中的所有源文件都具有__revision__值嵌入其中。这通常如何实现? – Imran
在构建和捆绑过程中,我生成一个__version__.py文件,其中包含版本 - 并且可以在没有版本库的情况下在程序中使用/查询。 – planetmaker
有没有很好的参考资料或材料来查看构建和捆绑过程的工作流程? – Imran