From 2dc83c82f943c138a6fae327a300cf5a4a6e1470 Mon Sep 17 00:00:00 2001 From: Tim Coutinho Date: Thu, 24 Dec 2020 19:11:46 -0700 Subject: [PATCH] Fix npm install --- shallow_backup/utils.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/shallow_backup/utils.py b/shallow_backup/utils.py index 31a7307c..188fa32d 100644 --- a/shallow_backup/utils.py +++ b/shallow_backup/utils.py @@ -1,26 +1,30 @@ import os import subprocess as sp +from shlex import split from shutil import rmtree, copytree +from typing import List, Union from .printing import * -def run_cmd(command: str): +def run_cmd(command: Union[str, List]): """ Wrapper on subprocess.run to handle shell commands as either a list of args or a single string. """ + if not isinstance(command, list): + command = split(command) + output = None try: - if not isinstance(command, list): - process = sp.run(command.split(), stdout=sp.PIPE, stderr=sp.DEVNULL) - return process - else: - process = sp.run(command, stdout=sp.PIPE, stderr=sp.DEVNULL) - return process + while "|" in command: + index = command.index("|") + first_command, command = command[:index], command[index + 1:] + output = sp.Popen(first_command, stdin=output.stdout if output else None, stdout=sp.PIPE, stderr=sp.DEVNULL) + return sp.run(command, stdout=sp.PIPE, stdin=output.stdout if output else None, stderr=sp.DEVNULL) except FileNotFoundError: # If package manager is missing return None -def run_cmd_write_stdout(command, filepath) -> int: +def run_cmd_write_stdout(command: str, filepath: str) -> int: """ Runs a command and then writes its stdout to a file. Returns the returncode if the return value is not 0. @@ -28,14 +32,16 @@ def run_cmd_write_stdout(command, filepath) -> int: :param: filepath str file to write command's stdout to """ process = run_cmd(command) - if process and process.returncode == 0: - with open(filepath, "w+") as f: - f.write(process.stdout.decode('utf-8')) - elif process: - print_path_red("An error occurred while running: $", command) + if process: + if process.returncode == 0: + with open(filepath, "w+") as f: + f.write(process.stdout.decode('utf-8')) + else: + print_path_red("An error occurred while running: $", command) + return process.returncode else: - print_path_red("An error occurred while running: $", command) + print_yellow("Package manager not present.") return -1