@ -24,12 +24,13 @@ def validate_content(content: str, source_file_dir: str,
""" Validate the content as a metadata file.
Args :
content : the entire content of a file to be validated as a metadata file .
source_file_dir : the directory of the metadata file that the license file
value is from ; this is needed to construct file paths to
license files .
repo_root_dir : the repository ' s root directory; this is needed to construct
file paths to license files .
content : the entire content of a file to be validated as a
metadata file .
source_file_dir : the directory of the metadata file that the
license file value is from ; this is needed to
construct file paths to license files .
repo_root_dir : the repository ' s root directory; this is needed
to construct file paths to license files .
Returns : the validation results for the given content .
"""
@ -40,14 +41,17 @@ def validate_content(content: str, source_file_dir: str,
return [ result ]
for dependency in dependencies :
dependency_results = dependency . validate ( source_file_dir = source_file_dir ,
repo_root_dir = repo_root_dir )
dependency_results = dependency . validate (
source_file_dir = source_file_dir , repo_root_dir = repo_root_dir )
results . extend ( dependency_results )
return results
def _construct_file_read_error ( filepath : str , cause : str ) - > vr . ValidationError :
""" Helper function to create a validation error for a file reading issue. """
def _construct_file_read_error ( filepath : str ,
cause : str ) - > vr . ValidationError :
""" Helper function to create a validation error for a
file reading issue .
"""
result = vr . ValidationError (
reason = " Cannot read metadata file. " ,
additional = [ f " Attempted to read ' { filepath } ' but { cause } . " ] )
@ -59,18 +63,19 @@ def validate_file(
repo_root_dir : str ,
reader : Callable [ [ str ] , Union [ str , bytes ] ] = None ,
) - > List [ vr . ValidationResult ] :
""" Validate the item located at the given filepath is a valid dependency
metadata file .
""" Validate the item located at the given filepath is a valid
dependency metadata file .
Args :
filepath : the path to validate ,
e . g . " /chromium/src/third_party/libname/README.chromium "
repo_root_dir : the repository ' s root directory; this is needed to construct
file paths to license files .
reader ( optional ) : callable function / method to read the content of the file .
Returns : the validation results for the given filepath and its contents , if
it exists .
filepath : the path to validate , e . g .
" /chromium/src/third_party/libname/README.chromium "
repo_root_dir : the repository ' s root directory; this is needed
to construct file paths to license files .
reader ( optional ) : callable function / method to read the content
of the file .
Returns : the validation results for the given filepath and its
contents , if it exists .
"""
if reader is None :
reader = gclient_utils . FileRead
@ -82,7 +87,9 @@ def validate_file(
except PermissionError :
return [ _construct_file_read_error ( filepath , " access denied " ) ]
except Exception as e :
return [ _construct_file_read_error ( filepath , f " unexpected error: ' { e } ' " ) ]
return [
_construct_file_read_error ( filepath , f " unexpected error: ' { e } ' " )
]
else :
if not isinstance ( content , str ) :
return [ _construct_file_read_error ( filepath , " decoding failed " ) ]
@ -99,21 +106,22 @@ def check_file(
repo_root_dir : str ,
reader : Callable [ [ str ] , Union [ str , bytes ] ] = None ,
) - > Tuple [ List [ str ] , List [ str ] ] :
""" Run metadata validation on the given filepath, and return all validation
errors and validation warnings .
""" Run metadata validation on the given filepath, and return all
validation errors and validation warnings .
Args :
filepath : the path to a metadata file ,
e . g . " /chromium/src/third_party/libname/README.chromium "
repo_root_dir : the repository ' s root directory; this is needed to construct
file paths to license files .
reader ( optional ) : callable function / method to read the content of the file .
filepath : the path to a metadata file , e . g .
" /chromium/src/third_party/libname/README.chromium "
repo_root_dir : the repository ' s root directory; this is needed
to construct file paths to license files .
reader ( optional ) : callable function / method to read the content
of the file .
Returns :
error_messages : the fatal validation issues present in the file ;
i . e . presubmit should fail .
warning_messages : the non - fatal validation issues present in the file ;
i . e . presubmit should still pass .
warning_messages : the non - fatal validation issues present in the
file ; i . e . presubmit should still pass .
"""
results = validate_file ( filepath = filepath ,
repo_root_dir = repo_root_dir ,
@ -124,11 +132,12 @@ def check_file(
for result in results :
message = result . get_message ( width = 60 )
# TODO(aredulla): Actually distinguish between validation errors and
# warnings. The quality of metadata is currently being uplifted, but is not
# yet guaranteed to pass validation. So for now, all validation results will
# be returned as warnings so CLs are not blocked by invalid metadata in
# presubmits yet. Bug: b/285453019.
# TODO(aredulla): Actually distinguish between validation errors
# and warnings. The quality of metadata is currently being
# uplifted, but is not yet guaranteed to pass validation. So for
# now, all validation results will be returned as warnings so
# CLs are not blocked by invalid metadata in presubmits yet.
# Bug: b/285453019.
# if result.is_fatal():
# error_messages.append(message)
# else: