diff --git a/services/etc/appender/file_appender.gni b/services/etc/appender/file_appender.gni new file mode 100755 index 0000000000000000000000000000000000000000..dfed821f0179b7be14257b23d02419cca286d7d3 --- /dev/null +++ b/services/etc/appender/file_appender.gni @@ -0,0 +1,124 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/python.gni") +import("//build/templates/common/copy.gni") + +# Append something (files or lines) to a file and install it. +# +# Variables: +# source: file to be appended (Required) +# deps: some targets deps on (Optional) +# output: the final install file name (Optional); +# If not set, it will install with the source file name +# files: files to be appended to the end of source file (Optional) +# lines: lines of strings to be appended to the end of souce file (Optional) +# +# Example: +# ohos_file_appender("cust_passwd") { +# source = "//base/startup/init/services/etc/passwd" +# deps = [ "//base/startup/init/services/etc:passwd" ] +# files = [ "cust_passwd", "passwd2" ] +# lines = [ "tses::1222:1222:/bin/false", "tses2::1223:1223:/bin/false" ] +# output = "passwd" +# } +# It will append files and lines to source passwd file after deps targets +# +template("ohos_file_appender") { + assert(defined(invoker.source), "source full target name must be defined.") + + _file_appender_target = "${target_name}_appended" + + if (defined(invoker.output)) { + _final_install_name = get_path_info(invoker.output, "file") + } else { + _final_install_name = get_path_info(invoker.source, "file") + } + + _appended_file = + target_gen_dir + "/${target_name}.appended/" + _final_install_name + + action_with_pydeps(_file_appender_target) { + script = "//base/startup/init/services/etc/appender/file_appender.py" + depfile = "${target_gen_dir}/${target_name}.d" + if (defined(invoker.deps)) { + deps = invoker.deps + } else { + deps = [] + } + args = [ + "--output", + rebase_path(_appended_file, root_build_dir), + "--source-file", + rebase_path(invoker.source, root_build_dir), + "--depfile", + rebase_path(depfile, root_build_dir), + ] + if (defined(invoker.files)) { + foreach(file, invoker.files) { + args += [ + "--append-file", + rebase_path(file, root_build_dir), + ] + } + } + if (defined(invoker.lines)) { + foreach(line, invoker.lines) { + args += [ + "--append-line", + line, + ] + } + } + inputs = [ invoker.source ] + outputs = [ _appended_file ] + } + + ohos_copy(target_name) { + forward_variables_from(invoker, + [ + "testonly", + "visibility", + + "deps", + "public_configs", + "subsystem_name", + "part_name", + + # For generate_module_info + "install_images", + "module_install_dir", + "relative_install_dir", + "symlink_target_name", + + # Open source license related + "license_file", + "license_as_sources", + ]) + if (defined(deps)) { + deps += [ ":$_file_appender_target" ] + } else { + deps = [ ":$_file_appender_target" ] + } + set_sources_assignment_filter([]) + sources = [ _appended_file ] + outputs = [ "${target_out_dir}/${target_name}/${_final_install_name}" ] + module_type = "etc" + install_enable = true + module_source_dir = "${target_out_dir}/${target_name}" + module_install_name = _final_install_name + if (defined(invoker.install_enable)) { + install_enable = invoker.install_enable + } + } +} diff --git a/services/etc/appender/file_appender.py b/services/etc/appender/file_appender.py new file mode 100755 index 0000000000000000000000000000000000000000..a7a7db2d574dfb80ba8a4fbf1b6ac53741b1183f --- /dev/null +++ b/services/etc/appender/file_appender.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import optparse +import os +import sys +import stat +import json + + +def parse_args(args): + from scripts.util import build_utils # noqa: E402 + + args = build_utils.expand_file_args(args) + + parser = optparse.OptionParser() + build_utils.add_depfile_option(parser) + parser.add_option('--output', help='fixed para file') + parser.add_option('--source-file', help='source para file') + parser.add_option('--append-file', action="append", type="string", dest="files", help='appended files') + parser.add_option('--append-line', action="append", type="string", dest="lines", help='appended lines') + + options, _ = parser.parse_args(args) + return options + + +def append_files(target_f, options): + # Read source file + with open(options.source_file, 'r') as source_f: + source_contents = source_f.read() + + target_f.write(source_contents) + if options.files: + for append_f in options.files: + with open(append_f, 'r') as src: + target_f.write(src.read()) + if options.lines: + for line in options.lines: + target_f.write(line) + target_f.write("\n") + + +def main(args): + sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, + os.pardir, os.pardir, os.pardir, os.pardir, "build")) + from scripts.util import build_utils # noqa: E402 + + options = parse_args(args) + + if options.files: + depfile_deps = ([options.source_file] + options.files) + else: + depfile_deps = ([options.source_file]) + + with os.fdopen(os.open(options.output, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as target_f: + append_files(target_f, options) + + build_utils.write_depfile(options.depfile, + options.output, depfile_deps, add_pydeps=False) + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/services/etc/appender/file_appender.pydeps b/services/etc/appender/file_appender.pydeps new file mode 100755 index 0000000000000000000000000000000000000000..2255c08a9c4c3b6cea238c86cf402da4ef5a1579 --- /dev/null +++ b/services/etc/appender/file_appender.pydeps @@ -0,0 +1,9 @@ +# Generated by running: +# build/print_python_deps.py --root base/startup/init/services/etc/appender --output base/startup/init/services/etc/appender/file_appender.pydeps base/startup/init/services/etc/appender/file_appender.py +../../../../../../build/gn_helpers.py +../../../../../../build/scripts/__init__.py +../../../../../../build/scripts/util/__init__.py +../../../../../../build/scripts/util/build_utils.py +../../../../../../build/scripts/util/md5_check.py +../../../../../../build/scripts/util/pycache.py +file_appender.py