Last active
September 8, 2025 01:45
-
-
Save wrouesnel/703d5d504e78d4fbcb5c02f0bf29427d to your computer and use it in GitHub Desktop.
Opinionated Python script for cloning Github repositories
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
| #!/usr/bin/env -S uv run --script | |
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = [ | |
| # "furl", | |
| # ] | |
| # /// | |
| """Clone git repos to the default directory structure""" | |
| import subprocess | |
| import sys | |
| import os | |
| from pathlib import Path | |
| import signal | |
| from furl import furl | |
| def main(): | |
| root_dir = Path("~/src").expanduser() | |
| # Parse the clone URL | |
| clone_url = furl(sys.argv[1]) | |
| clone_dir = root_dir / clone_url.host | |
| for seg in clone_url.path.segments[:-1]: | |
| clone_dir /= seg | |
| clone_dir.mkdir(parents=True, exist_ok=True) | |
| cmd = ["git","clone"] | |
| cmd.extend(sys.argv[1:]) | |
| env = os.environ.copy() | |
| p = subprocess.Popen( | |
| cmd, env=env, cwd=clone_dir.as_posix(), stdin=sys.stdin, stderr=sys.stderr, stdout=sys.stdout | |
| ) | |
| # Attach all signals and forward them to the subprocess | |
| def sighandler(signum, stack): | |
| p.send_signal(signum) | |
| for i in [x for x in dir(signal) if x.startswith("SIG")]: | |
| try: | |
| signum = getattr(signal, i) | |
| signal.signal(signum, sighandler) | |
| except (OSError, RuntimeError, ValueError) as m: # OSError for Python3, RuntimeError for 2 | |
| #print("Skipping {}".format(i)) | |
| pass | |
| p.wait() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment