Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: components/policy/tools/generate_policy_source.py

Issue 929353002: Fix generate_policy_source.py to honour supported_on ranges. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Mattias' final nit. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/policy/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 '''python %prog [options] platform chromium_os_flag template 6 '''python %prog [options] platform chromium_os_flag template
7 7
8 platform specifies which platform source is being generated for 8 platform specifies which platform source is being generated for
9 and can be one of (win, mac, linux) 9 and can be one of (win, mac, linux)
10 chromium_os_flag should be 1 if this is a Chromium OS build 10 chromium_os_flag should be 1 if this is a Chromium OS build
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 'choice'), 55 'choice'),
56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList', 56 'string-enum-list': ('TYPE_LIST', 'StringList', 'StringList',
57 'multi-select'), 57 'multi-select'),
58 } 58 }
59 59
60 class EnumItem: 60 class EnumItem:
61 def __init__(self, item): 61 def __init__(self, item):
62 self.caption = PolicyDetails._RemovePlaceholders(item['caption']) 62 self.caption = PolicyDetails._RemovePlaceholders(item['caption'])
63 self.value = item['value'] 63 self.value = item['value']
64 64
65 def __init__(self, policy, os, is_chromium_os): 65 def __init__(self, policy, chrome_major_version, os, is_chromium_os):
66 self.id = policy['id'] 66 self.id = policy['id']
67 self.name = policy['name'] 67 self.name = policy['name']
68 features = policy.get('features', {}) 68 features = policy.get('features', {})
69 self.can_be_recommended = features.get('can_be_recommended', False) 69 self.can_be_recommended = features.get('can_be_recommended', False)
70 self.can_be_mandatory = features.get('can_be_mandatory', True) 70 self.can_be_mandatory = features.get('can_be_mandatory', True)
71 self.is_deprecated = policy.get('deprecated', False) 71 self.is_deprecated = policy.get('deprecated', False)
72 self.is_device_only = policy.get('device_only', False) 72 self.is_device_only = policy.get('device_only', False)
73 self.schema = policy.get('schema', {}) 73 self.schema = policy.get('schema', {})
74 self.has_enterprise_default = 'default_for_enterprise_users' in policy 74 self.has_enterprise_default = 'default_for_enterprise_users' in policy
75 if self.has_enterprise_default: 75 if self.has_enterprise_default:
76 self.enterprise_default = policy['default_for_enterprise_users'] 76 self.enterprise_default = policy['default_for_enterprise_users']
77 77
78 expected_platform = 'chrome_os' if is_chromium_os else os.lower() 78 expected_platform = 'chrome_os' if is_chromium_os else os.lower()
79 self.platforms = [] 79 self.platforms = []
80 for platform, version in [ p.split(':') for p in policy['supported_on'] ]: 80 for platform, version_range in [ p.split(':')
81 if not version.endswith('-'): 81 for p in policy['supported_on'] ]:
82 split_result = version_range.split('-')
83 if len(split_result) != 2:
84 raise RuntimeError('supported_on must have exactly one dash: "%s"' % p)
85 (version_min, version_max) = split_result
86 if version_min == '':
87 raise RuntimeError('supported_on must define a start version: "%s"' % p)
88
89 # Skip if the current Chromium version does not support the policy.
90 if (int(version_min) > chrome_major_version or
91 version_max != '' and int(version_max) < chrome_major_version):
82 continue 92 continue
83 93
84 if platform.startswith('chrome.'): 94 if platform.startswith('chrome.'):
85 platform_sub = platform[7:] 95 platform_sub = platform[7:]
86 if platform_sub == '*': 96 if platform_sub == '*':
87 self.platforms.extend(['win', 'mac', 'linux']) 97 self.platforms.extend(['win', 'mac', 'linux'])
88 else: 98 else:
89 self.platforms.append(platform_sub) 99 self.platforms.append(platform_sub)
90 else: 100 else:
91 self.platforms.append(platform) 101 self.platforms.append(platform)
(...skipping 28 matching lines...) Expand all
120 result = '' 130 result = ''
121 pos = 0 131 pos = 0
122 for m in PolicyDetails.PH_PATTERN.finditer(text): 132 for m in PolicyDetails.PH_PATTERN.finditer(text):
123 result += text[pos:m.start(0)] 133 result += text[pos:m.start(0)]
124 result += m.group(2) or m.group(1) 134 result += m.group(2) or m.group(1)
125 pos = m.end(0) 135 pos = m.end(0)
126 result += text[pos:] 136 result += text[pos:]
127 return result 137 return result
128 138
129 139
140 def ParseVersionFile(version_path):
141 major_version = None
142 for line in open(version_path, 'r').readlines():
143 key, val = line.rstrip('\r\n').split('=', 1)
144 if key == 'MAJOR':
145 major_version = val
146 break
147 if major_version is None:
148 raise RuntimeError('VERSION file does not contain major version.')
149 return major_version
150
151
130 def main(): 152 def main():
131 parser = OptionParser(usage=__doc__) 153 parser = OptionParser(usage=__doc__)
132 parser.add_option('--pch', '--policy-constants-header', dest='header_path', 154 parser.add_option('--pch', '--policy-constants-header', dest='header_path',
133 help='generate header file of policy constants', 155 help='generate header file of policy constants',
134 metavar='FILE') 156 metavar='FILE')
135 parser.add_option('--pcc', '--policy-constants-source', dest='source_path', 157 parser.add_option('--pcc', '--policy-constants-source', dest='source_path',
136 help='generate source file of policy constants', 158 help='generate source file of policy constants',
137 metavar='FILE') 159 metavar='FILE')
138 parser.add_option('--cpp', '--cloud-policy-protobuf', 160 parser.add_option('--cpp', '--cloud-policy-protobuf',
139 dest='cloud_policy_proto_path', 161 dest='cloud_policy_proto_path',
140 help='generate cloud policy protobuf file', 162 help='generate cloud policy protobuf file',
141 metavar='FILE') 163 metavar='FILE')
142 parser.add_option('--csp', '--chrome-settings-protobuf', 164 parser.add_option('--csp', '--chrome-settings-protobuf',
143 dest='chrome_settings_proto_path', 165 dest='chrome_settings_proto_path',
144 help='generate chrome settings protobuf file', 166 help='generate chrome settings protobuf file',
145 metavar='FILE') 167 metavar='FILE')
146 parser.add_option('--cpd', '--cloud-policy-decoder', 168 parser.add_option('--cpd', '--cloud-policy-decoder',
147 dest='cloud_policy_decoder_path', 169 dest='cloud_policy_decoder_path',
148 help='generate C++ code decoding the cloud policy protobuf', 170 help='generate C++ code decoding the cloud policy protobuf',
149 metavar='FILE') 171 metavar='FILE')
150 parser.add_option('--ard', '--app-restrictions-definition', 172 parser.add_option('--ard', '--app-restrictions-definition',
151 dest='app_restrictions_path', 173 dest='app_restrictions_path',
152 help='generate an XML file as specified by ' 174 help='generate an XML file as specified by '
153 'Android\'s App Restriction Schema', 175 'Android\'s App Restriction Schema',
154 metavar='FILE') 176 metavar='FILE')
155 177
156 (opts, args) = parser.parse_args() 178 (opts, args) = parser.parse_args()
157 179
158 if len(args) != 3: 180 if len(args) != 4:
159 print 'exactly platform, chromium_os flag and input file must be specified.' 181 print('Please specify path to src/chrome/VERSION, platform, '
182 'chromium_os flag and input file as positional parameters.')
160 parser.print_help() 183 parser.print_help()
161 return 2 184 return 2
162 185
163 os = args[0] 186 version_path = args[0]
164 is_chromium_os = args[1] == '1' 187 os = args[1]
165 template_file_name = args[2] 188 is_chromium_os = args[2] == '1'
189 template_file_name = args[3]
166 190
191 major_version = ParseVersionFile(version_path)
167 template_file_contents = _LoadJSONFile(template_file_name) 192 template_file_contents = _LoadJSONFile(template_file_name)
168 policy_details = [ PolicyDetails(policy, os, is_chromium_os) 193 policy_details = [ PolicyDetails(policy, major_version, os, is_chromium_os)
169 for policy in _Flatten(template_file_contents) ] 194 for policy in _Flatten(template_file_contents) ]
170 sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name) 195 sorted_policy_details = sorted(policy_details, key=lambda policy: policy.name)
171 196
172 def GenerateFile(path, writer, sorted=False, xml=False): 197 def GenerateFile(path, writer, sorted=False, xml=False):
173 if path: 198 if path:
174 with open(path, 'w') as f: 199 with open(path, 'w') as f:
175 _OutputGeneratedWarningHeader(f, template_file_name, xml) 200 _OutputGeneratedWarningHeader(f, template_file_name, xml)
176 writer(sorted and sorted_policy_details or policy_details, os, f) 201 writer(sorted and sorted_policy_details or policy_details, os, f)
177 202
178 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True) 203 GenerateFile(opts.header_path, _WritePolicyConstantHeader, sorted=True)
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 # _WriteAppRestrictions body 1048 # _WriteAppRestrictions body
1024 f.write('<restrictions xmlns:android="' 1049 f.write('<restrictions xmlns:android="'
1025 'http://schemas.android.com/apk/res/android">\n\n') 1050 'http://schemas.android.com/apk/res/android">\n\n')
1026 for policy in policies: 1051 for policy in policies:
1027 if policy.is_supported and policy.restriction_type != 'invalid': 1052 if policy.is_supported and policy.restriction_type != 'invalid':
1028 WriteAppRestriction(policy) 1053 WriteAppRestriction(policy)
1029 f.write('</restrictions>') 1054 f.write('</restrictions>')
1030 1055
1031 if __name__ == '__main__': 1056 if __name__ == '__main__':
1032 sys.exit(main()) 1057 sys.exit(main())
OLDNEW
« no previous file with comments | « components/policy/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698