Skip to content

Commit 4bad184

Browse files
lethosorquietust
authored andcommitted
Add script to extract and flatten raws from v50 archive
1 parent 6af42c9 commit 4bad184

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

scripts/extract_raws.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
Extract raws from a DF archive file.
5+
6+
Only DF 50.xx for Windows is currently supported.
7+
'''
8+
9+
import argparse
10+
import os
11+
import pathlib
12+
import shutil
13+
import zipfile
14+
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument('input_file')
17+
parser.add_argument('output_dir')
18+
parser.add_argument('--overwrite', action='store_true', help='Remove output_dir entirely if it exists')
19+
args = parser.parse_args()
20+
21+
zf = zipfile.ZipFile(args.input_file)
22+
raw_files = {} # name -> full path in archive
23+
24+
for entry in zf.namelist():
25+
entry_path = pathlib.PurePath(entry)
26+
if (not entry_path.is_absolute()
27+
and entry_path.is_relative_to('data/vanilla')
28+
and len(entry_path.parts) > 2
29+
and entry_path.parts[2].startswith('vanilla_')
30+
and entry_path.suffix == '.txt'
31+
and entry_path.name != 'info.txt'
32+
):
33+
if entry_path.name in raw_files:
34+
raise RuntimeError('Duplicate filename: %r' % str(entry_path))
35+
else:
36+
raw_files[entry_path.name] = entry
37+
38+
if os.path.exists(args.output_dir):
39+
if args.overwrite:
40+
shutil.rmtree(args.output_dir)
41+
else:
42+
print('error: output directory already exists:', args.output_dir)
43+
exit(1)
44+
45+
os.mkdir(args.output_dir)
46+
47+
for out_filename, in_filepath in raw_files.items():
48+
with zf.open(in_filepath) as in_file, open(os.path.join(args.output_dir, out_filename), 'wb') as out_file:
49+
shutil.copyfileobj(in_file, out_file)

0 commit comments

Comments
 (0)