-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolutionUpgrader.py
169 lines (148 loc) · 5.24 KB
/
SolutionUpgrader.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import requests
import shutil
from datetime import datetime
import sys
import subprocess
import tempfile
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Use temp directory for lock file
lock_file = os.path.join(tempfile.gettempdir(), "SolutionUpgrader.lock")
# URL of the repository with the updated files
repo_url = "https://raw.githubusercontent.com/tomasmark79/MarkWareVCMake/refs/heads/main/"
files_to_update = [
# ".github/workflows/install.yml",
# ".github/workflows/macos.yml",
# ".github/workflows/ubuntu.yml",
# ".github/workflows/windows.yml",
# ".vscode/c_cpp_properties.json",
# ".vscode/keybindings.json",
# ".vscode/launch.json",
# ".vscode/settings.json",
# ".vscode/tasks.json",
# "cmake/Modules/FindX11.cmake",
# "cmake/CPM.cmake",
# "cmake/tools.cmake",
# "include/VCMLib/VCMLib.hpp",
# "Source/VCMLib.cpp",
# "Standalone/Source/Main.cpp",
# "Standalone/CMakeLists.txt",
# "Standalone/LICENSE",
# ".clang-format",
# ".cmake-format",
# "CMakeLists.txt",
# "conanfile.py",
# ".gitattributes",
# ".gitignore",
# "LICENSE",
# ".python-version",
# "README.md",
# "SolutionController.py",
# "SolutionRenamer.py",
"SolutionUpgrader.py"
]
def check_write_permissions(path):
try:
test_file = os.path.join(os.path.dirname(path) or '.', '.write_test')
with open(test_file, 'w') as f:
f.write('test')
os.remove(test_file)
return True
except (IOError, OSError):
return False
def create_backup_dir():
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
backup_dir = os.path.join("backup", timestamp)
os.makedirs(backup_dir, exist_ok=True)
return backup_dir
def can_update_file(file_path):
# Always allow self-update
if file_path == "SolutionUpgrader.py":
return True
try:
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
if "<VCMAKE_NO_UPDATE>" in line:
return False
except UnicodeDecodeError:
with open(file_path, 'r') as file:
for line in file:
if "<VCMAKE_NO_UPDATE>" in line:
return False
return True
def update_file(file_path, backup_dir):
if not check_write_permissions(file_path):
logging.error(f"No write permissions for {file_path}")
return False
try:
url = repo_url + file_path
response = requests.get(url, timeout=30, verify=True)
response.raise_for_status()
# Backup existing file
if os.path.exists(file_path) and file_path != "SolutionUpgrader.py":
backup_path = os.path.join(backup_dir, file_path)
os.makedirs(os.path.dirname(backup_path), exist_ok=True)
shutil.copy2(file_path, backup_path)
logging.info(f"Backed up: {file_path}")
# Create directory if needed
dir_name = os.path.dirname(file_path)
if dir_name:
os.makedirs(dir_name, exist_ok=True)
# Try UTF-8 first, fallback to detected encoding
try:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(response.text)
logging.info(f"Updated: {file_path}")
except UnicodeEncodeError:
with open(file_path, 'w', encoding=response.encoding) as file:
file.write(response.text)
logging.info(f"Updated with {response.encoding} encoding: {file_path}")
return True
except requests.RequestException as e:
logging.error(f"Failed to update {file_path}: {str(e)}")
except OSError as e:
logging.error(f"File system error for {file_path}: {str(e)}")
return False
def restart_script():
logging.info("Restarting script...")
if sys.platform.startswith('win'):
subprocess.Popen([sys.executable] + sys.argv, shell=True)
else:
subprocess.Popen([sys.executable] + sys.argv)
sys.exit(0)
def main():
if os.path.exists(lock_file):
os.remove(lock_file)
logging.info("Lock file removed")
return
backup_dir = None
updated_self = False
for file_path in files_to_update:
if os.path.exists(file_path):
if can_update_file(file_path):
logging.info(f"Updating: {file_path}")
# Vytvoř backup_dir pouze když je potřeba zálohovat
if file_path != "SolutionUpgrader.py" and backup_dir is None:
backup_dir = create_backup_dir()
# Update souboru
if update_file(file_path, backup_dir):
if file_path == "SolutionUpgrader.py":
updated_self = True
else:
logging.info(f"Skipped (protected): {file_path}")
else:
logging.info(f"Creating new file: {file_path}")
if backup_dir is None:
backup_dir = create_backup_dir()
update_file(file_path, backup_dir)
if updated_self:
with open(lock_file, 'w') as f:
f.write("lock")
restart_script()
if __name__ == "__main__":
main()