~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to make-buildvars

  • Committer: Curtis Hovey
  • Date: 2014-08-01 12:44:38 UTC
  • Revision ID: curtis@canonical.com-20140801124438-l48516pldkzh7g5n
Do not show all the files in the tarball because it distracts from the test output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
3
 
from argparse import ArgumentParser
4
 
import json
5
 
import logging
 
3
from textwrap import dedent
6
4
import re
7
5
import subprocess
8
6
import sys
9
 
from textwrap import dedent
10
7
import urllib2
11
8
 
 
9
from bzrlib.branch import Branch
 
10
from bzrlib.plugin import load_plugins
 
11
from bzrlib.revisionspec import RevisionSpec
 
12
 
12
13
 
13
14
def parse_version(string):
14
15
    return re.search('^const version = "(.*)"', string, re.M).group(1)
34
35
        return None, None
35
36
    if len(found.split('\n')) > 1:
36
37
        raise Exception(
37
 
            "More than one commit matched the branch or tag:\n{}".format(
38
 
                found))
 
38
            "More than one commit matched the branch or tag:\n{}".format(found))
39
39
    commit, ref = found.split('\t')
40
40
    return commit, ref
41
41
 
59
59
        commit, ref = ls_remote(repo_url, revision_spec, tag=True)
60
60
    if commit is None:
61
61
        commit = revision_spec
62
 
    return None, commit
63
 
 
64
 
 
65
 
def get_git_version(branch, commit):
66
 
    protocol, branch_name, repo_name = branch.split(':')
67
62
    domain, user, repo = repo_name.split('/')
68
63
    template = 'https://raw.githubusercontent.com/{}/{}/{}/version/version.go'
69
64
    file_url = template.format(user, repo, commit)
70
65
    response = urllib2.urlopen(file_url)
71
 
    return parse_version(response.read())
72
 
 
73
 
 
74
 
if __name__ == '__main__':
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:'):
85
 
        logging.error('Launchpad branches are no longer supported.')
86
 
        sys.exit(1)
87
 
    revno, revision_id = get_git_revision_info(branch, revision_spec)
88
 
    version = None
89
 
    version_str = ''
 
66
    version = parse_version(response.read())
 
67
    return None, commit, version
 
68
 
 
69
 
 
70
def get_bzr_revision_info(branch, revision_spec):
 
71
    """Find the revision_id, revno and juju-core version of a branch.
 
72
 
 
73
    :param branch: The location of the branch.
 
74
    :param revision_spec: a human-readable revision spec like '-1', 'revno:5'
 
75
        or 'revid:asdf'
 
76
    :return: a tuple of revno, revision-id, version
 
77
    """
 
78
    candidate = Branch.open(branch)
 
79
    candidate.lock_read()
90
80
    try:
91
 
        version = get_git_version(branch, revision_id)
92
 
        version_str = str(version)
 
81
        info = RevisionSpec.from_string(revision_spec).in_history(candidate)
 
82
        tree = candidate.repository.revision_tree(info.rev_id)
 
83
        tree.lock_read()
 
84
        try:
 
85
            file_id = tree.path2id('version/version.go')
 
86
            v_bytes = tree.get_file_text(file_id, path='version/version.go')
 
87
        finally:
 
88
            tree.unlock()
 
89
        version = parse_version(v_bytes)
 
90
        return info.revno, info.rev_id, version
93
91
    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
 
 
 
92
        candidate.unlock()
 
93
 
 
94
 
 
95
if __name__ == '__main__':
 
96
    load_plugins()
 
97
    branch = sys.argv[1]
 
98
    if sys.argv[1].startswith('lp:'):
 
99
        revision_spec = sys.argv[2] if len(sys.argv) > 2 else 'revno:-1'
 
100
        revno, revision_id, version = get_bzr_revision_info(
 
101
            branch, revision_spec)
 
102
    else:
 
103
        revision_spec = sys.argv[2] if len(sys.argv) > 2 else None
 
104
        revno, revision_id, version = get_git_revision_info(
 
105
            branch, revision_spec)
 
106
    with open('buildvars.bash', 'w') as f:
 
107
        f.write(dedent("""\
 
108
            export BRANCH=%s
 
109
            export REVISION_ID='%s'
 
110
            export VERSION=%s
 
111
        """ % (branch, revision_id, version)))
 
112
        if revno is not None:
 
113
            f.write('export REVNO=%s\n' % revno)