| 8 | import select |
| 9 | |
| 10 | class Utils: |
| 11 | |
| 12 | def __init__(self, name = "Python Script", interactive = True): |
| 13 | self.name = name |
| 14 | self.interactive = interactive |
| 15 | # Init our colors before we need to print anything |
| 16 | cwd = os.getcwd() |
| 17 | os.chdir(os.path.dirname(os.path.realpath(__file__))) |
| 18 | if os.path.exists("colors.json"): |
| 19 | self.colors_dict = json.load(open("colors.json")) |
| 20 | else: |
| 21 | self.colors_dict = {} |
| 22 | os.chdir(cwd) |
| 23 | |
| 24 | def check_admin(self): |
| 25 | # Returns whether or not we're admin |
| 26 | try: |
| 27 | is_admin = os.getuid() == 0 |
| 28 | except AttributeError: |
| 29 | is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 |
| 30 | return is_admin |
| 31 | |
| 32 | def elevate(self, file): |
| 33 | # Runs the passed file as admin |
| 34 | if self.check_admin(): |
| 35 | return |
| 36 | if os.name == "nt": |
| 37 | ctypes.windll.shell32.ShellExecuteW(None, "runas", '"{}"'.format(sys.executable), '"{}"'.format(file), None, 1) |
| 38 | else: |
| 39 | try: |
| 40 | p = subprocess.Popen(["which", "sudo"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 41 | c = p.communicate()[0].decode("utf-8", "ignore").replace("\n", "") |
| 42 | os.execv(c, [ sys.executable, 'python'] + sys.argv) |
| 43 | except: |
| 44 | exit(1) |
| 45 | |
| 46 | def compare_versions(self, vers1, vers2, **kwargs): |
| 47 | # Helper method to compare ##.## strings |
| 48 | # |
| 49 | # vers1 < vers2 = True |
| 50 | # vers1 = vers2 = None |
| 51 | # vers1 > vers2 = False |
| 52 | |
| 53 | # Sanitize the pads |
| 54 | pad = str(kwargs.get("pad", "")) |
| 55 | sep = str(kwargs.get("separator", ".")) |
| 56 | |
| 57 | ignore_case = kwargs.get("ignore_case", True) |
| 58 | |
| 59 | # Cast as strings |
| 60 | vers1 = str(vers1) |
| 61 | vers2 = str(vers2) |
| 62 | |
| 63 | if ignore_case: |
| 64 | vers1 = vers1.lower() |
| 65 | vers2 = vers2.lower() |
| 66 | |
| 67 | # Split and pad lists |
nothing calls this directly
no outgoing calls
no test coverage detected