kapsikkum-unmanic – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3  
4 """
5 unmanic.versioninfo.py
6  
7 Written by: Josh.5 <jsunnex@gmail.com>
8 Date: 04 May 2020, (10:52 AM)
9  
10 Copyright:
11 Copyright (C) Josh Sunnex - All Rights Reserved
12  
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19  
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22  
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
28 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
29 OR OTHER DEALINGS IN THE SOFTWARE.
30  
31 """
32  
33 import io
34 import os
35 import subprocess
36 import sys
37  
38 import unmanic.metadata as version_info
39  
40 DESCRIPTION = "TEST"
41  
42  
43 def name():
44 return str(version_info.__name)
45  
46  
47 def version():
48 if is_git_vcs():
49 git_version_info = get_git_version_info()
50 return git_version_info['short']
51 else:
52 return str(version_info.__version__)
53  
54  
55 def full_version():
56 if is_git_vcs():
57 git_version_info = get_git_version_info()
58 return git_version_info['long']
59 else:
60 return str(version_info.__version__)
61  
62  
63 def description():
64 return str(version_info.__description__)
65  
66  
67 def author():
68 return str(version_info.__author)
69  
70  
71 def email():
72 return str(version_info.__email)
73  
74  
75 def url():
76 """
77 Fetch the URL from the project version_info
78  
79 :return:
80 """
81 return str(version_info.__website__)
82  
83  
84 def branch_version():
85 return version()[:3]
86  
87  
88 def is_pre_release():
89 """
90 Returns either true if this is a alpha or beta release
91  
92 :return:
93 """
94 full_version_string = full_version()
95 return "alpha" in full_version_string.lower() or "beta" in full_version_string.lower()
96  
97  
98 def dev_status():
99 """
100 Returns the python module Development Status classifier
101 based on if the version string is alpha/beta or stable
102  
103 :return:
104 """
105 full_version_string = full_version()
106 if 'alpha' in full_version_string.lower():
107 return 'Development Status :: 3 - Alpha'
108 elif 'beta' in full_version_string or 'rc' in full_version_string.lower():
109 return 'Development Status :: 4 - Beta'
110 else:
111 return 'Development Status :: 5 - Production/Stable'
112  
113  
114 def changes():
115 """
116 Extract part of changelog pertaining to version.
117  
118 :return:
119 """
120 _version = version_info.__version__
121 with io.open(os.path.join(get_base_dir(), "CHANGES.txt"), 'r', encoding='utf8') as f:
122 lines = []
123 for line in f:
124 if line.startswith('====='):
125 if len(lines) > 1:
126 break
127 if lines:
128 lines.append(line)
129 elif line.startswith(_version):
130 lines.append(line)
131 return ''.join(lines[:-1])
132  
133  
134 def get_base_dir():
135 return os.path.abspath(os.path.dirname(sys.argv[0]))
136  
137  
138 def is_git_vcs():
139 """
140 Checks if the build is executed from a git VCS root
141  
142 :return:
143 """
144 if subprocess.call(["git", "branch"], stderr=subprocess.STDOUT, stdout=open(os.devnull, 'w')) != 0:
145 return False
146 else:
147 return True
148  
149  
150 def get_git_version_info():
151 """
152 Parse version information from git.
153  
154 Returns a long and short version string:
155 eg. short - 0.0.1 (full release)
156 eg. short - 0.0.1b2 (beta release)
157 eg. short - 0.0.1b2.post3 (beta release with commits since the last tag)
158 eg. long - 0.0.1~68b3db6 (full release)
159 eg. long - 0.0.1-beta2~68b3db6 (beta release)
160 eg. long - 0.0.1-beta2+68b3db6 (beta release with commits since the last tag)
161 eg. long - 0.0.1-beta2~68b3db6+dirty (beta release with uncommitted changes)
162 eg. long - 0.0.1-beta2+68b3db6+dirty (beta release with commits since the last tag & uncommitted changes)
163  
164 :return:
165 """
166 # Fetch the last tag
167 last_tag = subprocess.check_output(["git", "describe", "--tags", "--abbrev=0"]).strip().decode("utf-8")
168 # Fetch the current commit ID
169 current_commit = subprocess.check_output(["git", "rev-parse", "--verify", "--short", "HEAD"]).strip().decode("utf-8")
170  
171 # Create a long and short version string to return
172 short_version_string = last_tag
173 long_version_string = last_tag
174  
175 # Fetch the amount of commits since the last tag
176 distance_since_last_tag = subprocess.check_output(
177 ["git", "rev-list", last_tag + "..HEAD", "--count"]
178 ).strip().decode("utf-8")
179  
180 # Normalize short version string (saves getting spammed with useless warnings from setuptools about it)
181 if '-alpha' in short_version_string.lower():
182 short_version_string = short_version_string.replace("-alpha", "a")
183 elif '-beta' in short_version_string.lower():
184 short_version_string = short_version_string.replace("-beta", "b")
185 elif '-rc' in short_version_string.lower():
186 short_version_string = short_version_string.replace("-rc", "rc")
187  
188 # Append a post tag if this is not a clean tagged build
189 if int(distance_since_last_tag) > 0:
190 # There are commits since the last tag
191 # Modify the version strings
192 short_version_string = '{}.post{}'.format(short_version_string, distance_since_last_tag)
193 long_version_string = '{}+{}'.format(long_version_string, current_commit)
194 else:
195 long_version_string = '{}~{}'.format(long_version_string, current_commit)
196  
197 # Check if there are uncommitted changes on the directory
198 git_diff_status = subprocess.check_output(
199 "git diff-index --quiet HEAD -- || echo 'is_dirty'", shell=True
200 ).strip().decode("utf-8")
201 if git_diff_status == 'is_dirty':
202 # There are commits since the last tag
203 long_version_string = '{}+dirty'.format(long_version_string)
204  
205 return_dic = {
206 'short': short_version_string,
207 'long': long_version_string
208 }
209  
210 return return_dic