-
Notifications
You must be signed in to change notification settings - Fork 3
/
dosemu-downloaddos
executable file
·257 lines (227 loc) · 11.3 KB
/
dosemu-downloaddos
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
#!/usr/bin/python3
import argparse
import hashlib
import os
import re
import shutil
import subprocess
import sys
import urllib.parse
import urllib.request
import zipfile
from pathlib import Path
from tqdm import tqdm
FREEDOS_DEFAULT_MIRROR = 'https://www.ibiblio.org/pub/micro/pc-stuff/freedos/files'
FREEDOS11_URL = FREEDOS_DEFAULT_MIRROR + '/repositories/1.1'
FREEDOS12_URL = FREEDOS_DEFAULT_MIRROR + '/repositories/1.2'
FREEDOS13_URL = FREEDOS_DEFAULT_MIRROR + '/repositories/1.3'
FREEDOS_USERSPACE_TOOLS = [ 'assign', 'attrib', 'choice', 'comp', 'debug', 'devload', 'display', 'edit', 'edlin', 'fc',
'fdxms', 'find', 'format', 'label', 'mem', 'mode', 'nansi', 'share', 'sort', 'swsubst', 'tree', 'xcopy']
# currently unused
FREEDOS_ARCHIVES_EXTRA = ['defrag', 'deltree', 'diskcomp', 'diskcopy', 'exe2bin', 'more', 'move', 'replace', 'shsucdx']
FREEDOS11_APPS = []
FREEDOS12_APPS = FREEDOS13_APPS = ['dn2']
FREEDOS11_BOOT = ['kernel', 'command']
FREEDOS12_BOOT = FREEDOS13_BOOT = ['kernel', 'freecom']
FREEDOS11_HELP = ['help']
FREEDOS12_HELP = FREEDOS13_HELP = ['htmlhelp']
FREEDOS_UNIXLIKE_ARCHIVES = ['touch']
FREEDOS11_UTIL = ['touch']
FREEDOS12_UTIL = FREEDOS13_UTIL = ['wcd']
FREEDOS11_NET = ['curl', 'gopherus', 'ping']
FREEDOS12_NET = FREEDOS13_NET = ['curl', 'gopherus', 'links', 'ping', 'terminal']
FREEDOS_SOUND_ARCHIVES = ['dosmid', 'adplay', 'opencp']
FREEDOS_DEVEL_ARCHIVES = ['bwbasic']
freedos11_boot = [ FREEDOS11_URL + '/base/' + x + '.zip' for x in FREEDOS11_BOOT ]
freedos11_userspace = [ FREEDOS11_URL + '/apps/' + x + '.zip' for x in FREEDOS11_APPS ]
freedos11_userspace += [ FREEDOS11_URL + '/base/' + x + '.zip' for x in FREEDOS_USERSPACE_TOOLS + FREEDOS11_HELP ]
freedos11_userspace += [ FREEDOS11_URL + '/util/' + x + '.zip' for x in FREEDOS11_UTIL ]
freedos11_userspace += [ FREEDOS11_URL + '/net/' + x + '.zip' for x in FREEDOS11_NET ]
freedos11_userspace += [ FREEDOS11_URL + '/sound/' + x + '.zip' for x in FREEDOS_SOUND_ARCHIVES ]
freedos11_userspace += [ FREEDOS11_URL + '/devel/' + x + '.zip' for x in FREEDOS_DEVEL_ARCHIVES ]
freedos11_pkginfo = [ FREEDOS11_URL + '/pkg-info/' + x.split('/')[-1].removesuffix('zip') + 'txt'
for x in freedos11_boot + freedos11_userspace ]
freedos12_boot = [ FREEDOS12_URL + '/base/' + x + '.zip' for x in FREEDOS12_BOOT + FREEDOS12_HELP ]
freedos12_userspace = [ FREEDOS12_URL + '/apps/' + x + '.zip' for x in FREEDOS12_APPS ]
freedos12_userspace += [ FREEDOS12_URL + '/base/' + x + '.zip' for x in FREEDOS_USERSPACE_TOOLS + FREEDOS12_HELP ]
freedos12_userspace += [ FREEDOS12_URL + '/unix/' + x + '.zip' for x in FREEDOS_UNIXLIKE_ARCHIVES ]
freedos12_userspace += [ FREEDOS12_URL + '/util/' + x + '.zip' for x in FREEDOS12_UTIL ]
freedos12_userspace += [ FREEDOS12_URL + '/net/' + x + '.zip' for x in FREEDOS12_NET ]
freedos12_userspace += [ FREEDOS12_URL + '/sound/' + x + '.zip' for x in FREEDOS_SOUND_ARCHIVES ]
freedos12_userspace += [ FREEDOS12_URL + '/devel/' + x + '.zip' for x in FREEDOS_DEVEL_ARCHIVES ]
freedos12_pkginfo = [ FREEDOS12_URL + '/pkg-info/' + x.split('/')[-1].removesuffix('zip') + 'txt'
for x in freedos12_boot + freedos12_userspace ]
freedos13_boot = [ FREEDOS13_URL + '/base/' + x + '.zip' for x in FREEDOS13_BOOT ]
freedos13_userspace = [ FREEDOS13_URL + '/apps/' + x + '.zip' for x in FREEDOS13_APPS ]
freedos13_userspace += [ FREEDOS13_URL + '/base/' + x + '.zip' for x in FREEDOS_USERSPACE_TOOLS + FREEDOS13_HELP]
freedos13_userspace += [ FREEDOS13_URL + '/unix/' + x + '.zip' for x in FREEDOS_UNIXLIKE_ARCHIVES ]
freedos13_userspace += [ FREEDOS13_URL + '/util/' + x + '.zip' for x in FREEDOS13_UTIL ]
freedos13_userspace += [ FREEDOS13_URL + '/net/' + x + '.zip' for x in FREEDOS13_NET ]
freedos13_userspace += [ FREEDOS13_URL + '/sound/' + x + '.zip' for x in FREEDOS_SOUND_ARCHIVES ]
freedos13_userspace += [ FREEDOS13_URL + '/devel/' + x + '.zip' for x in FREEDOS_DEVEL_ARCHIVES ]
freedos13_pkginfo = [ FREEDOS13_URL + '/pkg-info/' + x.split('/')[-1].removesuffix('zip') + 'txt'
for x in freedos13_boot + freedos13_userspace ]
TMP_DIR = os.path.join('/tmp', os.path.basename(sys.argv[0]) + '-' + os.environ['USER'] + '-' + str(os.getpid()))
def check_sum_pkg_info(destination_file):
pkg_info_file = open(destination_file[:-3] + 'txt', 'r')
unchecked = True
for line in pkg_info_file.readlines():
if line.startswith('SHA'):
assert_sha256sum(destination_file, line[4:].strip())
unchecked = False
break
if unchecked:
print('Warning no SHA checksum in pkg-info. Could not verify checksum of ' + destination_file)
def check_sum(destination_file):
if Path(destination_file[:-3] + 'txt').is_file():
check_sum_pkg_info(destination_file)
else:
print('Warning could not verify checksum of ' + destination_file)
def download_file(source, destination_directory):
destination_file = os.path.join(destination_directory, urllib.parse.unquote(source.split("/")[-1]))
if Path(destination_file).is_file():
if not destination_file.endswith('.txt'):
check_sum(destination_file)
else:
print('Downloading ' + source + '...')
with urllib.request.urlopen(urllib.parse.quote_plus(source, "\./_-:")) as response, open(destination_file, 'wb') as f:
if hasattr(response, 'headers'):
length = response.headers['Content-length']
elif hasattr(response, 'getheader'):
length = response.getheader('content-length')
else:
length = None
if type(length) is str:
with tqdm(total=int(length), unit='B', maxinterval=0.5) as pbar:
while True:
chunk = response.read(16384)
if not chunk:
break
f.write(chunk)
pbar.update(len(chunk))
else:
f.write(response.read())
return destination_file
def calculate_sha256sum(filename):
with open(filename,"rb") as f:
return hashlib.sha256(f.read()).hexdigest();
def assert_sha256sum(file, sha256sum):
val = calculate_sha256sum(file)
if val == sha256sum:
print('Verified checksum ' + str(file))
else:
print('The downloaded file ' + str(file) + ' could not be verified.')
print('Actual SHA256SUM: ' + val)
print('Expected SHA256SUM: ' + sha256sum)
print('Please remove the file and rerun the script.')
sys.exit(1)
def download_files(imgurls, destination):
destination_files = []
for imgurl in imgurls:
destination_files.append(download_file(imgurl, destination))
return destination_files
def verify_noexistinginstall(destination):
if os.listdir(destination) != []:
print('There is a already an existing set of DOS installation files in ' + destination + '.')
sys.exit(1)
def derive_url_list(imgurl):
imgurls = []
filename = urllib.parse.unquote(imgurl.split("/")[-1])
if filename.casefold().find("disk") != -1:
if filename.casefold().find("of") != -1:
imgcnt = int(re.findall(r'(?i)disk\s?\d+\s?of\s(\d+)', filename)[0])
for i in range(1, imgcnt+1):
imgurls.append(imgurl.rsplit("/", 1)[0] + "/" + re.sub(r'(?i)(?P<one>disk\s?)\d+(?P<two>\s?of\s\d+)', r'\g<one>'+str(i) + r'\g<two>', filename))
else: # if the filenames have just one number, assume the last disk from the set was provided
imgcnt = int(re.findall(r'(?i)disk\s?(\d+)', filename)[0])
for i in range(1, imgcnt+1):
imgurls.append(imgurl.rsplit("/", 1)[0] + "/" + re.sub(r'(?i)(?P<one>disk\s?)\d+', r'\g<one>'+str(i), filename))
else:
imgurls.append(imgurl)
return imgurls;
def download_dos(dos_flavour, destination):
os.makedirs(destination, exist_ok=True)
print('Downloading checksums')
info = []
if dos_flavour.startswith("freedos13"):
info = freedos13_pkginfo
elif dos_flavour.startswith("freedos12"):
info = freedos12_pkginfo
elif dos_flavour.startswith("freedos11"):
info = freedos11_pkginfo
for pkg in info:
try:
download_file(pkg, destination)
except urllib.error.HTTPError:
pass
print('Downloading FreeDOS...')
pkgs = []
if dos_flavour in ['freedos13userspace', 'freedos13']:
pkgs += freedos13_userspace
if dos_flavour in ['freedos13']:
pkgs += freedos13_boot
if dos_flavour in ['freedos12userspace', 'freedos12']:
pkgs += freedos12_userspace
if dos_flavour in ['freedos12']:
pkgs += freedos12_boot
if dos_flavour in ['freedos11userspace', 'freedos11']:
pkgs += freedos11_userspace
if dos_flavour in ['freedos11']:
pkgs += freedos11_boot
for pkg in pkgs:
try:
download_file(pkg, destination)
except urllib.error.HTTPError:
print('WARNING: The chosen version of FreeDOS does not contain ' + pkg + '!')
print('Downloading done')
def urls_ok(urls, verbose=False):
failures = 0
def test_url(url):
try:
req = urllib.request.Request(url, method="HEAD")
resp = urllib.request.urlopen(req)
a = resp.read()
if (verbose):
print("INFO: Checked (%s)" % url)
except urllib.error.HTTPError:
print("FAIL: Does not exist (%s)" % url)
return False
return True
for url in urls:
if not test_url(url):
failures += 1
return failures == 0
parser = argparse.ArgumentParser(description="""Script to download either FreeDOS or a set of disk images.
Multiple versions of FreeDOS are preconfigured.
When a custom URL to a disk image is provided, based on known filename patterns, all related images are downloaded and extracted.
Either a name should contain \"Disk ? of x\" or \"diskx\" where 1 is the first disk and x is the last disk.
The destination directory will contain all files from all disk images.""")
group = parser.add_mutually_exclusive_group()
group.add_argument("-l", "--list", help="List available DOS variants", action="store_true")
group.add_argument("-t", "--test", help="Test that URLs exist", action="store_true")
group.add_argument("-o", "--os", help="Download the specified DOS", type=str)
parser.add_argument("-d", "--destination", help="Specify/override the destination directory", type=str)
parser.add_argument("-s", "--sha256sum", help="Specify one or more sha256sum(s)", nargs='+', type=str)
args = parser.parse_args()
if args.list:
print('freedos13 FreeDOS 1.3 (2022)')
print('freedos13userspace FreeDOS 1.3 userspace (2022)')
print('freedos12 FreeDOS 1.2 (2016)')
print('freedos12userspace FreeDOS 1.2 userspace (2016)')
print('freedos11 FreeDOS 1.1 (2011)')
print('freedos11userspace FreeDOS 1.1 userspace (2011)')
sys.exit(0)
if args.test:
urls = (freedos11_boot + freedos11_userspace +
freedos12_boot + freedos12_userspace +
freedos13_boot + freedos13_userspace)
if not urls_ok(urls, True):
sys.exit(1)
sys.exit(0)
if args.os:
dos_to_download = args.os
if args.destination:
destination = args.destination
else:
destination = os.path.join(os.environ['HOME'], '.cache', 'dosemu', args.os.replace('userspace', ''))
download_dos(dos_to_download, destination)