Skip to content

Instantly share code, notes, and snippets.

@mnaberez
Created March 23, 2022 15:55
Show Gist options
  • Save mnaberez/de3b3c52aeb0d5daba763983feba4a32 to your computer and use it in GitHub Desktop.
Save mnaberez/de3b3c52aeb0d5daba763983feba4a32 to your computer and use it in GitHub Desktop.
Read all PRG and SEQ files from a disk using cbmread
#!/usr/bin/env python3
"""
Read all PRG and SEQ files from a disk using cbmread.
Other file types (e.g. REL, USR) are ignored.
Usage: %s <device number>
"""
import shlex
import subprocess
import sys
def read_directory(device_number):
"""Read disk directory and the return filenames as a list like:
['foo,p', 'bar,s']
"""
cmd = "cbmctrl dir %d" % device_number
print(cmd)
out = subprocess.check_output(cmd, shell=True)
lines = [ l.rstrip() for l in out.decode('utf-8', 'ignore').splitlines() ]
for line in lines:
print(line)
filenames_with_types = []
for line in lines:
if not (line.endswith("prg") or line.endswith("seq")):
continue
blocks, quoted_filename_and_type = line.split(None, 1)
quoted_filename, filetype = quoted_filename_and_type.rsplit(None, 1)
filename = quoted_filename.replace('"', '')
filename_and_type = "%s,%s" % (filename, filetype)
filenames_with_types.append(filename_and_type)
return filenames_with_types
def read_files(device_number, filenames_with_types):
"""Read the given files from the disk"""
cmd = "cbmread %d" % device_number
for filename_with_type in filenames_with_types:
cmd += ' ' + shlex.quote(filename_with_type)
print(cmd)
out = subprocess.check_call(cmd, shell=True)
def read_all_files(device_number):
"""Read all files from the disk"""
filenames_with_types = read_directory(device_number)
read_files(device_number, filenames_with_types)
def main():
if len(sys.argv) != 2:
sys.stderr.write(__doc__ % sys.argv[0])
sys.exit(1)
device_number = int(sys.argv[1])
read_all_files(device_number)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment