Skip to content

Commit 1516995

Browse files
Support for |change| argument to |GetPreferredTrySlaves()|, try 2.
Needed to make it so changes containing only *.mm are only sent to Mac trybots by default. BUG=none TEST=none Review URL: http://codereview.chromium.org/8059009 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@102930 0039d316-1c4b-4281-b951-d872f2087c98
1 parent db06f18 commit 1516995

7 files changed

+152
-73
lines changed

gcl.py

+10
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,21 @@ def TryChange(change_info, args, swallow_exception):
961961
if change_info.patchset:
962962
trychange_args.extend(["--patchset", str(change_info.patchset)])
963963
file_list = change_info.GetFileNames()
964+
change = presubmit_support.SvnChange(change_info.name,
965+
change_info.description,
966+
change_info.GetLocalRoot(),
967+
change_info.GetFiles(),
968+
change_info.issue,
969+
change_info.patchset,
970+
None)
964971
else:
965972
file_list = []
973+
change = None
974+
966975
trychange_args.extend(args)
967976
return trychange.TryChange(
968977
trychange_args,
978+
change=change,
969979
file_list=file_list,
970980
swallow_exception=swallow_exception,
971981
prog='gcl try',

git_cl.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ def SetIssue(self, issue):
488488
self.SetPatchset(0)
489489
self.has_issue = False
490490

491-
def RunHook(self, committing, upstream_branch, may_prompt, verbose, author):
492-
"""Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
491+
def GetChange(self, upstream_branch, author):
493492
root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip() or '.'
494493
absroot = os.path.abspath(root)
495494

@@ -511,7 +510,7 @@ def RunHook(self, committing, upstream_branch, may_prompt, verbose, author):
511510

512511
if not author:
513512
author = RunGit(['config', 'user.email']).strip() or None
514-
change = presubmit_support.GitChange(
513+
return presubmit_support.GitChange(
515514
name,
516515
description,
517516
absroot,
@@ -520,6 +519,10 @@ def RunHook(self, committing, upstream_branch, may_prompt, verbose, author):
520519
patchset,
521520
author)
522521

522+
def RunHook(self, committing, upstream_branch, may_prompt, verbose, author):
523+
"""Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
524+
change = self.GetChange(upstream_branch, author)
525+
523526
# Apply watchlists on upload.
524527
if not committing:
525528
watchlist = watchlists.Watchlists(change.RepositoryRoot())

git_try.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import subprocess2
1414
import third_party.upload
1515
import trychange
16+
import git_cl
1617

1718

1819
def GetRietveldIssueNumber():
@@ -53,8 +54,10 @@ def GetRietveldServerUrl():
5354
# Hack around a limitation in logging.
5455
logging.getLogger().handlers = []
5556
try:
57+
cl = git_cl.Changelist()
58+
change = cl.GetChange(cl.GetUpstreamBranch(), None)
5659
sys.exit(trychange.TryChange(
57-
args, file_list=[], swallow_exception=False,
60+
args, change, file_list=[], swallow_exception=False,
5861
prog='git try',
5962
extra_epilog='\n'
6063
'git try will diff against your tracked branch and will '

presubmit_support.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cStringIO # Exposed through the API.
1717
import fnmatch
1818
import glob
19+
import inspect
1920
import logging
2021
import marshal # Exposed through the API.
2122
import optparse
@@ -875,7 +876,7 @@ def ListRelevantPresubmitFiles(files, root):
875876

876877
class GetTrySlavesExecuter(object):
877878
@staticmethod
878-
def ExecPresubmitScript(script_text, presubmit_path, project):
879+
def ExecPresubmitScript(script_text, presubmit_path, project, change):
879880
"""Executes GetPreferredTrySlaves() from a single presubmit script.
880881
881882
Args:
@@ -894,10 +895,14 @@ def ExecPresubmitScript(script_text, presubmit_path, project):
894895

895896
function_name = 'GetPreferredTrySlaves'
896897
if function_name in context:
897-
try:
898-
result = eval(function_name + '(' + repr(project) + ')', context)
899-
except TypeError:
900-
result = eval(function_name + '()', context)
898+
get_preferred_try_slaves = context[function_name]
899+
function_info = inspect.getargspec(get_preferred_try_slaves)
900+
if len(function_info[0]) == 1:
901+
result = get_preferred_try_slaves(project)
902+
elif len(function_info[0]) == 2:
903+
result = get_preferred_try_slaves(project, change)
904+
else:
905+
result = get_preferred_try_slaves()
901906
if not isinstance(result, types.ListType):
902907
raise PresubmitFailure(
903908
'Presubmit functions must return a list, got a %s instead: %s' %
@@ -913,7 +918,8 @@ def ExecPresubmitScript(script_text, presubmit_path, project):
913918
return result
914919

915920

916-
def DoGetTrySlaves(changed_files,
921+
def DoGetTrySlaves(change,
922+
changed_files,
917923
repository_root,
918924
default_presubmit,
919925
project,
@@ -942,15 +948,15 @@ def DoGetTrySlaves(changed_files,
942948
output_stream.write("Running default presubmit script.\n")
943949
fake_path = os.path.join(repository_root, 'PRESUBMIT.py')
944950
results += executer.ExecPresubmitScript(
945-
default_presubmit, fake_path, project)
951+
default_presubmit, fake_path, project, change)
946952
for filename in presubmit_files:
947953
filename = os.path.abspath(filename)
948954
if verbose:
949955
output_stream.write("Running %s\n" % filename)
950956
# Accept CRLF presubmit script.
951957
presubmit_script = gclient_utils.FileRead(filename, 'rU')
952958
results += executer.ExecPresubmitScript(
953-
presubmit_script, filename, project)
959+
presubmit_script, filename, project, change)
954960

955961
slaves = list(set(results))
956962
if slaves and verbose:

tests/presubmit_unittest.py

+34-11
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def testMembersChanged(self):
153153
'OutputApi', 'ParseFiles', 'PresubmitFailure',
154154
'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs',
155155
'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO',
156-
'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'json',
156+
'fix_encoding', 'fnmatch', 'gclient_utils', 'glob', 'inspect', 'json',
157157
'load_files',
158158
'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle',
159159
'presubmit_canned_checks', 'random', 're', 'rietveld', 'scm',
@@ -669,11 +669,18 @@ def CheckChangeOnCommit(input_api, output_api):
669669

670670
def testGetTrySlavesExecuter(self):
671671
self.mox.ReplayAll()
672-
672+
change = presubmit.Change(
673+
'foo',
674+
'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n',
675+
self.fake_root_dir,
676+
None,
677+
0,
678+
0,
679+
None)
673680
executer = presubmit.GetTrySlavesExecuter()
674-
self.assertEqual([], executer.ExecPresubmitScript('', '', ''))
675-
self.assertEqual(
676-
[], executer.ExecPresubmitScript('def foo():\n return\n', '', ''))
681+
self.assertEqual([], executer.ExecPresubmitScript('', '', '', change))
682+
self.assertEqual([],
683+
executer.ExecPresubmitScript('def foo():\n return\n', '', '', change))
677684

678685
# bad results
679686
starts_with_space_result = [' starts_with_space']
@@ -682,7 +689,7 @@ def testGetTrySlavesExecuter(self):
682689
for result in starts_with_space_result, not_list_result1, not_list_result2:
683690
self.assertRaises(presubmit.PresubmitFailure,
684691
executer.ExecPresubmitScript,
685-
self.presubmit_tryslave % result, '', '')
692+
self.presubmit_tryslave % result, '', '', change)
686693

687694
# good results
688695
expected_result = ['1', '2', '3']
@@ -692,20 +699,31 @@ def testGetTrySlavesExecuter(self):
692699
self.assertEqual(
693700
result,
694701
executer.ExecPresubmitScript(
695-
self.presubmit_tryslave % result, '', ''))
702+
self.presubmit_tryslave % result, '', '', change))
696703

697704
def testGetTrySlavesExecuterWithProject(self):
698705
self.mox.ReplayAll()
699706

707+
change = presubmit.Change(
708+
'foo',
709+
'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n',
710+
self.fake_root_dir,
711+
None,
712+
0,
713+
0,
714+
None)
715+
700716
executer = presubmit.GetTrySlavesExecuter()
701717
expected_result1 = ['1', '2']
702718
expected_result2 = ['a', 'b', 'c']
703719
script = self.presubmit_tryslave_project % (
704720
repr('foo'), repr(expected_result1), repr(expected_result2))
705721
self.assertEqual(
706-
expected_result1, executer.ExecPresubmitScript(script, '', 'foo'))
722+
expected_result1, executer.ExecPresubmitScript(script, '', 'foo',
723+
change))
707724
self.assertEqual(
708-
expected_result2, executer.ExecPresubmitScript(script, '', 'bar'))
725+
expected_result2, executer.ExecPresubmitScript(script, '', 'bar',
726+
change))
709727

710728
def testDoGetTrySlaves(self):
711729
join = presubmit.os.path.join
@@ -730,13 +748,18 @@ def testDoGetTrySlaves(self):
730748
self.presubmit_tryslave % '["linux"]')
731749
self.mox.ReplayAll()
732750

751+
change = presubmit.Change(
752+
'mychange', '', self.fake_root_dir, [], 0, 0, None)
753+
733754
output = StringIO.StringIO()
734755
self.assertEqual(['win'],
735-
presubmit.DoGetTrySlaves([filename], self.fake_root_dir,
756+
presubmit.DoGetTrySlaves(change, [filename],
757+
self.fake_root_dir,
736758
None, None, False, output))
737759
output = StringIO.StringIO()
738760
self.assertEqual(['win', 'linux'],
739-
presubmit.DoGetTrySlaves([filename, filename_linux],
761+
presubmit.DoGetTrySlaves(change,
762+
[filename, filename_linux],
740763
self.fake_root_dir, None, None,
741764
False, output))
742765

tests/trychange_unittest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class SVNUnittest(TryChangeTestsBase):
5959
"""trychange.SVN tests."""
6060
def testMembersChanged(self):
6161
members = [
62-
'AutomagicalSettings', 'GetCodeReviewSetting', 'ReadRootFile',
63-
'GenerateDiff', 'GetFileNames',
62+
'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting',
63+
'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples',
6464
]
6565
# If this test fails, you should add the relevant test.
6666
self.compareMembers(trychange.SVN, members)
@@ -83,8 +83,8 @@ class GITUnittest(TryChangeTestsBase):
8383
"""trychange.GIT tests."""
8484
def testMembersChanged(self):
8585
members = [
86-
'AutomagicalSettings', 'GetCodeReviewSetting', 'ReadRootFile',
87-
'GenerateDiff', 'GetFileNames',
86+
'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting',
87+
'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples',
8888
]
8989
# If this test fails, you should add the relevant test.
9090
self.compareMembers(trychange.GIT, members)

0 commit comments

Comments
 (0)