Commit 139c1efd authored by anina koehler's avatar anina koehler Committed by Commit Bot

Run policy-presubmit when presubmit is changed

When policy templates are changed, syntax_check_policy_template_json.py is run as part of the presubmit checks. It should also be run when changes to the file itself were made.

Bug: 1045930
Change-Id: Ic8870bd748d80d87b99b93889640cf2ebbbf0b27
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2022794Reviewed-by: default avatarAlexander Hendrich <hendrich@chromium.org>
Commit-Queue: Anina Koehler <aninak@google.com>
Cr-Commit-Position: refs/heads/master@{#737240}
parent 6c52146d
...@@ -23,58 +23,55 @@ def _GetPolicyTemplates(template_path): ...@@ -23,58 +23,55 @@ def _GetPolicyTemplates(template_path):
def _CheckPolicyTemplatesSyntax(input_api, output_api): def _CheckPolicyTemplatesSyntax(input_api, output_api):
local_path = input_api.PresubmitLocalPath() local_path = input_api.PresubmitLocalPath()
filepath = input_api.os_path.join(local_path, 'policy_templates.json') filepath = input_api.os_path.join(local_path, 'policy_templates.json')
if any(f.AbsoluteLocalPath() == filepath
for f in input_api.AffectedFiles()): try:
old_sys_path = sys.path template_affected_file = next(iter(f \
try: for f in input_api.change.AffectedFiles() \
tools_path = input_api.os_path.normpath( if f.AbsoluteLocalPath() == filepath))
input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools')) except:
sys.path = [ tools_path ] + sys.path template_affected_file = None
# Optimization: only load this when it's needed.
import syntax_check_policy_template_json old_sys_path = sys.path
device_policy_proto_path = input_api.os_path.join( try:
local_path, '../proto/chrome_device_policy.proto') tools_path = input_api.os_path.normpath(
args = ["--device_policy_proto_path=" + device_policy_proto_path] input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools'))
sys.path = [ tools_path ] + sys.path
root = input_api.change.RepositoryRoot() # Optimization: only load this when it's needed.
import syntax_check_policy_template_json
current_version = None device_policy_proto_path = input_api.os_path.join(
original_file_contents = None local_path, '../proto/chrome_device_policy.proto')
args = ["--device_policy_proto_path=" + device_policy_proto_path]
# Check if there is a tag that allows us to bypass compatibility checks.
# This can be used in situations where there is a bug in the validation root = input_api.change.RepositoryRoot()
# code or if a policy change needs to urgently be submitted.
if not input_api.change.tags.get('BYPASS_POLICY_COMPATIBILITY_CHECK'): current_version = None
# Get the current version from the VERSION file so that we can check original_file_contents = None
# which policies are un-released and thus can be changed at will.
try: # Check if there is a tag that allows us to bypass compatibility checks.
version_path = input_api.os_path.join( # This can be used in situations where there is a bug in the validation
root, 'chrome', 'VERSION') # code or if a policy change needs to urgently be submitted.
with open(version_path, "rb") as f: if not input_api.change.tags.get('BYPASS_POLICY_COMPATIBILITY_CHECK'):
current_version = int(f.readline().split("=")[1]) # Get the current version from the VERSION file so that we can check
print ('Checking policies against current version: ' + # which policies are un-released and thus can be changed at will.
current_version) try:
except: version_path = input_api.os_path.join(root, 'chrome', 'VERSION')
pass with open(version_path, "rb") as f:
current_version = int(f.readline().split("=")[1])
# Get the original file contents of the policy file so that we can check print ('Checking policies against current version: ' +
# the compatibility of template changes in it current_version)
template_path = input_api.os_path.join( except:
root, 'components', 'policy', 'resources', 'policy_templates.json') pass
affected_files = input_api.change.AffectedFiles()
template_affected_file = next(iter(f \ # Get the original file contents of the policy file so that we can check
for f in affected_files if f.AbsoluteLocalPath() == template_path)) # the compatibility of template changes in it
if template_affected_file is not None: if template_affected_file is not None:
original_file_contents = \ original_file_contents = '\n'.join(template_affected_file.OldContents())
'\n'.join(template_affected_file.OldContents())
checker = syntax_check_policy_template_json.PolicyTemplateChecker()
checker = syntax_check_policy_template_json.PolicyTemplateChecker() if checker.Run(args, filepath, original_file_contents, current_version) > 0:
if checker.Run(args, filepath, return [output_api.PresubmitError('Syntax error(s) in file:', [filepath])]
original_file_contents, current_version) > 0: finally:
return [output_api.PresubmitError('Syntax error(s) in file:', sys.path = old_sys_path
[filepath])]
finally:
sys.path = old_sys_path
return [] return []
...@@ -210,11 +207,14 @@ def _CheckMissingPlaceholders(input_api, output_api, template_path): ...@@ -210,11 +207,14 @@ def _CheckMissingPlaceholders(input_api, output_api, template_path):
def _CommonChecks(input_api, output_api): def _CommonChecks(input_api, output_api):
results = [] results = []
root = input_api.change.RepositoryRoot() root = input_api.change.RepositoryRoot()
template_path = template_path = input_api.os_path.join( template_path = input_api.os_path.join(
root, 'components', 'policy', 'resources', 'policy_templates.json') root, 'components', 'policy', 'resources', 'policy_templates.json')
# policies in chrome/test/data/policy/policy_test_cases.json. # policies in chrome/test/data/policy/policy_test_cases.json.
test_cases_path = input_api.os_path.join( test_cases_path = input_api.os_path.join(
root, 'chrome', 'test', 'data', 'policy', 'policy_test_cases.json') root, 'chrome', 'test', 'data', 'policy', 'policy_test_cases.json')
syntax_check_path = input_api.os_path.join(
root, 'components', 'policy', 'tools',
'syntax_check_policy_template_json.py')
affected_files = input_api.change.AffectedFiles() affected_files = input_api.change.AffectedFiles()
results.extend(_CheckMissingPlaceholders(input_api, output_api, results.extend(_CheckMissingPlaceholders(input_api, output_api,
...@@ -223,15 +223,17 @@ def _CommonChecks(input_api, output_api): ...@@ -223,15 +223,17 @@ def _CommonChecks(input_api, output_api):
for f in affected_files) for f in affected_files)
tests_changed = any(f.AbsoluteLocalPath() == test_cases_path \ tests_changed = any(f.AbsoluteLocalPath() == test_cases_path \
for f in affected_files) for f in affected_files)
syntax_check_changed = any(f.AbsoluteLocalPath() == syntax_check_path \
for f in affected_files)
if template_changed or tests_changed: if template_changed or tests_changed or syntax_check_changed:
try: try:
policies = _GetPolicyTemplates(template_path) policies = _GetPolicyTemplates(template_path)
except: except:
results.append(output_api.PresubmitError('Invalid Python/JSON syntax.')) results.append(output_api.PresubmitError('Invalid Python/JSON syntax.'))
return results return results
results.extend(_CheckPolicyTestCases(input_api, output_api, policies)) results.extend(_CheckPolicyTestCases(input_api, output_api, policies))
if template_changed: if template_changed or syntax_check_changed:
results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api)) results.extend(_CheckPolicyTemplatesSyntax(input_api, output_api))
results.extend(_CheckPolicyHistograms(input_api, output_api, policies)) results.extend(_CheckPolicyHistograms(input_api, output_api, policies))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment