mirror of
https://github.com/josegonzalez/python-github-backup.git
synced 2026-01-20 20:23:09 +01:00
Only insert development path into sys.path when running from a git checkout (when ../.git exists). This makes the script more robust by only using the development tree when available and falling back to installed package otherwise.
72 lines
1.8 KiB
Python
Executable File
72 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
|
|
# If we are running from a git-checkout, we can run against the development
|
|
# tree without installing.
|
|
if os.path.exists(os.path.join(os.path.dirname(__file__), "..", ".git")):
|
|
sys.path.insert(
|
|
0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
)
|
|
|
|
|
|
from github_backup.github_backup import (
|
|
backup_account,
|
|
backup_repositories,
|
|
check_git_lfs_install,
|
|
filter_repositories,
|
|
get_authenticated_user,
|
|
logger,
|
|
mkdir_p,
|
|
parse_args,
|
|
retrieve_repositories,
|
|
)
|
|
|
|
logging.basicConfig(
|
|
format="%(asctime)s.%(msecs)03d: %(message)s",
|
|
datefmt="%Y-%m-%dT%H:%M:%S",
|
|
level=logging.INFO,
|
|
)
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
if args.quiet:
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
output_directory = os.path.realpath(args.output_directory)
|
|
if not os.path.isdir(output_directory):
|
|
logger.info("Create output directory {0}".format(output_directory))
|
|
mkdir_p(output_directory)
|
|
|
|
if args.lfs_clone:
|
|
check_git_lfs_install()
|
|
|
|
if args.log_level:
|
|
log_level = logging.getLevelName(args.log_level.upper())
|
|
if isinstance(log_level, int):
|
|
logger.root.setLevel(log_level)
|
|
|
|
if not args.as_app:
|
|
logger.info("Backing up user {0} to {1}".format(args.user, output_directory))
|
|
authenticated_user = get_authenticated_user(args)
|
|
else:
|
|
authenticated_user = {"login": None}
|
|
|
|
repositories = retrieve_repositories(args, authenticated_user)
|
|
repositories = filter_repositories(args, repositories)
|
|
backup_repositories(args, output_directory, repositories)
|
|
backup_account(args, output_directory)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as e:
|
|
logger.error(str(e))
|
|
sys.exit(1)
|