This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
version.py
52 lines (45 loc) · 1.71 KB
/
version.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
import email.utils
import textwrap
from os import getenv
def version():
"""
Extract static version part from VERSION file,
git HEAD commit hash and message (if git module can be imported) and
CircleCI build number.
:return: (str, str, str, str)
"""
static_version = open('VERSION').read().strip()
try:
import git
except ImportError:
commit = msg = None
else:
repo = git.Repo('.')
head = repo.head.object
commit = str(head)
msg = head.message
build_number = getenv('CIRCLE_BUILD_NUM', '0')
return static_version, commit, msg, build_number
def version_string(static_version, commit_hash, build_number):
"""
Format a full version string version.build_number~commit_hash.
:param static_version: manually managed (static) version part, e.g. 0.1.5
:param commit_hash: commit hash (optional)
:param build_number: build number (doesn't have to be a number
:return: str
"""
return '{}.{}~{}'.format(static_version, build_number, commit_hash[:7]) if commit_hash \
else '{}.{}'.format(static_version, build_number)
def write_changelog():
import debian.changelog
ver, commit, msg, build_number = version()
ver_str = version_string(ver, commit, build_number)
ch = debian.changelog.Changelog(open('debian/changelog'))
ch.new_block(package='wott-agent',
version=ver_str,
distributions='stable',
urgency='medium',
author="%s <%s>" % debian.changelog.get_maintainer(),
date=email.utils.formatdate(None, True))
ch.add_change(textwrap.indent(msg, ' * '))
ch.write_to_open_file(open('debian/changelog', 'w'))