-
-
Save wvandeun/b2139a94bfb264c4bf208f369127e834 to your computer and use it in GitHub Desktop.
A script to generate the equivalent of a symlink for python scripts that require a pipenv
This file contains 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
#!/usr/bin/env python3.7 | |
""" | |
Usage: pipenvlink path/to/script.py | |
Result: bash file named 'script' that runs script.py in its venv from anywhere | |
""" | |
import os | |
import sys | |
from pathlib import Path | |
from subprocess import run | |
# Get absolute path to target script | |
script = Path(sys.argv[1]).resolve() | |
assert script.exists() | |
# Generate absolute path for new 'link' file | |
out = Path(script.stem).resolve() | |
# Change to script's parent dir so can execute pipenv | |
os.chdir(script.parent) | |
# Use pipenv to get path to venv's python | |
python = run('pipenv --py', shell=True, text=True, capture_output=True, check=True).stdout.strip() | |
# Create 'link' file | |
if out.exists() and input(f"Overwrite {out}? (y/n) ") != 'y': | |
sys.exit("Aborted") | |
out.write_text(f'#!/bin/bash\nexec "{python}" "{script}" "${{@:1}}"') | |
# Make link file executable | |
run(f'chmod u+x {out}', shell=True, check=True) | |
print(f"Created {out}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment