1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import optparse 17import os 18import sys 19import stat 20import json 21 22 23def parse_args(args): 24 from scripts.util import build_utils # noqa: E402 25 26 args = build_utils.expand_file_args(args) 27 28 parser = optparse.OptionParser() 29 build_utils.add_depfile_option(parser) 30 parser.add_option('--output', help='fixed para file') 31 parser.add_option('--source-file', help='source para file') 32 parser.add_option('--append-file', action="append", type="string", dest="files", help='appended files') 33 parser.add_option('--append-line', action="append", type="string", dest="lines", help='appended lines') 34 35 options, _ = parser.parse_args(args) 36 return options 37 38 39def append_files(target_f, options): 40 # Read source file 41 with open(options.source_file, 'r') as source_f: 42 source_contents = source_f.read() 43 44 target_f.write(source_contents) 45 if options.files: 46 for append_f in options.files: 47 with open(append_f, 'r') as src: 48 target_f.write(src.read()) 49 if options.lines: 50 for line in options.lines: 51 target_f.write(line) 52 target_f.write("\n") 53 54 55def main(args): 56 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 57 os.pardir, os.pardir, os.pardir, os.pardir, "build")) 58 from scripts.util import build_utils # noqa: E402 59 60 options = parse_args(args) 61 62 if options.files: 63 depfile_deps = ([options.source_file] + options.files) 64 else: 65 depfile_deps = ([options.source_file]) 66 67 with os.fdopen(os.open(options.output, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as target_f: 68 append_files(target_f, options) 69 70 build_utils.write_depfile(options.depfile, 71 options.output, depfile_deps, add_pydeps=False) 72 73 74if __name__ == '__main__': 75 sys.exit(main(sys.argv[1:])) 76