-
Notifications
You must be signed in to change notification settings - Fork 0
/
everdrive-sort.py
executable file
·84 lines (68 loc) · 1.91 KB
/
everdrive-sort.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
#!/usr/bin/env python
##
## everdrive-sort.py by Naomi Peori <[email protected]>
## Sort roms using EverDrive compatible datfiles.
##
import os
import sys
import zipfile
import multiprocessing
##
## Check the arguments.
##
if len(sys.argv) < 2:
print "\nUsage:", sys.argv[0], "<source>"
sys.exit()
source = sys.argv[1]
if not os.path.isdir(source):
print "ERROR: Source directory doesn't exist:", source
sys.exit()
##
## Load the datfiles.
##
datfolder = "00_DATFILES"
roms = {}
datfiles = os.listdir(datfolder)
for dirname, subdirectories, filenames in os.walk(datfolder):
for filename in filenames:
with open(dirname + "/" + filename, "r") as file:
for line in file.read().splitlines():
temp1, name, temp3, temp4, crc32 = line.split("\t");
crc32 = int(crc32, 16)
if not crc32 in roms:
roms[crc32] = []
roms[crc32].append(name)
##
## Functions
##
def scanDirectory(directory):
if os.path.isdir(directory):
for dirname, subdirectories, filenames in os.walk(directory):
[pool.apply_async(scanFile, args=(dirname + "/" + filename,)) for filename in filenames]
for subdirectory in subdirectories:
scanDirectory(dirname + "/" + subdirectory)
def scanFile(filename):
if filename.endswith(".zip"):
print filename
with zipfile.ZipFile(filename, mode="r", allowZip64=True) as file:
for info in file.infolist():
matchFile(info.CRC, file.read(info.filename))
def matchFile(crc, data):
if crc in roms:
for filename in roms[crc]:
writeFile(filename, data)
def writeFile(filename, data):
if not os.path.isfile(filename):
directory = os.path.dirname(filename)
if not os.path.isdir(directory):
os.makedirs(os.path.dirname(filename))
with open(filename, "w") as file:
print " " + filename
file.write(data)
##
## Main Program
##
pool = multiprocessing.Pool()
scanDirectory(source)
pool.close()
pool.join()