0
Files
android_webview
apps
ash
base
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
cloud_print
codelabs
components
content
courgette
crypto
dbus
device
docs
extensions
fuchsia
gin
google_apis
google_update
gpu
headless
infra
ios
ipc
jingle
media
mojo
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
skia
sql
storage
styleguide
testing
third_party
tools
accessibility
aggregation_service
android
binary_size
bisect_repackage
cfi
check_ecs_deps
checkbins
checklicenses
checkperms
checkteamtags
chrome_extensions
clang
code_coverage
compile_test
coverity
cr
cros
cygprofile
determinism
diagnosis
dromaeo_benchmark_runner
dump_process_memory
emacs
find_runtime_symbols
flags
flakiness
fuchsia
gdb
generate_library_loader
generate_shim_headers
generate_stubs
get_swarming_logs
git
gn
grit
gritsettings
idl_parser
imagediff
infra
ipc_fuzzer
json_comment_eater
json_schema_compiler
highlighters
test
BUILD.gn
DIR_METADATA
OWNERS
PRESUBMIT.py
cc_generator.py
code.py
code_test.py
compiler.py
cpp_bundle_generator.py
cpp_bundle_generator_test.py
cpp_generator.py
cpp_namespace_environment.py
cpp_type_generator.py
cpp_type_generator_test.py
cpp_util.py
cpp_util_test.py
feature_compiler.py
feature_compiler_test.py
features_cc_generator.py
features_compiler.py
features_h_generator.py
h_generator.py
idl_schema.py
idl_schema_test.py
js_externs_generator.py
js_externs_generator_test.py
js_interface_generator.py
js_interface_generator_test.py
js_util.py
json_features.gni
json_parse.py
json_schema.py
json_schema_api.gni
json_schema_test.py
manifest_parse_util.cc
manifest_parse_util.h
memoize.py
model.py
model_test.py
namespace_resolver.py
preview.py
schema_loader.py
schema_util.py
schema_util_test.py
util.cc
util.h
util_cc_helper.py
json_to_struct
l10n
linux
lldb
luci-go
mac
mb
md_browser
media_engagement_preload
memory
metrics
msan
oopif
origin_trials
page_cycler
perf
polymer
privacy_budget
protoc_wrapper
python
real_world_impact
resources
resultdb
security
site_compare
stats_viewer
strict_enum_value_checker
style_variable_generator
sublime
symsrc
tcmalloc
tests
traceline
tracing
traffic_annotation
translation
typescript
ubsan
usb_gadget
v8_context_snapshot
valgrind
variations
vim
vscode
web_bluetooth
web_dev_style
win
.style.yapf
DEPS
DIR_METADATA
OWNERS
apply_cpplint_header_guard.py
auto-nav.py
autotest.py
bash-completion
bisect-builds.py
bisect_test.py
boilerplate.py
buildstate.bat
buildstate.py
check_git_config.py
check_grd_for_unused_strings.py
clang-format-js
diagnose-me.py
download_optimization_profile.py
gypv8sh.py
include_tracer.py
ipc_messages_log.py
licenses.py
make-gtest-filter.py
multi_process_rss.py
nocompile_driver.py
omahaproxy.py
perry.py
remove_duplicate_includes.py
remove_stale_pyc_files.py
roll_webgl_conformance.py
run-swarmed.py
sort-headers.py
sort_sources.py
uberblame.py
unused-symbols-report.py
update_pgo_profiles.py
yes_no.py
ui
url
weblayer
.clang-format
.clang-tidy
.eslintrc.js
.git-blame-ignore-revs
.gitattributes
.gitignore
.gn
.mailmap
.vpython
.vpython3
.yapfignore
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
DEPS
DIR_METADATA
ENG_REVIEW_OWNERS
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings
src/tools/json_schema_compiler/features_compiler.py
Ari Chivukula f89535e101 Python3Migration-PresubmitChecks: json_schema_compiler
This CL is part of a larger effort to deprecate python2 in PRESUBMIT.
json_schema_compiler's PRESUBMIT file will now run unit tests in
python3. In order to do this, every file's #! must specify python3
(in this dir, most were ambiguous and simply said python).

Bug: 1212101
Change-Id: I870ac1cc70325c79bccfd3df3d37432f24b97f54
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2921953
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Ari Chivukula <arichiv@chromium.org>
Cr-Commit-Position: refs/heads/master@{#887392}
2021-05-27 23:36:06 +00:00

81 lines
2.8 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Generator for C++ features from json files.
Usage example:
features_compiler.py --destdir gen --root /home/Work/src _permissions.json
"""
from __future__ import print_function
import optparse
import os
from schema_loader import SchemaLoader
from features_cc_generator import CCGenerator
from features_h_generator import HGenerator
from model import CreateFeature
def _GenerateSchema(filename, root, destdir, namespace):
"""Generates C++ features files from the json file |filename|.
"""
# Load in the feature permissions from the JSON file.
schema = os.path.normpath(filename)
schema_loader = SchemaLoader(os.path.dirname(os.path.relpath(schema, root)),
os.path.dirname(schema),
[],
None)
schema_filename = os.path.splitext(schema)[0]
feature_defs = schema_loader.LoadSchema(schema)
# Generate a list of the features defined and a list of their models.
feature_list = []
for feature_def, feature in feature_defs.items():
feature_list.append(CreateFeature(feature_def, feature))
source_file_dir, _ = os.path.split(schema)
relpath = os.path.relpath(os.path.normpath(source_file_dir), root)
full_path = os.path.join(relpath, schema)
generators = [
('%s.cc' % schema_filename, CCGenerator()),
('%s.h' % schema_filename, HGenerator())
]
# Generate and output the code for all features.
output_code = []
for filename, generator in generators:
code = generator.Generate(feature_list, full_path, namespace).Render()
if destdir:
with open(os.path.join(destdir, relpath, filename), 'w') as f:
f.write(code)
output_code += [filename, '', code, '']
return '\n'.join(output_code)
if __name__ == '__main__':
parser = optparse.OptionParser(
description='Generates a C++ features model from JSON schema',
usage='usage: %prog [option]... schema')
parser.add_option('-r', '--root', default='.',
help='logical include root directory. Path to schema files from '
'specified dir will be the include path.')
parser.add_option('-d', '--destdir',
help='root directory to output generated files.')
parser.add_option('-n', '--namespace', default='generated_features',
help='C++ namespace for generated files. e.g extensions::api.')
(opts, filenames) = parser.parse_args()
# Only one file is currently specified.
if len(filenames) != 1:
raise ValueError('One (and only one) file is required (for now).')
result = _GenerateSchema(filenames[0], opts.root, opts.destdir,
opts.namespace)
if not opts.destdir:
print(result)