from datetime import datetime import json import time import random global MOCK_LIC_USE_INFO MOCK_LIC_USE_INFO = { "27003@szmaslic03": { "name": "27003@szmaslic03", "total": 2, "used": 1, "used_info": [ { "user": "haoxiang.ran", "host": "szl3bc12808", "lic": "szmaslic03", "login": "0.41h" } ] }, "27003@szmaslic04": { "name": "27003@szmaslic04", "total": 2, "used": 1, "used_info": [ { "user": "macro.yang", "host": "szl3bc12804", "lic": "szmaslic04", "login": "52.28h" } ] }, "27003@szmaslic06": { "name": "27003@szmaslic06", "total": 2, "used": 2, "used_info": [ { "user": "zhenhao.he", "host": "szl3bc12804", "lic": "szmaslic06", "login": "0.20h" }, { "user": "phillip.chan", "host": "szl3bc12810", "lic": "szmaslic06", "login": "0.16h" } ] }, "27003@szmaslic07": { "name": "27003@szmaslic07", "total": 2, "used": 1, "used_info": [ { "user": "zhiyi.li", "host": "szl3bc12806", "lic": "szmaslic07", "login": "2.96h" } ] }, "27003@szmaslic08": { "name": "27003@szmaslic08", "total": 2, "used": 2, "used_info": [ { "user": "soo.liu", "host": "szl3bc12809", "lic": "szmaslic08", "login": "6.55h" }, { "user": "haichao.ou", "host": "szl3bc12806", "lic": "szmaslic08", "login": "5.93h" } ] }, "27003@szmaslic09": { "name": "27003@szmaslic09", "total": 2, "used": 2, "used_info": [ { "user": "louie.liang", "host": "szl3bc12810", "lic": "szmaslic09", "login": "8.31h" }, { "user": "zabbix", "host": "szl3bc06409", "lic": "szmaslic09", "login": "0.90h" } ] }, "27003@szmaslic10": { "name": "27003@szmaslic10", "total": 2, "used": 2, "used_info": [ { "user": "harvey.li", "host": "szl3bc12810", "lic": "szmaslic10", "login": "57.38h" }, { "user": "kw.hu", "host": "szl3bc12809", "lic": "szmaslic10", "login": "1.41h" } ] } } def _read_sample_file(file_name:str='lic_info.json'): with open(file_name, 'r') as f: return json.load(f) def _write_sample_file(lic_use_info, file_name:str='lic_info.json'): with open(file_name, 'w') as f: json.dump(lic_use_info, f, indent=4) def mock_ipaddress(domain): id = int(domain[-2:]) if id > 10: return None return f"192.168.1.{id}" def mock_lmutil(cmd): lic_name = cmd.split(" ")[-1] lic_use_info = _read_sample_file() info = lic_use_info.get(lic_name, None) if info is None: return '' used_template = " {user} {host} /dev/tty (v23) ({lic}/27003 337), start {start_time}\n" template = """ Users of VCAST_C_ENT_0800: (Total of {total} licenses issued; Total of {used} licenses in use) "VCAST_C_ENT_0800" v23, vendor: vector, expiry: permanent(no expiration date) vendor_string: CUST:64925: floating license {used_info} Users of CCAST_0801: (Total of 2 licenses issued; Total of 0 licenses in use) """ used_info = '' for item in info.get("used_info", []): start_time = time.time() - float(item.get("login")[:-1]) * 3600 start_time = datetime.fromtimestamp(start_time) start_time = start_time.strftime("%a %m/%d %H:%M") used_info += used_template.format(user=item.get("user"), host=item.get("host"), lic=item.get("lic"), start_time=start_time) ret = template.format(total=info.get("total"), used=info.get("used"), used_info=used_info) return ret def mock_license_add_user(lic, user, host): key = lic lic_use_info = _read_sample_file() temp = lic_use_info.get(key, None) used_info = { "user": user, "host": host, "lic": lic, "login": f"{random.randint(1, 1000)/ 100.0:.2f}h" } if temp is None: lic_use_info[key] = { "name": key, "total": 2, "used": 0, "used_info": [used_info] } else: lic_use_info[key]["used"] += 1 lic_use_info[key]["used_info"].append(used_info) _write_sample_file(lic_use_info) def mock_license_remove_user(user, host): lic_use_info = _read_sample_file() for _, info in lic_use_info.items(): for item in info.get("used_info", []): if item.get("user") == user and item.get("host") == host: info["used"] -= 1 info["used_info"].remove(item) _write_sample_file(lic_use_info) if __name__ == "__main__": _write_sample_file(MOCK_LIC_USE_INFO)