Skip to content

Instantly share code, notes, and snippets.

@rvanlaar
Created May 30, 2020 17:40
Show Gist options
  • Save rvanlaar/692ee66510b8a6e406b5105d39b490ff to your computer and use it in GitHub Desktop.
Save rvanlaar/692ee66510b8a6e406b5105d39b490ff to your computer and use it in GitHub Desktop.
Copies hfs files from a 'mounted' disk.
# Mount iso: hmount path/to/file.iso
# Do: hls -aqRF1 > files.txt
# and run: python hfscopy.py
# explanation
# -a Show all files
# -q print '?' instead of escaped chars
# -R descend into all directories
# -F enable file type flag, dir: executable:*
# -1 output 1 item per line
# Ideas:
# copy 'real' filename over when it has a '?'.
# do a double pass, first create the directories
# convert it into objects, an object per file/directory?
import os.path
import subprocess
def is_dir(name: str) -> bool:
return name.endswith(':')
def test_dir() -> None:
assert is_dir("non-dir") is False
assert is_dir("dir:")
def is_path(name: str) -> bool:
"""A path starts and ends with a ':'"""
return name[0] == name[-1] == ":"
def test_is_path() -> None:
assert is_path(":Data:Movie:")
assert is_path("Data:") is False
assert is_path(":Data") is False
def convert_path(path) -> str:
paths = [p for p in path.split(':') if p]
return os.path.join(*paths)
def test_convert_path() -> None:
assert convert_path(":Data:Player:") == "Data/Movie"
def create_path(hfs_path) -> str:
to_path = convert_path(hfs_path)
subprocess.run(["mkdir", "-p", f"{to_path}"])
return to_path
def copy_file(hfs_path, to_path, name):
to_name = os.path.join(to_path, name)
subprocess.run(["hcopy", "-m", f"{hfs_path}{name}", f"{to_name}"]
def main():
to_path: str = "."
hfs_path: str = ":"
test_data = open("files.txt")
for name in test_data.readlines():
name = name.strip()
if not name:
# empty line
continue
if not is_path(name) and is_dir(name):
# dir listed in filelist, not dir
continue
if is_path(name):
hfs_path = name
to_path = create_path(hfs_path)
continue
if name.endswith('*'):
# strip 'executable flag'
name = name[:-1]
copy_file(hfs_path, to_path, name)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment