1
2
3
4
5
6
7
8
11
13 return json.loads("""
14 {
15 "codename": "Hurricane Ridge",
16 "version": "1.7.0",
17 "post": "0",
18 "rc": "1"
19 }
20 """)
21
22 import json
23 import os
24 import subprocess
25
26 try:
27
28 MY_DIR = os.path.dirname(os.path.abspath(__file__))
29 except:
30 MY_DIR = None
31
33 try:
34 return bool(subprocess.check_output(
35 ["git", "diff", "--name-only"], stderr=subprocess.PIPE,
36 cwd=MY_DIR,
37 ).splitlines())
38 except (OSError, subprocess.CalledProcessError):
39 return False
40
42 try:
43 return os.path.join(subprocess.check_output(
44 ["git", "rev-parse", "--show-toplevel"], stderr=subprocess.PIPE,
45 cwd=MY_DIR,
46 ).strip(), version_file)
47 except (OSError, subprocess.CalledProcessError):
48 return None
49
51 """Returns the number of commits since version.yaml was changed."""
52 try:
53 last_commit_to_touch_version_file = subprocess.check_output(
54 ["git", "log", "--no-merges", "-n", "1", "--pretty=format:%H",
55 version_file], cwd=MY_DIR, stderr=subprocess.PIPE,
56 ).strip()
57
58 all_commits = subprocess.check_output(
59 ["git", "log", "--no-merges", "-n", "1000", "--pretty=format:%H"],
60 stderr=subprocess.PIPE, cwd=MY_DIR,
61 ).splitlines()
62 return all_commits.index(last_commit_to_touch_version_file)
63 except (OSError, subprocess.CalledProcessError, ValueError):
64 return None
65
66
68 try:
69 return subprocess.check_output(
70 ["git", "log", "--no-merges", "-n", "1", "--pretty=format:%H"],
71 stderr=subprocess.PIPE, cwd=MY_DIR,
72 ).strip()
73 except (OSError, subprocess.CalledProcessError):
74 return None
75
77 current_hash = get_current_git_hash()
78
79 if current_hash is None:
80 version_data["error"] = "Not in a git repository."
81
82 else:
83 version_data["revisionid"] = current_hash
84 version_data["dirty"] = is_tree_dirty()
85 version_data["dev"] = number_of_commit_since(
86 get_version_file_path(version_path))
87
88
89 pep440 = version_data["version"]
90 if int(version_data.get("post", 0)) > 0:
91 pep440 += ".post" + version_data["post"]
92
93 elif int(version_data.get("rc", 0)) > 0:
94 pep440 += ".rc" + version_data["rc"]
95
96 if version_data.get("dev", 0):
97
98 last = version_data["version"].rsplit(".", 1)
99 version_data["version"] = "%s.%s" % (last[0], int(last[1]) + 1)
100 pep440 = version_data["version"] + ".dev" + str(version_data["dev"])
101
102 version_data["pep440"] = pep440
103
104 return version_data
105