I looking for a long file names to short converter

Page 1/3
| 2 | 3

By gdx

Prophet (2714)

gdx's picture

22-02-2019, 10:05

Hello developers,
I am looking for a person to develop a small utility to rename the Roms file in a directory according to their checksum. The program I am looking for must do the following things.

  • No GUI
  • Rename a .ROM file or several in a directory with a name up to 8 characters depending on its checksum (CRC32 or SHA1).
  • The extension name remains .ROM.
  • The ideal would be to do a version for Mac, linux and Windows.

I think it's not difficult to program and it will help the community. If someone is a volunteer, I take care of the database, a UTF-8 text file like below or differently if you prefer.

Gradius 2 - Nemesis 2 (JE)
d63e20369f98487767810a0c57603bef6a2a07e5
GRADIUS2
Gradius 2 - Nemesis 2 (JE) [enhanced by FRS]
f45d9c1190f188f57a26422ed013576f6456a075
GRAD2FRS

Three lines per Rom. The first line is for info only but can be used to rename the Rom with a long name as option.

Login or register to post comments

By Manuel

Ascended (15369)

Manuel's picture

22-02-2019, 10:51

How must the program determine the short name?

By gdx

Prophet (2714)

gdx's picture

22-02-2019, 12:10

With the database and the checksum.

By Pencioner

Paladin (895)

Pencioner's picture

22-02-2019, 12:53

For which OS - Windows/Linux/... ?

By Manuel

Ascended (15369)

Manuel's picture

22-02-2019, 15:51

Ah, could be done in a few lines of python....

By Latok

msx guru (3641)

Latok's picture

22-02-2019, 16:52

Manuel wrote:

Ah, could be done in a few lines of python....

And we have a volunteer!

By Pencioner

Paladin (895)

Pencioner's picture

22-02-2019, 16:57

Manuel wrote:

Ah, could be done in a few lines of python....

Hm, right, then it is OS independent. Then I'm a volunteer (as Manuel is quite busy with openMSX, i guess)

By TomH

Champion (327)

TomH's picture

22-02-2019, 19:43

If you're going to knock it up in Python, where there's really no difference in difficulty between reading a JSON, CSV or Windows-style INI, maybe a more formal database format would be desirable? There are more than two naming conventions in the world, after all.

By TomH

Champion (327)

TomH's picture

22-02-2019, 23:13

Okay, so I'm terrible at Python and that's just the way things are, but I came up with the following:

#!/usr/bin/env python

import argparse
import hashlib
import json
import os


def rename(file_name, files_by_hash, convention, hasher, hash_key):
	# Grab the hash.
	try:
		# Grab the hash of this file.
		with open(file_name, 'rb') as file:
			while True:
				next_blob = file.read(65536)
				if not next_blob:
					break
				hasher.update(next_blob)

	except IOError:
		print("Couldn't open %s" % (file_name))

	# Warn the user if no matching hash is found.
	hexdigest = hasher.hexdigest()
	if not hexdigest in files_by_hash:
		print("Couldn't find entry for %s %s, to map file %s" % (hash_key, hexdigest, file_name))
		return

	file = files_by_hash[hexdigest]
	if not hash_key in file:
		print("No entry for convention %s to map file %s with hash" % (hash_key, file_name, hexdigest))
		return

	# Attempt the rename.
	new_name = os.path.join(os.path.split(file_name)[0], file[hash_key])
	try:	
		os.rename(file_name, new_name)
	except Exception as e:
		print("Couldn't rename %s to %s; %s" % (file_name, new_name, str(e)))


def main():
	# Construct an argument parser to grab command-line arguments.
	parser = argparse.ArgumentParser()
	parser.add_argument('-c', '--convention', default='short', help='The filename convention to apply')
	parser.add_argument('-d', '--definitions', default=os.path.join(os.path.split(__file__)[0], 'files.json'), help='The filename of a JSON file that defines proper file mappings')
	parser.add_argument('files', nargs='+')
	arguments = parser.parse_args()

	# Attempt to open and parse the definitions file.
	try:
		file_list = json.load(open(arguments.definitions))
		keys = [value["sha1"] for value in file_list]
		files_by_hash = dict(zip(keys, file_list))
	except IOError:
		print("Couldn't find definitions file %s; use the -d argument to provide an alternative" % arguments.definitions)
		return
	except KeyError:
		print("%s is invalidly formed; missing at least one 'sha1' entry" % arguments.definitions)
		return
	except ValueError as e:
		print("%s doesn't look like JSON; %s" % (arguments.definitions, str(e)))
		return

	# Map everything requested that's available.
	for file in arguments.files:
		if os.path.isfile(file):
			rename(file, files_by_hash, arguments.convention, hashlib.sha1(), "sha1")


if __name__ == '__main__':
	main()

... which expects to parse a JSON file of mapping definitions that looks like:

[
    {
        "sha1": "0ef3..",
        "short": "DOSNAME.DOS",
        "tosec": "TOSEC-esque name, with hacked-in metadata [EU] [FGROS] (82XXX) [9B2] [WXGAB23] (Twelve!)"
    },
    {
       ... etc ...
    }
    ... etc ...
]

So, an array of dictionaries. Each dictionary should have a "sha1" entry for matching. It can then have any other potential names for the file you like. You pick which set you're interested in via the -c/--convention command-line argument. E.g. with the particular names I picked above:

sha_rename --convention short *

Or:

sha_rename --convention tosec *

I picked JSON because most platforms can parse it really easily, the meaning of parts is self-documenting, and it doesn't pin you in to exactly two types of name per SHA1.

It's likely there are a million potential improvements to the Python. It's mostly untested. Good luck!

By gdx

Prophet (2714)

gdx's picture

23-02-2019, 09:55

I started the database file.

MSX FILES RENAMER.zip

The format is as I had indicated before. Say if it's suitable. Otherwise, the format you prefer. My list is not based on tosec. I intend to make a more complete database.

To make the database takes many time so I'm looking for someone or people to take care of the development of the program.

By Pencioner

Paladin (895)

Pencioner's picture

23-02-2019, 10:03

@gdx i have started coding it some minutes ago (maybe right before you posted the file in forum) Smile

Page 1/3
| 2 | 3