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 json 20import stat 21 22sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 23 os.pardir, os.pardir, os.pardir, "build")) 24from scripts.util import build_utils # noqa: E402 25 26 27def parse_args(args): 28 args = build_utils.expand_file_args(args) 29 30 parser = optparse.OptionParser() 31 build_utils.add_depfile_option(parser) 32 parser.add_option('--output', help='fixed para file') 33 parser.add_option('--source-file', help='source para file') 34 parser.add_option('--extra', action="append", type="string", dest="extra", help='extra params') 35 36 options, _ = parser.parse_args(args) 37 return options 38 39 40def parse_params(line, contents): 41 line = line.strip() 42 pos = line.find('=') 43 if pos <= 0: 44 return 45 name = line[:pos] 46 value = line[pos + 1:] 47 name = name.strip() 48 value = value.strip() 49 contents[name] = value 50 51 52def parse_extra_params(extras, contents): 53 for extra in extras: 54 extra = extra.strip() 55 parse_params(extra, contents) 56 57 58def fix_para_file(options): 59 contents = {} 60 61 # Read source file 62 with open(options.source_file, 'r') as f: 63 lines = f.readlines() 64 for line in lines: 65 line = line.strip() 66 # Strip comments 67 if line.startswith('#') or not line: 68 continue 69 parse_params(line, contents) 70 71 if options.extra: 72 parse_extra_params(options.extra, contents) 73 74 flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC 75 modes = stat.S_IWUSR | stat.S_IRUSR | stat.S_IWGRP | stat.S_IRGRP 76 with os.fdopen(os.open(options.output, flags, modes), 'w') as f: 77 for key in contents: 78 f.write("".join([key, "=", contents[key], '\n'])) 79 80 81def main(args): 82 options = parse_args(args) 83 84 depfile_deps = ([options.source_file]) 85 86 fix_para_file(options) 87 build_utils.write_depfile(options.depfile, 88 options.output, depfile_deps, add_pydeps=False) 89 90if __name__ == '__main__': 91 sys.exit(main(sys.argv[1:])) 92