-
Notifications
You must be signed in to change notification settings - Fork 2
/
mint-upgrade
executable file
·183 lines (150 loc) · 5.19 KB
/
mint-upgrade
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python3
import os
import re
import shutil
import subprocess
import sys
from os import path
from dataclasses import dataclass
# WORK IN PROGRESS, DO NOT USE
precious = [
'KEYBOARD.TBL', # keyboard layout
'sys-root/opt/GEM/teradesk/TERADESK.INF', # desktop configuration
'xaaes/320200.16', # xaaes desktop background
'xaaes/640200.4'
]
@dataclass(frozen=True)
class MintInstall:
'''filesystem installation of FreeMiNT'''
version: str
short_version: str
path: str
def getpaths(self):
sysdir = path.join(self.path, 'MINT', self.version)
loader = f"mint-{self.short_version}.prg"
loader_path = path.join(self.path, 'AUTO', loader)
if not path.isfile(loader_path):
loader = loader.upper()
loader_path = path.join(self.path, 'AUTO', loader)
if not path.isfile(loader_path):
print(
f"no loader {loader_path} found for mint version {self.version}",
file=sys.stderr)
return sysdir, loader_path
def usage():
print(f"usage: {sys.argv[0]} build_dir partition_to_upgrade")
def backup(install):
backup_dir = path.join(install.path, 'BACKUP.MNT')
sysdir = install.getpaths()[0]
os.makedirs(backup_dir, exist_ok=True)
backups = []
for prec in precious:
src = path.join(sysdir, prec)
dest = path.join(backup_dir, path.basename(prec))
if not path.exists(src):
continue
elif path.isfile(src):
shutil.copy(src, dest)
elif path.isdir(src):
shutil.copytree(src, dest)
else:
print(f"{src}: unrecognized file type",
file=sys.stderr)
sys.exit(1)
backups.append({'originalpath': prec, 'backup': dest})
return backups
def copy(install_source, install_dest):
source_mint = path.join(install_source.path, 'mint')
target_mint = path.join(install_dest.path, 'MINT')
if not path.isdir(source_mint):
print(f"unable to find a source version in {source_mint}",
file=sys.stderr)
sys.exit(1)
shutil.copytree(source_mint, target_mint, dirs_exist_ok=True)
shutil.copytree(path.join(source, 'auto'),
path.join(target, 'AUTO'),
dirs_exist_ok=True)
def get_versions(rootdir):
sysdir_parent = path.join(rootdir, 'mint')
if not path.isdir(sysdir_parent):
sysdir_parent = path.join(rootdir, 'MINT')
if not path.isdir(sysdir_parent):
print(f"{sysdir_parent} does not contain a mint/MINT directory")
sys.exit(1)
(param, dirs, all_files) = next(os.walk(sysdir_parent))
match = None
for dir in dirs:
match = re.search(r'\d-\d{2}-(.{3})', dir)
if match is None:
print(f"unable to find a FreeMiNT version in {sysdir_parent}",
file=sys.stderr)
sys.exit(1)
version = match.group()
short_version = match.group(1)
return version, short_version
def revert(source, target):
version, short_version = get_versions(source)
sysdir, loader = get_mint_paths(target, version, short_version)
shutil.rmtree(sysdir)
os.unlink(loader)
def mark_distfiles(new, verbose=False):
marked = []
sysdir = new.getpaths()[0]
for file in precious:
full_p = path.join(sysdir, file)
if not path.isfile(full_p):
continue
new_full_p = path.splitext(full_p)[0] + '.VEN'
shutil.move(full_p, new_full_p)
marked.append(new_full_p)
if verbose is True:
print('vendor files renamed', marked)
return marked
def restore(backups, install):
restored = []
sysdir = install.getpaths()[0]
for backup in backups:
src = backup['backup']
dest = path.join(sysdir, backup['originalpath'])
if path.isfile(src):
shutil.copy(src, dest)
elif path.isdir(src):
shutil.copytree(src, dest)
else:
print(f"could not find file {src} to restore",
file=sys.stderr)
sys.exit(1)
restored.append(dest)
return restored
def remove_install(install):
sysdir, loader = install.getpaths()
removed = []
if path.isfile(loader):
os.unlink(loader)
removed.append(loader)
if path.isdir(sysdir):
shutil.rmtree(sysdir)
removed.append(loader)
if __name__ == '__main__':
if (len(sys.argv) != 3 or not path.isdir(sys.argv[1])
or not path.isdir(sys.argv[2])):
usage()
sys.exit(1)
print("if your FreeMiNT sysdir is not in the first disk partition "\
"you will need to replace references to u:/c/ in mint.cnf and "\
"xaaes.cnf accordingly")
source = sys.argv[1]
target = sys.argv[2]
old = MintInstall(*get_versions(target), target)
git = MintInstall(*get_versions(source), source)
new = MintInstall(git.version, git.short_version, target)
if old.version == git.version:
print(f"{old.path} already contains a {git.version} Mint installation",
file=sys.stderr)
sys.exit(1)
backups = backup(old)
copy(git, old)
mark_distfiles(new)
restore(backups, new)
remove_install(old)
sys.exit(0)