-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools64.py
executable file
·325 lines (284 loc) · 9.85 KB
/
tools64.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/python
import re
import shutil
import subprocess
import urllib
import urllib.parse
from contextlib import contextmanager
from dataclasses import dataclass, field
from pathlib import Path
from typing import Generator
from utils import dospath, fat32names, fixname, flatten_dir, remove_in
is_win = False
def get_filename(url: Path) -> str:
uq = urllib.parse.unquote_plus(url.name)
n = fixname(Path(uq).name)
return n
@dataclass
class Release:
"""Represent a C64 release like a Demo or a Games"""
id: int = -1
rank: int = 0
rating: float = 0
title: str = ""
group: str = ""
groups: list[str] = field(default_factory=list)
downloads: list[str] = field(default_factory=list)
coder: str = ""
artist: str = ""
composer: str = ""
event: str = ""
place: int = 0
compo: str = ""
published: str = ""
year: int = -1
comment: str = ""
type: str = ""
language: str = ""
sids: list[str] = field(default_factory=list)
def is_crack(self):
return self.type.endswith("Crack")
def is_demo(self):
return self.type.endswith("Demo")
def is_intro(self):
return self.type.endswith("Intro")
def is_music(self):
return self.type.endswith("Music")
def is_graphics(self):
return self.type.endswith("Graphics")
def is_game(self):
return self.type.endswith("Game")
def is_commercial(self):
packs = [
"Loadstar",
"CP Verlag",
"Binary Zone",
" Club",
"(Preview",
" PD",
"(Created",
"Public Domain",
"Not Publ",
"Unknown",
]
rc = not any(s in self.published for s in packs)
return rc
def is_typein(self):
if self.comment.startswith("from the book"):
return True
return any(
s in self.published
for s in [
"Commodore Info",
"Creative Computing",
"Infopress",
"Hebdogiciel",
"Markt",
"Verlag",
"Books",
"Magazine",
"Publications",
"Press",
]
)
def format(self, template: str) -> str:
d: dict[str, str | int | float] = {}
for key, val in self.__dict__.items():
if isinstance(val, str):
d[key] = fixname(val)
elif isinstance(val, (int, float)):
d[key] = val
d["groups"] = ",".join(self.groups)
d["I"] = self.title[0].upper()
d["A"] = fixname(self.title[0].upper())
d["i"] = self.title[0]
d["a"] = fixname(self.title[0])
d["qyear"] = "XXXX" if self.year == -1 else self.year
# Support nested curlies; if nested variable is empty or negative,
# the outer scope is removed.
r = re.compile(r"{[^{}]*({[^{}]+})[^{}]*}")
while True:
m = r.search(template)
if not m:
break
s0, e0 = m.start(0), m.end(0)
s1, e1 = m.start(1), m.end(1)
x = template[s1:e1].format(**d)
if x == "-1" or x == "":
template = template[:s0] + template[e0:]
else:
template = template[:s0] + template[s0 + 1 : e0 - 1] + template[e0:]
return template.format(**d)
show_output = False
def show_run_output(show: bool):
global show_output
show_output = show
def run(args: list[str | Path], cwd: Path | None = None, nostderr: bool = False) -> int:
err = subprocess.DEVNULL if nostderr else None
out = None if show_output else subprocess.DEVNULL
if cwd is not None:
return subprocess.call(args, stdout=out, stderr=err, cwd=cwd)
return subprocess.call(args, stdout=out)
def log(text: str):
if show_output:
print(text)
keep = {
".PRG",
".D64",
".T64",
".DIZ",
".NFO",
".TXT",
".REU",
".G64",
".D81",
".CRT",
".TAP",
}
def unpack(
archive: Path,
targetdir: Path,
prg_to_d64: bool = False,
d64_to_prg: bool = False,
t64_to_prg: bool = True,
):
"""
Generic unpack for archives containing c64 programs. Archive will be unpacked, and
esoteric C64 formats will be converted to d64 or prg.
.crt, .tap, .reu & .g64 will not be converted and kept.
Info files (.txt, .nfo, .diz) will also be kept.
`archive` - path to downloaded archive containg one C64 'release'.
`targetdir` - Extracted/converted files will be placed here
`to_d64` - If true, pack programs into disk images
`to_prg` - If true, extract programs from disk images
"""
# Make sure target directory exists and is empty
targetdir.mkdir(parents=True, exist_ok=True)
remove_in(targetdir)
if is_win:
archive = dospath(archive)
targetdir = dospath(targetdir)
ext = archive.suffix.upper()
while True:
file_name = get_filename(archive)
if ext == ".GZ":
# Gzip replaces original file so copy it to targetdir first
shutil.copyfile(archive, targetdir / file_name)
archive = targetdir / file_name
if run(["gzip", "-d", archive]):
# Sometimes gzip files aren't.
archive.rename(archive.with_suffix(""))
archive = archive.with_suffix("")
ext = archive.suffix.upper()
if ext == ".ZIP":
# subprocess.call(["unzip", "-n", "-j", archive, "-d", targetdir])
run(["7z", "e", archive, "-y", f"-o{targetdir}"])
elif ext == ".RAR":
run(["unrar", "e", "-o-", "-y", archive.absolute()], cwd=targetdir)
elif ext == ".TGZ":
run(["tar", "-xzf", archive.absolute()], cwd=targetdir)
elif ext == ".TAR":
run(["tar", "-xf", archive.absolute()], cwd=targetdir)
elif ext == ".LHA" or ext == ".LZH":
run(["lha", "x", archive.absolute()], cwd=targetdir)
else:
with open(archive, "rb") as af:
header = af.read(8)
if header[:4] == b"PK\x03\x04" or header[:4] == b"PK\x05\x06":
log(f"{file_name} looks like a zipfile")
ext = ".ZIP"
continue
elif header[:4] == b"Rar!":
log(f"{file_name} looks like a RAR file")
ext = ".RAR"
continue
else:
if archive.parent != targetdir:
shutil.copyfile(archive, targetdir / file_name)
break
flatten_dir(targetdir)
# OK, `targetdir` should now contain all unpacked files
# Convert some isoteric formats
for r in targetdir.iterdir():
ext = r.suffix.upper()
if r.is_dir():
continue
if ext == ".GZ":
run(["gunzip", r])
elif r.name[:2] == "1!":
run(["zip2disk", r.name[2:]], cwd=targetdir)
for f in targetdir.glob(f"?!{r.name[2:]}"):
f.unlink()
elif r.suffix.upper() == ".T64" and t64_to_prg:
tmp = targetdir / "tape"
tmp.mkdir()
run(["cbmconvert", "-t", r.absolute()], cwd=tmp, nostderr=True)
all_basic = all(
(x.read_bytes()[:2])[1] == 8 if x.suffix.upper() == ".PRG" else False
for x in tmp.iterdir()
)
if all_basic:
flatten_dir(targetdir)
r.unlink()
else:
log(f"T64 '{r.name}' has non standard files, keeping...")
remove_in(tmp)
tmp.rmdir()
elif r.suffix.upper() == ".LNX":
file_name = r.with_suffix(".d64")
run(["cbmconvert", "-D4", file_name.name, "-l", r.name], cwd=targetdir)
r.unlink()
elif r.suffix.upper() == ".P00":
contents = r.read_bytes()
r.with_suffix(".prg").write_bytes(contents[0x1A:])
r.unlink()
else:
pass
# At this point we hopfully have only PRG or D64 in our targetdir
# Unpack D64 to PRG if requested
if d64_to_prg:
for r in targetdir.iterdir():
if r.suffix.upper() == ".D64":
rc = run(["cbmconvert", "-N", "-d", r.name], cwd=targetdir, nostderr=True)
if rc != 0:
log(f"cbmconvert returned {rc} for {r.name}")
foundprog = False
for r2 in targetdir.iterdir():
ext = r2.suffix.upper()
if ext == ".DEL" or ext == ".USR":
r2.unlink()
elif ext == ".PRG":
foundprog = True
if foundprog:
r.unlink()
# Put all unpacked PRG into a d64 if requested
elif prg_to_d64:
progs: list[str] = []
for r in targetdir.iterdir():
if r.suffix.upper() == ".PRG":
progs.append(r.name)
if progs:
file_name = get_filename(archive.with_suffix(".d64"))
#print(f"Putting {progs} into {n}")
subprocess.call(["cbmconvert", "-n", "-D4", file_name] + progs, cwd=targetdir)
for p in progs:
(targetdir / p).unlink()
for r in targetdir.iterdir():
ext = r.suffix.upper()
if ext in keep:
pass
elif ext == ".SEQ":
r.rename(r.with_suffix(".prg"))
else:
sz = r.stat().st_size
if sz == 174848:
r.rename(r.with_suffix(".d64"))
else:
# print("Checking if %s is a PRG" % (r,))
file = open(r, "rb")
x = file.read(2)
if x == b"\x01\x08":
r.rename(r.with_suffix(".prg"))
else:
r.unlink()
fat32names(targetdir)