-
Notifications
You must be signed in to change notification settings - Fork 0
/
adcdutil.py
executable file
·301 lines (251 loc) · 8.96 KB
/
adcdutil.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
#!/usr/bin/env python3
#
# SPDX-License-Identifier: MIT
import os
import shutil
import subprocess
import sys
import zipfile
import click
from pycdlib import PyCdlib
from pycdlib.pycdlibexception import PyCdlibInvalidInput
def requireCommands(commands):
missing = []
for cmd in commands:
if shutil.which(cmd) is None:
missing.append(cmd)
if len(missing) > 0:
click.echo(
"Required commands not found: {}".format(" ".join(missing)), err=True
)
sys.exit(1)
def walkISO(filename, extension):
found = {}
iso = PyCdlib()
iso.open(filename)
for root, dirs, files in iso.walk(iso_path="/"):
for f in files:
if ".{}".format(extension) in f.lower():
parent = os.path.basename(root)
if parent not in found:
found[parent] = []
found[parent].append(os.path.splitext(f)[0])
iso.close()
return found
def getTapes(iso):
return walkISO(iso, "ipl")
def getVolumes(iso):
return walkISO(iso, "zip")
def checkPath(path, overwrite=False):
if os.path.exists(path):
if overwrite:
click.echo(
"{} already exists, overwriting as requested".format(path), err=True
)
return True
else:
click.echo("{} already exists, aborting!".format(path), err=True)
sys.exit(1)
else:
return False
def extractZip(zip_filename):
with zipfile.ZipFile(zip_filename) as zf:
contents = zf.namelist()
try:
zf.testzip()
for fname in contents:
yield (fname, zf.open(fname))
except NotImplementedError as e:
for fname in contents:
p = subprocess.Popen(
["unzip", "-p", zip_filename, fname],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
yield (fname, p.stdout)
def getFileFromIso(iso_filename, dist, filename, dest=None, overwrite=False):
if dest is None:
dest = os.getcwd()
dest_filename = os.path.join(dest, filename)
iso = PyCdlib()
iso.open(iso_filename)
if checkPath(dest_filename, overwrite):
os.remove(dest_filename)
try:
iso.get_file_from_iso(dest_filename, iso_path=os.path.join("/", dist, filename))
except PyCdlibInvalidInput as e:
iso.get_file_from_iso(
dest_filename, iso_path=os.path.join("/", dist, "{};1".format(filename))
)
iso.close()
return dest_filename
def convertTape(filename, dist, tape, compression="zlib", dest=None, overwrite=False):
if dest is None:
dest = os.getcwd()
tape_in = getFileFromIso(filename, dist, "{}.IPL".format(tape), dest, overwrite)
tape_out = os.path.join(dest, "{}.het".format(tape))
cmd = ["hetupd"]
if compression == "zlib":
cmd.append("-z")
elif compression == "bzip2":
cmd.append("-b")
elif compression == "none":
cmd.append("-d")
cmd += [tape_in, tape_out]
subprocess.run(
cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True
)
os.remove(tape_in)
return tape_out
def extractVolume(filename, dist, volume, dest=None, overwrite=False):
if dest is None:
dest = os.getcwd()
images = []
zip_filename = getFileFromIso(
filename, dist, "{}.ZIP".format(volume), dest, overwrite
)
for fname, zfp in extractZip(zip_filename):
image_name = os.path.basename(fname)
image = os.path.join(dest, image_name)
checkPath(image, overwrite)
with open(image, "wb+") as f:
f.write(zfp.read())
images.append(image)
os.remove(zip_filename)
return sorted(images)
@click.group()
def cli():
"""A simple CLI to inspect and convert ADCD distributions into a format usable by the Hercules emulator."""
pass
@cli.command()
@click.option(
"-d",
"--destination",
type=click.Path(exists=True, file_okay=False),
help="Path for the conversion output.",
)
@click.option(
"-f", "--force/--no-force", default=False, help="Overwrite existing files."
)
@click.option(
"-c",
"--compression",
type=click.Choice(["zlib", "bzip2", "none"]),
default="zlib",
help="Compression format.",
)
@click.option("-q", "--quiet/--verbose", default=False)
@click.option(
"-o",
"--output-format",
type=click.Choice(["CKD", "CCKD", "FBA", "CFBA"]),
default="CCKD",
help="Volume output format.",
)
@click.argument("iso", type=click.Path(exists=True, dir_okay=False), nargs=-1)
def convert(destination, force, compression, quiet, output_format, iso):
"""Convert ADCD image into Hercules formats."""
requireCommands(["dasdcopy", "hetupd", "unzip"])
for iso_image in iso:
click.echo(iso_image)
iso_tapes = getTapes(iso_image)
if not iso_tapes:
click.echo("No tapes found.", err=True)
for dist, tapes in iso_tapes.items():
for tape in tapes:
if not quiet:
click.echo("Converting {} tape {}...".format(dist, tape))
tape_out = convertTape(
iso_image, dist, tape, compression, destination, force
)
if not quiet:
click.echo(
"{} tape {} converted to {}".format(
dist, tape, os.path.basename(tape_out)
)
)
iso_volumes = getVolumes(iso_image)
if not iso_volumes:
click.echo("No volumes found.", err=True)
for dist, vols in iso_volumes.items():
for vol in vols:
if not quiet:
click.echo("Extracting {} volume {}...".format(dist, vol))
images = extractVolume(iso_image, dist, vol, destination, force)
if len(images) == 0:
click.echo("No images extracted!", err=True)
sys.exit(1)
elif len(images) == 1:
vol_in = images[0]
if not quiet:
click.echo(
"Found single-file image: {}".format(
os.path.basename(vol_in)
)
)
else:
vol_in = "{}.img".format(images[0].split("_")[0])
if not quiet:
click.echo(
"Found multi-file image, converting to single-file: {}".format(
os.path.basename(vol_in)
)
)
cmd = ["dasdcopy", "-q"]
if checkPath(vol_in, force):
cmd.append("-r")
cmd += ["-lfs", images[0], vol_in]
subprocess.run(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
for img in images:
os.remove(img)
vol_out = "{}.{}".format(
os.path.splitext(vol_in)[0], output_format.lower()
)
if not quiet:
click.echo("Converting to {}...".format(output_format))
cmd = ["dasdcopy", "-q"]
if checkPath(vol_out, force):
cmd.append("-r")
if compression == "zlib":
cmd.append("-z")
elif compression == "bzip2":
cmd.append("-bz2")
elif compression == "none":
cmd.append("-0")
cmd += ["-o", output_format.upper(), vol_in, vol_out]
subprocess.run(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
os.remove(vol_in)
if not quiet:
click.echo(
"{} volume {} converted to {}".format(
dist, vol, os.path.basename(vol_out)
)
)
click.echo()
@cli.command()
@click.argument("iso", type=click.Path(exists=True, dir_okay=False), nargs=-1)
def dump(iso):
"""Dump details and contents of an ADCD image."""
for iso_image in iso:
click.echo(iso_image)
iso_tapes = getTapes(iso_image)
if iso_tapes:
for dist, tapes in iso_tapes.items():
click.echo("{} tapes: {}".format(dist, " ".join(sorted(tapes))))
iso_volumes = getVolumes(iso_image)
if iso_volumes:
for dist, vols in iso_volumes.items():
click.echo("{} volumes: {}".format(dist, " ".join(sorted(vols))))
click.echo()
if __name__ == "__main__":
cli()