From 2599d822e5c528dd415f7fb2638d488832a4d62c Mon Sep 17 00:00:00 2001 From: Gino D Date: Sun, 8 Jan 2023 01:48:23 +0100 Subject: [PATCH] Major updates implemented --- src/picowatch.py | 201 ++++++++++++++++++++++++++++------------------- 1 file changed, 122 insertions(+), 79 deletions(-) diff --git a/src/picowatch.py b/src/picowatch.py index 1fff4a3..9d54925 100644 --- a/src/picowatch.py +++ b/src/picowatch.py @@ -724,15 +724,18 @@ class Picowatch(object): else: 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('/')) - fh, tempname = tempfile.mkstemp() + fh, tempname = tempfile.mkstemp('.py') try: with os.fdopen(fh, 'wb') as tmp: 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: input('Press Enter to delete temp file.') os.remove(tempname) @@ -742,13 +745,25 @@ class Picowatch(object): changes = [] try: + subprocess.check_output(['git', 'add', '-A'], 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')]: if not filename: 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', '??']: changes.append((1, filename)) @@ -766,48 +781,71 @@ class Picowatch(object): if return_output: return changes - tab = Tab(4, nb_columns=2) - tab.head('[ ]', 'Filename') + if changes: + tab = Tab(4, nb_columns=2) + tab.head('[ ]', 'Filename') - for status, filename in changes: - if status == 1: - tab.line('[-]', filename) - else: - tab.line('[+]', filename) + for status, filename in changes: + if status == 1: + tab.line('[+]', filename) + else: + tab.line('[-]', filename) + else: + print('Repository is clean') - def push(self): - tab = Tab(4, 30, 15, 15, nb_columns=5) - tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception') + def commit(self, message: str = ''): changes = self.status(return_output=True) - for filepath in [filename for status, filename in changes if status == -1]: - filepath = filepath.strip('/') - status, output, exception = self.filesystem.rm(filepath) + if changes: + tab = Tab(4, 30, 15, 15, nb_columns=5) + tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception') - if status: - for filename, checked, exception in output: - if checked: - tab.line('[-]', filename, '', 'DELETED') - else: - tab.line('[?]', filename, '', '', exception) + for filepath in [filename for status, filename in changes if status == -1]: + filepath = filepath.strip('/') + status, output, exception = self.filesystem.rm(filepath) + + if status: + 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: - 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('/') + message = message.strip('"').strip("'") 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: - tab.line('[?]', destination, '', '', str(e)) - - print('Pyboard up to date.') - + try: + message = e.output.decode('utf-8').strip() + except: + message = str(e).strip() + else: + print('Pyboard is up to date') + def compile(self, filename: str): _, error = mpy_cross.run(filename, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).communicate() @@ -861,51 +899,56 @@ while True: match message.strip().split(' '): case ['?' | 'help']: print('These are common Picowatch keywords:\n') - tab = Tab(35, nb_columns=2) - tab.head('Keywords', 'Description') - tab.line('uname', 'Pyboard name and version') - tab.line('exit | ctrl + c', 'Exit Picowatch Terminal') - tab.line('ctrl + c (while is running)', 'Interrupts the currently running code') - tab.line('ls []', 'List information about the file(s) on the Pyboard (the root directory by default)') - tab.line('cat ', 'Concatenate source code to standard output') - tab.line('rm | delete ', 'Delete file or directory contents on the Pyboard') - tab.line('put | upload ', 'Upload file or directory contents from the PC to the Pyboard') - tab.line('get | download ', 'Download file or directory contents from the Pyboard to the PC. Warning: this may cause file corruption.') - tab.line('diff ', 'Compare source code from Pyboard with source code on PC') - tab.line('mpy | compile ', 'Compile source file to mpy file') - tab.line('install ', 'Install a package lib') - tab.line('status', 'Show the working tree status (Git is required)') - tab.line('push', 'Update Pyboard along with associated files (Git is required)') - tab.line('? | help', 'Show keywords and their description') - tab.line('. | boot', 'Do a soft reset and run main.py (if exists) in REPL mode') - tab.line('! | test []', 'Run a script from PC on the Pyboard and print out the results in raw-REPL mode') - tab.line('!! | run []', 'Run a script on the Pyboard and print out the results in raw-REPL mode') - case ['uname']: + tab = Tab(12, 10, 32, nb_columns=4) + tab.head('Keywords', 'Shortcut', 'Parameters', 'Description') + tab.line('help', '?', '', 'Show keywords and their description') + tab.line('boot', '.', '', 'Do a soft reset and run main.py (if exists) in REPL mode') + tab.line('test', '!', ' (default: main.py)', 'Run a script from PC on the Pyboard and print out the results in raw-REPL mode') + tab.line('run', '!!', ' (default: main.py)', 'Run a script on the Pyboard and print out the results in raw-REPL mode') + tab.line('ctrl + c', 'exit', '', 'Exit Picowatch Terminal or interrupts the currently running code (in REPL or raw-REPL mode)') + tab.line('uname', 'os', '', 'Pyboard name and version') + tab.line('edit', 'vim', ' [use vim]', 'Edit specified file from the PC (vim or vscode is required)') + tab.line('scan', 'ls', ' (default: /)', 'List information about the file(s) on the Pyboard') + tab.line('source', 'cat', '', 'Concatenate source code to standard output') + tab.line('delete', 'rm', '', 'Delete file or directory contents on the Pyboard') + tab.line('upload', 'put', '', 'Upload file or directory contents from the PC to the Pyboard') + tab.line('download', 'get', '', 'Download file or directory contents from the Pyboard to the PC. Warning: this may cause file corruption.') + tab.line('compare', 'diff', ' [use vim]', 'Compare source code from PC with source code from the Pyboard (vim or vscode is required)') + tab.line('compile', 'mpy', '', 'Compile source file to mpy file') + tab.line('install', 'ins', '', 'Install a package lib') + tab.line('status', 'mod', '', 'Show the working tree status (Git is required)') + tab.line('commit', 'sync', ' (default: "")', 'Synchronize Pyboard along with associated commits (Git is required)') + case ['os' | 'uname']: picowatch.terminal('import os;print(os.uname().machine, os.uname().version)') - case ['ls' | 'list', *source]: - picowatch.listing(source[0] if source else '/') - case ['cat' | 'code', source]: - picowatch.contents(source) - case ['rm' | 'delete', source]: - picowatch.delete(source) - case ['put' | 'upload', source]: - picowatch.upload(source) - case ['get' | 'download', source]: - picowatch.download(source) - case ['diff' | 'compare', source]: - picowatch.compare(source) - case ['status']: + case ['ls' | 'scan', *file]: + picowatch.listing(file[0] if file else '/') + case ['vim' | 'edit', file, *use_vim]: + if len(use_vim) > 0: + subprocess.call(['vim', file], shell=True) + else: + subprocess.call(['code', file], shell=True) + case ['cat' | 'source', file]: + picowatch.contents(file) + case ['rm' | 'delete', path]: + picowatch.delete(path) + case ['put' | 'upload', path]: + 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) - case ['push']: - picowatch.push() - case ['mpy' | 'compile', source]: - picowatch.compile(source) - case ['install', package_name]: + case ['sync' | 'commit', *message]: + picowatch.commit(message=' '.join(message).strip()) + case ['mpy' | 'compile', file]: + picowatch.compile(file) + case ['ins' | 'install', package_name]: print('# TODO') - case ['!' | 'test', *source]: - picowatch.test(source[0] if source else 'main.py') - case ['!!' | 'run', *source]: - picowatch.launch(source[0] if source else 'main.py') + case ['!' | 'test', *file]: + picowatch.test(file[0] if file else 'main.py') + case ['!!' | 'run', *file]: + picowatch.launch(file[0] if file else 'main.py') case ['.'| 'boot']: picowatch.boot() case ['exit']: