Created
August 18, 2023 11:22
-
-
Save xitij2000/e6e540aa2362e6f94a880695b574a94d to your computer and use it in GitHub Desktop.
tutor-devstack-plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from urllib.parse import urlparse | |
import click | |
from tutor import hooks | |
from tutor import config as tutor_config | |
from tutor.commands.dev import DevContext | |
GITHUB_PR_REGEX = re.compile( | |
r"\/(?P[a-z0-9-_]+)\/(?P[a-z0-9-_]+)\/pull\/(?P[0-9]+)" | |
) | |
GITLAB_PR_REGEX = re.compile( | |
r"\/(?P[a-z0-9-_\/]+)\/-\/merge_requests\/(?P[0-9]+)" | |
) | |
def get_github_package_url(url): | |
""" | |
For a given github PR return a pip installable url | |
""" | |
org, repo, pr = GITHUB_PR_REGEX.match(url.path).groups() | |
return f"git+https://github.com/{org}/{repo}.git@refs/pull/{pr}/head" | |
def get_gitlab_package_url(url): | |
""" | |
For a given gitlab PR return a pip installable url | |
""" | |
project, mr = GITLAB_PR_REGEX.match(url.path).groups() | |
return f"git+https://gitlab.com/{project}/@refs/merge-requests/{mr}/head" | |
def get_pip_installable_requirement(package): | |
""" | |
For a given url return something that can be passed to pip install | |
""" | |
if package.startswith("http"): | |
url = urlparse(package) | |
if url.hostname == "github.com": | |
requirement = get_github_package_url(url) | |
elif url.hostname == "gitlab.com": | |
requirement = get_gitlab_package_url(url) | |
else: | |
return None | |
else: | |
requirement = package | |
return requirement | |
@click.command(help="Install an package") | |
@click.argument("package") | |
@click.pass_context | |
def install_package(context, package): | |
config = tutor_config.load(context.obj.root) | |
dev = DevContext(context.obj.root) | |
requirement = get_pip_installable_requirement(package) | |
if not requirement: | |
click.echo("Unsupported URL") | |
return | |
dev.job_runner(config).docker_compose("exec", "lms", "pip", "install", requirement) | |
dev.job_runner(config).docker_compose("exec", "cms", "pip", "install", requirement) | |
hooks.Filters.CLI_COMMANDS.add_items([install_package]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment