Coverage for src/hods/version.py: 100.00%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

32 statements  

1"""hods - home directory synchronization. 

2 

3Copyright (C) 2016-2020 Mathias Stelzer <knoppo@rolln.de> 

4 

5hods is free software: you can redistribute it and/or modify 

6it under the terms of the GNU General Public License as published by 

7the Free Software Foundation, either version 3 of the License, or 

8(at your option) any later version. 

9 

10hods is distributed in the hope that it will be useful, 

11but WITHOUT ANY WARRANTY; without even the implied warranty of 

12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

13GNU General Public License for more details. 

14 

15You should have received a copy of the GNU General Public License 

16along with this program. If not, see <http://www.gnu.org/licenses/>. 

17""" 

18import datetime 

19import os 

20import subprocess 

21from collections import namedtuple 

22 

23Version = namedtuple('Version', ['major', 'minor', 'patch', 'pre_release', 'sub']) 

24 

25 

26# Change the version here! 

27# * Use pre_release 'final' and sub 0 for stable 

28# * Use pre_release 'alpha' and sub 0 for development (instead of 'dev') 

29# 

30# Examples: 

31# (0, 1, 0, 'final', 0) -> '0.1.0' 

32# (42, 1, 3, 'final', 0) -> '42.1.3' 

33# (0, 1, 0, 'alpha', 0) -> '0.1.0.dev20170101133742' 

34# (0, 1, 0, 'alpha', 1) -> '0.1.0a1' 

35# (1, 0, 1, 'rc', 0) -> '1.0.1rc0' 

36# (1, 0, 1, 'rc', 2) -> '1.0.1rc2' 

37VERSION = Version(0, 5, 0, 'alpha', 0) 

38 

39 

40def get_version(version): 

41 """Return a PEP 440-compliant version.""" 

42 assert len(version) == 5 

43 assert version[3] in ('alpha', 'beta', 'rc', 'final') 

44 

45 parts = 2 if version[2] == 0 else 3 

46 main = '.'.join([str(p) for p in version[:parts]]) 

47 

48 sub = '' 

49 if version[3] == 'alpha' and version[4] == 0: 

50 timestamp = get_git_commit_timestamp() 

51 if not timestamp: 

52 timestamp = 0 

53 sub = '.dev{}'.format(timestamp) 

54 elif version[3] != 'final': 

55 mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'} 

56 sub = mapping[version[3]] + str(version[4]) 

57 

58 return main + sub 

59 

60 

61def get_git_commit_timestamp(): 

62 """Return a numeric identifier of the latest git changeset. 

63 

64 The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format. 

65 This value isn't guaranteed to be unique, but collisions are very unlikely, 

66 so it's sufficient for generating the development version numbers. 

67 """ 

68 repo_dir = os.path.dirname(os.path.abspath(__file__)) 

69 git_log = subprocess.Popen( 

70 'git log --pretty=format:%ct --quiet -1 HEAD', 

71 stdout=subprocess.PIPE, stderr=subprocess.PIPE, 

72 shell=True, cwd=repo_dir, universal_newlines=True, 

73 ) 

74 timestamp = git_log.communicate()[0] 

75 try: 

76 timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)) 

77 except ValueError: 

78 return None 

79 return timestamp.strftime('%Y%m%d%H%M%S') 

80 

81 

82__version_info__ = VERSION 

83__version__ = get_version(__version_info__)