Skip to content

Commit

Permalink
custom command
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaitan committed Feb 5, 2024
1 parent 5964c72 commit 4f49cc1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/run_script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ name: Shbin Run Script
on:
workflow_dispatch:
inputs:
command:
description: 'Command to use'
required: true
type: string

file_path:
description: 'Relative path of the Python script to run'
description: 'Relative path of the script to run'
required: true
type: string

Expand All @@ -20,9 +25,8 @@ jobs:
run: pip install https://github.com/Shiphero/shbin/archive/refs/heads/gha.zip
- name: Download script
run: shbin dl ${{ inputs.file_path }}
- name: Run Python script
run: |
python $(basename "${{ inputs.file_path }}") > output.txt
- name: Run script
run: ${{ inputs.command}} $(basename "${{ inputs.file_path }}") > output.txt
- name: Upload results
run: |
rm $(basename "${{ inputs.file_path }}")
Expand Down
66 changes: 44 additions & 22 deletions shbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Usage:
shbin dl <url_or_path>
shbin run <url_or_path>
shbin run [--logs] [--command=<command>] <url_or_path>
shbin (<path>... | -x | -) [-f <file-name>] [-n] [-m <message>] [-d <target-dir>]
[--namespace=<namespace>] [--url-link-to-pages]
shbin (-h | --help)
Expand All @@ -36,6 +36,7 @@
-d <target-dir>, --target-dir=<target-dir> Optional (sub)directory to upload file/s.
--namespace=<namespace> Base namespace to upload. Default to
SHBIN_NAMESPACE envvar or "{user}/".
--command=<command> How to run the given file. Default "auto".
-p, --url-link-to-pages Reformat the url to link to Github pages.
"""

Expand Down Expand Up @@ -98,37 +99,58 @@ def get_extension(content):
else:
return guess_extension(magic.from_buffer(content, mime=True))


def normalize_path(url_or_path, repo):
return re.sub(rf"^https://github\.com/{repo.full_name}/(blob|tree)/{repo.default_branch}/", "", url_or_path).rstrip("/")
return re.sub(rf"^https://github\.com/{repo.full_name}/(blob|tree)/{repo.default_branch}/", "", url_or_path).rstrip(
"/"
)


def run(url_or_path, repo, user):
def run(url_or_path, repo, user, show_logs=True, command="auto"):
path = normalize_path(url_or_path, repo)
wf = repo.get_workflow("run_script.yml")
wf.create_dispatch(repo.default_branch, {"file_path": path})

if command == "auto":
import ipdb;ipdb.set_trace()
executables = {"py": "python", "sh": "bash", "js": "node", "rb": "ruby", "php": "php", "go": "go run"}
extension = path.rpartition(".")[-1]
try:
command = executables[extension]
except KeyError:
raise DocoptExit(f"Unknown command for .{extension} files")

wf.create_dispatch(repo.default_branch, {"file_path": path, "command": command})
while True:
run = wf.get_runs(user, repo.default_branch, event="workflow_dispatch")[0]
print(run.status)
if run.status == "in_progress":
job = run.jobs()[0]
break
break
time.sleep(1)
continue

print(job.html_url)
url = job.logs_url()
sentinel = "Cleaning up orphan processes" # determines the logs is finished
seen = 0
while True:
response = requests.get(url)
lines = response.text.splitlines()
new_lines = lines[seen:]
seen = len(lines)
for line in new_lines:
print(line)
if sentinel in line:
break


print(f"⚙️ {job.html_url}")

if show_logs:
url = job.logs_url()
sentinel = "Cleaning up orphan processes" # determines the logs is finished
seen = 0
while True:
response = requests.get(url)
lines = response.text.splitlines()
new_lines = lines[seen:]
seen = len(lines)
for line in new_lines:
print(line)
if sentinel in line:
break
while job.status != "completed":
job.update()
time.sleep(1)

output = repo.get_contents(f"{user}/{path}_run_{run.id}/output.txt").decoded_content.decode("utf-8")
print(f"[green]Result:[/green]\n\n{output}")
print(f"More: shbin dl {path}_run_{run.id}")


def download(url_or_path, repo, user):
"""
Expand Down Expand Up @@ -184,7 +206,7 @@ def main(argv=None) -> None:
if args["dl"]:
return download(args["<url_or_path>"], repo, user)
elif args["run"]:
return run(args["<url_or_path>"], repo, user)
return run(args["<url_or_path>"], repo, user, args["--logs"], args["--command"])
elif args["--from-clipboard"] or args["<path>"] == ["-"]:
if args["--from-clipboard"]:
try:
Expand Down

0 comments on commit 4f49cc1

Please sign in to comment.