2017-02-27 74 views
0

我定義了一個Python文件模板中pycharm 2016.3包括善變修訂版本號蟒蛇文件模板pycharm

__author__ = ${USER} 
__date__ = ${DATE} 
__copyright__ = "" 
__credits__ = [""] 
__license__ = "" 
__revision__ = "" 
__maintainer__ = ${USER} 
__status__ = "Development" 

的版本號如下我想用命令的輸出「汞ID -n 「這給了我從mercurial中提取的當前版本號。

什麼是最好的方法來做到這一點?

回答

1

產生一個子進程並調用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輸出作爲版本號:這是一個只對該特定回購實例具有本地特性的值,可能會在相同回購的不同克隆之間有所不同。使用哈希和/或提交時間作爲版本(以及)。

+0

謝謝!你可以評論如何在源文件中嵌入從get_hg_version()獲得的__revision__值嗎?所以,當我想要發佈項目文件進行部署時,我希望項目中的所有源文件都具有__revision__值嵌入其中。這通常如何實現? – Imran

+0

在構建和捆綁過程中,我生成一個__version__.py文件,其中包含版本 - 並且可以在沒有版本庫的情況下在程序中使用/查詢。 – planetmaker

+0

有沒有很好的參考資料或材料來查看構建和捆綁過程的工作流程? – Imran