Major updates implemented

This commit is contained in:
Gino D
2023-01-08 01:48:23 +01:00
parent acdfba39ad
commit 2599d822e5

View File

@@ -724,15 +724,18 @@ class Picowatch(object):
else: else:
tab.line('[?]', filepath, exception) tab.line('[?]', filepath, exception)
def compare(self, filepath: str): def compare(self, filepath: str, use_vim: bool = True):
content, _ = self.filesystem.get(filepath.strip('./').strip('/')) content, _ = self.filesystem.get(filepath.strip('./').strip('/'))
fh, tempname = tempfile.mkstemp() fh, tempname = tempfile.mkstemp('.py')
try: try:
with os.fdopen(fh, 'wb') as tmp: with os.fdopen(fh, 'wb') as tmp:
tmp.write(content) tmp.write(content)
subprocess.Popen(f'code --diff "{tempname}" "{os.path.join(LISTENING_TO, filepath)}"', stdout=subprocess.PIPE, shell=True).communicate() if use_vim:
subprocess.call(['vim', '-d', os.path.join(LISTENING_TO, filepath), tempname], shell=True)
else:
subprocess.call(['code', '--diff', os.path.join(LISTENING_TO, filepath), tempname], shell=True)
finally: finally:
input('Press Enter to delete temp file.') input('Press Enter to delete temp file.')
os.remove(tempname) os.remove(tempname)
@@ -742,13 +745,25 @@ class Picowatch(object):
changes = [] changes = []
try: try:
subprocess.check_output(['git', 'add', '-A'], stderr=subprocess.STDOUT)
output = subprocess.check_output(['git', 'status', '-s'], stderr=subprocess.STDOUT) output = subprocess.check_output(['git', 'status', '-s'], stderr=subprocess.STDOUT)
for filename in [f.strip() for f in output.decode('utf-8').split('\n')]: for filename in [f.strip() for f in output.decode('utf-8').split('\n')]:
if not filename: if not filename:
continue continue
status, filename = filename.split(' ') columns = filename.split(' ')
if len(columns) == 2:
status, filename = columns
elif len(columns) == 3:
status, _, filename = columns
elif len(columns) == 5:
changes.append((1, columns[4]))
changes.append((-1, columns[2]))
continue
else:
continue
if status in ['A', 'M', '??']: if status in ['A', 'M', '??']:
changes.append((1, filename)) changes.append((1, filename))
@@ -766,48 +781,71 @@ class Picowatch(object):
if return_output: if return_output:
return changes return changes
tab = Tab(4, nb_columns=2) if changes:
tab.head('[ ]', 'Filename') tab = Tab(4, nb_columns=2)
tab.head('[ ]', 'Filename')
for status, filename in changes: for status, filename in changes:
if status == 1: if status == 1:
tab.line('[-]', filename) tab.line('[+]', filename)
else: else:
tab.line('[+]', filename) tab.line('[-]', filename)
else:
print('Repository is clean')
def push(self): def commit(self, message: str = ''):
tab = Tab(4, 30, 15, 15, nb_columns=5)
tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
changes = self.status(return_output=True) changes = self.status(return_output=True)
for filepath in [filename for status, filename in changes if status == -1]: if changes:
filepath = filepath.strip('/') tab = Tab(4, 30, 15, 15, nb_columns=5)
status, output, exception = self.filesystem.rm(filepath) tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
if status: for filepath in [filename for status, filename in changes if status == -1]:
for filename, checked, exception in output: filepath = filepath.strip('/')
if checked: status, output, exception = self.filesystem.rm(filepath)
tab.line('[-]', filename, '', 'DELETED')
else: if status:
tab.line('[?]', filename, '', '', exception) for filename, checked, exception in output:
if checked:
tab.line('[-]', filename, '', 'DELETED')
else:
tab.line('[?]', filename, '', '', exception)
else:
tab.line('[?]', filepath, '', '', exception)
queue = []
for filepath in [filename for status, filename in changes if status == 1]:
queue.extend(self.internal_ls(filepath))
for source, size in queue:
destination = source.replace(LISTENING_TO.replace(os.sep, '/'), '').strip('/')
try:
tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination))
except Exception as e:
tab.line('[?]', destination, '', '', str(e))
print('-' * 50)
if not message:
message = 'Synchronize Pyboard along with associated commit(s)'
else: else:
tab.line('[?]', filepath, '', '', exception) message = message.strip('"').strip("'")
queue = []
for filepath in [filename for status, filename in changes if status == 1]:
queue.extend(self.internal_ls(filepath))
for source, size in queue:
destination = source.replace(LISTENING_TO.replace(os.sep, '/'), '').strip('/')
try: try:
tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination)) output = subprocess.check_output(['git', 'commit', '-am', message], stderr=subprocess.STDOUT)
for line in output.decode('utf-8').split('\n'):
print(line)
except Exception as e: except Exception as e:
tab.line('[?]', destination, '', '', str(e)) try:
message = e.output.decode('utf-8').strip()
print('Pyboard up to date.') except:
message = str(e).strip()
else:
print('Pyboard is up to date')
def compile(self, filename: str): def compile(self, filename: str):
_, error = mpy_cross.run(filename, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).communicate() _, error = mpy_cross.run(filename, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).communicate()
@@ -861,51 +899,56 @@ while True:
match message.strip().split(' '): match message.strip().split(' '):
case ['?' | 'help']: case ['?' | 'help']:
print('These are common Picowatch keywords:\n') print('These are common Picowatch keywords:\n')
tab = Tab(35, nb_columns=2) tab = Tab(12, 10, 32, nb_columns=4)
tab.head('Keywords', 'Description') tab.head('Keywords', 'Shortcut', 'Parameters', 'Description')
tab.line('uname', 'Pyboard name and version') tab.line('help', '?', '', 'Show keywords and their description')
tab.line('exit | ctrl + c', 'Exit Picowatch Terminal') tab.line('boot', '.', '', 'Do a soft reset and run main.py (if exists) in REPL mode')
tab.line('ctrl + c (while is running)', 'Interrupts the currently running code') tab.line('test', '!', '<file> (default: main.py)', 'Run a script from PC on the Pyboard and print out the results in raw-REPL mode')
tab.line('ls [<source>]', 'List information about the file(s) on the Pyboard (the root directory by default)') tab.line('run', '!!', '<file> (default: main.py)', 'Run a script on the Pyboard and print out the results in raw-REPL mode')
tab.line('cat <source>', 'Concatenate source code to standard output') tab.line('ctrl + c', 'exit', '', 'Exit Picowatch Terminal or interrupts the currently running code (in REPL or raw-REPL mode)')
tab.line('rm | delete <source>', 'Delete file or directory contents on the Pyboard') tab.line('uname', 'os', '', 'Pyboard name and version')
tab.line('put | upload <source>', 'Upload file or directory contents from the PC to the Pyboard') tab.line('edit', 'vim', '<file> [use vim]', 'Edit specified file from the PC (vim or vscode is required)')
tab.line('get | download <source>', 'Download file or directory contents from the Pyboard to the PC. Warning: this may cause file corruption.') tab.line('scan', 'ls', '<path> (default: /)', 'List information about the file(s) on the Pyboard')
tab.line('diff <source>', 'Compare source code from Pyboard with source code on PC') tab.line('source', 'cat', '<file>', 'Concatenate source code to standard output')
tab.line('mpy | compile <source>', 'Compile source file to mpy file') tab.line('delete', 'rm', '<path>', 'Delete file or directory contents on the Pyboard')
tab.line('install <package-name>', 'Install a package lib') tab.line('upload', 'put', '<path>', 'Upload file or directory contents from the PC to the Pyboard')
tab.line('status', 'Show the working tree status (Git is required)') tab.line('download', 'get', '<path>', 'Download file or directory contents from the Pyboard to the PC. Warning: this may cause file corruption.')
tab.line('push', 'Update Pyboard along with associated files (Git is required)') tab.line('compare', 'diff', '<file> [use vim]', 'Compare source code from PC with source code from the Pyboard (vim or vscode is required)')
tab.line('? | help', 'Show keywords and their description') tab.line('compile', 'mpy', '<python file>', 'Compile source file to mpy file')
tab.line('. | boot', 'Do a soft reset and run main.py (if exists) in REPL mode') tab.line('install', 'ins', '<package name>', 'Install a package lib')
tab.line('! | test [<source>]', 'Run a script from PC on the Pyboard and print out the results in raw-REPL mode') tab.line('status', 'mod', '', 'Show the working tree status (Git is required)')
tab.line('!! | run [<source>]', 'Run a script on the Pyboard and print out the results in raw-REPL mode') tab.line('commit', 'sync', '<message> (default: "")', 'Synchronize Pyboard along with associated commits (Git is required)')
case ['uname']: case ['os' | 'uname']:
picowatch.terminal('import os;print(os.uname().machine, os.uname().version)') picowatch.terminal('import os;print(os.uname().machine, os.uname().version)')
case ['ls' | 'list', *source]: case ['ls' | 'scan', *file]:
picowatch.listing(source[0] if source else '/') picowatch.listing(file[0] if file else '/')
case ['cat' | 'code', source]: case ['vim' | 'edit', file, *use_vim]:
picowatch.contents(source) if len(use_vim) > 0:
case ['rm' | 'delete', source]: subprocess.call(['vim', file], shell=True)
picowatch.delete(source) else:
case ['put' | 'upload', source]: subprocess.call(['code', file], shell=True)
picowatch.upload(source) case ['cat' | 'source', file]:
case ['get' | 'download', source]: picowatch.contents(file)
picowatch.download(source) case ['rm' | 'delete', path]:
case ['diff' | 'compare', source]: picowatch.delete(path)
picowatch.compare(source) case ['put' | 'upload', path]:
case ['status']: picowatch.upload(path)
case ['get' | 'download', path]:
picowatch.download(file)
case ['diff' | 'compare', file, *use_vim]:
picowatch.compare(file, use_vim=len(use_vim) > 0)
case ['mod' | 'status']:
picowatch.status(return_output=False) picowatch.status(return_output=False)
case ['push']: case ['sync' | 'commit', *message]:
picowatch.push() picowatch.commit(message=' '.join(message).strip())
case ['mpy' | 'compile', source]: case ['mpy' | 'compile', file]:
picowatch.compile(source) picowatch.compile(file)
case ['install', package_name]: case ['ins' | 'install', package_name]:
print('# TODO') print('# TODO')
case ['!' | 'test', *source]: case ['!' | 'test', *file]:
picowatch.test(source[0] if source else 'main.py') picowatch.test(file[0] if file else 'main.py')
case ['!!' | 'run', *source]: case ['!!' | 'run', *file]:
picowatch.launch(source[0] if source else 'main.py') picowatch.launch(file[0] if file else 'main.py')
case ['.'| 'boot']: case ['.'| 'boot']:
picowatch.boot() picowatch.boot()
case ['exit']: case ['exit']: