產生一個子進程並調用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