@ -310,32 +310,6 @@ def prompt_should_continue(prompt_string):
return response in ( ' y ' , ' yes ' )
return response in ( ' y ' , ' yes ' )
def _ShouldRunPresubmit ( script_text , use_python3 ) :
""" Try to figure out whether these presubmit checks should be run under
python2 or python3 . We need to do this without actually trying to
compile the text , since the text might compile in one but not the
other .
Args :
script_text : The text of the presubmit script .
use_python3 : if true , will use python3 instead of python2 by default
if USE_PYTHON3 is not specified .
Return :
A boolean if presubmit should be executed
"""
if os . getenv ( ' LUCI_OMIT_PYTHON2 ' ) == ' true ' :
# If LUCI omits python2, run all presubmits with python3, regardless of
# USE_PYTHON3 variable.
return True
m = re . search ( ' ^USE_PYTHON3 = (True|False)$ ' , script_text , flags = re . MULTILINE )
if m :
use_python3 = m . group ( 1 ) == ' True '
return ( ( sys . version_info . major == 2 ) and not use_python3 ) or \
( ( sys . version_info . major == 3 ) and use_python3 )
# Top level object so multiprocessing can pickle
# Top level object so multiprocessing can pickle
# Public access through OutputApi object.
# Public access through OutputApi object.
class _PresubmitResult ( object ) :
class _PresubmitResult ( object ) :
@ -1412,17 +1386,14 @@ def ListRelevantPresubmitFiles(files, root):
class GetPostUploadExecuter ( object ) :
class GetPostUploadExecuter ( object ) :
def __init__ ( self , change , gerrit_obj , use_python3 ):
def __init__ ( self , change , gerrit_obj ):
"""
"""
Args :
Args :
change : The Change object .
change : The Change object .
gerrit_obj : provides basic Gerrit codereview functionality .
gerrit_obj : provides basic Gerrit codereview functionality .
use_python3 : if true , will use python3 instead of python2 by default
if USE_PYTHON3 is not specified .
"""
"""
self . change = change
self . change = change
self . gerrit = gerrit_obj
self . gerrit = gerrit_obj
self . use_python3 = use_python3
def ExecPresubmitScript ( self , script_text , presubmit_path ) :
def ExecPresubmitScript ( self , script_text , presubmit_path ) :
""" Executes PostUploadHook() from a single presubmit script.
""" Executes PostUploadHook() from a single presubmit script.
@ -1479,15 +1450,13 @@ def _MergeMasters(masters1, masters2):
return result
return result
def DoPostUploadExecuter ( change , gerrit_obj , verbose , use_python3 = False ):
def DoPostUploadExecuter ( change , gerrit_obj , verbose ):
""" Execute the post upload hook.
""" Execute the post upload hook.
Args :
Args :
change : The Change object .
change : The Change object .
gerrit_obj : The GerritAccessor object .
gerrit_obj : The GerritAccessor object .
verbose : Prints debug info .
verbose : Prints debug info .
use_python3 : if true , default to using Python3 for presubmit checks
rather than Python2 .
"""
"""
python_version = ' Python %s ' % sys . version_info . major
python_version = ' Python %s ' % sys . version_info . major
sys . stdout . write ( ' Running %s post upload checks ... \n ' % python_version )
sys . stdout . write ( ' Running %s post upload checks ... \n ' % python_version )
@ -1496,7 +1465,7 @@ def DoPostUploadExecuter(change, gerrit_obj, verbose, use_python3=False):
if not presubmit_files and verbose :
if not presubmit_files and verbose :
sys . stdout . write ( ' Warning, no PRESUBMIT.py found. \n ' )
sys . stdout . write ( ' Warning, no PRESUBMIT.py found. \n ' )
results = [ ]
results = [ ]
executer = GetPostUploadExecuter ( change , gerrit_obj , use_python3 )
executer = GetPostUploadExecuter ( change , gerrit_obj )
# The root presubmit file should be executed after the ones in subdirectories.
# The root presubmit file should be executed after the ones in subdirectories.
# i.e. the specific post upload hooks should run before the general ones.
# i.e. the specific post upload hooks should run before the general ones.
# Thus, reverse the order provided by ListRelevantPresubmitFiles.
# Thus, reverse the order provided by ListRelevantPresubmitFiles.
@ -1506,14 +1475,9 @@ def DoPostUploadExecuter(change, gerrit_obj, verbose, use_python3=False):
filename = os . path . abspath ( filename )
filename = os . path . abspath ( filename )
# Accept CRLF presubmit script.
# Accept CRLF presubmit script.
presubmit_script = gclient_utils . FileRead ( filename ) . replace ( ' \r \n ' , ' \n ' )
presubmit_script = gclient_utils . FileRead ( filename ) . replace ( ' \r \n ' , ' \n ' )
if _ShouldRunPresubmit ( presubmit_script , use_python3 ) :
if verbose :
if sys . version_info [ 0 ] == 2 :
sys . stdout . write ( ' Running %s \n ' % filename )
sys . stdout . write (
results . extend ( executer . ExecPresubmitScript ( presubmit_script , filename ) )
' Running %s under Python 2. Add USE_PYTHON3 = True to prevent '
' this. \n ' % filename )
elif verbose :
sys . stdout . write ( ' Running %s \n ' % filename )
results . extend ( executer . ExecPresubmitScript ( presubmit_script , filename ) )
if not results :
if not results :
return 0
return 0
@ -1532,8 +1496,7 @@ def DoPostUploadExecuter(change, gerrit_obj, verbose, use_python3=False):
class PresubmitExecuter ( object ) :
class PresubmitExecuter ( object ) :
def __init__ ( self , change , committing , verbose , gerrit_obj , dry_run = None ,
def __init__ ( self , change , committing , verbose , gerrit_obj , dry_run = None ,
thread_pool = None , parallel = False , use_python3 = False ,
thread_pool = None , parallel = False , no_diffs = False ) :
no_diffs = False ) :
"""
"""
Args :
Args :
change : The Change object .
change : The Change object .
@ -1542,8 +1505,6 @@ class PresubmitExecuter(object):
dry_run : if true , some Checks will be skipped .
dry_run : if true , some Checks will be skipped .
parallel : if true , all tests reported via input_api . RunTests for all
parallel : if true , all tests reported via input_api . RunTests for all
PRESUBMIT files will be run in parallel .
PRESUBMIT files will be run in parallel .
use_python3 : if true , will use python3 instead of python2 by default
if USE_PYTHON3 is not specified .
no_diffs : if true , implies that - - files or - - all was specified so some
no_diffs : if true , implies that - - files or - - all was specified so some
checks can be skipped , and some errors will be messages .
checks can be skipped , and some errors will be messages .
"""
"""
@ -1555,7 +1516,6 @@ class PresubmitExecuter(object):
self . more_cc = [ ]
self . more_cc = [ ]
self . thread_pool = thread_pool
self . thread_pool = thread_pool
self . parallel = parallel
self . parallel = parallel
self . use_python3 = use_python3
self . no_diffs = no_diffs
self . no_diffs = no_diffs
def ExecPresubmitScript ( self , script_text , presubmit_path ) :
def ExecPresubmitScript ( self , script_text , presubmit_path ) :
@ -1728,7 +1688,6 @@ def DoPresubmitChecks(change,
dry_run = None ,
dry_run = None ,
parallel = False ,
parallel = False ,
json_output = None ,
json_output = None ,
use_python3 = False ,
no_diffs = False ) :
no_diffs = False ) :
""" Runs all presubmit checks that apply to the files in the change.
""" Runs all presubmit checks that apply to the files in the change.
@ -1750,8 +1709,6 @@ def DoPresubmitChecks(change,
dry_run : if true , some Checks will be skipped .
dry_run : if true , some Checks will be skipped .
parallel : if true , all tests specified by input_api . RunTests in all
parallel : if true , all tests specified by input_api . RunTests in all
PRESUBMIT files will be run in parallel .
PRESUBMIT files will be run in parallel .
use_python3 : if true , default to using Python3 for presubmit checks
rather than Python2 .
no_diffs : if true , implies that - - files or - - all was specified so some
no_diffs : if true , implies that - - files or - - all was specified so some
checks can be skipped , and some errors will be messages .
checks can be skipped , and some errors will be messages .
Return :
Return :
@ -1785,31 +1742,19 @@ def DoPresubmitChecks(change,
os . remove ( python2_usage_log_file )
os . remove ( python2_usage_log_file )
thread_pool = ThreadPool ( )
thread_pool = ThreadPool ( )
executer = PresubmitExecuter ( change , committing , verbose , gerrit_obj ,
executer = PresubmitExecuter ( change , committing , verbose , gerrit_obj ,
dry_run , thread_pool , parallel , use_python3 ,
dry_run , thread_pool , parallel , no_diffs )
no_diffs )
skipped_count = 0 ;
if default_presubmit :
if default_presubmit :
if verbose :
if verbose :
sys . stdout . write ( ' Running default presubmit script. \n ' )
sys . stdout . write ( ' Running default presubmit script. \n ' )
fake_path = os . path . join ( change . RepositoryRoot ( ) , ' PRESUBMIT.py ' )
fake_path = os . path . join ( change . RepositoryRoot ( ) , ' PRESUBMIT.py ' )
if _ShouldRunPresubmit ( default_presubmit , use_python3 ) :
results + = executer . ExecPresubmitScript ( default_presubmit , fake_path )
results + = executer . ExecPresubmitScript ( default_presubmit , fake_path )
else :
skipped_count + = 1
for filename in presubmit_files :
for filename in presubmit_files :
filename = os . path . abspath ( filename )
filename = os . path . abspath ( filename )
# Accept CRLF presubmit script.
# Accept CRLF presubmit script.
presubmit_script = gclient_utils . FileRead ( filename ) . replace ( ' \r \n ' , ' \n ' )
presubmit_script = gclient_utils . FileRead ( filename ) . replace ( ' \r \n ' , ' \n ' )
if _ShouldRunPresubmit ( presubmit_script , use_python3 ) :
if verbose :
if sys . version_info [ 0 ] == 2 :
sys . stdout . write ( ' Running %s \n ' % filename )
sys . stdout . write (
results + = executer . ExecPresubmitScript ( presubmit_script , filename )
' Running %s under Python 2. Add USE_PYTHON3 = True to prevent '
' this. \n ' % filename )
elif verbose :
sys . stdout . write ( ' Running %s \n ' % filename )
results + = executer . ExecPresubmitScript ( presubmit_script , filename )
else :
skipped_count + = 1
results + = thread_pool . RunAsync ( )
results + = thread_pool . RunAsync ( )
@ -1880,7 +1825,6 @@ def DoPresubmitChecks(change,
for warning in messages . get ( ' Warnings ' , [ ] )
for warning in messages . get ( ' Warnings ' , [ ] )
] ,
] ,
' more_cc ' : executer . more_cc ,
' more_cc ' : executer . more_cc ,
' skipped_presubmits ' : skipped_count ,
}
}
gclient_utils . FileWrite (
gclient_utils . FileWrite (
@ -2079,8 +2023,6 @@ def main(argv=None):
' wildcards can also be used. ' )
' wildcards can also be used. ' )
parser . add_argument ( ' --source_controlled_only ' , action = ' store_true ' ,
parser . add_argument ( ' --source_controlled_only ' , action = ' store_true ' ,
help = ' Constrain \' files \' to those in source control. ' )
help = ' Constrain \' files \' to those in source control. ' )
parser . add_argument ( ' --use-python3 ' , action = ' store_true ' ,
help = ' Use python3 for presubmit checks by default ' )
parser . add_argument ( ' --no_diffs ' , action = ' store_true ' ,
parser . add_argument ( ' --no_diffs ' , action = ' store_true ' ,
help = ' Assume that all " modified " files have no diffs. ' )
help = ' Assume that all " modified " files have no diffs. ' )
options = parser . parse_args ( argv )
options = parser . parse_args ( argv )
@ -2107,8 +2049,7 @@ def main(argv=None):
try :
try :
if options . post_upload :
if options . post_upload :
return DoPostUploadExecuter ( change , gerrit_obj , options . verbose ,
return DoPostUploadExecuter ( change , gerrit_obj , options . verbose )
options . use_python3 )
with canned_check_filter ( options . skip_canned ) :
with canned_check_filter ( options . skip_canned ) :
return DoPresubmitChecks (
return DoPresubmitChecks (
change ,
change ,
@ -2120,7 +2061,6 @@ def main(argv=None):
options . dry_run ,
options . dry_run ,
options . parallel ,
options . parallel ,
options . json_output ,
options . json_output ,
options . use_python3 ,
options . no_diffs )
options . no_diffs )
except PresubmitFailure as e :
except PresubmitFailure as e :
import utils
import utils