mirror of
https://github.com/josegonzalez/python-github-backup.git
synced 2025-12-05 16:18:02 +01:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
87dab293ed | ||
|
|
0244af4e05 | ||
|
|
eca9f0f7df | ||
|
|
afa2a6d587 | ||
|
|
b77ea48d74 | ||
|
|
f378254188 | ||
|
|
83128e986a | ||
|
|
17e4f9a125 | ||
|
|
e59d1e3a68 | ||
|
|
de860ee5a9 | ||
|
|
cb054c2631 | ||
|
|
c142707a90 | ||
|
|
7cccd42ec9 | ||
|
|
9a539b1d6b | ||
|
|
cd2372183e | ||
|
|
bd346de898 | ||
|
|
6e3cbe841a | ||
|
|
8b95f187ad | ||
|
|
2b9549ffde | ||
|
|
fb2c3ca921 | ||
|
|
4f4785085d | ||
|
|
76895dcf69 | ||
|
|
1d50a4038b | ||
|
|
9d31ccfba9 | ||
|
|
27a1ba2d04 | ||
|
|
f157ea107f | ||
|
|
a129cc759a | ||
|
|
bb551a83f4 | ||
|
|
9b1b4a9ebc | ||
|
|
e6b6eb8bef | ||
|
|
0b3f120e2b | ||
|
|
990249b80b | ||
|
|
cefb226545 | ||
|
|
ea22ffdf26 | ||
|
|
0f21d7b8a4 | ||
|
|
cb33b9bab7 | ||
|
|
68c48cb0b3 | ||
|
|
922a3c5a6e | ||
|
|
d4055eb99c | ||
|
|
d8a330559c | ||
|
|
de93824498 | ||
|
|
2efeaa7580 | ||
|
|
647810a2f0 | ||
|
|
0dfe5c342a | ||
|
|
1d6e1abab1 |
79
CHANGES.rst
79
CHANGES.rst
@@ -1,6 +1,85 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.18.0 (2018-02-22)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Add option to fetch followers/following JSON data. [Stephen Greene]
|
||||||
|
|
||||||
|
0.17.0 (2018-02-20)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Short circuit gists backup process. [W. Harrison Wright]
|
||||||
|
|
||||||
|
- Formatting. [W. Harrison Wright]
|
||||||
|
|
||||||
|
- Add ability to backup gists. [W. Harrison Wright]
|
||||||
|
|
||||||
|
0.16.0 (2018-01-22)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Change option to --all-starred. [W. Harrison Wright]
|
||||||
|
|
||||||
|
- JK don't update documentation. [W. Harrison Wright]
|
||||||
|
|
||||||
|
- Put starred clone repoistories under a new option. [W. Harrison
|
||||||
|
Wright]
|
||||||
|
|
||||||
|
- Add comment. [W. Harrison Wright]
|
||||||
|
|
||||||
|
- Add ability to clone starred repos. [W. Harrison Wright]
|
||||||
|
|
||||||
|
0.14.1 (2017-10-11)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Fix arg not defined error. [Edward Pfremmer]
|
||||||
|
|
||||||
|
Ref: https://github.com/josegonzalez/python-github-backup/issues/69
|
||||||
|
|
||||||
|
0.14.0 (2017-10-11)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Added a check to see if git-lfs is installed when doing an LFS clone.
|
||||||
|
[pieterclaerhout]
|
||||||
|
|
||||||
|
- Added support for LFS clones. [pieterclaerhout]
|
||||||
|
|
||||||
|
- Add pypi info to readme. [Albert Wang]
|
||||||
|
|
||||||
|
- Explicitly support python 3 in package description. [Albert Wang]
|
||||||
|
|
||||||
|
- Add couple examples to help new users. [Yusuf Tran]
|
||||||
|
|
||||||
|
0.13.2 (2017-05-06)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Fix remotes while updating repository. [Dima Gerasimov]
|
||||||
|
|
||||||
|
0.13.1 (2017-04-11)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Fix error when repository has no updated_at value. [Nicolai Ehemann]
|
||||||
|
|
||||||
|
0.13.0 (2017-04-05)
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
- Add OS check for OSX specific keychain args. [Martin O'Reilly]
|
||||||
|
|
||||||
|
Keychain arguments are only supported on Mac OSX.
|
||||||
|
Added check for operating system so we give a
|
||||||
|
"Keychain arguments are only supported on Mac OSX"
|
||||||
|
error message rather than a "No password item matching the
|
||||||
|
provided name and account could be found in the osx keychain"
|
||||||
|
error message
|
||||||
|
|
||||||
|
|
||||||
|
- Add support for storing PAT in OSX keychain. [Martin O'Reilly]
|
||||||
|
|
||||||
|
Added additional optional arguments and README guidance for storing
|
||||||
|
and accessing a Github personal access token (PAT) in the OSX
|
||||||
|
keychain
|
||||||
|
|
||||||
|
|
||||||
0.12.1 (2017-03-27)
|
0.12.1 (2017-03-27)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|||||||
76
README.rst
76
README.rst
@@ -2,6 +2,8 @@
|
|||||||
github-backup
|
github-backup
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
|PyPI| |Python Versions|
|
||||||
|
|
||||||
backup a github user or organization
|
backup a github user or organization
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
@@ -26,14 +28,17 @@ Usage
|
|||||||
CLI Usage is as follows::
|
CLI Usage is as follows::
|
||||||
|
|
||||||
github-backup [-h] [-u USERNAME] [-p PASSWORD] [-t TOKEN]
|
github-backup [-h] [-u USERNAME] [-p PASSWORD] [-t TOKEN]
|
||||||
[-o OUTPUT_DIRECTORY] [-i] [--starred] [--watched]
|
[-o OUTPUT_DIRECTORY] [-i] [--starred] [--all-starred]
|
||||||
[--all] [--issues] [--issue-comments] [--issue-events]
|
[--watched] [--followers] [--following] [--all]
|
||||||
[--pulls] [--pull-comments] [--pull-commits] [--labels]
|
[--issues] [--issue-comments] [--issue-events] [--pulls]
|
||||||
[--hooks] [--milestones] [--repositories] [--bare]
|
[--pull-comments] [--pull-commits] [--labels] [--hooks]
|
||||||
[--wikis] [--skip-existing]
|
[--milestones] [--repositories] [--bare] [--lfs]
|
||||||
|
[--wikis] [--gists] [--starred-gists] [--skip-existing]
|
||||||
[-L [LANGUAGES [LANGUAGES ...]]] [-N NAME_REGEX]
|
[-L [LANGUAGES [LANGUAGES ...]]] [-N NAME_REGEX]
|
||||||
[-H GITHUB_HOST] [-O] [-R REPOSITORY] [-P] [-F]
|
[-H GITHUB_HOST] [-O] [-R REPOSITORY] [-P] [-F]
|
||||||
[--prefer-ssh] [-v]
|
[--prefer-ssh] [-v]
|
||||||
|
[--keychain-name OSX_KEYCHAIN_ITEM_NAME]
|
||||||
|
[--keychain-account OSX_KEYCHAIN_ITEM_ACCOUNT]
|
||||||
USER
|
USER
|
||||||
|
|
||||||
Backup a github account
|
Backup a github account
|
||||||
@@ -49,12 +54,16 @@ CLI Usage is as follows::
|
|||||||
password for basic auth. If a username is given but
|
password for basic auth. If a username is given but
|
||||||
not a password, the password will be prompted for.
|
not a password, the password will be prompted for.
|
||||||
-t TOKEN, --token TOKEN
|
-t TOKEN, --token TOKEN
|
||||||
personal access or OAuth token
|
personal access or OAuth token, or path to token
|
||||||
|
(file://...)
|
||||||
-o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY
|
-o OUTPUT_DIRECTORY, --output-directory OUTPUT_DIRECTORY
|
||||||
directory at which to backup the repositories
|
directory at which to backup the repositories
|
||||||
-i, --incremental incremental backup
|
-i, --incremental incremental backup
|
||||||
--starred include starred repositories in backup
|
--starred include JSON output of starred repositories in backup
|
||||||
|
--all-starred include starred repositories in backup
|
||||||
--watched include watched repositories in backup
|
--watched include watched repositories in backup
|
||||||
|
--followers include JSON output of followers in backup
|
||||||
|
--following include JSON output of following users in backup
|
||||||
--all include everything in backup
|
--all include everything in backup
|
||||||
--issues include issues in backup
|
--issues include issues in backup
|
||||||
--issue-comments include issue comments in backup
|
--issue-comments include issue comments in backup
|
||||||
@@ -68,7 +77,11 @@ CLI Usage is as follows::
|
|||||||
--milestones include milestones in backup
|
--milestones include milestones in backup
|
||||||
--repositories include repository clone in backup
|
--repositories include repository clone in backup
|
||||||
--bare clone bare repositories
|
--bare clone bare repositories
|
||||||
|
--lfs clone LFS repositories (requires Git LFS to be
|
||||||
|
installed, https://git-lfs.github.com)
|
||||||
--wikis include wiki clone in backup
|
--wikis include wiki clone in backup
|
||||||
|
--gists include gists in backup
|
||||||
|
--starred-gists include starred gists in backup
|
||||||
--skip-existing skip project if a backup directory exists
|
--skip-existing skip project if a backup directory exists
|
||||||
-L [LANGUAGES [LANGUAGES ...]], --languages [LANGUAGES [LANGUAGES ...]]
|
-L [LANGUAGES [LANGUAGES ...]], --languages [LANGUAGES [LANGUAGES ...]]
|
||||||
only allow these languages
|
only allow these languages
|
||||||
@@ -83,6 +96,12 @@ CLI Usage is as follows::
|
|||||||
-F, --fork include forked repositories
|
-F, --fork include forked repositories
|
||||||
--prefer-ssh Clone repositories using SSH instead of HTTPS
|
--prefer-ssh Clone repositories using SSH instead of HTTPS
|
||||||
-v, --version show program's version number and exit
|
-v, --version show program's version number and exit
|
||||||
|
--keychain-name OSX_KEYCHAIN_ITEM_NAME
|
||||||
|
OSX ONLY: name field of password item in OSX keychain
|
||||||
|
that holds the personal access or OAuth token
|
||||||
|
--keychain-account OSX_KEYCHAIN_ITEM_ACCOUNT
|
||||||
|
OSX ONLY: account field of password item in OSX
|
||||||
|
keychain that holds the personal access or OAuth token
|
||||||
|
|
||||||
|
|
||||||
The package can be used to backup an *entire* organization or repository, including issues and wikis in the most appropriate format (clones for wikis, json files for issues).
|
The package can be used to backup an *entire* organization or repository, including issues and wikis in the most appropriate format (clones for wikis, json files for issues).
|
||||||
@@ -91,3 +110,46 @@ Authentication
|
|||||||
==============
|
==============
|
||||||
|
|
||||||
Note: Password-based authentication will fail if you have two-factor authentication enabled.
|
Note: Password-based authentication will fail if you have two-factor authentication enabled.
|
||||||
|
|
||||||
|
Using the Keychain on Mac OSX
|
||||||
|
=============================
|
||||||
|
Note: On Mac OSX the token can be stored securely in the user's keychain. To do this:
|
||||||
|
|
||||||
|
1. Open Keychain from "Applications -> Utilities -> Keychain Access"
|
||||||
|
2. Add a new password item using "File -> New Password Item"
|
||||||
|
3. Enter a name in the "Keychain Item Name" box. You must provide this name to github-backup using the --keychain-name argument.
|
||||||
|
4. Enter an account name in the "Account Name" box, enter your Github username as set above. You must provide this name to github-backup using the --keychain-account argument.
|
||||||
|
5. Enter your Github personal access token in the "Password" box
|
||||||
|
|
||||||
|
Note: When you run github-backup, you will be asked whether you want to allow "security" to use your confidential information stored in your keychain. You have two options:
|
||||||
|
|
||||||
|
1. **Allow:** In this case you will need to click "Allow" each time you run `github-backup`
|
||||||
|
2. **Always Allow:** In this case, you will not be asked for permission when you run `github-backup` in future. This is less secure, but is required if you want to schedule `github-backup` to run automatically
|
||||||
|
|
||||||
|
About Git LFS
|
||||||
|
=============
|
||||||
|
|
||||||
|
When you use the "--lfs" option, you will need to make sure you have Git LFS installed.
|
||||||
|
|
||||||
|
Instructions on how to do this can be found on https://git-lfs.github.com.
|
||||||
|
|
||||||
|
Examples
|
||||||
|
========
|
||||||
|
|
||||||
|
Backup all repositories::
|
||||||
|
|
||||||
|
export ACCESS_TOKEN=SOME-GITHUB-TOKEN
|
||||||
|
github-backup WhiteHouse --token $ACCESS_TOKEN --organization --output-directory /tmp/white-house --repositories
|
||||||
|
|
||||||
|
Backup a single organization repository with everything else (wiki, pull requests, comments, issues etc)::
|
||||||
|
|
||||||
|
export ACCESS_TOKEN=SOME-GITHUB-TOKEN
|
||||||
|
ORGANIZATION=docker
|
||||||
|
REPO=cli
|
||||||
|
# e.g. git@github.com:docker/cli.git
|
||||||
|
github-backup $ORGANIZATION -P -t $ACCESS_TOKEN -o . --all -O -R $REPO
|
||||||
|
|
||||||
|
.. |PyPI| image:: https://img.shields.io/pypi/v/github-backup.svg
|
||||||
|
:target: https://pypi.python.org/pypi/github-backup/
|
||||||
|
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/github-backup.svg
|
||||||
|
:target: https://github.com/albertyw/github-backup
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import select
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import platform
|
||||||
try:
|
try:
|
||||||
# python 3
|
# python 3
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
@@ -68,11 +69,15 @@ def logging_subprocess(popenargs,
|
|||||||
"""
|
"""
|
||||||
child = subprocess.Popen(popenargs, stdout=subprocess.PIPE,
|
child = subprocess.Popen(popenargs, stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE, **kwargs)
|
stderr=subprocess.PIPE, **kwargs)
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
log_info("Windows operating system detected - no subprocess logging will be returned")
|
||||||
|
|
||||||
log_level = {child.stdout: stdout_log_level,
|
log_level = {child.stdout: stdout_log_level,
|
||||||
child.stderr: stderr_log_level}
|
child.stderr: stderr_log_level}
|
||||||
|
|
||||||
def check_io():
|
def check_io():
|
||||||
|
if sys.platform == 'win32':
|
||||||
|
return
|
||||||
ready_to_read = select.select([child.stdout, child.stderr],
|
ready_to_read = select.select([child.stdout, child.stderr],
|
||||||
[],
|
[],
|
||||||
[],
|
[],
|
||||||
@@ -154,11 +159,23 @@ def parse_args():
|
|||||||
parser.add_argument('--starred',
|
parser.add_argument('--starred',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='include_starred',
|
dest='include_starred',
|
||||||
|
help='include JSON output of starred repositories in backup')
|
||||||
|
parser.add_argument('--all-starred',
|
||||||
|
action='store_true',
|
||||||
|
dest='all_starred',
|
||||||
help='include starred repositories in backup')
|
help='include starred repositories in backup')
|
||||||
parser.add_argument('--watched',
|
parser.add_argument('--watched',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='include_watched',
|
dest='include_watched',
|
||||||
help='include watched repositories in backup')
|
help='include watched repositories in backup')
|
||||||
|
parser.add_argument('--followers',
|
||||||
|
action='store_true',
|
||||||
|
dest='include_followers',
|
||||||
|
help='include JSON output of followers in backup')
|
||||||
|
parser.add_argument('--following',
|
||||||
|
action='store_true',
|
||||||
|
dest='include_following',
|
||||||
|
help='include JSON output of following users in backup')
|
||||||
parser.add_argument('--all',
|
parser.add_argument('--all',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='include_everything',
|
dest='include_everything',
|
||||||
@@ -207,10 +224,22 @@ def parse_args():
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
dest='bare_clone',
|
dest='bare_clone',
|
||||||
help='clone bare repositories')
|
help='clone bare repositories')
|
||||||
|
parser.add_argument('--lfs',
|
||||||
|
action='store_true',
|
||||||
|
dest='lfs_clone',
|
||||||
|
help='clone LFS repositories (requires Git LFS to be installed, https://git-lfs.github.com)')
|
||||||
parser.add_argument('--wikis',
|
parser.add_argument('--wikis',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='include_wiki',
|
dest='include_wiki',
|
||||||
help='include wiki clone in backup')
|
help='include wiki clone in backup')
|
||||||
|
parser.add_argument('--gists',
|
||||||
|
action='store_true',
|
||||||
|
dest='include_gists',
|
||||||
|
help='include gists in backup')
|
||||||
|
parser.add_argument('--starred-gists',
|
||||||
|
action='store_true',
|
||||||
|
dest='include_starred_gists',
|
||||||
|
help='include starred gists in backup')
|
||||||
parser.add_argument('--skip-existing',
|
parser.add_argument('--skip-existing',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
dest='skip_existing',
|
dest='skip_existing',
|
||||||
@@ -251,13 +280,37 @@ def parse_args():
|
|||||||
parser.add_argument('-v', '--version',
|
parser.add_argument('-v', '--version',
|
||||||
action='version',
|
action='version',
|
||||||
version='%(prog)s ' + __version__)
|
version='%(prog)s ' + __version__)
|
||||||
|
parser.add_argument('--keychain-name',
|
||||||
|
dest='osx_keychain_item_name',
|
||||||
|
help='OSX ONLY: name field of password item in OSX keychain that holds the personal access or OAuth token')
|
||||||
|
parser.add_argument('--keychain-account',
|
||||||
|
dest='osx_keychain_item_account',
|
||||||
|
help='OSX ONLY: account field of password item in OSX keychain that holds the personal access or OAuth token')
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def get_auth(args, encode=True):
|
def get_auth(args, encode=True):
|
||||||
auth = None
|
auth = None
|
||||||
|
|
||||||
if args.token:
|
if args.osx_keychain_item_name:
|
||||||
|
if not args.osx_keychain_item_account:
|
||||||
|
log_error('You must specify both name and account fields for osx keychain password items')
|
||||||
|
else:
|
||||||
|
if platform.system() != 'Darwin':
|
||||||
|
log_error("Keychain arguments are only supported on Mac OSX")
|
||||||
|
try:
|
||||||
|
with open(os.devnull,'w') as devnull:
|
||||||
|
token = (subprocess.check_output([
|
||||||
|
'security','find-generic-password',
|
||||||
|
'-s',args.osx_keychain_item_name,
|
||||||
|
'-a',args.osx_keychain_item_account,
|
||||||
|
'-w' ], stderr=devnull).strip())
|
||||||
|
auth = token + ':' + 'x-oauth-basic'
|
||||||
|
except:
|
||||||
|
log_error('No password item matching the provided name and account could be found in the osx keychain.')
|
||||||
|
elif args.osx_keychain_item_account:
|
||||||
|
log_error('You must specify both name and account fields for osx keychain password items')
|
||||||
|
elif args.token:
|
||||||
_path_specifier = 'file://'
|
_path_specifier = 'file://'
|
||||||
if args.token.startswith(_path_specifier):
|
if args.token.startswith(_path_specifier):
|
||||||
args.token = open(args.token[len(_path_specifier):],
|
args.token = open(args.token[len(_path_specifier):],
|
||||||
@@ -305,12 +358,15 @@ def get_github_repo_url(args, repository):
|
|||||||
if args.prefer_ssh:
|
if args.prefer_ssh:
|
||||||
return repository['ssh_url']
|
return repository['ssh_url']
|
||||||
|
|
||||||
|
if repository.get('is_gist'):
|
||||||
|
return repository['git_pull_url']
|
||||||
|
|
||||||
auth = get_auth(args, False)
|
auth = get_auth(args, False)
|
||||||
if auth:
|
if auth:
|
||||||
repo_url = 'https://{0}@{1}/{2}/{3}.git'.format(
|
repo_url = 'https://{0}@{1}/{2}/{3}.git'.format(
|
||||||
auth,
|
auth,
|
||||||
get_github_host(args),
|
get_github_host(args),
|
||||||
args.user,
|
repository['owner']['login'],
|
||||||
repository['name'])
|
repository['name'])
|
||||||
else:
|
else:
|
||||||
repo_url = repository['clone_url']
|
repo_url = repository['clone_url']
|
||||||
@@ -442,6 +498,13 @@ def _request_url_error(template, retry_timeout):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_git_lfs_install():
|
||||||
|
exit_code = subprocess.call(['git', 'lfs', 'version'])
|
||||||
|
if exit_code != 0:
|
||||||
|
log_error('The argument --lfs requires you to have Git LFS installed.\nYou can get it from https://git-lfs.github.com.')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def retrieve_repositories(args):
|
def retrieve_repositories(args):
|
||||||
log_info('Retrieving repositories')
|
log_info('Retrieving repositories')
|
||||||
single_request = False
|
single_request = False
|
||||||
@@ -459,7 +522,34 @@ def retrieve_repositories(args):
|
|||||||
args.user,
|
args.user,
|
||||||
args.repository)
|
args.repository)
|
||||||
|
|
||||||
return retrieve_data(args, template, single_request=single_request)
|
repos = retrieve_data(args, template, single_request=single_request)
|
||||||
|
|
||||||
|
if args.all_starred:
|
||||||
|
starred_template = 'https://{0}/user/starred'.format(get_github_api_host(args))
|
||||||
|
starred_repos = retrieve_data(args, starred_template, single_request=False)
|
||||||
|
# flag each repo as starred for downstream processing
|
||||||
|
for item in starred_repos:
|
||||||
|
item.update({'is_starred': True})
|
||||||
|
repos.extend(starred_repos)
|
||||||
|
|
||||||
|
if args.include_gists:
|
||||||
|
gists_template = 'https://{0}/gists'.format(get_github_api_host(args))
|
||||||
|
gists = retrieve_data(args, gists_template, single_request=False)
|
||||||
|
# flag each repo as a gist for downstream processing
|
||||||
|
for item in gists:
|
||||||
|
item.update({'is_gist': True})
|
||||||
|
repos.extend(gists)
|
||||||
|
|
||||||
|
if args.include_starred_gists:
|
||||||
|
starred_gists_template = 'https://{0}/gists/starred'.format(get_github_api_host(args))
|
||||||
|
starred_gists = retrieve_data(args, starred_gists_template, single_request=False)
|
||||||
|
# flag each repo as a starred gist for downstream processing
|
||||||
|
for item in starred_gists:
|
||||||
|
item.update({'is_gist': True,
|
||||||
|
'is_starred': True})
|
||||||
|
repos.extend(starred_gists)
|
||||||
|
|
||||||
|
return repos
|
||||||
|
|
||||||
|
|
||||||
def filter_repositories(args, unfiltered_repositories):
|
def filter_repositories(args, unfiltered_repositories):
|
||||||
@@ -467,7 +557,8 @@ def filter_repositories(args, unfiltered_repositories):
|
|||||||
|
|
||||||
repositories = []
|
repositories = []
|
||||||
for r in unfiltered_repositories:
|
for r in unfiltered_repositories:
|
||||||
if r['owner']['login'] == args.user:
|
# gists can be anonymous, so need to safely check owner
|
||||||
|
if r.get('owner', {}).get('login') == args.user or r.get('is_starred'):
|
||||||
repositories.append(r)
|
repositories.append(r)
|
||||||
|
|
||||||
name_regex = None
|
name_regex = None
|
||||||
@@ -479,11 +570,11 @@ def filter_repositories(args, unfiltered_repositories):
|
|||||||
languages = [x.lower() for x in args.languages]
|
languages = [x.lower() for x in args.languages]
|
||||||
|
|
||||||
if not args.fork:
|
if not args.fork:
|
||||||
repositories = [r for r in repositories if not r['fork']]
|
repositories = [r for r in repositories if not r.get('fork')]
|
||||||
if not args.private:
|
if not args.private:
|
||||||
repositories = [r for r in repositories if not r['private']]
|
repositories = [r for r in repositories if not r.get('private') or r.get('public')]
|
||||||
if languages:
|
if languages:
|
||||||
repositories = [r for r in repositories if r['language'] and r['language'].lower() in languages] # noqa
|
repositories = [r for r in repositories if r.get('language') and r.get('language').lower() in languages] # noqa
|
||||||
if name_regex:
|
if name_regex:
|
||||||
repositories = [r for r in repositories if name_regex.match(r['name'])]
|
repositories = [r for r in repositories if name_regex.match(r['name'])]
|
||||||
|
|
||||||
@@ -495,7 +586,7 @@ def backup_repositories(args, output_directory, repositories):
|
|||||||
repos_template = 'https://{0}/repos'.format(get_github_api_host(args))
|
repos_template = 'https://{0}/repos'.format(get_github_api_host(args))
|
||||||
|
|
||||||
if args.incremental:
|
if args.incremental:
|
||||||
last_update = max(repository['updated_at'] for repository in repositories) # noqa
|
last_update = max(list(repository['updated_at'] for repository in repositories) or [time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime())]) # noqa
|
||||||
last_update_path = os.path.join(output_directory, 'last_update')
|
last_update_path = os.path.join(output_directory, 'last_update')
|
||||||
if os.path.exists(last_update_path):
|
if os.path.exists(last_update_path):
|
||||||
args.since = open(last_update_path).read().strip()
|
args.since = open(last_update_path).read().strip()
|
||||||
@@ -505,17 +596,36 @@ def backup_repositories(args, output_directory, repositories):
|
|||||||
args.since = None
|
args.since = None
|
||||||
|
|
||||||
for repository in repositories:
|
for repository in repositories:
|
||||||
backup_cwd = os.path.join(output_directory, 'repositories')
|
if repository.get('is_gist'):
|
||||||
repo_cwd = os.path.join(backup_cwd, repository['name'])
|
repo_cwd = os.path.join(output_directory, 'gists', repository['id'])
|
||||||
|
elif repository.get('is_starred'):
|
||||||
|
# put starred repos in -o/starred/${owner}/${repo} to prevent collision of
|
||||||
|
# any repositories with the same name
|
||||||
|
repo_cwd = os.path.join(output_directory, 'starred', repository['owner']['login'], repository['name'])
|
||||||
|
else:
|
||||||
|
repo_cwd = os.path.join(output_directory, 'repositories', repository['name'])
|
||||||
|
|
||||||
repo_dir = os.path.join(repo_cwd, 'repository')
|
repo_dir = os.path.join(repo_cwd, 'repository')
|
||||||
repo_url = get_github_repo_url(args, repository)
|
repo_url = get_github_repo_url(args, repository)
|
||||||
|
|
||||||
if args.include_repository or args.include_everything:
|
include_gists = (args.include_gists or args.include_starred_gists)
|
||||||
fetch_repository(repository['name'],
|
if (args.include_repository or args.include_everything) \
|
||||||
|
or (include_gists and repository.get('is_gist')):
|
||||||
|
repo_name = repository.get('name') if not repository.get('is_gist') else repository.get('id')
|
||||||
|
fetch_repository(repo_name,
|
||||||
repo_url,
|
repo_url,
|
||||||
repo_dir,
|
repo_dir,
|
||||||
skip_existing=args.skip_existing,
|
skip_existing=args.skip_existing,
|
||||||
bare_clone=args.bare_clone)
|
bare_clone=args.bare_clone,
|
||||||
|
lfs_clone=args.lfs_clone)
|
||||||
|
|
||||||
|
if repository.get('is_gist'):
|
||||||
|
# dump gist information to a file as well
|
||||||
|
output_file = '{0}/gist.json'.format(repo_cwd)
|
||||||
|
with codecs.open(output_file, 'w', encoding='utf-8') as f:
|
||||||
|
json_dump(repository, f)
|
||||||
|
|
||||||
|
continue # don't try to back anything else for a gist; it doesn't exist
|
||||||
|
|
||||||
download_wiki = (args.include_wiki or args.include_everything)
|
download_wiki = (args.include_wiki or args.include_everything)
|
||||||
if repository['has_wiki'] and download_wiki:
|
if repository['has_wiki'] and download_wiki:
|
||||||
@@ -523,7 +633,8 @@ def backup_repositories(args, output_directory, repositories):
|
|||||||
repo_url.replace('.git', '.wiki.git'),
|
repo_url.replace('.git', '.wiki.git'),
|
||||||
os.path.join(repo_cwd, 'wiki'),
|
os.path.join(repo_cwd, 'wiki'),
|
||||||
skip_existing=args.skip_existing,
|
skip_existing=args.skip_existing,
|
||||||
bare_clone=args.bare_clone)
|
bare_clone=args.bare_clone,
|
||||||
|
lfs_clone=args.lfs_clone)
|
||||||
|
|
||||||
if args.include_issues or args.include_everything:
|
if args.include_issues or args.include_everything:
|
||||||
backup_issues(args, repo_cwd, repository, repos_template)
|
backup_issues(args, repo_cwd, repository, repos_template)
|
||||||
@@ -713,7 +824,8 @@ def fetch_repository(name,
|
|||||||
remote_url,
|
remote_url,
|
||||||
local_dir,
|
local_dir,
|
||||||
skip_existing=False,
|
skip_existing=False,
|
||||||
bare_clone=False):
|
bare_clone=False,
|
||||||
|
lfs_clone=False):
|
||||||
if bare_clone:
|
if bare_clone:
|
||||||
if os.path.exists(local_dir):
|
if os.path.exists(local_dir):
|
||||||
clone_exists = subprocess.check_output(['git',
|
clone_exists = subprocess.check_output(['git',
|
||||||
@@ -744,7 +856,7 @@ def fetch_repository(name,
|
|||||||
|
|
||||||
remotes = subprocess.check_output(['git', 'remote', 'show'],
|
remotes = subprocess.check_output(['git', 'remote', 'show'],
|
||||||
cwd=local_dir)
|
cwd=local_dir)
|
||||||
remotes = [i.strip() for i in remotes.decode('utf-8')]
|
remotes = [i.strip() for i in remotes.decode('utf-8').splitlines()]
|
||||||
|
|
||||||
if 'origin' not in remotes:
|
if 'origin' not in remotes:
|
||||||
git_command = ['git', 'remote', 'rm', 'origin']
|
git_command = ['git', 'remote', 'rm', 'origin']
|
||||||
@@ -755,7 +867,10 @@ def fetch_repository(name,
|
|||||||
git_command = ['git', 'remote', 'set-url', 'origin', remote_url]
|
git_command = ['git', 'remote', 'set-url', 'origin', remote_url]
|
||||||
logging_subprocess(git_command, None, cwd=local_dir)
|
logging_subprocess(git_command, None, cwd=local_dir)
|
||||||
|
|
||||||
git_command = ['git', 'fetch', '--all', '--force', '--tags', '--prune']
|
if lfs_clone:
|
||||||
|
git_command = ['git', 'lfs', 'fetch', '--all', '--force', '--tags', '--prune']
|
||||||
|
else:
|
||||||
|
git_command = ['git', 'fetch', '--all', '--force', '--tags', '--prune']
|
||||||
logging_subprocess(git_command, None, cwd=local_dir)
|
logging_subprocess(git_command, None, cwd=local_dir)
|
||||||
else:
|
else:
|
||||||
log_info('Cloning {0} repository from {1} to {2}'.format(
|
log_info('Cloning {0} repository from {1} to {2}'.format(
|
||||||
@@ -763,9 +878,15 @@ def fetch_repository(name,
|
|||||||
masked_remote_url,
|
masked_remote_url,
|
||||||
local_dir))
|
local_dir))
|
||||||
if bare_clone:
|
if bare_clone:
|
||||||
git_command = ['git', 'clone', '--mirror', remote_url, local_dir]
|
if lfs_clone:
|
||||||
|
git_command = ['git', 'lfs', 'clone', '--mirror', remote_url, local_dir]
|
||||||
|
else:
|
||||||
|
git_command = ['git', 'clone', '--mirror', remote_url, local_dir]
|
||||||
else:
|
else:
|
||||||
git_command = ['git', 'clone', remote_url, local_dir]
|
if lfs_clone:
|
||||||
|
git_command = ['git', 'lfs', 'clone', remote_url, local_dir]
|
||||||
|
else:
|
||||||
|
git_command = ['git', 'clone', remote_url, local_dir]
|
||||||
logging_subprocess(git_command, None)
|
logging_subprocess(git_command, None)
|
||||||
|
|
||||||
|
|
||||||
@@ -773,25 +894,41 @@ def backup_account(args, output_directory):
|
|||||||
account_cwd = os.path.join(output_directory, 'account')
|
account_cwd = os.path.join(output_directory, 'account')
|
||||||
|
|
||||||
if args.include_starred or args.include_everything:
|
if args.include_starred or args.include_everything:
|
||||||
output_file = '{0}/starred.json'.format(account_cwd)
|
output_file = "{0}/starred.json".format(account_cwd)
|
||||||
template = "https://{0}/users/{1}/starred"
|
template = "https://{0}/users/{1}/starred".format(get_github_api_host(args), args.user)
|
||||||
template = template.format(get_github_api_host(args), args.user)
|
|
||||||
_backup_data(args,
|
_backup_data(args,
|
||||||
'starred repositories',
|
"starred repositories",
|
||||||
template,
|
template,
|
||||||
output_file,
|
output_file,
|
||||||
account_cwd)
|
account_cwd)
|
||||||
|
|
||||||
if args.include_watched or args.include_everything:
|
if args.include_watched or args.include_everything:
|
||||||
output_file = '{0}/watched.json'.format(account_cwd)
|
output_file = "{0}/watched.json".format(account_cwd)
|
||||||
template = "https://{0}/users/{1}/subscriptions"
|
template = "https://{0}/users/{1}/subscriptions".format(get_github_api_host(args), args.user)
|
||||||
template = template.format(get_github_api_host(args), args.user)
|
|
||||||
_backup_data(args,
|
_backup_data(args,
|
||||||
'watched repositories',
|
"watched repositories",
|
||||||
template,
|
template,
|
||||||
output_file,
|
output_file,
|
||||||
account_cwd)
|
account_cwd)
|
||||||
|
|
||||||
|
if args.include_followers or args.include_everything:
|
||||||
|
output_file = "{0}/followers.json".format(account_cwd)
|
||||||
|
template = "https://{0}/users/{1}/followers".format(get_github_api_host(args), args.user)
|
||||||
|
_backup_data(args,
|
||||||
|
"followers",
|
||||||
|
template,
|
||||||
|
output_file,
|
||||||
|
account_cwd)
|
||||||
|
|
||||||
|
if args.include_following or args.include_everything:
|
||||||
|
output_file = "{0}/following.json".format(account_cwd)
|
||||||
|
template = "https://{0}/users/{1}/following".format(get_github_api_host(args), args.user)
|
||||||
|
_backup_data(args,
|
||||||
|
"following",
|
||||||
|
template,
|
||||||
|
output_file,
|
||||||
|
account_cwd)
|
||||||
|
|
||||||
|
|
||||||
def _backup_data(args, name, template, output_file, output_directory):
|
def _backup_data(args, name, template, output_file, output_directory):
|
||||||
skip_existing = args.skip_existing
|
skip_existing = args.skip_existing
|
||||||
@@ -822,6 +959,9 @@ def main():
|
|||||||
log_info('Create output directory {0}'.format(output_directory))
|
log_info('Create output directory {0}'.format(output_directory))
|
||||||
mkdir_p(output_directory)
|
mkdir_p(output_directory)
|
||||||
|
|
||||||
|
if args.lfs_clone:
|
||||||
|
check_git_lfs_install()
|
||||||
|
|
||||||
log_info('Backing up user {0} to {1}'.format(args.user, output_directory))
|
log_info('Backing up user {0} to {1}'.format(args.user, output_directory))
|
||||||
|
|
||||||
repositories = retrieve_repositories(args)
|
repositories = retrieve_repositories(args)
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__version__ = '0.12.1'
|
__version__ = '0.18.0'
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -39,6 +39,8 @@ setup(
|
|||||||
'License :: OSI Approved :: MIT License',
|
'License :: OSI Approved :: MIT License',
|
||||||
'Programming Language :: Python :: 2.6',
|
'Programming Language :: Python :: 2.6',
|
||||||
'Programming Language :: Python :: 2.7',
|
'Programming Language :: Python :: 2.7',
|
||||||
|
'Programming Language :: Python :: 3.5',
|
||||||
|
'Programming Language :: Python :: 3.6',
|
||||||
],
|
],
|
||||||
description='backup a github user or organization',
|
description='backup a github user or organization',
|
||||||
long_description=open_file('README.rst').read(),
|
long_description=open_file('README.rst').read(),
|
||||||
|
|||||||
Reference in New Issue
Block a user