Created
May 30, 2025 09:56
-
-
Save wolfmanjm/c1977aef3a2f49506c5f83e982f2320f to your computer and use it in GitHub Desktop.
upload for swd2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
# this script will concatenate the file specified into the upload.fs | |
# it strips comments and handles #require | |
# | |
import argparse | |
import os | |
import re | |
import sys | |
parser = argparse.ArgumentParser() | |
parser.add_argument("file", metavar="FILE", help="file to send") | |
parser.add_argument("-I", "--include-path", metavar="DIR", action="append", default=[".", ".."], help="append directory to include paths") | |
parser.add_argument("-o", "--output-path", metavar="STRING", default=".", help="output file path") | |
args = parser.parse_args() | |
errors = 0 | |
included = [] | |
rc = re.compile(r"\s+(\\ .*)") # remove \ comment at end of line | |
rc2 = re.compile(r"[ \t]\( .* \)") # remove ( ) comment in line | |
class FileNotFoundException(Exception): | |
def __init__(self, fname): | |
self.fname = fname | |
def __str__(self): | |
return f"file not found: {self.fname}" | |
def process_file(parent_fname, parent_lineno, input_filename, outfile): | |
lineno = 0 | |
n_bytes_sent = 0 | |
try: | |
try: | |
paths = filter(lambda p: os.path.exists(p), map(lambda d: os.path.join(d, input_filename), args.include_path)) | |
fpath = next(paths) | |
except StopIteration: | |
raise FileNotFoundException(input_filename) | |
sys.stderr.write(f"*** Uploading {fpath} ***\n") | |
with open(fpath, 'r') as infile: | |
for line in infile: | |
lineno += 1 | |
if re.match(r"^\s*\\\s.*", line): | |
continue | |
elif re.match(r"^\s*$", line): | |
continue | |
elif "compiletoflash" in line: | |
sys.stderr.write(f"** Warning: compiletoflash stripped from file {fpath}\n") | |
continue | |
elif "compiletoram" in line: | |
sys.stderr.write(f"** Warning: compiletoram stripped from file {fpath}\n") | |
continue | |
elif line.strip().startswith('#require ') or line.strip().startswith('#include '): | |
fn = line.strip().split(' ', 1)[1] | |
if fn not in included: | |
sys.stderr.write(f"*** Including {fn} ***\n") | |
included.append(fn) | |
try: | |
n_bytes_sent += process_file(input_filename, lineno, fn, outfile) | |
except FileNotFoundError: | |
sys.stderr.write(f"** Warning: File {fn} not found.\n") | |
errors += 1 | |
else: | |
line = rc.sub('', line) | |
line = rc2.sub('', line) | |
outfile.write(line) | |
n_bytes_sent += len(line) | |
return n_bytes_sent | |
except (OSError, FileNotFoundException) as e: | |
if parent_fname is not None: | |
sys.stderr.write(f"*** {parent_fname}({parent_lineno}): {e} ***\n") | |
else: | |
sys.stderr.write(f"*** {fname}: {e} ***\n") | |
sys.exit(1) | |
ofn = f"{args.output_path}/upload.fs" | |
sys.stderr.write(f"*** output to {ofn} ***\n") | |
with open(ofn, 'w') as outfile: | |
n = process_file(None, 0, args.file, outfile) | |
sys.stderr.write(f"*** sent {n} bytes\n") | |
if errors == 0: | |
os.system('pkill -QUIT swd2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment