From 66eda482806970914b14de50058dbd86de323df3 Mon Sep 17 00:00:00 2001 From: Savoy Date: Tue, 16 Mar 2021 14:51:39 -0500 Subject: [PATCH] Bug Fix stdout properly prints onto one line instead of every change propogating as a newline. --- ipm.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ipm.py b/ipm.py index 5dccf13..877b30c 100755 --- a/ipm.py +++ b/ipm.py @@ -23,6 +23,7 @@ import re import sqlite3 import subprocess as sp import socket +import time try: import argcomplete @@ -87,6 +88,7 @@ class Sql: node TEXT NOT NULL) ''') cur.close() + self.conn.commit() def __enter__(self): return self.conn @@ -115,8 +117,11 @@ def execute(cmd): ''' popen = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.STDOUT, - universal_newlines=True) - for stdout_line in iter(popen.stdout.readline, ""): + universal_newlines=False) + # reopen to get \r recognized as new line for same line output + # https://www.koldfront.dk/making_subprocesspopen_in_python_3_play_nice_with_elaborate_output_1594 + nice_stdout = open(os.dup(popen.stdout.fileno()), newline='') + for stdout_line in nice_stdout: yield stdout_line popen.stdout.close() return_code = popen.wait() @@ -190,16 +195,26 @@ def add(path: str, desc: str=''): tipo, recu = ft[path.is_dir()] ipfs_path = path.as_posix() cmd = ['ipfs', 'add', '--progress'] - regex = r'added (Qm[A-Za-z0-9]+) {}(?:\n|)$'.format(path.name) + + # Backslash any regex characters that could be in the file name before + # adding. + filename = path.name + for char in ['[', ']', '(', ')', '?', '*']: + filename = filename.replace(char, f'\{char}') + + regex = r'added (Qm[A-Za-z0-9]+) {}(?:\n|)$'.format(filename) + # Runs the generated IPFS command cmd.extend((recu, ipfs_path)) cmd = [x for x in cmd if x is not None] out = [] for line in execute(cmd): - print(line, end='') if line.startswith('added') or line.startswith('pinned'): out.append(line) + #print('\n') + print(line, end='', flush=True) + time.sleep(.1) # Gets the final hash for the upload (so in the case of a dir, the hash of # the dir being uploaded). -- 2.45.2