irongit

Simply downloads a folder from a github repo into current directory.

'initial commit'

huncholanehuncholaneauthored
commit ab100a9d6653519011870eb8529768f873edfc74Browse files

4 files changed, +67 -0

+2-0.gitignore
@@ -0,0 +1,2 @@
1+build
2+*.egg-info
+0-0README.md

No content changes (mode or rename only).

+49-0main.py
@@ -0,0 +1,49 @@
1+import json
2+import os
3+import shutil
4+import requests
5+from urllib.parse import urlparse
6+from sys import argv
7+import tempfile
8+
9+
10+def download(url: str, dest=None):
11+ split = url.split("/")
12+ try:
13+ s1, s2, s3, owner, repo, _, branch, *path = split
14+ path = "/".join(path)
15+ except:
16+ try:
17+ s1, s2, s3, owner, repo = split
18+ path = None
19+ except:
20+ print("Invalid URL")
21+ gh_base = "/".join([s1, s2, s3, owner, repo])
22+ if not dest:
23+ if path:
24+ dest = path
25+ else:
26+ dest = repo
27+ print(f"Owner: {owner}")
28+ print(f"Repo: {repo}")
29+ print(f"Branch: {branch}")
30+ print(f"Path: {path}")
31+ print(f"Destination: {dest}")
32+
33+ # clone into a temporary directory
34+ with tempfile.TemporaryDirectory() as tmpdir:
35+ os.system(f"git clone {gh_base} {tmpdir}/{repo}")
36+ s = os.path.join(tmpdir, repo)
37+ shutil.rmtree(os.path.join(s, ".git"))
38+ if path:
39+ s = os.path.join(s, path)
40+ d = os.path.join(os.getcwd(), dest)
41+ shutil.copytree(s, d, dirs_exist_ok=True)
42+
43+
44+def main():
45+ download(argv[1])
46+
47+
48+if __name__ == "__main__":
49+ download(argv[1])
+16-0setup.py
@@ -0,0 +1,16 @@
1+from setuptools import setup, find_packages
2+
3+setup(
4+ name="gh-downloader",
5+ version="0.1",
6+ packages=find_packages(),
7+ py_modules=["main"],
8+ install_requires=[
9+ "requests",
10+ ],
11+ entry_points={
12+ "console_scripts": [
13+ "gh-downloader=main:main",
14+ ],
15+ },
16+)