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 21import operator 22 23 24def parse_args(args): 25 from scripts.util import build_utils # noqa: E402 26 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('--append-file', action="append", type="string", dest="files", help='appended files') 34 parser.add_option('--append-line', action="append", type="string", dest="lines", help='appended lines') 35 36 options, _ = parser.parse_args(args) 37 return options 38 39 40def load_group_file_as_dict(source_f): 41 source_dict = {} 42 for line in source_f: 43 arr = line.strip().split(":") 44 if arr: 45 key = arr[0] 46 passwd_gid_value = [arr[1], arr[2]] 47 value = [':'.join(passwd_gid_value), arr[3]] 48 source_dict[key] = value 49 return source_dict 50 51 52def insert_append_user_value(key, arr, source_dict): 53 if source_dict[key][1] and arr[3]: 54 if arr[3] not in source_dict[key][1]: 55 user_value = [source_dict[key][1], arr[3]] 56 source_dict[key][1] = ','.join(user_value) 57 elif source_dict[key][1] == " " and arr[3]: 58 source_dict[key][1] = arr[3] 59 60 61def get_append_value(src, source_dict): 62 for line in src: 63 if line != "\n": 64 arr = line.strip().split(":") 65 key = arr[0] 66 passwd_gid_value = [arr[1], arr[2]] 67 if len(arr) > 3: 68 value = [':'.join(passwd_gid_value), arr[3]] 69 else: 70 value = [':'.join(passwd_gid_value), " "] 71 if key in source_dict.keys(): 72 insert_append_user_value(key, arr, source_dict) 73 else: 74 source_dict[key] = value 75 76 77def append_group_by_lines(options, source_dict): 78 for line in options.lines: 79 arr = line.strip().split(":") 80 key = arr[0] 81 passwd_gid_value = [arr[1], arr[2]] 82 if len(arr) > 3: 83 value = [':'.join(passwd_gid_value), arr[3]] 84 else: 85 value = [':'.join(passwd_gid_value), " "] 86 if key in source_dict.keys(): 87 insert_append_user_value(key, arr, source_dict) 88 else: 89 source_dict[key] = value 90 91 92def append_group_by_files(options, source_dict): 93 for append_f in options.files: 94 with open(append_f, 'r') as src: 95 get_append_value(src, source_dict) 96 97 98def append_group_files(target_f, options): 99 with open(options.source_file, 'r') as source_f: 100 source_dict = load_group_file_as_dict(source_f) 101 if options.files: 102 append_group_by_files(options, source_dict) 103 if options.lines: 104 append_group_by_lines(options, source_dict) 105 for item in source_dict: 106 target_f.write(f"{item}:{':'.join(source_dict[item])}\n") 107 108 109def append_passwd_files(target_f, options): 110 # Read source file 111 with open(options.source_file, 'r') as source_f: 112 source_contents = source_f.read() 113 114 target_f.write(source_contents) 115 if options.files: 116 for append_f in options.files: 117 with open(append_f, 'r') as src: 118 target_f.write(src.read()) 119 if options.lines: 120 for line in options.lines: 121 target_f.write(line) 122 target_f.write("\n") 123 124 125def main(args): 126 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 127 os.pardir, os.pardir, os.pardir, os.pardir, "build")) 128 from scripts.util import build_utils # noqa: E402 129 130 options = parse_args(args) 131 132 if options.files: 133 depfile_deps = ([options.source_file] + options.files) 134 else: 135 depfile_deps = ([options.source_file]) 136 137 with os.fdopen(os.open(options.output, os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as target_f: 138 if operator.contains(options.source_file, "group"): 139 append_group_files(target_f, options) 140 else: 141 append_passwd_files(target_f, options) 142 143 build_utils.write_depfile(options.depfile, 144 options.output, depfile_deps, add_pydeps=False) 145 146 147if __name__ == '__main__': 148 sys.exit(main(sys.argv[1:])) 149