Skip to content

Commit

Permalink
refactor: use tutor utils to execute and print
Browse files Browse the repository at this point in the history
  • Loading branch information
MaferMazu committed Sep 24, 2024
1 parent 5c5e1ef commit add7f2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 58 deletions.
52 changes: 10 additions & 42 deletions tutorpicasso/commands/enable_private_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from packaging.version import Version
from tutor import config as tutor_config
from tutor.__about__ import __version__ as tutor_version
from tutor import utils as tutor_utils


@click.command(name="enable-private-packages", help="Enable picasso private packages")
Expand Down Expand Up @@ -35,23 +36,14 @@ def enable_private_packages() -> None:
f"{package} is missing one of the required keys: 'name', 'repo', 'version'"
)

if os.path.isdir(f'{private_requirements_path}/{info["name"]}'):
subprocess.call(
["rm", "-rf", f'{private_requirements_path}/{info["name"]}']
)
requirement_path = f'{private_requirements_path}/{info["name"]}'
if os.path.isdir(requirement_path):
tutor_utils.execute("rm", "-rf", requirement_path)

process = subprocess.run(
["git", "clone", "-b", info["version"], info["repo"]],
cwd=private_requirements_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
tutor_utils.execute(
"git", "clone", "-b", info["version"], info["repo"], requirement_path
)

click.echo(process.stdout)
if process.stderr:
click.echo(process.stderr)

handle_private_requirements_by_tutor_version(info, private_requirements_path)


Expand Down Expand Up @@ -89,20 +81,9 @@ def _enable_private_requirements_before_quince(
with open(private_requirements_txt, "w") as file:
file.write("")

echo_command = f'echo "-e ./{info["name"]}/" >> {private_requirements_txt}'

process = subprocess.run(
echo_command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
echo_command = f'echo "-e ./{info["name"]}/" >> {private_requirements_txt}'.split()

click.echo(process.stdout)

if process.stderr:
click.echo(process.stderr)
tutor_utils.execute(*echo_command)


def _enable_private_requirements_latest(
Expand All @@ -115,23 +96,10 @@ def _enable_private_requirements_latest(
info (Dict[str, str]): A dictionary containing metadata about the requirement. Expected to have a "name" key.
private_requirements_path (str): The root directory where private requirements are stored.
"""
process = subprocess.run(
[
"tutor",
"mounts",
"add",
f'{private_requirements_path}/{info["name"]}',
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
tutor_utils.execute(
"tutor", "mounts", "add", f'{private_requirements_path}/{info["name"]}'
)

click.echo(process.stdout)

if process.stderr:
click.echo(process.stderr)


def get_picasso_packages(settings: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
"""
Expand Down
24 changes: 8 additions & 16 deletions tutorpicasso/commands/enable_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import click
from tutor import config as tutor_config
from tutor import utils as tutor_utils


@click.command(name="enable-themes", help="Enable picasso themes")
Expand Down Expand Up @@ -43,20 +44,11 @@ def enable_themes() -> None:

theme_version = theme.get("version", "")
theme_repo = theme.get("repo", "")
process = subprocess.run(
[
"git",
"clone",
"-b",
str(theme_version),
str(theme_repo),
str(theme_path),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
tutor_utils.execute(
"git",
"clone",
"-b",
str(theme_version),
str(theme_repo),
str(theme_path),
)

click.echo(process.stdout)
if process.stderr:
click.echo(process.stderr)
2 changes: 2 additions & 0 deletions tutorpicasso/commands/run_extra_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import click
from tutor import config as tutor_config
from tutor import fmt as tutor_fmt

COMMAND_CHAINING_OPERATORS = ["&&", "&", "||", "|", ";"]

Expand Down Expand Up @@ -80,6 +81,7 @@ def run_command(command: str) -> None:
Args:
command (str): Tutor command.
"""
click.echo(tutor_fmt.command(command))
with subprocess.Popen(
command,
shell=True,
Expand Down

0 comments on commit add7f2c

Please sign in to comment.