From 3eba1c25c742969da2152ca422c7afe2368121f1 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Tue, 25 Jul 2017 04:10:22 +0300 Subject: [PATCH] Change version util and version number. --- aiogram/__init__.py | 2 +- aiogram/utils/versions.py | 41 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/aiogram/__init__.py b/aiogram/__init__.py index 93dc0b7a..a42aaf6a 100644 --- a/aiogram/__init__.py +++ b/aiogram/__init__.py @@ -2,6 +2,6 @@ from .utils.versions import Version, Stage from .bot import Bot -VERSION = Version(0, 3, 2, stage=Stage.FINAL, build=3) +VERSION = Version(0, 3, 3, stage=Stage.DEV, build=0) __version__ = VERSION.version diff --git a/aiogram/utils/versions.py b/aiogram/utils/versions.py index 61de1c26..d25607c6 100644 --- a/aiogram/utils/versions.py +++ b/aiogram/utils/versions.py @@ -1,5 +1,11 @@ +import datetime +import os +import subprocess + from .helper import Helper, HelperMode, Item +# Based on https://github.com/django/django/blob/master/django/utils/version.py + class Version: def __init__(self, major=0, minor=0, maintenance=0, stage='final', build=0): @@ -40,6 +46,10 @@ class Version: def build(self): return self.__raw_version[4] + @property + def raw_version(self): + return self.raw_version + def get_version(self): """ Returns a PEP 440-compliant version number from VERSION. @@ -56,9 +66,13 @@ class Version: main = self.get_main_version() sub = '' - - if version[3] != 'final': - mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'} + print(version[3], Stage.DEV, version[3] == Stage.DEV) + if version[3] == Stage.DEV and version[4] == 0: + git_changeset = self.get_git_changeset() + if git_changeset: + sub = '.dev{0}'.format(git_changeset) + elif version[3] != Stage.FINAL: + mapping = {Stage.ALPHA: 'a', Stage.BETA: 'b', Stage.RC: 'rc', Stage.DEV: 'dev'} sub = mapping[version[3]] + str(version[4]) return str(main + sub) @@ -73,6 +87,26 @@ class Version: parts = 2 if version[2] == 0 else 3 return '.'.join(str(x) for x in version[:parts]) + def get_git_changeset(self): + """Return a numeric identifier of the latest git changeset. + The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format. + This value isn't guaranteed to be unique, but collisions are very unlikely, + so it's sufficient for generating the development version numbers. + """ + repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + print(repo_dir) + git_log = subprocess.Popen( + 'git log --pretty=format:%ct --quiet -1 HEAD', + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + shell=True, cwd=repo_dir, universal_newlines=True, + ) + timestamp = git_log.communicate()[0] + try: + timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)) + except ValueError: + return None + return timestamp.strftime('%Y%m%d%H%M%S') + def __str__(self): return self.version @@ -87,3 +121,4 @@ class Stage(Helper): ALPHA = Item() BETA = Item() RC = Item() + DEV = Item()