Bumps OVS version to 2.8 for OVN
[apex.git] / apex / build / build_utils.py
1 ##############################################################################
2 # Copyright (c) 2017 Feng Pan (fpan@redhat.com) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import argparse
11 import git
12 import logging
13 import os
14 from pygerrit2.rest import GerritRestAPI
15 import re
16 import shutil
17 import sys
18
19
20 def clone_fork(args):
21     ref = None
22     logging.info("Cloning {}".format(args.repo))
23
24     try:
25         cm = git.Repo(search_parent_directories=True).commit().message
26     except git.exc.InvalidGitRepositoryError:
27         logging.debug('Current Apex directory is not a git repo: {}'
28                       .format(os.getcwd()))
29         cm = ''
30
31     logging.info("Current commit message: {}".format(cm))
32     m = re.search('{}:\s*(\S+)'.format(args.repo), cm)
33
34     if m:
35         change_id = m.group(1)
36         logging.info("Using change ID {} from {}".format(change_id, args.repo))
37         rest = GerritRestAPI(url=args.url)
38         change_str = "changes/{}?o=CURRENT_REVISION".format(change_id)
39         change = rest.get(change_str)
40         try:
41             assert change['status'] not in 'ABANDONED' 'CLOSED',\
42                 'Change {} is in {} state'.format(change_id, change['status'])
43             if change['status'] == 'MERGED':
44                 logging.info('Change {} is merged, ignoring...'
45                              .format(change_id))
46             else:
47                 current_revision = change['current_revision']
48                 ref = change['revisions'][current_revision]['ref']
49                 logging.info('setting ref to {}'.format(ref))
50         except KeyError:
51             logging.error('Failed to get valid change data structure from url '
52                           '{}/{}, data returned: \n{}'
53                           .format(change_id, change_str, change))
54             raise
55
56     # remove existing file or directory named repo
57     if os.path.exists(args.repo):
58         if os.path.isdir(args.repo):
59             shutil.rmtree(args.repo)
60         else:
61             os.remove(args.repo)
62
63     ws = git.Repo.clone_from("{}/{}".format(args.url, args.repo),
64                              args.repo, b=args.branch)
65     if ref:
66         git_cmd = ws.git
67         git_cmd.fetch("{}/{}".format(args.url, args.repo), ref)
68         git_cmd.checkout('FETCH_HEAD')
69         logging.info('Checked out commit:\n{}'.format(ws.head.commit.message))
70
71
72 def get_parser():
73     parser = argparse.ArgumentParser()
74     parser.add_argument('--debug', action='store_true', default=False,
75                         help="Turn on debug messages")
76     subparsers = parser.add_subparsers()
77     fork = subparsers.add_parser('clone-fork',
78                                  help='Clone fork of dependent repo')
79     fork.add_argument('-r', '--repo', required=True, help='Name of repository')
80     fork.add_argument('-u', '--url',
81                       default='https://gerrit.opnfv.org/gerrit',
82                       help='Gerrit URL of repository')
83     fork.add_argument('-b', '--branch',
84                       default='master',
85                       help='Branch to checkout')
86     fork.set_defaults(func=clone_fork)
87     return parser
88
89
90 def main():
91     parser = get_parser()
92     args = parser.parse_args(sys.argv[1:])
93     if args.debug:
94         logging_level = logging.DEBUG
95     else:
96         logging_level = logging.INFO
97
98     logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
99                         datefmt='%m/%d/%Y %I:%M:%S %p',
100                         level=logging_level)
101     if hasattr(args, 'func'):
102         args.func(args)
103     else:
104         parser.print_help()
105         exit(1)
106
107 if __name__ == "__main__":
108     main()