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