Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --get option to read contents of existing file #26

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions luatool/luatool.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,56 @@ def openserial(args):
parser.add_argument('-r', '--restart', action='store_true', help='Restart MCU after upload')
parser.add_argument('-d', '--dofile', action='store_true', help='Run the Lua script after upload')
parser.add_argument('-v', '--verbose', action='store_true', help="Show progress messages.")
parser.add_argument('-g', '--get', default=None, help='Get contents of specified file on MCU')
parser.add_argument('-l', '--list', action='store_true', help='List files on device')
parser.add_argument('-w', '--wipe', action='store_true', help='Delete all lua/lc files on device.')
args = parser.parse_args()

if args.get:
s = opendevice(args)

writeln(s, "=file.open('" + args.get + "', 'r')\r", 0)
line = ""
while True:
char = readdata(s, 1)
if char == '' or char == chr(62):
break
line += char

if char == chr(62):
char = readdata(s, 1)

line = line.strip()
if line == "nil":
sys.stderr.write("File %s does not exist on device\n" % args.get)
sys.exit(1)
if line != "true":
raise Exception('No proper answer from MCU')

# file.readline() includes trailing newlines so they are doubled
# detect EOF as "nil" followed by single newline and prompt (prompt could appear elsewhere)
writeln(s, "local l; repeat l = file.readline(); print(l) until l == nil;file.close()\r", 0)

line = ""
while True:
char = readdata(s, 1)
if char == '':
break
if char == chr(13) or char == chr(10): # LF or CR
prevch = char

# Must be a second newline if still printing the file
char = readdata(s, 1)
if prevch != char: # EOF, skip previous line ("nil")
break

sys.stdout.write(line + prevch)
line = ""
continue

line += char
sys.exit(0)

if args.list:
s = openserial(args)
writeln("local l = file.list();for k,v in pairs(l) do print('name:'..k..', size:'..v)end\r", 0)
Expand Down