Skip to content

Commit

Permalink
Gem support (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Coutinho <tmcoutinho42@gmail.com>
  • Loading branch information
alichtman and tim-coutinho authored Dec 11, 2021
1 parent dbd87d7 commit 5547929
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
12 changes: 6 additions & 6 deletions shallow_backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ def run_cmd_if_no_dry_run(command, dest, dry_run) -> int:
if not dry_run:
overwrite_dir_prompt_if_needed(backup_path, skip)

# ruby
print_pkg_mgr_backup("gem")
command = "gem list"
dest = f"{backup_path}/gem_list.txt"
run_cmd_if_no_dry_run(command, dest, dry_run)

# brew
print_pkg_mgr_backup("brew")
command = f"brew bundle dump --file {backup_path}/brew_list.txt"
Expand All @@ -152,6 +146,12 @@ def run_cmd_if_no_dry_run(command, dest, dry_run) -> int:
if not run_cmd(command):
print_yellow("brew package manager not found.")

# ruby
print_pkg_mgr_backup("gem")
command = r"gem list | tail -n+1 | sed -E 's/\((default: )?(.*)\)/--version \2/'"
dest = f"{backup_path}/gem_list.txt"
run_cmd_if_no_dry_run(command, dest, dry_run)

# cargo
print_pkg_mgr_backup("cargo")
command = r"cargo install --list | grep '^\w.*:$' | sed -E 's/ v(.*):$/ --version \1/'"
Expand Down
4 changes: 3 additions & 1 deletion shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def run_cmd_if_no_dry_run(command, dry_run) -> int:
elif pm == "macports":
print_red_bold("WARNING: Macports reinstallation is not supported.")
elif pm == "gem":
print_red_bold("WARNING: Gem reinstallation is not supported.")
print_pkg_mgr_reinstall(pm)
cmd = f"cat {packages_path}/gem_list.txt | xargs -L 1 gem install"
run_cmd_if_no_dry_run(cmd, dry_run)
elif pm == "cargo":
print_pkg_mgr_reinstall(pm)
cmd = f"cat {packages_path}/cargo_list.txt | xargs -L 1 cargo install"
Expand Down
26 changes: 14 additions & 12 deletions shallow_backup/utils.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
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.
Returns 0 on success, and -1 on failure.
: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)
return process.returncode
return 0
else:
print_path_red("An error occurred while running: $", command)
return -1
Expand Down

0 comments on commit 5547929

Please sign in to comment.