irongit

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

49 lines1.2 KBPython
1import json
2import os
3import shutil
4import requests
5from urllib.parse import urlparse
6from sys import argv
7import tempfile
8
9
10def 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
44def main():
45 download(argv[1])
46
47
48if __name__ == "__main__":
49 download(argv[1])