• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#coding=utf-8
3
4#
5# Copyright (c) 2024 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import string
20import sys
21import os
22
23class GroupFileParser():
24    def __init__(self):
25        self._group = {}
26
27    def load_file(self, file_name):
28        if not os.path.exists(file_name):
29            return
30        try:
31            with open(file_name, encoding='utf-8') as fp:
32                line = fp.readline()
33                while line :
34                    if line.startswith("#") or len(line) < 3:
35                        line = fp.readline()
36                        continue
37                    group_info = line.strip("\n").split(":")
38                    if len (group_info) < 3:
39                        line = fp.readline()
40                        continue
41                    self._handle_group_info(group_info)
42                    line = fp.readline()
43        except:
44            pass
45
46    def dump(self):
47        for group in self._group.values() :
48            print(str(group))
49
50    def _handle_group_info(self, group_info):
51        name = group_info[0].strip()
52        user_names = name
53        if len(group_info) > 3 and len(group_info[3]) > 0:
54            user_names = group_info[3]
55        old_group = self._group.get(name)
56        if old_group:
57            return
58        group = {
59            "name" : name,
60            "groupId" : int(group_info[2], 10),
61            "user_names" : user_names
62        }
63        self._group[name] = group
64
65class PasswdFileParser():
66    def __init__(self):
67        self._passwd = {}
68        self._passwd_yellow = {}
69        self._name_list = []
70        self._uid_list = []
71
72    def load_file(self, file_name):
73        if not os.path.exists(file_name):
74            return
75        try:
76            with open(file_name, encoding='utf-8') as fp:
77                line = fp.readline()
78                while line :
79                    if line.startswith("#") or len(line) < 3:
80                        line = fp.readline()
81                        continue
82                    passwd_info = line.strip("\n").split(":")
83                    if len (passwd_info) < 4:
84                        line = fp.readline()
85                        continue
86                    self._handle_passwd_info(passwd_info)
87                    line = fp.readline()
88        except:
89            pass
90
91    def dump(self):
92        for group in self._passwd.values() :
93            print(str(group))
94
95    def _handle_passwd_info(self, passwd_info):
96        name = passwd_info[0].strip()
97        self._uid_list.append(int(passwd_info[2], 10))
98        self._name_list.append(name)
99        old_passwd = self._passwd.get(name)
100        if old_passwd:
101            return
102
103        gid = int(passwd_info[3], 10)
104        uid = int(passwd_info[2], 10)
105
106        passwd = {
107            "name" : name,
108            "groupId" : gid,
109            "passwdId" : uid
110        }
111        self._passwd[name] = passwd
112
113def _create_arg_parser():
114    import argparse
115    parser = argparse.ArgumentParser(description='Collect group information from system/etc/group dir.')
116    parser.add_argument('-i', '--input',
117                        help='input group files base directory example "out/rk3568/packages/phone/" ', required=True)
118
119    parser.add_argument('-o', '--output',
120                        help='output group information database directory', required=False)
121    return parser
122
123def create_user_group_parser(base_path):
124    path = os.path.join(base_path, "packages/phone")
125    parser = GroupFileParser()
126    parser.load_file(os.path.join(path, "system/etc/group"))
127
128    passwd = PasswdFileParser()
129    passwd.load_file(os.path.join(path, "system/etc/passwd"))
130    return parser, passwd
131
132if __name__ == '__main__':
133    args_parser = _create_arg_parser()
134    options = args_parser.parse_args()
135    create_user_group_parser(options.input)
136
137