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

Fix npm install #293

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions shallow_backup/utils.py
@@ -1,38 +1,44 @@
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:
tim-coutinho marked this conversation as resolved.
Show resolved Hide resolved
"""
Runs a command and then writes its stdout to a file.
Returns the returncode if the return value is not 0.
:param: command str representing command to run
: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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this conditional back to the way it was before. I think that was easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I changed it to this so that it would differentiate between a package manager not existing on the system and an actual error occurring. As is, if you don't have any package managers, you're getting a bunch of red-colored errors spit out at you rather than a simple warning saying it doesn't exist.

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)
Expand Down