diff options
author | Luis Lozano <llozano@chromium.org> | 2015-12-15 13:49:30 -0800 |
---|---|---|
committer | Luis Lozano <llozano@chromium.org> | 2015-12-16 17:36:06 +0000 |
commit | f2a3ef46f75d2196a93d3ed27f4d1fcf22b54fbe (patch) | |
tree | 185d243c7eed7c7a0db6f0e640746cadc1479ea9 /binary_search_tool | |
parent | 2a66f70fef907c1cb15229cb58e5129cb620ac98 (diff) | |
download | toolchain-utils-f2a3ef46f75d2196a93d3ed27f4d1fcf22b54fbe.tar.gz |
Run pyformat on all the toolchain-utils files.
This gets rid of a lot of lint issues.
Ran by doing this:
for f in *.py; do echo -n "$f " ; if [ -x $f ]; then pyformat -i
--remove_trailing_comma --yapf --force_quote_type=double $f ; else
pyformat -i --remove_shebang --remove_trailing_comma --yapf
--force_quote_type=double $f ; fi ; done
BUG=chromium:567921
TEST=Ran simple crosperf run.
Change-Id: I59778835fdaa5f706d2e1765924389f9e97433d1
Reviewed-on: https://chrome-internal-review.googlesource.com/242031
Reviewed-by: Luis Lozano <llozano@chromium.org>
Commit-Queue: Luis Lozano <llozano@chromium.org>
Tested-by: Luis Lozano <llozano@chromium.org>
Reviewed-by: Yunlian Jiang <yunlian@google.com>
Diffstat (limited to 'binary_search_tool')
-rwxr-xr-x | binary_search_tool/__init__.py | 1 | ||||
-rwxr-xr-x | binary_search_tool/binary_search_perforce.py | 291 | ||||
-rwxr-xr-x | binary_search_tool/binary_search_state.py | 208 | ||||
-rwxr-xr-x | binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py | 148 | ||||
-rwxr-xr-x | binary_search_tool/cros_pkg/cros_pkg_undo_eclean.py | 48 | ||||
-rwxr-xr-x | binary_search_tool/test/__init__.py | 1 | ||||
-rwxr-xr-x | binary_search_tool/test/binary_search_tool_tester.py | 13 | ||||
-rwxr-xr-x | binary_search_tool/test/common.py | 1 | ||||
-rwxr-xr-x | binary_search_tool/test/gen_init_list.py | 4 | ||||
-rwxr-xr-x | binary_search_tool/test/gen_obj.py | 11 | ||||
-rwxr-xr-x | binary_search_tool/test/is_good.py | 1 |
11 files changed, 385 insertions, 342 deletions
diff --git a/binary_search_tool/__init__.py b/binary_search_tool/__init__.py index e69de29b..8b137891 100755 --- a/binary_search_tool/__init__.py +++ b/binary_search_tool/__init__.py @@ -0,0 +1 @@ + diff --git a/binary_search_tool/binary_search_perforce.py b/binary_search_tool/binary_search_perforce.py index 3bfa2b12..129f78c5 100755 --- a/binary_search_tool/binary_search_perforce.py +++ b/binary_search_tool/binary_search_perforce.py @@ -10,52 +10,61 @@ from utils import command_executer from utils import logger from utils import misc + def _GetP4ClientSpec(client_name, p4_paths): - p4_string = "" + p4_string = '' for p4_path in p4_paths: - if " " not in p4_path: - p4_string += " -a %s" % p4_path + if ' ' not in p4_path: + p4_string += ' -a %s' % p4_path else: - p4_string += " -a \"" + (" //" + client_name + "/").join(p4_path) + "\"" + p4_string += " -a \"" + (' //' + client_name + '/').join(p4_path) + "\"" return p4_string -def GetP4Command(client_name, p4_port, p4_paths, revision, checkoutdir, p4_snapshot=""): - command = "" +def GetP4Command(client_name, + p4_port, + p4_paths, + revision, + checkoutdir, + p4_snapshot=''): + command = '' if p4_snapshot: - command += "mkdir -p " + checkoutdir + command += 'mkdir -p ' + checkoutdir for p4_path in p4_paths: real_path = p4_path[1] - if real_path.endswith("..."): - real_path = real_path.replace("/...", "") - command += ("; mkdir -p " + checkoutdir + "/" + - os.path.dirname(real_path)) - command += ("&& rsync -lr " + p4_snapshot + "/" + real_path + - " " + checkoutdir + "/" + os.path.dirname(real_path)) + if real_path.endswith('...'): + real_path = real_path.replace('/...', '') + command += ( + '; mkdir -p ' + checkoutdir + '/' + os.path.dirname(real_path)) + command += ('&& rsync -lr ' + p4_snapshot + '/' + real_path + ' ' + + checkoutdir + '/' + os.path.dirname(real_path)) return command - command += " export P4CONFIG=.p4config" - command += " && mkdir -p " + checkoutdir - command += " && cd " + checkoutdir - command += " && cp ${HOME}/.p4config ." - command += " && chmod u+w .p4config" + command += ' export P4CONFIG=.p4config' + command += ' && mkdir -p ' + checkoutdir + command += ' && cd ' + checkoutdir + command += ' && cp ${HOME}/.p4config .' + command += ' && chmod u+w .p4config' command += " && echo \"P4PORT=" + p4_port + "\" >> .p4config" command += " && echo \"P4CLIENT=" + client_name + "\" >> .p4config" - command += (" && g4 client " + - _GetP4ClientSpec(client_name, p4_paths)) - command += " && g4 sync " - command += " && cd -" + command += (' && g4 client ' + _GetP4ClientSpec(client_name, p4_paths)) + command += ' && g4 sync ' + command += ' && cd -' return command + class BinarySearchPoint: + def __init__(self, revision, status, tag=None): self.revision = revision self.status = status self.tag = tag + class BinarySearcher: + def __init__(self): self.sorted_list = [] self.index_log = [] @@ -66,23 +75,21 @@ class BinarySearcher: pass def SetSortedList(self, sorted_list): - assert(len(sorted_list) > 0) + assert (len(sorted_list) > 0) self.sorted_list = sorted_list self.index_log = [] self.hi = len(sorted_list) - 1 self.lo = 0 self.points = {} for i in range(len(self.sorted_list)): - bsp = BinarySearchPoint(self.sorted_list[i], -1, "Not yet done.") + bsp = BinarySearchPoint(self.sorted_list[i], -1, 'Not yet done.') self.points[i] = bsp def SetStatus(self, status, tag=None): - message = ("Revision: %s index: %d returned: %d" % - (self.sorted_list[self.current], - self.current, - status)) + message = ('Revision: %s index: %d returned: %d' % + (self.sorted_list[self.current], self.current, status)) logger.GetLogger().LogOutput(message) - assert(status == 0 or status == 1 or status == 2) + assert (status == 0 or status == 1 or status == 2) self.index_log.append(self.current) self.status_log.append(status) bsp = BinarySearchPoint(self.sorted_list[self.current], status, tag) @@ -96,22 +103,20 @@ class BinarySearcher: self.lo = self.current + 1 elif status == 1: self.hi = self.current - logger.GetLogger().LogOutput("lo: %d hi: %d\n" % (self.lo, self.hi)) - self.current = (self.lo + self.hi)/2 + logger.GetLogger().LogOutput('lo: %d hi: %d\n' % (self.lo, self.hi)) + self.current = (self.lo + self.hi) / 2 if self.lo == self.hi: - message = ("Search complete. First bad version: %s" - " at index: %d" % - (self.sorted_list[self.current], - self.lo)) + message = ('Search complete. First bad version: %s' + ' at index: %d' % (self.sorted_list[self.current], self.lo)) logger.GetLogger().LogOutput(message) return True for index in range(self.lo, self.hi): if index not in self.skipped_indices: return False - logger.GetLogger().LogOutput( - "All skipped indices between: %d and %d\n" % (self.lo, self.hi)) + logger.GetLogger().LogOutput('All skipped indices between: %d and %d\n' % + (self.lo, self.hi)) return True # Does a better job with chromeos flakiness. @@ -130,23 +135,23 @@ class BinarySearcher: else: self.current = element[1] return - assert len(q), "Queue should never be 0-size!" + assert len(q), 'Queue should never be 0-size!' def GetNextFlakyLinear(self): - current_hi = self.current - current_lo = self.current - while True: - if current_hi < self.hi and current_hi not in self.skipped_indices: - self.current = current_hi - break - if current_lo >= self.lo and current_lo not in self.skipped_indices: - self.current = current_lo - break - if current_lo < self.lo and current_hi >= self.hi: - break - - current_hi += 1 - current_lo -= 1 + current_hi = self.current + current_lo = self.current + while True: + if current_hi < self.hi and current_hi not in self.skipped_indices: + self.current = current_hi + break + if current_lo >= self.lo and current_lo not in self.skipped_indices: + self.current = current_lo + break + if current_lo < self.lo and current_hi >= self.hi: + break + + current_hi += 1 + current_lo -= 1 def GetNext(self): self.current = (self.hi + self.lo) / 2 @@ -155,52 +160,56 @@ class BinarySearcher: self.GetNextFlakyBinary() # TODO: Add an estimated time remaining as well. - message = ("Estimated tries: min: %d max: %d\n" % + message = ('Estimated tries: min: %d max: %d\n' % (1 + math.log(self.hi - self.lo, 2), self.hi - self.lo - len(self.skipped_indices))) logger.GetLogger().LogOutput(message) - message = ("lo: %d hi: %d current: %d version: %s\n" % - (self.lo, self.hi, self.current, - self.sorted_list[self.current])) + message = ('lo: %d hi: %d current: %d version: %s\n' % + (self.lo, self.hi, self.current, self.sorted_list[self.current])) logger.GetLogger().LogOutput(message) logger.GetLogger().LogOutput(str(self)) return self.sorted_list[self.current] def SetLoRevision(self, lo_revision): self.lo = self.sorted_list.index(lo_revision) + def SetHiRevision(self, hi_revision): self.hi = self.sorted_list.index(hi_revision) + def GetAllPoints(self): - to_return = "" + to_return = '' for i in range(len(self.sorted_list)): - to_return += ("%d %d %s\n" % - (self.points[i].status, - i, - self.points[i].revision)) + to_return += ('%d %d %s\n' % (self.points[i].status, i, + self.points[i].revision)) return to_return + def __str__(self): - to_return = "" - to_return += "Current: %d\n" % self.current - to_return += str(self.index_log) + "\n" + to_return = '' + to_return += 'Current: %d\n' % self.current + to_return += str(self.index_log) + '\n' revision_log = [] for index in self.index_log: revision_log.append(self.sorted_list[index]) - to_return += str(revision_log) + "\n" - to_return += str(self.status_log) + "\n" - to_return += "Skipped indices:\n" - to_return += str(self.skipped_indices) + "\n" + to_return += str(revision_log) + '\n' + to_return += str(self.status_log) + '\n' + to_return += 'Skipped indices:\n' + to_return += str(self.skipped_indices) + '\n' to_return += self.GetAllPoints() return to_return + class RevisionInfo: + def __init__(self, date, client, description): self.date = date self.client = client self.description = description self.status = -1 + class VCSBinarySearcher: + def __init__(self): self.bs = BinarySearcher() self.rim = {} @@ -208,28 +217,37 @@ class VCSBinarySearcher: self.checkout_dir = None self.current_revision = None pass + def Initialize(self): pass + def GetNextRevision(self): pass + def CheckoutRevision(self, revision): pass + def SetStatus(self, status): pass + def Cleanup(self): pass + def SetGoodRevision(self, revision): if revision is None: return - assert(revision in self.bs.sorted_list) + assert (revision in self.bs.sorted_list) self.bs.SetLoRevision(revision) + def SetBadRevision(self, revision): if revision is None: return - assert(revision in self.bs.sorted_list) + assert (revision in self.bs.sorted_list) self.bs.SetHiRevision(revision) + class P4BinarySearcher(VCSBinarySearcher): + def __init__(self, p4_port, p4_paths, test_command): VCSBinarySearcher.__init__(self) self.p4_port = p4_port @@ -237,50 +255,56 @@ class P4BinarySearcher(VCSBinarySearcher): self.test_command = test_command self.checkout_dir = tempfile.mkdtemp() self.ce = command_executer.GetCommandExecuter() - self.client_name = "binary-searcher-$HOSTNAME-$USER" - self.job_log_root = "/home/asharif/www/coreboot_triage/" + self.client_name = 'binary-searcher-$HOSTNAME-$USER' + self.job_log_root = '/home/asharif/www/coreboot_triage/' + def Initialize(self, good_revision=None, bad_revision=None): self.Cleanup() - command = GetP4Command(self.client_name, self.p4_port, self.p4_paths, 1, self.checkout_dir) + command = GetP4Command(self.client_name, self.p4_port, self.p4_paths, 1, + self.checkout_dir) self.ce.RunCommand(command) - command = "cd %s && g4 changes ..." % self.checkout_dir + command = 'cd %s && g4 changes ...' % self.checkout_dir [retval, out, err] = self.ce.RunCommand(command, True) - self.changes = re.findall("Change (\d+)", out) - change_infos = re.findall("Change (\d+) on ([\d/]+) by ([^\s]+) ('[^']*')", out) + self.changes = re.findall('Change (\d+)', out) + change_infos = re.findall("Change (\d+) on ([\d/]+) by ([^\s]+) ('[^']*')", + out) for change_info in change_infos: ri = RevisionInfo(change_info[1], change_info[2], change_info[3]) self.rim[change_info[0]] = ri # g4 gives changes in reverse chronological order. self.changes.reverse() self.bs.SetSortedList(self.changes) + def SetStatus(self, status): self.rim[self.current_revision].status = status return self.bs.SetStatus(status) + def GetNextRevision(self): next_revision = self.bs.GetNext() self.current_revision = next_revision return next_revision + def CleanupCLs(self): - if not os.path.isfile(self.checkout_dir + "/.p4config"): - command = "cd %s" % self.checkout_dir - command += " && cp ${HOME}/.p4config ." + if not os.path.isfile(self.checkout_dir + '/.p4config'): + command = 'cd %s' % self.checkout_dir + command += ' && cp ${HOME}/.p4config .' command += " && echo \"P4PORT=" + self.p4_port + "\" >> .p4config" command += " && echo \"P4CLIENT=" + self.client_name + "\" >> .p4config" self.ce.RunCommand(command) - command = "cd %s" % self.checkout_dir - command += "; g4 changes -c %s" % self.client_name + command = 'cd %s' % self.checkout_dir + command += '; g4 changes -c %s' % self.client_name [retval, out, err] = self.ce.RunCommand(command, True) - changes = re.findall("Change (\d+)", out) + changes = re.findall('Change (\d+)', out) if len(changes) != 0: - command = "cd %s" % self.checkout_dir + command = 'cd %s' % self.checkout_dir for change in changes: - command += "; g4 revert -c %s" % change + command += '; g4 revert -c %s' % change self.ce.RunCommand(command) def CleanupClient(self): - command = "cd %s" % self.checkout_dir - command += "; g4 revert ..." - command += "; g4 client -d %s" % self.client_name + command = 'cd %s' % self.checkout_dir + command += '; g4 revert ...' + command += '; g4 client -d %s' % self.client_name self.ce.RunCommand(command) def Cleanup(self): @@ -288,25 +312,20 @@ class P4BinarySearcher(VCSBinarySearcher): self.CleanupClient() def __str__(self): - to_return = "" + to_return = '' for change in self.changes: ri = self.rim[change] if ri.status == -1: - to_return = "%s\t%d\n" % (change, ri.status) + to_return = '%s\t%d\n' % (change, ri.status) else: - to_return += ("%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n" % - (change, - ri.status, - ri.date, - ri.client, - ri.description, - self.job_log_root + change + ".cmd", - self.job_log_root + change + ".out", - self.job_log_root + change + ".err")) + to_return += ('%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\n' % + (change, ri.status, ri.date, ri.client, ri.description, + self.job_log_root + change + '.cmd', + self.job_log_root + change + '.out', + self.job_log_root + change + '.err')) return to_return - class P4GCCBinarySearcher(P4BinarySearcher): # TODO: eventually get these patches from g4 instead of creating them manually def HandleBrokenCLs(self, current_revision): @@ -315,30 +334,33 @@ class P4GCCBinarySearcher(P4BinarySearcher): problematic_ranges.append([44528, 44539]) problematic_ranges.append([44528, 44760]) problematic_ranges.append([44335, 44882]) - command = "pwd" + command = 'pwd' for pr in problematic_ranges: if cr in range(pr[0], pr[1]): - patch_file = "/home/asharif/triage_tool/%d-%d.patch" % (pr[0], pr[1]) + patch_file = '/home/asharif/triage_tool/%d-%d.patch' % (pr[0], pr[1]) f = open(patch_file) patch = f.read() f.close() - files = re.findall("--- (//.*)", patch) - command += "; cd %s" % self.checkout_dir + files = re.findall('--- (//.*)', patch) + command += '; cd %s' % self.checkout_dir for f in files: - command += "; g4 open %s" % f - command += "; patch -p2 < %s" % patch_file + command += '; g4 open %s' % f + command += '; patch -p2 < %s' % patch_file self.current_ce.RunCommand(command) def CheckoutRevision(self, current_revision): job_logger = logger.Logger(self.job_log_root, current_revision, - True, subdir="") + True, + subdir='') self.current_ce = command_executer.GetCommandExecuter(job_logger) self.CleanupCLs() # Change the revision of only the gcc part of the toolchain. - command = ("cd %s/gcctools/google_vendor_src_branch/gcc && g4 revert ...; g4 sync @%s" % - (self.checkout_dir, current_revision)) + command = ( + 'cd %s/gcctools/google_vendor_src_branch/gcc && g4 revert ...; g4 sync' + ' @%s' + % (self.checkout_dir, current_revision)) self.current_ce.RunCommand(command) self.HandleBrokenCLs(current_revision) @@ -347,30 +369,37 @@ class P4GCCBinarySearcher(P4BinarySearcher): def Main(argv): """The main function.""" # Common initializations -### command_executer.InitCommandExecuter(True) + ### command_executer.InitCommandExecuter(True) ce = command_executer.GetCommandExecuter() rootdir = misc.GetRoot(sys.argv[0])[0] parser = optparse.OptionParser() - parser.add_option("-n", "--num_tries", dest="num_tries", - default="100", - help="Number of tries.") - parser.add_option("-g", "--good_revision", dest="good_revision", - help="Last known good revision.") - parser.add_option("-b", "--bad_revision", dest="bad_revision", - help="Last known bad revision.") - parser.add_option("-s", - "--script", - dest="script", - help="Script to run for every version.") + parser.add_option('-n', + '--num_tries', + dest='num_tries', + default='100', + help='Number of tries.') + parser.add_option('-g', + '--good_revision', + dest='good_revision', + help='Last known good revision.') + parser.add_option('-b', + '--bad_revision', + dest='bad_revision', + help='Last known bad revision.') + parser.add_option('-s', + '--script', + dest='script', + help='Script to run for every version.') [options, args] = parser.parse_args(argv) # First get all revisions -### p4_paths = ["//depot2/gcctools/google_vendor_src_branch/gcc/gcc-4.4.3/README.google"] - p4_paths = ["//depot2/gcctools/google_vendor_src_branch/gcc/gcc-4.4.3/...", - "//depot2/gcctools/google_vendor_src_branch/binutils/binutils-2.20.1-mobile/...", - "//depot2/gcctools/google_vendor_src_branch/binutils/binutils-20100303/..."] - p4gccbs = P4GCCBinarySearcher("perforce2:2666", p4_paths, "") - + ### p4_paths = ["//depot2/gcctools/google_vendor_src_branch/gcc/gcc-4.4.3/README.google"] + p4_paths = [ + '//depot2/gcctools/google_vendor_src_branch/gcc/gcc-4.4.3/...', + '//depot2/gcctools/google_vendor_src_branch/binutils/binutils-2.20.1-mobile/...', + '//depot2/gcctools/google_vendor_src_branch/binutils/binutils-20100303/...' + ] + p4gccbs = P4GCCBinarySearcher('perforce2:2666', p4_paths, '') # Main loop: terminated = False @@ -387,9 +416,9 @@ def Main(argv): # Now run command to get the status ce = command_executer.GetCommandExecuter() - command = "%s %s" % (script, p4gccbs.checkout_dir) + command = '%s %s' % (script, p4gccbs.checkout_dir) status = ce.RunCommand(command) - message = ("Revision: %s produced: %d status\n" % + message = ('Revision: %s produced: %d status\n' % (current_revision, status)) logger.GetLogger().LogOutput(message) terminated = p4gccbs.SetStatus(status) @@ -397,14 +426,14 @@ def Main(argv): logger.GetLogger().LogOutput(str(p4gccbs)) if not terminated: - logger.GetLogger().LogOutput("Tries: %d expired." % num_tries) + logger.GetLogger().LogOutput('Tries: %d expired.' % num_tries) logger.GetLogger().LogOutput(str(p4gccbs.bs)) except (KeyboardInterrupt, SystemExit): - logger.GetLogger().LogOutput("Cleaning up...") + logger.GetLogger().LogOutput('Cleaning up...') finally: logger.GetLogger().LogOutput(str(p4gccbs.bs)) status = p4gccbs.Cleanup() -if __name__ == "__main__": +if __name__ == '__main__': Main(sys.argv) diff --git a/binary_search_tool/binary_search_state.py b/binary_search_tool/binary_search_state.py index 2f90f78d..274f2c6b 100755 --- a/binary_search_tool/binary_search_state.py +++ b/binary_search_tool/binary_search_state.py @@ -8,12 +8,12 @@ import tempfile # Programtically adding utils python path to PYTHONPATH if os.path.isabs(sys.argv[0]): - utils_pythonpath = os.path.abspath( - '{0}/..'.format(os.path.dirname(sys.argv[0]))) + utils_pythonpath = os.path.abspath('{0}/..'.format(os.path.dirname(sys.argv[ + 0]))) else: wdir = os.getcwd() - utils_pythonpath = os.path.abspath( - '{0}/{1}/..'.format(wdir, os.path.dirname(sys.argv[0]))) + utils_pythonpath = os.path.abspath('{0}/{1}/..'.format(wdir, os.path.dirname( + sys.argv[0]))) sys.path.append(utils_pythonpath) # Now we do import from utils from utils import command_executer @@ -21,10 +21,11 @@ from utils import logger import binary_search_perforce -STATE_FILE = "%s.state" % sys.argv[0] +STATE_FILE = '%s.state' % sys.argv[0] class BinarySearchState(object): + def __init__(self, get_initial_items, switch_to_good, switch_to_bad, test_script, incremental, prune, iterations, prune_iterations, verify_level, file_args): @@ -49,36 +50,38 @@ class BinarySearchState(object): def SwitchToGood(self, item_list): if self.incremental: - self.l.LogOutput( - "Incremental set. Wanted to switch %s to good" % str(item_list)) + self.l.LogOutput('Incremental set. Wanted to switch %s to good' % + str(item_list)) incremental_items = [ - item for item in item_list if item not in self.currently_good_items] + item for item in item_list if item not in self.currently_good_items + ] item_list = incremental_items - self.l.LogOutput( - "Incremental set. Actually switching %s to good" % str(item_list)) + self.l.LogOutput('Incremental set. Actually switching %s to good' % + str(item_list)) if not item_list: return - self.l.LogOutput("Switching %s to good" % str(item_list)) + self.l.LogOutput('Switching %s to good' % str(item_list)) self.RunSwitchScript(self.switch_to_good, item_list) self.currently_good_items = self.currently_good_items.union(set(item_list)) self.currently_bad_items.difference_update(set(item_list)) def SwitchToBad(self, item_list): if self.incremental: - self.l.LogOutput( - "Incremental set. Wanted to switch %s to bad" % str(item_list)) + self.l.LogOutput('Incremental set. Wanted to switch %s to bad' % + str(item_list)) incremental_items = [ - item for item in item_list if item not in self.currently_bad_items] + item for item in item_list if item not in self.currently_bad_items + ] item_list = incremental_items - self.l.LogOutput( - "Incremental set. Actually switching %s to bad" % str(item_list)) + self.l.LogOutput('Incremental set. Actually switching %s to bad' % + str(item_list)) if not item_list: return - self.l.LogOutput("Switching %s to bad" % str(item_list)) + self.l.LogOutput('Switching %s to bad' % str(item_list)) self.RunSwitchScript(self.switch_to_bad, item_list) self.currently_bad_items = self.currently_bad_items.union(set(item_list)) self.currently_good_items.difference_update(set(item_list)) @@ -86,14 +89,14 @@ class BinarySearchState(object): def RunSwitchScript(self, switch_script, item_list): if self.file_args: temp_file = tempfile.mktemp() - f = open(temp_file, "wb") - f.write("\n".join(item_list)) + f = open(temp_file, 'wb') + f.write('\n'.join(item_list)) f.close() - command = "%s %s" % (switch_script, temp_file) + command = '%s %s' % (switch_script, temp_file) else: - command = "%s %s" % (switch_script, " ".join(item_list)) + command = '%s %s' % (switch_script, ' '.join(item_list)) ret = self.ce.RunCommand(command) - assert ret == 0, "Switch script %s returned %d" % (switch_script, ret) + assert ret == 0, 'Switch script %s returned %d' % (switch_script, ret) def TestScript(self): command = self.test_script @@ -101,15 +104,15 @@ class BinarySearchState(object): def DoVerify(self): for _ in range(int(self.verify_level)): - self.l.LogOutput("Resetting all items to good to verify.") + self.l.LogOutput('Resetting all items to good to verify.') self.SwitchToGood(self.all_items) status = self.TestScript() - assert status == 0, "When reset_to_good, status should be 0." + assert status == 0, 'When reset_to_good, status should be 0.' - self.l.LogOutput("Resetting all items to bad to verify.") + self.l.LogOutput('Resetting all items to bad to verify.') self.SwitchToBad(self.all_items) status = self.TestScript() - assert status == 1, "When reset_to_bad, status should be 1." + assert status == 1, 'When reset_to_bad, status should be 1.' def DoSearch(self): num_bad_items_history = [] @@ -120,14 +123,14 @@ class BinarySearchState(object): if not terminated: break if not self.prune: - self.l.LogOutput("Not continuning further, --prune is not set") + self.l.LogOutput('Not continuning further, --prune is not set') break # Prune is set. prune_index = self.bs.current if prune_index == len(self.all_items) - 1: - self.l.LogOutput("First bad item is the last item. Breaking.") - self.l.LogOutput("Only bad item is: %s" % self.all_items[-1]) + self.l.LogOutput('First bad item is the last item. Breaking.') + self.l.LogOutput('Only bad item is: %s' % self.all_items[-1]) break num_bad_items = len(self.all_items) - prune_index @@ -135,12 +138,11 @@ class BinarySearchState(object): if (num_bad_items_history[-num_bad_items:] == [num_bad_items for _ in range(num_bad_items)]): - self.l.LogOutput("num_bad_items_history: %s for past %d iterations. " - "Breaking." % - (str(num_bad_items_history), - num_bad_items)) - self.l.LogOutput( - "Bad items are: %s" % " ".join(self.all_items[prune_index:])) + self.l.LogOutput('num_bad_items_history: %s for past %d iterations. ' + 'Breaking.' % (str(num_bad_items_history), + num_bad_items)) + self.l.LogOutput('Bad items are: %s' % + ' '.join(self.all_items[prune_index:])) break new_all_items = list(self.all_items) @@ -150,9 +152,8 @@ class BinarySearchState(object): if prune_index: new_all_items = new_all_items[prune_index - 1:] - self.l.LogOutput("Old list: %s. New list: %s" % - (str(self.all_items), - str(new_all_items))) + self.l.LogOutput('Old list: %s. New list: %s' % (str(self.all_items), + str(new_all_items))) # FIXME: Do we need to Convert the currently good items to bad self.PopulateItemsUsingList(new_all_items) @@ -171,9 +172,9 @@ class BinarySearchState(object): terminated = self.bs.SetStatus(status) if terminated: - self.l.LogOutput("Terminated!") + self.l.LogOutput('Terminated!') if not terminated: - self.l.LogOutput("Ran out of iterations searching...") + self.l.LogOutput('Ran out of iterations searching...') self.l.LogOutput(str(self)) return terminated @@ -192,8 +193,8 @@ class BinarySearchState(object): self.l = None self.ce = None # TODO Implement save/restore -### return - f = open(STATE_FILE, "wb") + ### return + f = open(STATE_FILE, 'wb') pickle.dump(self, f) f.close() @@ -217,24 +218,24 @@ class BinarySearchState(object): border_item = self.bs.GetNext() index = self.all_items.index(border_item) - next_bad_items = self.all_items[:index+1] - next_good_items = self.all_items[index+1:] + next_bad_items = self.all_items[:index + 1] + next_good_items = self.all_items[index + 1:] return [next_bad_items, next_good_items] def __str__(self): - ret = "" - ret += "all: %s\n" % str(self.all_items) - ret += "currently_good: %s\n" % str(self.currently_good_items) - ret += "currently_bad: %s\n" % str(self.currently_bad_items) + ret = '' + ret += 'all: %s\n' % str(self.all_items) + ret += 'currently_good: %s\n' % str(self.currently_good_items) + ret += 'currently_bad: %s\n' % str(self.currently_bad_items) ret += str(self.bs) return ret def _CanonicalizeScript(script_name): script_name = os.path.expanduser(script_name) - if not script_name.startswith("/"): - return os.path.join(".", script_name) + if not script_name.startswith('/'): + return os.path.join('.', script_name) def Main(argv): @@ -242,60 +243,60 @@ def Main(argv): # Common initializations parser = optparse.OptionParser() - parser.add_option("-n", - "--iterations", - dest="iterations", - help="Number of iterations to try in the search.", - default="50") - parser.add_option("-i", - "--get_initial_items", - dest="get_initial_items", - help="Script to run to get the initial objects.") - parser.add_option("-g", - "--switch_to_good", - dest="switch_to_good", - help="Script to run to switch to good.") - parser.add_option("-b", - "--switch_to_bad", - dest="switch_to_bad", - help="Script to run to switch to bad.") - parser.add_option("-t", - "--test_script", - dest="test_script", - help=("Script to run to test the " - "output after packages are built.")) - parser.add_option("-p", - "--prune", - dest="prune", - action="store_true", + parser.add_option('-n', + '--iterations', + dest='iterations', + help='Number of iterations to try in the search.', + default='50') + parser.add_option('-i', + '--get_initial_items', + dest='get_initial_items', + help='Script to run to get the initial objects.') + parser.add_option('-g', + '--switch_to_good', + dest='switch_to_good', + help='Script to run to switch to good.') + parser.add_option('-b', + '--switch_to_bad', + dest='switch_to_bad', + help='Script to run to switch to bad.') + parser.add_option('-t', + '--test_script', + dest='test_script', + help=('Script to run to test the ' + 'output after packages are built.')) + parser.add_option('-p', + '--prune', + dest='prune', + action='store_true', default=False, - help=("Script to run to test the output after " - "packages are built.")) - parser.add_option("-c", - "--noincremental", - dest="noincremental", - action="store_true", + help=('Script to run to test the output after ' + 'packages are built.')) + parser.add_option('-c', + '--noincremental', + dest='noincremental', + action='store_true', default=False, - help="Do not propagate good/bad changes incrementally.") - parser.add_option("-f", - "--file_args", - dest="file_args", - action="store_true", + help='Do not propagate good/bad changes incrementally.') + parser.add_option('-f', + '--file_args', + dest='file_args', + action='store_true', default=False, - help="Use a file to pass arguments to scripts.") - parser.add_option("-v", - "--verify_level", - dest="verify_level", - default="1", - help=("Check binary search assumptions N times " - "before starting.")) - parser.add_option("-N", - "--prune_iterations", - dest="prune_iterations", - help="Number of prune iterations to try in the search.", - default="100") - - logger.GetLogger().LogOutput(" ".join(argv)) + help='Use a file to pass arguments to scripts.') + parser.add_option('-v', + '--verify_level', + dest='verify_level', + default='1', + help=('Check binary search assumptions N times ' + 'before starting.')) + parser.add_option('-N', + '--prune_iterations', + dest='prune_iterations', + help='Number of prune iterations to try in the search.', + default='100') + + logger.GetLogger().LogOutput(' '.join(argv)) [options, _] = parser.parse_args(argv) if not (options.get_initial_items and options.switch_to_good and @@ -326,9 +327,10 @@ def Main(argv): bss.DoSearch() except (KeyboardInterrupt, SystemExit): - print "C-c pressed" + print 'C-c pressed' bss.SaveState() return 0 -if __name__ == "__main__": + +if __name__ == '__main__': sys.exit(Main(sys.argv)) diff --git a/binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py b/binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py index bed7106d..d2158c87 100755 --- a/binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py +++ b/binary_search_tool/cros_pkg/cros_pkg_create_cleanup_script.py @@ -15,12 +15,13 @@ import sys def Usage(parser, msg): - print "ERROR: " + msg - parser.print_help() - sys.exit(1) + print 'ERROR: ' + msg + parser.print_help() + sys.exit(1) + def Main(argv): - """ + """ The script cros_pkg_setup.sh make two main changes that need to be undone: 1). It creates a soft link making /build/${board} point to /build/${board}.work, and 2). It saves a copy of the build_image @@ -29,76 +30,83 @@ def Main(argv): it was a real tree or a soft link. If it was soft link, it saved the old value of the link, then deleted it and created the new link. If it was a real tree, it renamed the tree to /build/${board}.save, and then created the - new soft link. If the /build/${board} did not previously exist, then it just + new soft link. If the /build/${board} did not previously exist, then it + just created the new soft link. This function takes arguments that tell it exactly what cros_pkg_setup.sh actually did, then generates a script to undo those exact changes. """ - parser = argparse.ArgumentParser() - parser.add_argument("--board", dest="board", required=True, - help="Chromeos board for packages/image.") - - parser.add_argument("--old_tree_missing", dest="tree_existed", - action="store_false", - help="Did /build/${BOARD} exist.", default=True) - - parser.add_argument("--renamed_tree", dest="renamed_tree", - action="store_true", - help="Was /build/${BOARD} saved & renamed.", - default=False) - - parser.add_argument("--old_link", dest="old_link", - help=("The original build tree soft link.")) - - options = parser.parse_args(argv[1:]) - - if options.old_link or options.renamed_tree: - if not options.tree_existed: - Usage(parser, "If --tree_existed is False, cannot have " - "--renamed_tree or --old_link") - - if options.old_link and options.renamed_tree: - Usage(parser, "--old_link and --renamed_tree are incompatible options.") - + parser = argparse.ArgumentParser() + parser.add_argument('--board', + dest='board', + required=True, + help='Chromeos board for packages/image.') + + parser.add_argument('--old_tree_missing', + dest='tree_existed', + action='store_false', + help='Did /build/${BOARD} exist.', + default=True) + + parser.add_argument('--renamed_tree', + dest='renamed_tree', + action='store_true', + help='Was /build/${BOARD} saved & renamed.', + default=False) + + parser.add_argument('--old_link', + dest='old_link', + help=('The original build tree soft link.')) + + options = parser.parse_args(argv[1:]) + + if options.old_link or options.renamed_tree: + if not options.tree_existed: + Usage(parser, 'If --tree_existed is False, cannot have ' + '--renamed_tree or --old_link') + + if options.old_link and options.renamed_tree: + Usage(parser, '--old_link and --renamed_tree are incompatible options.') + + if options.tree_existed: + if not options.old_link and not options.renamed_tree: + Usage(parser, 'If --tree_existed is True, then must have either ' + '--old_link or --renamed_tree') + + out_filename = 'cros_pkg_' + options.board + '_cleanup.sh' + + with open(out_filename, 'w') as out_file: + out_file.write('#!/bin/bash\n\n') + # First, remove the 'new' soft link. + out_file.write('sudo rm /build/%s\n' % options.board) if options.tree_existed: - if not options.old_link and not options.renamed_tree: - Usage(parser, "If --tree_existed is True, then must have either " - "--old_link or --renamed_tree") - - out_filename = "cros_pkg_" + options.board + "_cleanup.sh" - - with open(out_filename, "w") as out_file: - out_file.write("#!/bin/bash\n\n") - # First, remove the 'new' soft link. - out_file.write("sudo rm /build/%s\n" % options.board) - if options.tree_existed: - if options.renamed_tree: - # Old build tree existed and was a real tree, so it got - # renamed. Move the renamed tree back to the original tree. - out_file.write("sudo mv /build/%s.save /build/%s\n" - % (options.board, options.board)) - else: - # Old tree existed and was already a soft link. Re-create the - # original soft link. - original_link = options.old_link - if original_link[0] == "'": - original_link = original_link[1:] - if original_link[-1] == "'": - original_link = original_link[:-1] - out_file.write("sudo ln -s %s /build/%s\n" % (original_link, - options.board)) - out_file.write("\n") - # Restore the original saved version of build_image script. - out_file.write("mv ~/trunk/src/scripts/build_image.save " - "~/trunk/src/scripts/build_image\n\n") - # Remove cros_pkg_common.sh file - out_file.write("rm cros_pkg_common.sh\n") - - return 0 - - -if __name__ == "__main__": - retval = Main(sys.argv) - sys.exit(retval) + if options.renamed_tree: + # Old build tree existed and was a real tree, so it got + # renamed. Move the renamed tree back to the original tree. + out_file.write('sudo mv /build/%s.save /build/%s\n' % + (options.board, options.board)) + else: + # Old tree existed and was already a soft link. Re-create the + # original soft link. + original_link = options.old_link + if original_link[0] == "'": + original_link = original_link[1:] + if original_link[-1] == "'": + original_link = original_link[:-1] + out_file.write('sudo ln -s %s /build/%s\n' % (original_link, + options.board)) + out_file.write('\n') + # Restore the original saved version of build_image script. + out_file.write('mv ~/trunk/src/scripts/build_image.save ' + '~/trunk/src/scripts/build_image\n\n') + # Remove cros_pkg_common.sh file + out_file.write('rm cros_pkg_common.sh\n') + + return 0 + + +if __name__ == '__main__': + retval = Main(sys.argv) + sys.exit(retval) diff --git a/binary_search_tool/cros_pkg/cros_pkg_undo_eclean.py b/binary_search_tool/cros_pkg/cros_pkg_undo_eclean.py index a5c612c5..1600d2ce 100755 --- a/binary_search_tool/cros_pkg/cros_pkg_undo_eclean.py +++ b/binary_search_tool/cros_pkg/cros_pkg_undo_eclean.py @@ -13,28 +13,30 @@ import sys import os + def Main(args): - if args: - filename = args[0] - if not os.path.exists(filename): - return 1 - else: - return 1 - - outname = filename + ".edited" - with open(filename, "r") as input_file: - lines = input_file.readlines() - with open(outname, "w") as out_file: - for line in lines: - if line.find("eclean") >= 0: - out_line = "# " + line - else: - out_line = line - out_file.write(out_line) - - return 0 - -if __name__ == "__main__": - retval = Main(sys.argv[1:]) - sys.exit(retval) + if args: + filename = args[0] + if not os.path.exists(filename): + return 1 + else: + return 1 + + outname = filename + '.edited' + with open(filename, 'r') as input_file: + lines = input_file.readlines() + with open(outname, 'w') as out_file: + for line in lines: + if line.find('eclean') >= 0: + out_line = '# ' + line + else: + out_line = line + out_file.write(out_line) + + return 0 + + +if __name__ == '__main__': + retval = Main(sys.argv[1:]) + sys.exit(retval) diff --git a/binary_search_tool/test/__init__.py b/binary_search_tool/test/__init__.py index e69de29b..8b137891 100755 --- a/binary_search_tool/test/__init__.py +++ b/binary_search_tool/test/__init__.py @@ -0,0 +1 @@ + diff --git a/binary_search_tool/test/binary_search_tool_tester.py b/binary_search_tool/test/binary_search_tool_tester.py index 91b5845d..3b8e44ce 100755 --- a/binary_search_tool/test/binary_search_tool_tester.py +++ b/binary_search_tool/test/binary_search_tool_tester.py @@ -1,7 +1,6 @@ #!/usr/bin/python # Copyright 2012 Google Inc. All Rights Reserved. - """Tests for bisecting tool.""" __author__ = 'shenhan@google.com (Han Shen)' @@ -32,15 +31,13 @@ class BisectingUtilsTest(unittest.TestCase): """Cleanup temp files.""" os.remove(common.OBJECTS_FILE) os.remove(common.WORKING_SET_FILE) - print 'Deleted "{0}" and "{1}"'.format( - common.OBJECTS_FILE, common.WORKING_SET_FILE) + print 'Deleted "{0}" and "{1}"'.format(common.OBJECTS_FILE, + common.WORKING_SET_FILE) def runTest(self): - args = ['--get_initial_items', './gen_init_list.py', - '--switch_to_good', './switch_to_good.py', - '--switch_to_bad', './switch_to_bad.py', - '--test_script', './is_good.py', - '--prune', '--file_args'] + args = ['--get_initial_items', './gen_init_list.py', '--switch_to_good', + './switch_to_good.py', '--switch_to_bad', './switch_to_bad.py', + '--test_script', './is_good.py', '--prune', '--file_args'] binary_search_state.Main(args) _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( diff --git a/binary_search_tool/test/common.py b/binary_search_tool/test/common.py index c0e1a3b3..cc0a02e4 100755 --- a/binary_search_tool/test/common.py +++ b/binary_search_tool/test/common.py @@ -5,6 +5,7 @@ DEFAULT_BAD_OBJECT_NUMBER = 23 OBJECTS_FILE = 'objects.txt' WORKING_SET_FILE = 'working_set.txt' + def ReadWorkingSet(): working_set = [] f = open(WORKING_SET_FILE, 'r') diff --git a/binary_search_tool/test/gen_init_list.py b/binary_search_tool/test/gen_init_list.py index 4ac5da83..02a111ab 100755 --- a/binary_search_tool/test/gen_init_list.py +++ b/binary_search_tool/test/gen_init_list.py @@ -9,8 +9,8 @@ import common def Main(): ce = command_executer.GetCommandExecuter() - _, l, _ = ce.RunCommandWOutput( - 'cat {0} | wc -l'.format(common.OBJECTS_FILE), print_to_console=False) + _, l, _ = ce.RunCommandWOutput('cat {0} | wc -l'.format(common.OBJECTS_FILE), + print_to_console=False) for i in range(0, int(l)): print i diff --git a/binary_search_tool/test/gen_obj.py b/binary_search_tool/test/gen_obj.py index 3d1e32f9..f817049f 100755 --- a/binary_search_tool/test/gen_obj.py +++ b/binary_search_tool/test/gen_obj.py @@ -25,10 +25,14 @@ def Main(argv): 0 always. """ parser = optparse.OptionParser() - parser.add_option('-n', '--obj_num', dest='obj_num', + parser.add_option('-n', + '--obj_num', + dest='obj_num', default=common.DEFAULT_OBJECT_NUMBER, help=('Number of total objects.')) - parser.add_option('-b', '--bad_obj_num', dest='bad_obj_num', + parser.add_option('-b', + '--bad_obj_num', + dest='bad_obj_num', default=common.DEFAULT_BAD_OBJECT_NUMBER, help=('Number of bad objects. Must be great than or equal ' 'to zero and less than total object number.')) @@ -39,8 +43,7 @@ def Main(argv): bad_to_gen = int(options.bad_obj_num) obj_list = [] for i in range(obj_num): - if (bad_to_gen > 0 and - random.randint(1, obj_num) <= bad_obj_num): + if (bad_to_gen > 0 and random.randint(1, obj_num) <= bad_obj_num): obj_list.append(1) bad_to_gen -= 1 else: diff --git a/binary_search_tool/test/is_good.py b/binary_search_tool/test/is_good.py index 60b2b50d..a420aa59 100755 --- a/binary_search_tool/test/is_good.py +++ b/binary_search_tool/test/is_good.py @@ -17,4 +17,3 @@ def Main(): if __name__ == '__main__': retval = Main() sys.exit(retval) - |