1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2022 Huawei Device Co., Ltd.
5#
6# HDF is dual licensed: you can use it either under the terms of
7# the GPL, or the BSD license, at your option.
8# See the LICENSE file in the root of this repository for complete details.
9
10
11import os
12import random
13import re
14
15import hdf_utils
16
17
18class OperateGroupPasswd(object):
19    def __init__(self, tool_settings, root_path):
20        super(OperateGroupPasswd, self).__init__()
21        etc_path = tool_settings.get_passwd_group_config()
22        self.group_newline = etc_path.get("group").get("info_temp")
23        self.passwd_newline = etc_path.get("passwd").get("info_temp")
24
25        self.passwd_file = os.path.join(root_path, etc_path.get("passwd").get("path"))
26        self.group_file = os.path.join(root_path, etc_path.get("group").get("path"))
27
28        self.passwd_lines = hdf_utils.read_file_lines(self.passwd_file)
29        self.group_lines = hdf_utils.read_file_lines(self.group_file)
30
31        self.passwd_group_name_list = []
32        self.temp_id = self.get_id()
33
34    def operate_group(self, name):
35        result_group = self.group_newline.format(peripheral_name=name, uid=self.temp_id)
36        if result_group.split(":")[0] not in self.passwd_group_name_list:
37            self.group_lines.append(result_group)
38            hdf_utils.write_file_lines(self.group_file, self.group_lines)
39        return self.group_file
40
41    def operate_passwd(self, name):
42        result_passwd = self.passwd_newline.format(peripheral_name=name, uid=self.temp_id)
43        if result_passwd.split(":")[0] not in self.passwd_group_name_list:
44            self.passwd_lines.append(result_passwd)
45            hdf_utils.write_file_lines(self.passwd_file, self.passwd_lines)
46        return self.passwd_file
47
48    def get_id(self):
49        passwd_group_id_list = []
50        for line in self.group_lines:
51            id_re_result = re.search(r"x:\d+", line)
52            if id_re_result:
53                gid = id_re_result.group().split(":")[-1]
54                passwd_group_id_list.append(int(gid))
55            self.passwd_group_name_list.append(line.split(":")[0])
56        while True:
57            temp_id = self.generate_id(max(passwd_group_id_list))
58            if temp_id not in passwd_group_id_list:
59                break
60        return temp_id
61
62    def delete_group_passwd(self, name, file_path):
63        base_group_passwd_str = ":".join(self.group_newline.strip().strip(":").split(":")[:-1])
64        res_base = base_group_passwd_str.format(peripheral_name=name)
65        temp_lines = hdf_utils.read_file_lines(file_path)
66        for index, line in enumerate(temp_lines):
67            if line.startswith(res_base):
68                del temp_lines[index]
69                break
70        hdf_utils.write_file_lines(file_path, temp_lines)
71
72    @staticmethod
73    def generate_id(max_border):
74        max_border += 20
75        return random.randint(99, max_border)
76
77