285.1.1
by Aaron Bentley
Generate buildvars.bash via python, include version and revision-id. |
1 |
#!/usr/bin/env python
|
2 |
||
711.1.2
by Aaron Bentley
Emit buildvars as json, add revision_build. |
3 |
from argparse import ArgumentParser |
4 |
import json |
|
712.1.1
by Aaron Bentley
Remove support for Launchpad branches. |
5 |
import logging |
285.1.1
by Aaron Bentley
Generate buildvars.bash via python, include version and revision-id. |
6 |
import re |
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
7 |
import subprocess |
285.1.1
by Aaron Bentley
Generate buildvars.bash via python, include version and revision-id. |
8 |
import sys |
712.1.1
by Aaron Bentley
Remove support for Launchpad branches. |
9 |
from textwrap import dedent |
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
10 |
import urllib2 |
285.1.1
by Aaron Bentley
Generate buildvars.bash via python, include version and revision-id. |
11 |
|
712.1.2
by Aaron Bentley
Remove revno from json, fix lint. |
12 |
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
13 |
def parse_version(string): |
14 |
return re.search('^const version = "(.*)"', string, re.M).group(1) |
|
15 |
||
16 |
||
501.1.2
by Curtis Hovey
Simplify ls_remote use to get heads or tags. |
17 |
def ls_remote(repo_url, ref, head=False, tag=False): |
501.1.7
by Curtis Hovey
Update doc. |
18 |
"""Return a tuple fo the matching commit hash and ref.
|
19 |
||
20 |
None, None is returned when no matching commit was found.
|
|
21 |
||
22 |
:raise: an exception when more than one commit matched
|
|
23 |
:return: a tuple of commit, ref
|
|
24 |
"""
|
|
501.1.2
by Curtis Hovey
Simplify ls_remote use to get heads or tags. |
25 |
command = ['git', 'ls-remote'] |
26 |
if head: |
|
27 |
command.append('--heads') |
|
28 |
if tag: |
|
29 |
command.append('--tags') |
|
30 |
command.extend([repo_url, ref]) |
|
31 |
found = subprocess.check_output(command) |
|
501.1.5
by Curtis Hovey
Let urllib2 raise its error. |
32 |
found = found.strip() |
33 |
if len(found) == 0: |
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
34 |
return None, None |
501.1.5
by Curtis Hovey
Let urllib2 raise its error. |
35 |
if len(found.split('\n')) > 1: |
36 |
raise Exception( |
|
712.1.2
by Aaron Bentley
Remove revno from json, fix lint. |
37 |
"More than one commit matched the branch or tag:\n{}".format( |
38 |
found)) |
|
501.1.5
by Curtis Hovey
Let urllib2 raise its error. |
39 |
commit, ref = found.split('\t') |
40 |
return commit, ref |
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
41 |
|
42 |
||
43 |
def get_git_revision_info(branch, revision_spec): |
|
501.1.7
by Curtis Hovey
Update doc. |
44 |
"""Find the commit and juju-core version of a branch.
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
45 |
|
46 |
Returns a tuple comparable to revno, revision-id, version.
|
|
501.1.7
by Curtis Hovey
Update doc. |
47 |
The revno is always None. The revision-id is the commit hash.
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
48 |
|
49 |
:param branch: The location of the git branch.
|
|
50 |
:param revision_spec: a human-readable revision spec like 'HEAD', '1.18.0',
|
|
51 |
or '1dcf4e4fe1'
|
|
501.1.7
by Curtis Hovey
Update doc. |
52 |
:return: a tuple of None, revision-id, version
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
53 |
"""
|
54 |
protocol, branch_name, repo_name = branch.split(':') |
|
55 |
repo_url = 'https://{}.git'.format(repo_name) |
|
56 |
if revision_spec in (None, '-1', 'HEAD'): |
|
501.1.2
by Curtis Hovey
Simplify ls_remote use to get heads or tags. |
57 |
commit, ref = ls_remote(repo_url, branch_name, head=True) |
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
58 |
else: |
501.1.2
by Curtis Hovey
Simplify ls_remote use to get heads or tags. |
59 |
commit, ref = ls_remote(repo_url, revision_spec, tag=True) |
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
60 |
if commit is None: |
61 |
commit = revision_spec |
|
1283.1.1
by Aaron Bentley
make-buildvars writes revision even if no version. |
62 |
return None, commit |
63 |
||
64 |
||
65 |
def get_git_version(branch, commit): |
|
66 |
protocol, branch_name, repo_name = branch.split(':') |
|
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
67 |
domain, user, repo = repo_name.split('/') |
68 |
template = 'https://raw.githubusercontent.com/{}/{}/{}/version/version.go' |
|
69 |
file_url = template.format(user, repo, commit) |
|
501.1.5
by Curtis Hovey
Let urllib2 raise its error. |
70 |
response = urllib2.urlopen(file_url) |
1283.1.1
by Aaron Bentley
make-buildvars writes revision even if no version. |
71 |
return parse_version(response.read()) |
501.1.1
by Curtis Hovey
Added git querying to make-buildvars. |
72 |
|
73 |
||
285.1.1
by Aaron Bentley
Generate buildvars.bash via python, include version and revision-id. |
74 |
if __name__ == '__main__': |
711.1.2
by Aaron Bentley
Emit buildvars as json, add revision_build. |
75 |
parser = ArgumentParser() |
76 |
parser.add_argument('branch', help='The branch being built') |
|
77 |
parser.add_argument('revision', nargs='?', default=None, |
|
78 |
help='The revision being built') |
|
79 |
parser.add_argument('--revision-build', default=None, |
|
80 |
help='The build number.') |
|
81 |
args = parser.parse_args() |
|
82 |
branch = args.branch |
|
83 |
revision_spec = args.revision |
|
84 |
if branch.startswith('lp:'): |
|
712.1.1
by Aaron Bentley
Remove support for Launchpad branches. |
85 |
logging.error('Launchpad branches are no longer supported.') |
86 |
sys.exit(1) |
|
1283.1.1
by Aaron Bentley
make-buildvars writes revision even if no version. |
87 |
revno, revision_id = get_git_revision_info(branch, revision_spec) |
88 |
version = None |
|
89 |
version_str = '' |
|
90 |
try: |
|
91 |
version = get_git_version(branch, revision_id) |
|
92 |
version_str = str(version) |
|
93 |
finally: |
|
94 |
with open('buildvars.bash', 'w') as f: |
|
95 |
f.write(dedent("""\ |
|
96 |
export BRANCH=%s |
|
97 |
export REVISION_ID='%s' |
|
98 |
export VERSION=%s |
|
99 |
""" % (branch, revision_id, version_str))) |
|
100 |
if revno is not None: |
|
101 |
f.write('export REVNO=%s\n' % revno) |
|
102 |
with open('buildvars.json', 'w') as f: |
|
103 |
json.dump({ |
|
104 |
'branch': branch, |
|
105 |
'revision_id': revision_id, |
|
106 |
'version': version, |
|
107 |
'revision_build': args.revision_build, |
|
108 |
}, f, indent=2) |
|
109 |