blob: 5fb7c6abccb8d6a049928368474a2289603b9f62 [file] [log] [blame]
Ningning Xia84190b82018-04-16 15:01:40 -07001# Copyright 2018 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Constants and util methods to interact with skylab inventory repo."""
6
7import logging
Ningning Xiaa043aad2018-04-23 15:07:09 -07008import re
9
10import common
Ningning Xia84190b82018-04-16 15:01:40 -070011
12from autotest_lib.client.common_lib import revision_control
Ningning Xiaa043aad2018-04-23 15:07:09 -070013from chromite.lib import gob_util
Ningning Xia9c188b92018-04-27 15:34:23 -070014
15try:
16 from skylab_inventory import text_manager
17except ImportError:
18 pass
Ningning Xia84190b82018-04-16 15:01:40 -070019
Ningning Xiaa043aad2018-04-23 15:07:09 -070020
21INTERNAL_GERRIT_HOST = 'chrome-internal-review.googlesource.com'
22INTERNAL_GERRIT_HOST_URL = 'https://%s' % INTERNAL_GERRIT_HOST
Ningning Xia84190b82018-04-16 15:01:40 -070023# The git url of the internal skylab_inventory
24INTERNAL_INVENTORY_REPO_URL = ('https://chrome-internal.googlesource.com/'
25 'chromeos/infra_internal/skylab_inventory.git')
Ningning Xiaa043aad2018-04-23 15:07:09 -070026INTERNAL_INVENTORY_CHANGE_PATTERN = (
Prathmesh Prabhuc44db202018-06-22 14:12:17 -070027 r'https://chrome-internal-review.googlesource.com/c/chromeos/'
Ningning Xiaa043aad2018-04-23 15:07:09 -070028 'infra_internal/skylab_inventory/\\+/([0-9]*)')
Ningning Xiaea02ab12018-05-11 15:08:57 -070029MSG_INVALID_IN_SKYLAB = 'This is currently not supported with --skylab.'
Allen Li75e3d342018-08-06 19:04:31 +000030MSG_ONLY_VALID_IN_SKYLAB = 'This only applies to actions on skylab inventory.'
Ningning Xiaa043aad2018-04-23 15:07:09 -070031
32
Ningning Xia9c188b92018-04-27 15:34:23 -070033class SkylabInventoryNotImported(Exception):
34 """skylab_inventory is not imported."""
35
36
Ningning Xiaa043aad2018-04-23 15:07:09 -070037class InventoryRepoChangeNotFound(Exception):
38 """Error raised when no inventory repo change number is found."""
39
40
Ningning Xiad23cf2f2018-06-04 17:50:35 -070041class InventoryRepoDirNotClean(Exception):
42 """Error raised when the given inventory_repo_dir contains local changes."""
43
44
Ningning Xiaa043aad2018-04-23 15:07:09 -070045def get_cl_url(change_number):
46 return INTERNAL_GERRIT_HOST_URL + '/' + str(change_number)
Ningning Xia84190b82018-04-16 15:01:40 -070047
48
Ningning Xiaef35cb52018-05-04 17:58:20 -070049def get_cl_message(change_number):
50 return ('Please submit the CL at %s to make the change effective.' %
51 get_cl_url(change_number))
52
53
Ningning Xia84190b82018-04-16 15:01:40 -070054def construct_commit_message(subject, bug=None, test=None):
55 """Construct commit message for skylab inventory repo commit.
56
57 @param subject: Commit message subject.
58 @param bug: Bug number of the commit.
59 @param test: Tests of the commit.
60
61 @return: A commit message string.
62 """
63 return '\n'.join([subject, '', 'BUG=%s' % bug, 'TEST=%s' % test])
64
65
Ningning Xiaa043aad2018-04-23 15:07:09 -070066def extract_inventory_change(output):
67 """Extract the change number from the output.
68
69 @param output: The git command output containing the change gerrit url.
70
71 @return: The change number (int) of the inventory change.
72 """
73 m = re.search(INTERNAL_INVENTORY_CHANGE_PATTERN, output)
74
75 if not m:
Prathmesh Prabhuc44db202018-06-22 14:12:17 -070076 raise InventoryRepoChangeNotFound(
77 'Could not extract CL number from "%r"' % output)
Ningning Xiaa043aad2018-04-23 15:07:09 -070078
79 return int(m.group(1))
80
81
82def submit_inventory_change(change_number):
83 """Set review labels and submit the inventory change.
84
85 @param change_number: The change number (int) of the inventory change.
86 """
87 logging.info('Setting review labels for %s.',
88 get_cl_url(change_number))
89 gob_util.SetReview(
90 INTERNAL_GERRIT_HOST,
91 change=change_number,
92 labels={'Code-Review': 2, 'Verified': 1},
93 msg='Set TBR by "atest --skylab"',
94 notify='OWNER')
95
96 logging.info('Submitting the change.')
97 gob_util.SubmitChange(
98 INTERNAL_GERRIT_HOST,
99 change=change_number)
100
101
Ningning Xia84190b82018-04-16 15:01:40 -0700102class InventoryRepo(object):
103 """Class to present a inventory repository."""
104
Ningning Xiae8714052018-04-30 18:58:54 -0700105
Ningning Xia84190b82018-04-16 15:01:40 -0700106 def __init__(self, inventory_repo_dir):
107 self.inventory_repo_dir = inventory_repo_dir
108 self.git_repo = None
109
Ningning Xiae8714052018-04-30 18:58:54 -0700110
Ningning Xia84190b82018-04-16 15:01:40 -0700111 def initialize(self):
112 """Initialize inventory repo at the given dir."""
Ningning Xiaa043aad2018-04-23 15:07:09 -0700113 self.git_repo = revision_control.GitRepo(
Ningning Xia84190b82018-04-16 15:01:40 -0700114 self.inventory_repo_dir,
115 giturl=INTERNAL_INVENTORY_REPO_URL,
116 abs_work_tree=self.inventory_repo_dir)
117
Ningning Xiaa043aad2018-04-23 15:07:09 -0700118 if self.git_repo.is_repo_initialized():
Ningning Xiad23cf2f2018-06-04 17:50:35 -0700119 if self.git_repo.status():
120 raise InventoryRepoDirNotClean(
121 'The inventory_repo_dir "%s" contains uncommitted '
122 'changes. Please clean up the local repo directory or '
123 'use another clean directory.' % self.inventory_repo_dir)
124
Ningning Xia84190b82018-04-16 15:01:40 -0700125 logging.info('Inventory repo was already initialized, start '
126 'pulling.')
Ningning Xiaee3e3a92018-05-22 17:38:51 -0700127 self.git_repo.checkout('master')
Ningning Xiaa043aad2018-04-23 15:07:09 -0700128 self.git_repo.pull()
Ningning Xia84190b82018-04-16 15:01:40 -0700129 else:
130 logging.info('No inventory repo was found, start cloning.')
Prathmesh Prabhuaf1bd7b2018-06-22 14:11:51 -0700131 self.git_repo.clone(shallow=True)
Ningning Xia84190b82018-04-16 15:01:40 -0700132
133
Ningning Xiae8714052018-04-30 18:58:54 -0700134 def get_data_dir(self, data_subdir='skylab'):
Ningning Xia84190b82018-04-16 15:01:40 -0700135 """Get path to the data dir."""
Ningning Xiae8714052018-04-30 18:58:54 -0700136 return text_manager.get_data_dir(self.inventory_repo_dir, data_subdir)
137
Ningning Xiaa043aad2018-04-23 15:07:09 -0700138
139 def upload_change(self, commit_message, draft=False, dryrun=False,
140 submit=False):
141 """Commit and upload the change to gerrit.
142
143 @param commit_message: Commit message of the CL to upload.
144 @param draft: Boolean indicating whether to upload the CL as a draft.
145 @param dryrun: Boolean indicating whether to run upload as a dryrun.
146 @param submit: Boolean indicating whether to submit the CL directly.
147
148 @return: Change number (int) of the CL if it's uploaded to Gerrit.
149 """
150 self.git_repo.commit(commit_message)
151
Ningning Xiaee3e3a92018-05-22 17:38:51 -0700152 remote = self.git_repo.remote()
Ningning Xiaa043aad2018-04-23 15:07:09 -0700153 output = self.git_repo.upload_cl(
Ningning Xiaee3e3a92018-05-22 17:38:51 -0700154 remote, 'master', draft=draft, dryrun=dryrun)
Ningning Xiaa043aad2018-04-23 15:07:09 -0700155
156 if not dryrun:
157 change_number = extract_inventory_change(output)
158
159 if submit:
160 submit_inventory_change(change_number)
161
162 return change_number