/
file2clrdat.py
328 lines (268 loc) · 10.9 KB
/
file2clrdat.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
326
327
328
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
file2clrdat.py: Outputs a file in ClrMamePro dat format with rom info (hashes,
size...) from a file or files in a directory. This will speed up the process
of update ClrMamePro .dat files, without using ClrMamePro.
Usage:
file2clrdat.py INPUT_ROM
file2clrdat.py INPUT_ROM [(-d DAT_FILE [-m DIR] [-u DIR] [-c])] [-s SEARCH_TYPE]
file2clrdat.py --help
Arguments:
INPUT_ROM Input rom file o directory with roms.
Options:
-d DAT_FILE, --datfile=DAT_FILE Dat file that will be searched, type of
search can be changed with "-s" option.
-m MATCHED_DIR, --matcheddir=DIR INPUT_ROM will be moved to MATCHED_DIR
if exists in DAT_FILE.
Must be used with "-d" option.
-u UNMATCHED_DIR --unmatcheddir=DIR INPUT_ROM will be moved to UNMATCHED_DIR
if not exists in DAT_FILE.
Must be used with "-d" option.
-c, --copy Copy roms instead move them.
Must be used with "-m" or "-u" options.
-s SEARCH_TYPE, --searchtype=type Set type of search that "-d" option will
use. Valid "types": size, crc, md5, sha1
Must be used with "-d" option.
[default: sha1]
-h, --help Show this help screen.
Output:
INPUT_ROM_romdata, File with all info from INPUT_ROM
Optional: Move scanned roms to folders.
Examples:
- Generate rom info (rom.bin_rondata) from a single rom file:
file2clrdat.py "c:\my_downloads\rom.bin"
- Scan a directory, match md5 hashes of every file in that directory with
a dat file, matched files will be moved to "dupe_roms" directory, files not
found in dat file will be moved to "new_roms" directory adding a file with
rom data for every file:
file2clrdat.py "c:\my_downloaded_roms" -s md5 -d "c:\my_dat_wip\my_dat.dat" -m "c:\my_dat_wip\dupe_roms" -u "c:\my_dat_wip\new_roms"
Author: Rodrigo Rega <contacto@rodrigorega.es>
Script URL: https://github.com/rodrigorega/File2clrdat
License: CC-BY-SA 3.0 license (http://creativecommons.org/licenses/by/3.0/
"""
from __future__ import print_function # used for print to files
import sys # needed for get python version and argv
import string # needed for templating
import os # needed for file and path manipulations
import shutil # needed for copy files
from file import File # class for get file data (hashes, size, etc)
from lxml import objectify # for parsing dat file
class File2clrdat(object):
"""
file2clrdat class
"""
def __init__(self, input_path, datfile_path,
search_type, matched_dir, unmatched_dir, copy_scanned):
"""
Class initializer
Return: None
:type input_path: string
:param input_path: file or directory to work with
:type datfile_path: string
:param datfile_path: work dat file
:type search_type: string
:param search_type: value that will be searched in dat file
"""
self.input_path = input_path
self.datfile_path = datfile_path
self.search_type = search_type
self.matched_dir = matched_dir
self.unmatched_dir = unmatched_dir
self.copy_scanned = copy_scanned
input_path = None
search_type = None
datfile_path = None
matched_dir = None
unmatched_dir = None
copy_scanned = False
file_data = None
VALID_SEARCH_TYPES = ['size', 'md5', 'crc', 'sha1']
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
clrmamepro_dtd_file = 'C:\lab\_wip\File2clrdat\datafile.dtd'
rom_template_file = os.path.join(SCRIPT_PATH, 'ClrMamePro_rom_dat.tpl')
rom_template_content = None
rom_template_populated = None
def generate_rom_data(self):
"""
Calls functions to according if input is file or directory
Return: None
"""
# TODO: Move input_path initializer arg to this function
if os.path.isfile(self.input_path):
self.__process_file()
elif os.path.isdir(self.input_path):
self.__process_directory()
else:
print("Invalid file or directory")
def search_in_datfile(self, search_type, search_content):
"""
Checks if a given content is inside dat file
Return: If content was found, returns filename
:type search_type: string
:param search_type: Type of search (md5, size, crc...)
:type search_content: string
:param search_content: Content that will be matched
"""
with open(self.datfile_path) as f_xml:
xml_data = f_xml.read()
datafile = objectify.fromstring(xml_data)
xml_error = self.file_data.validate_xml_with_dtd(datafile, self.clrmamepro_dtd_file)
if xml_error:
print('- Provided file is not a valid ClrMamePro dat file.')
print(xml_error)
sys.exit(5)
for games in datafile.getchildren():
for game in games.getchildren():
if game.get(search_type) == search_content:
return game.get("name")
def __process_file(self):
"""
Calls all methods to process a file
Return: None
"""
self.file_data = File(self.input_path)
self.file_data.get_hashes()
if self.datfile_path:
rom_found_in_datfile = self.search_in_datfile(
self.search_type, getattr(self.file_data, self.search_type))
if rom_found_in_datfile:
self.__print_match_found_in_datfile(rom_found_in_datfile)
if self.matched_dir:
self._move_or_copy_file(self.matched_dir)
else:
if self.unmatched_dir:
self._move_or_copy_file(self.unmatched_dir)
self.__get_template_content()
self.__populate_template()
self.__write_populated_template()
else:
self.__get_template_content()
self.__populate_template()
self.__write_populated_template()
def _move_or_copy_file(self, destination_dir):
"""
Move or copy rom file to a given directory.
If exists it will be renamed.
Return: None
:type destination_dir: string
:param destination_dir: Directory where file will be moved
"""
destination_file = os.path.join(destination_dir, self.file_data.name)
destination_file = self.file_data.compose_unique_filename(destination_file, 'timestamp')
if self.copy_scanned:
shutil.copy2(self.file_data.path_and_name, destination_file)
else:
shutil.move(self.file_data.path_and_name, destination_file)
def __print_match_found_in_datfile(self, rom_found_in_datfile):
"""
Print found match message
Return: None
:type input_romfile: string
:param input_romfile: Input file
:type rom_found_in_datfile: string
:param rom_found_in_datfile: Rom matched in datfile
"""
message = """
Input rom file found in provided dat file:
- Input rom file: %s
- Rom name in dat: %s
""" % (self.input_path, rom_found_in_datfile)
print(message)
def __process_directory_recursive(self):
"""
Calls all methods to process a directory and all subdirectories
Return: None
"""
for root, dirs, files in os.walk(self.input_path):
for thisfile in files:
self.input_path = os.path.join(root, thisfile)
self.__process_file()
def __process_directory(self):
"""
Calls all methods to process a directory
Return: None
"""
path = self.input_path
for thisfile in os.listdir(path):
path_and_file = os.path.join(path, thisfile)
if os.path.isfile(path_and_file):
self.input_path = path_and_file
self.__process_file()
def __get_template_content(self):
"""
Gets content of template file
Return: None
"""
f_rom_template = open(self.rom_template_file)
self.rom_template_content = string.Template(f_rom_template.read())
f_rom_template.close()
def __populate_template(self):
"""
Puts file data inside template
Return: None
"""
template_dictionary = {
'gameName': self.file_data.nameNoExtension,
'romDescription': self.file_data.nameNoExtension,
'romName': self.file_data.name,
'romSize': self.file_data.size,
'romCrc': self.file_data.crc,
'romMd5': self.file_data.md5,
'romSha1': self.file_data.sha1
}
self.rom_template_populated = (
self.rom_template_content.safe_substitute(template_dictionary)
)
def __write_populated_template(self):
"""
Writes rom data to output file
Return: None
"""
if self.unmatched_dir:
dst_dir = self.unmatched_dir
else:
dst_dir = self.file_data.path
romdata_file = os.path.join(dst_dir, self.file_data.name + '_romdata')
romdata_file = self.file_data.compose_unique_filename(romdata_file, 'timestamp')
f_output = open(romdata_file, "w")
print(self.rom_template_populated, file=f_output)
f_output.close()
def _validate_python_version():
'''
Check Python version
Return: None
'''
MAJOR, MINOR, MICRO, RELEASELEVEL, SERIAL = sys.version_info
if (MAJOR, MINOR) < (2, 7):
print('You must use Python 2,7 or higher')
sys.exit(2)
if __name__ == "__main__":
"""
Main
Return: None
"""
_validate_python_version()
try:
from docopt import docopt
except:
print ("""
Please install docopt using:
pip install docopt
For more info refer to: https://github.com/docopt/docopt
""")
raise
ARGS = docopt(__doc__, version='1.0.0rc2')
MY_FILE2CLRDAT = File2clrdat(
ARGS['INPUT_ROM'], ARGS['--datfile'], ARGS['--searchtype'],
ARGS['--matcheddir'], ARGS['--unmatcheddir'], ARGS['--copy'])
if ARGS['--matcheddir'] and ARGS['--unmatcheddir']:
if ARGS['--matcheddir'] == ARGS['--unmatcheddir']:
print ('MATCHED_DIR and UNMATCHED_DIR must be different.')
sys.exit(3)
if ARGS['--searchtype']:
if not ARGS['--searchtype'] in MY_FILE2CLRDAT.VALID_SEARCH_TYPES:
print('- %s is not a valid search type. Use --help to more info.'
% ARGS['--searchtype'])
sys.exit(4)
MY_FILE2CLRDAT.generate_rom_data()