Skip to content

Commit

Permalink
refactor: improve the naming in enable_private_packages
Browse files Browse the repository at this point in the history
  • Loading branch information
MaferMazu committed Aug 26, 2024
1 parent c8eefa0 commit 5d8dc5b
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions tutorpicasso/commands/enable_private_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,60 @@ def enable_private_packages() -> None:
tutor_root = context.root
tutor_conf = tutor_config.load(tutor_root)

private_requirements_root = f"{tutor_root}/env/build/openedx/private_requirements"
private_requirements_path = f"{tutor_root}/env/build/openedx/requirements"
packages = get_picasso_packages(tutor_conf)

if not os.path.exists(private_requirements_root):
os.makedirs(private_requirements_root)
if not os.path.exists(private_requirements_path):
os.makedirs(private_requirements_path)

for package, info in packages.items():
if not {"name", "repo", "version"}.issubset(info):
raise click.ClickException(
f"{package} is missing one of the required keys: 'name', 'repo', 'version'"
)

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

subprocess.call(
["git", "clone", "-b", info["version"], info["repo"]],
cwd=private_requirements_root,
cwd=private_requirements_path,
)

handle_private_requirements_by_tutor_version(info, private_requirements_root)
handle_private_requirements_by_tutor_version(info, private_requirements_path)


def handle_private_requirements_by_tutor_version(info: Dict[str, str], private_requirements_path: str) -> None:
def handle_private_requirements_by_tutor_version(
info: Dict[str, str], private_requirements_path: str
) -> None:
"""
Handle the private requirements based on the Tutor version.
Args:
info (Dict[str, str]): A dictionary containing metadata about the package. Expected to have a "name" key.
private_requirements_path (str): The directory path to store the private packages.
info (Dict[str, str]): A dictionary containing metadata about the requirement. Expected to have a "name" key.
private_requirements_path (str): The directory path to store the private requirements.
"""
tutor_version_obj = Version(tutor_version)
quince_version_obj = Version("v17.0.0")

if tutor_version_obj < quince_version_obj:
private_txt_path = f"{private_requirements_path}/private.txt"
_enable_private_packages_before_quince(info, private_txt_path)
_enable_private_requirements_before_quince(info, private_txt_path)
else:
_enable_private_packages_latest(info, private_requirements_path)
_enable_private_requirements_latest(info, private_requirements_path)


def _enable_private_packages_before_quince(
def _enable_private_requirements_before_quince(
info: Dict[str, str], private_requirements_txt: str
) -> None:
"""
Copy the package name in the private.txt file to ensure that packages are added in the build process for Tutor versions < Quince.
Copy the requirement name in the private.txt file to ensure that requirements are added in the build process for Tutor versions < Quince.
Args:
info (Dict[str, str]): A dictionary containing metadata about the package. Expected to have a "name" key.
private_requirements_txt (str): The file path to `private.txt`, which stores the list of private packages to be included in the build.
info (Dict[str, str]): A dictionary containing metadata about the requirement. Expected to have a "name" key.
private_requirements_txt (str): The file path to `private.txt`, which stores the list of private requirements to be included in the build.
"""
if not os.path.exists(private_requirements_txt):
with open(private_requirements_txt, "w") as file:
Expand All @@ -84,22 +86,22 @@ def _enable_private_packages_before_quince(
subprocess.call(echo_command, shell=True)


def _enable_private_packages_latest(
info: Dict[str, str], private_requirements_root: str
def _enable_private_requirements_latest(
info: Dict[str, str], private_requirements_path: str
) -> None:
"""
Use the tutor mounts method to ensure that packages are added in the build process.
Use the tutor mounts method to ensure that requirements are added in the build process.
Args:
info (Dict[str, str]): A dictionary containing metadata about the package. Expected to have a "name" key.
private_requirements_root (str): The root directory where private packages are stored.
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.
"""
subprocess.call(
[
"tutor",
"mounts",
"add",
f'{private_requirements_root}/{info["name"]}',
f'{private_requirements_path}/{info["name"]}',
]
)

Expand Down

0 comments on commit 5d8dc5b

Please sign in to comment.