forked from rowinggolfer/openmolar2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_manager.py
executable file
·126 lines (106 loc) · 4.6 KB
/
version_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
## ##
## Copyright 2011-2012, Neil Wallace <neil@openmolar.com> ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###############################################################################
'''
This module is used as a "hook" for mercurial so that revision updates occur
whenever an update or commit is performed on the repo (or nested sub repo).
Openmolar uses standard major.minor.point versioning.
This is stored in version_number.py
the majority of the code in this module is taken from
https://bitbucket.org/dowski/mercurial-version-info-plugin
'''
import os
import re
import sys
import time
from mercurial import util
cur_dir = os.path.dirname(__file__)
sys.path.append(cur_dir)
from version_number import VERSION_NUMBER
f = open(os.path.join(cur_dir, "setup.cnf"))
data = f.read()
f.close()
new_data = re.sub("version = \d+\.\d+\.\d+",
"version = %s"% VERSION_NUMBER,
data)
if data != new_data:
print ("Updated Major Version Numbering")
f = open(os.path.join(os.path.dirname(__file__), "setup.cnf"), "w")
f.write(new_data)
f.close()
TEMPLATE = '''\
################################################################################
# file autogenerated by openmolar's version_manager.py #
################################################################################
"version_number for repo %(URL)r"
VERSION = %(version)r
revision_id = %(revision_id)r
revision_number = %(revision_number)r
branch = %(branch)r
tags = %(tags)r
date = %(date)r
'''
def hook(ui, repo, node=None, **params):
url = repo.url()
print ("executing mercurial hook %s"% __file__)
print ("writing version file for hg repo %s"% url)
conf = _load_config(ui)
changeset = repo[node]
if not changeset.node():
# this can happen when this function is called as an extension in a
# working directory. in that case get the first parent changeset.
changeset = changeset.parents()[0]
versionfile = open(os.path.join(repo.root, conf['version_file_path']), 'w')
_write_version_info(url, changeset, versionfile)
def _write_version_info(url, changeset, versionfile):
template_vars = {
'URL':url,
'version':VERSION_NUMBER,
'revision_id':changeset.node().encode('hex'),
'revision_number':changeset.rev(),
'branch':changeset.branch(),
'tags':changeset.tags(),
'date':time.ctime(changeset.date()[0]),
}
versionfile.write(TEMPLATE % template_vars)
def _load_config(ui):
conf = dict(ui.configitems('hgversioninfo'))
if not conf:
raise util.Abort('You need a [hgversioninfo] section in your config')
if not conf.get('version_file_path'):
msg = ('You need to define the version_file_path in your '
'[hgversioninfo] config section.')
raise util.Abort(msg)
return conf
def main():
from mercurial import ui, hg
from mercurial.error import RepoError
repo_paths = ["."]
for subrepo in ("common", "server", "admin", "client"):
repo_paths.append(os.path.join("src", "lib_openmolar", subrepo))
for repo_path in repo_paths:
try:
repo = hg.repository(ui.ui(), repo_path)
hook(repo.ui, repo)
except RepoError:
print ("WARNING skipping subrepo %s - not found"%
os.path.abspath(repo_path))
if __name__ == "__main__":
main()