diff --git a/generate.py b/generate.py index c4f7c34..80676ad 100755 --- a/generate.py +++ b/generate.py @@ -2,6 +2,7 @@ import getopt import hashlib +import os.path import re import shutil import sys @@ -521,6 +522,7 @@ def main(argv: List[str]): 'debug', 'move', 'symlink', + 'relative', 'chunk-size=', 'threads=', 'header-file=', @@ -570,6 +572,7 @@ def main(argv: List[str]): language_weight = 3 move = False symlink = False + relative = False global THREADS global RULES global MAX_FILE_SIZE @@ -650,6 +653,7 @@ def main(argv: List[str]): sys.exit(help_msg('invalid output DIR: %s' % output_dir)) move |= opt == '--move' symlink |= opt == '--symlink' + relative |= opt == '--relative' if opt == '--chunk-size': CHUNK_SIZE = int(arg) if opt == '--threads': @@ -914,7 +918,8 @@ def include_candidate(x: GameEntry) -> bool: rom_input_path, rom_output_path, move, - symlink) + symlink, + relative) copied_files.add(rom_input_path) else: log( @@ -938,7 +943,7 @@ def include_candidate(x: GameEntry) -> bool: curr_out_dir.mkdir(parents=True, exist_ok=True) if symlink: curr_out_dir = curr_out_dir / file_name - transfer_file(full_path, curr_out_dir, move, symlink) + transfer_file(full_path, curr_out_dir, move, symlink, relative) else: printed_items.append(file_name) break @@ -955,7 +960,8 @@ def include_candidate(x: GameEntry) -> bool: rom_input_path, rom_output_dir, move, - symlink) + symlink, + relative) shutil.copystat( str(full_path), str(rom_output_dir)) @@ -1047,14 +1053,18 @@ def transfer_file( input_path: Path, output_path: Path, move: bool, - symlink: bool) -> None: + symlink: bool, + relative: bool) -> None: try: if move: print('Moving [%s] to [%s]' % (input_path, output_path)) shutil.move(str(input_path), str(output_path)) elif symlink: print('Linking [%s] to [%s]' % (input_path, output_path)) - output_path.symlink_to(input_path.resolve()) + if not relative: + output_path.symlink_to(input_path.resolve()) + else: + output_path.symlink_to(os.path.relpath(input_path, output_path.parent)) else: print('Copying [%s] to [%s]' % (input_path, output_path)) shutil.copy2(str(input_path), str(output_path)) @@ -1114,6 +1124,10 @@ def help_msg(s: Optional[Union[str, Exception]] = None) -> str: 'Please note newer versions of Windows 10 require ' 'elevated privileges to create symlinks', + '\t--relative\t\t' + 'If set along with --symlink, will create ' + 'relative symlinks instead of absolute', + '\t--group-by-first-letter\t' 'If set, groups ROMs on the output directory in subfolders according ' 'to the first letter in their name',