This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
83 lines (60 loc) · 2.32 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import ctypes
import os
import pathlib
import platform
import sys
from typing import List
def remove_duplicate_filters(filters: List[pathlib.Path]) -> List[pathlib.Path]:
normalized_project_path_set = set()
unique_filters = []
# 确保同时存在 xxx_2012, xxx_2019时,版本较新的会在前面
filters.sort(reverse=True)
for filter in filters:
normalized_project_path = str(filter)
normalized_project_path = remove_suffix(normalized_project_path, ".vcxproj.filters")
normalized_project_path = remove_suffix(normalized_project_path, "_2012")
normalized_project_path = remove_suffix(normalized_project_path, "_2019")
if normalized_project_path in normalized_project_path_set:
continue
normalized_project_path_set.add(normalized_project_path)
unique_filters.append(filter)
return unique_filters
def remove_suffix(s: str, suffix: str) -> str:
if s.endswith(suffix):
s = s[: -len(suffix)]
return s
def make_sure_dir_exists(dir_path):
# debug_print("make_sure_dir_exists: " + dir_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path, exist_ok=True)
# base on https://gist.github.com/GaryLee/d1cf2089c3a515691919
def run_as_admin():
if not is_windows():
return
shell32 = ctypes.windll.shell32
if shell32.IsUserAnAdmin():
return True
argv = sys.argv
if hasattr(sys, '_MEIPASS'):
# Support pyinstaller wrapped program.
arguments = argv[1:]
else:
arguments = argv
argument_line = " ".join(arguments)
executable = sys.executable
print("当前链接类型为软链接,在windows下需要管理员权限,请在弹出的uac框中选择确认,从而使用管理员权限运行。参数信息如下:\n\t", executable, argument_line)
ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
print("run_as_admin ok, exit current process, actual work will be done by elevated process")
sys.exit(0)
def is_windows():
return platform.system() == "Windows"
def pause(ctx: str = ""):
if ctx != "":
print(ctx)
if is_windows():
pause_cmd = "PAUSE"
else:
pause_cmd = 'read -r -p "Press Enter to continue..." key'
os.system(pause_cmd)