Source code for revive.utils.license_utils

''''''
"""
    POLIXIR REVIVE, copyright (C) 2021-2024 Polixir Technologies Co., Ltd., is 
    distributed under the GNU Lesser General Public License (GNU LGPL). 
    POLIXIR REVIVE is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
"""
import os
import re
import json
import argparse

from io import StringIO
from wurlitzer import pipes, STDOUT
from pyarmor.cli.hdinfo import main as hdinfo_main



[docs] def get_machine_info(output="./machine_info.json", online=False): r""" Retrieves machine information using pyarmor. Args: output (str): The path to save the machine information as a json file. Defaults to "./machine_info.json". online (bool): Whether to return the machine information as a string instead of saving it to a file. Defaults to False. Returns: str: A string containing machine information if online is True, else None. """ assert output.endswith(".json"), f"Machine info should be saved as a json file. -> {output}" out = StringIO() with pipes(stdout=out, stderr=STDOUT): hdinfo_main([]) lines = out.getvalue().split("\n") info = {} for line in lines: key_value = line.split(":", 1) if len(key_value) == 2: key = key_value[0].strip() value = key_value[1] value = value.replace("'","") value = value.replace('"',"") value = value.strip() info[key] = value hd_info = { "machine_id":[], "harddisk" : [], "mac" : [], "ip" : [], } if "Machine ID" in info.keys(): hd_info["machine_id"].append(info["Machine ID"]) if "Default Harddisk Serial Number" in info.keys() and "failed" not in info["Default Harddisk Serial Number"] and "Permission denied" not in info["Default Harddisk Serial Number"]: hd_info["harddisk"].append(info["Default Harddisk Serial Number"]) if "Default Mac address" in info.keys(): hd_info["mac"].append(info["Default Mac address"]) if "Default IPv4 address" in info.keys(): hd_info["ip"].append(info["Default IPv4 address"]) machine_info = {"hd_txt": lines, "hd_info": hd_info} if online: with open(output,"w") as f: json.dump(machine_info,f) with open(output, "r") as f: machine_info = f.readlines()[0] return machine_info with open(output,"w") as f: print(f"Svae machine info -> {output}") json.dump(machine_info,f)
if __name__ == "__main__": parser = argparse.ArgumentParser(description='The file path where the machine information is saved.') parser.add_argument('-o', '--output', default="./machine_info.json", help="The file save path of machine information.") args = parser.parse_args() print(get_machine_info(args.output , online=True))