github_backup/__main__.py (view raw)
1from github import Github, Auth
2from git import Repo
3from git.exc import GitCommandError
4from pathlib import Path
5import logging, json, os
6logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
7logger = logging.getLogger(__name__)
8logger.setLevel(logging.INFO)
9
10with open("config.json", "r") as in_file:
11 config = json.loads("".join(in_file.readlines()))
12
13def get_config(key, default=None):
14 try:
15 return config[key]
16 except KeyError:
17 if default is None:
18 logger.error("Missing config key: {}.".format(key))
19 exit(1)
20 return default
21
22github_user = get_config("github_username")
23github_token = get_config("github_auth_token")
24repo_dir = get_config("repo_dir")
25blacklist = set(get_config("blacklist", []))
26suffix = ".git"
27suffix_len = len(suffix)
28
29g = Github(auth=Auth.Token(github_token))
30repos = g.get_user().get_repos()
31
32def handle_repo(r):
33 repo_name = r.name
34 repo_path = Path(os.path.join(repo_dir, repo_name + suffix))
35
36 if repo_path.exists():
37 logger.info("Updating " + repo_name)
38 repo = Repo(repo_path)
39 repo.remote().fetch("+refs/heads/*:refs/heads/*")
40 else:
41 url = f"https://{github_user}:{github_token}@github.com/{r.owner.login}/{repo_name}.git"
42 logger.info("Cloning " + repo_name)
43 repo = Repo.clone_from(url, repo_path, bare=True)
44
45 repo_desc = "" if r.description is None else r.description
46 with open(os.path.join(repo_path, "description"), "w") as out_file:
47 out_file.write(repo_desc + "\n")
48 return repo_name
49
50results = set([ handle_repo(repo) for repo in repos if repo.name not in blacklist ])
51subfolders = set([ f.name[:-suffix_len] for f in os.scandir(repo_dir) if f.is_dir() ])
52
53print("Untracked repositories:")
54print(sorted(subfolders.difference(results)))