-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs.py
176 lines (152 loc) · 5.14 KB
/
vcs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
# Copyright (C) Duncan Macleod (2013)
#
# This file is part of GWpy
#
# GWpy 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.
#
# GWpy 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 GWpy. If not, see <http://www.gnu.org/licenses/>
"""Git version generator
"""
__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'
__credits__ = 'Adam Mercer <adam.mercer@ligo.org>'
import os
import time
from distutils.version import (Version, LooseVersion, StrictVersion)
from git import Repo
from jinja2 import Template
VERSION_PY_TEMPLATE = Template("""# -*- coding: utf-8 -*-
{% if 'author' in package %}\
# Copyright (C) {{ package['author'] }} ({{ package['year'] }})
# package metadata
__author__ = "{{ package['author'] }} <{{ package['email'] }}>"\
{% endif %}
__version__ = '{{ version.vstring }}'
__date__ = '{{ status.datestr }}'
# package version
version = '{{ version.vstring }}'
major = {{ version.version[0] }}
minor = {{ version.version[1] }}
micro = {{ version.version[2] }}
debug = {{ not version.vstring.replace('.', '').isdigit() }}
release = {{ version.version[0] > 0 and \
version.vstring.replace('.', '').isdigit() }}
# repository version information
git_hash = '{{ status.commit.hexsha }}'
{% if status.tag %}\
git_tag = '{{ status.tag.name }}'\
{% else %}\
git_tag = None\
{% endif %}
git_branch = '{{ status.branch.name }}'
git_author = "{{ status.author }}"
git_committer = "{{ status.committer }}"
git_is_dirty = {{ status.is_dirty() }}
""")
class GitStatus(object):
def __init__(self, path=os.curdir):
self.repo = Repo(path=path)
def is_dirty(self):
if isinstance(self.repo.is_dirty, bool):
return self.repo.is_dirty
else:
return self.repo.is_dirty()
is_dirty.__doc__ = Repo.is_dirty.__doc__
@property
def commit(self):
try:
return self.branch.commit
except TypeError:
return self.repo.head.commit
@property
def branch(self):
try:
b = self.repo.active_branch
except TypeError:
if len(self.repo.branches) == 1:
return self.repo.branches[0]
else:
raise
else:
if isinstance(b, str):
for branch in self.repo.branches:
if branch.name == b:
return branch
raise RuntimeError("Cannot resolve active branch.")
else:
return b
@property
def tag(self):
for tag in self.tags:
if tag.commit == self.commit:
return tag
return None
@property
def tags(self):
return sorted(self.repo.tags,
key=lambda t: t.commit.committed_date)
@property
def revision(self):
"""The number of commits between the HEAD and the last tag.
"""
try:
tag = self.tags[-1]
except IndexError:
start = ''
else:
start = '%s..' % tag.name
return self.repo.git.rev_list('%sHEAD' % start).count('\n')
@property
def version(self):
if self.tag:
v = self.tag.name.strip('v')
else:
try:
v = self.tags[-1].name.strip('v')
except IndexError:
v = '0.0.0'
if self.is_dirty():
v += '.dev'
elif self.revision:
v += '.dev%d' % self.revision
return LooseVersion(v)
@property
def date(self):
t = self.commit.committed_date
if isinstance(t, time.struct_time):
return t
else:
return time.gmtime(t)
@property
def datestr(self):
return time.strftime('%Y-%m-%d %H:%M:%S +0000', self.date)
@property
def author(self):
return "%s <%s>" % (self.commit.author.name, self.commit.author.email)
@property
def committer(self):
return "%s <%s>" % (self.commit.committer.name,
self.commit.committer.email)
# ------------------------------------------------------------------------
# Write
def write(self, fobj, version=None, **package_metadata):
"""Write the contents of this `GitStatus` to a file object.
"""
if version is None:
version = self.version
if not isinstance(version, Version):
version = LooseVersion(str(version))
if len(version.version) < 3 or not isinstance(version.version[2], int):
version.version.insert(2, 'None')
package_metadata.setdefault('year', time.gmtime().tm_year)
fobj.write(VERSION_PY_TEMPLATE.render(status=self, version=version,
package=package_metadata))