Skip to content

Commit

Permalink
Add option to make symlinks relative
Browse files Browse the repository at this point in the history
  • Loading branch information
gvbr committed Jan 19, 2022
1 parent cfaca14 commit a1aaeea
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import getopt
import hashlib
import os.path
import re
import shutil
import sys
Expand Down Expand Up @@ -521,6 +522,7 @@ def main(argv: List[str]):
'debug',
'move',
'symlink',
'relative',
'chunk-size=',
'threads=',
'header-file=',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit a1aaeea

Please sign in to comment.