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):
def _CheckPolicyTemplatesSyntax(input_api, output_api):
local_path = input_api.PresubmitLocalPath()
filepath = input_api.os_path.join(local_path, 'policy_templates.json')
if any(f.AbsoluteLocalPath() == filepath
for f in input_api.AffectedFiles()):
old_sys_path = sys.path
try:
tools_path = input_api.os_path.normpath(
input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools'))
sys.path = [ tools_path ] + sys.path
# Optimization: only load this when it's needed.
import syntax_check_policy_template_json
device_policy_proto_path = input_api.os_path.join(
local_path, '../proto/chrome_device_policy.proto')
args = ["--device_policy_proto_path=" + device_policy_proto_path]
root = input_api.change.RepositoryRoot()
current_version = None
original_file_contents = None
# 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
# code or if a policy change needs to urgently be submitted.
if not input_api.change.tags.get('BYPASS_POLICY_COMPATIBILITY_CHECK'):
# Get the current version from the VERSION file so that we can check
# which policies are un-released and thus can be changed at will.
try:
version_path = input_api.os_path.join(
root, 'chrome', 'VERSION')
with open(version_path, "rb") as f:
current_version = int(f.readline().split("=")[1])
print ('Checking policies against current version: ' +
current_version)
except:
pass
# Get the original file contents of the policy file so that we can check
# the compatibility of template changes in it
template_path = input_api.os_path.join(
root, 'components', 'policy', 'resources', 'policy_templates.json')
affected_files = input_api.change.AffectedFiles()
template_affected_file = next(iter(f \
for f in affected_files if f.AbsoluteLocalPath() == template_path))
if template_affected_file is not None:
original_file_contents = \
'\n'.join(template_affected_file.OldContents())
checker = syntax_check_policy_template_json.PolicyTemplateChecker()
if checker.Run(args, filepath,
original_file_contents, current_version) > 0:
return [output_api.PresubmitError('Syntax error(s) in file:',
[filepath])]
finally:
sys.path = old_sys_path
try:
template_affected_file = next(iter(f \
for f in input_api.change.AffectedFiles() \
if f.AbsoluteLocalPath() == filepath))
except:
template_affected_file = None
old_sys_path = sys.path
try:
tools_path = input_api.os_path.normpath(
input_api.os_path.join(local_path, input_api.os_path.pardir, 'tools'))
sys.path = [ tools_path ] + sys.path
# Optimization: only load this when it's needed.
import syntax_check_policy_template_json
device_policy_proto_path = input_api.os_path.join(
local_path, '../proto/chrome_device_policy.proto')
args = ["--device_policy_proto_path=" + device_policy_proto_path]
root = input_api.change.RepositoryRoot()
current_version = None
original_file_contents = None
# 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
# code or if a policy change needs to urgently be submitted.
if not input_api.change.tags.get('BYPASS_POLICY_COMPATIBILITY_CHECK'):
# Get the current version from the VERSION file so that we can check
# which policies are un-released and thus can be changed at will.
try:
version_path = input_api.os_path.join(root, 'chrome', 'VERSION')
with open(version_path, "rb") as f:
current_version = int(f.readline().split("=")[1])
print ('Checking policies against current version: ' +
current_version)
except:
pass
# Get the original file contents of the policy file so that we can check
# the compatibility of template changes in it
if template_affected_file is not None:
original_file_contents = '\n'.join(template_affected_file.OldContents())
checker = syntax_check_policy_template_json.PolicyTemplateChecker()
if checker.Run(args, filepath, original_file_contents, current_version) > 0:
return [output_api.PresubmitError('Syntax error(s) in file:', [filepath])]
finally:
sys.path = old_sys_path
return []
......@@ -210,11 +207,14 @@ def _CheckMissingPlaceholders(input_api, output_api, template_path):
def _CommonChecks(input_api, output_api):
results = []
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')
# policies in chrome/test/data/policy/policy_test_cases.json.
test_cases_path = input_api.os_path.join(
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()
results.extend(_CheckMissingPlaceholders(input_api, output_api,
......@@ -223,15 +223,17 @@ def _CommonChecks(input_api, output_api):
for f in affected_files)
tests_changed = any(f.AbsoluteLocalPath() == test_cases_path \
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:
policies = _GetPolicyTemplates(template_path)
except:
results.append(output_api.PresubmitError('Invalid Python/JSON syntax.'))
return results
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(_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