Commit cdd64f0b authored by Pavol Marko's avatar Pavol Marko Committed by Commit Bot

generate_policy_source.py: Support --all-chrome-versions

Support generating policy sources from policy_templates.json without
version restrictions. This is useful for testing where we may want to
set policies that are not yet supported or are not supported anymore.

  python components/policy/tools/generate_policy_source.py \
    --policy-templates-file=components/policy/resources/policy_templates.json \
    --target-platform=chromeos --chrome-version-file=chrome/VERSION \
    --policy-constants-source=test.cc
and
  python components/policy/tools/generate_policy_source.py \
    --policy-templates-file=components/policy/resources/policy_templates.json \
    --target-platform=chromeos --all-chrome-versions \
    --policy-constants-source=test2.cc
then diff test.cc test2.cc

Bug: 976424
Test: Run
Change-Id: I3d0b7a0676ba771aec06ff7df0465e3b9539fd62
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1686668
Commit-Queue: Pavol Marko <pmarko@chromium.org>
Reviewed-by: default avatarLutz Justen <ljusten@chromium.org>
Cr-Commit-Position: refs/heads/master@{#686588}
parent 582fdbe0
...@@ -5,11 +5,12 @@ ...@@ -5,11 +5,12 @@
'''python %prog [options] '''python %prog [options]
Pass at least: Pass at least:
--chrome-version-file <path to src/chrome/VERSION> --chrome-version-file <path to src/chrome/VERSION> or --all-chrome-versions
--target-platform <which platform the target code will be generated for and can --target-platform <which platform the target code will be generated for and can
be one of (win, mac, linux, chromeos, fuchsia)> be one of (win, mac, linux, chromeos, fuchsia)>
--policy_templates <path to the policy_templates.json input file>.''' --policy_templates <path to the policy_templates.json input file>.'''
from __future__ import with_statement from __future__ import with_statement
from collections import namedtuple from collections import namedtuple
from collections import OrderedDict from collections import OrderedDict
...@@ -103,10 +104,12 @@ class PolicyDetails: ...@@ -103,10 +104,12 @@ class PolicyDetails:
if version_min == '': if version_min == '':
raise RuntimeError('supported_on must define a start version: "%s"' % p) raise RuntimeError('supported_on must define a start version: "%s"' % p)
# Skip if the current Chromium version does not support the policy. # Skip if filtering by Chromium version and the current Chromium version
if (int(version_min) > chrome_major_version or # does not support the policy.
version_max != '' and int(version_max) < chrome_major_version): if chrome_major_version:
continue if (int(version_min) > chrome_major_version or
version_max != '' and int(version_max) < chrome_major_version):
continue
if platform.startswith('chrome.'): if platform.startswith('chrome.'):
platform_sub = platform[7:] platform_sub = platform[7:]
...@@ -195,15 +198,15 @@ class PolicyAtomicGroup: ...@@ -195,15 +198,15 @@ class PolicyAtomicGroup:
def ParseVersionFile(version_path): def ParseVersionFile(version_path):
major_version = None chrome_major_version = None
for line in open(version_path, 'r').readlines(): for line in open(version_path, 'r').readlines():
key, val = line.rstrip('\r\n').split('=', 1) key, val = line.rstrip('\r\n').split('=', 1)
if key == 'MAJOR': if key == 'MAJOR':
major_version = val chrome_major_version = val
break break
if major_version is None: if chrome_major_version is None:
raise RuntimeError('VERSION file does not contain major version.') raise RuntimeError('VERSION file does not contain major version.')
return int(major_version) return int(chrome_major_version)
def main(): def main():
...@@ -276,6 +279,12 @@ def main(): ...@@ -276,6 +279,12 @@ def main():
dest='chrome_version_file', dest='chrome_version_file',
help='path to src/chrome/VERSION', help='path to src/chrome/VERSION',
metavar='FILE') metavar='FILE')
parser.add_option(
'--all-chrome-versions',
action='store_true',
dest='all_chrome_versions',
default=False,
help='do not restrict generated policies by chrome version')
parser.add_option( parser.add_option(
'--target-platform', '--target-platform',
dest='target_platform', dest='target_platform',
...@@ -289,12 +298,25 @@ def main(): ...@@ -289,12 +298,25 @@ def main():
metavar='FILE') metavar='FILE')
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
if (not opts.chrome_version_file or not opts.target_platform or has_arg_error = False
not opts.policy_templates_file):
print('Please specify at least:\n' if not opts.target_platform:
'--chrome-version-file=<path to src/chrome/VERSION>\n' print('Error: Missing --target-platform=<platform>')
'--target-platform=<platform>\n' has_arg_error = True
'--policy-templates-file=<path to policy_templates.json')
if not opts.policy_templates_file:
print('Error: Missing'
' --policy-templates-file=<path to policy_templates.json>')
has_arg_error = True
if not opts.chrome_version_file and not opts.all_chrome_versions:
print('Error: Missing'
' --chrome-version-file=<path to src/chrome/VERSION>\n'
' or --all-chrome-versions')
has_arg_error = True
if has_arg_error:
print('')
parser.print_help() parser.print_help()
return 2 return 2
...@@ -307,11 +329,15 @@ def main(): ...@@ -307,11 +329,15 @@ def main():
if target_platform == 'chromeos': if target_platform == 'chromeos':
target_platform = 'chrome_os' target_platform = 'chrome_os'
major_version = ParseVersionFile(version_path) if opts.all_chrome_versions:
chrome_major_version = None
else:
chrome_major_version = ParseVersionFile(version_path)
template_file_contents = _LoadJSONFile(template_file_name) template_file_contents = _LoadJSONFile(template_file_name)
risk_tags = RiskTags(template_file_contents) risk_tags = RiskTags(template_file_contents)
policy_details = [ policy_details = [
PolicyDetails(policy, major_version, target_platform, PolicyDetails(policy, chrome_major_version, target_platform,
risk_tags.GetValidTags()) risk_tags.GetValidTags())
for policy in template_file_contents['policy_definitions'] for policy in template_file_contents['policy_definitions']
if policy['type'] != 'group' if policy['type'] != 'group'
......
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