githubtakeout 0.1.0__tar.gz → 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: githubtakeout
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Archive public Git Repos and Gists from GitHub
5
5
  Author: Corey Goldberg
6
6
  Maintainer: Corey Goldberg
@@ -27,7 +27,7 @@ Dynamic: license-file
27
27
 
28
28
  # githubtakeout
29
29
 
30
- ## Archive public Git Repos and Gists from GitHub
30
+ ## Archive Git Repos and Gists from GitHub
31
31
 
32
32
  ---
33
33
 
@@ -41,11 +41,11 @@ Dynamic: license-file
41
41
  ## About:
42
42
 
43
43
  `githubtakeout` is a data export tool for archiving Git repositories hosted on GitHub.
44
- It clones a user's public repos and creates an archive of each.
44
+ It clones a user's repos and creates an archive of each.
45
45
 
46
- By default, it doesn't save commit history or branches (`.git` directory), or Gist
47
- repositories (both can be enabled with command line options). It also doesn't access
48
- private repositories or secret gists.
46
+ It supports public/private repos and public/secret gists. By default, it doesn't save
47
+ commit history or branches (`.git` directory), or Gist repositories (both can be enabled
48
+ with command line options).
49
49
 
50
50
  When you run the program, archives of your repos will be saved in a directory named
51
51
  `backups` inside your current working directory, unless a different location is specified
@@ -62,38 +62,32 @@ as tarballs (`.tar.gz`) using the `--format=tar` option.
62
62
  - GitPython
63
63
  - PyGithub
64
64
 
65
- ## Installing and Running:
65
+ ## Installation:
66
66
 
67
- ### Install from PyPI:
67
+ Install from [PyPI](https://pypi.org/project/githubtakeout):
68
68
 
69
69
  ```
70
70
  pip install githubtakeout
71
71
  ```
72
72
 
73
- ### Create/Activate Virtual Environment, Install from PyPI, Run:
73
+ ## Authentication:
74
74
 
75
- ```
76
- python3 -m venv venv
77
- source venv/bin/activate
78
- pip install githubtakeout
79
- githubtakeout <github username>
80
- ```
75
+ By default, `githubtakeout` will only retrieve an account's public repos. To access
76
+ private repos and secret gists, you need to authenticate.
81
77
 
82
- ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
78
+ First, you must create a [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)
79
+ on Github (either a fine-grained or classic personal access token). Once you have a token, you can set the `GITHUB_TOKEN` environment variable:
83
80
 
84
81
  ```
85
- git clone git@github.com:cgoldberg/githubtakeout.git
86
- cd ./githubtakeout
87
- python3 -m venv venv
88
- source venv/bin/activate
89
- pip install .
90
- githubtakeout <github username>
82
+ $ export GITHUB_TOKEN=<auth token created on GitHub>
91
83
  ```
92
84
 
93
- ### Usage:
85
+ If you prefer to be prompted for your token each time you run the program, use the `--token` argument.
86
+
87
+ ### CLI Options:
94
88
 
95
89
  ```
96
- usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] username
90
+ usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] [--token] username
97
91
 
98
92
  positional arguments:
99
93
  username GitHub username
@@ -105,4 +99,28 @@ options:
105
99
  --gists include gists
106
100
  --history include commit history and branches (.git directory)
107
101
  --list list repos only
102
+ --token prompt for auth toke
103
+ ```
104
+
105
+
106
+ ## Usage Examples:
107
+
108
+ ### Create/Activate Virtual Environment, Install from PyPI, Run:
109
+
110
+ ```
111
+ python3 -m venv venv
112
+ source venv/bin/activate
113
+ pip install githubtakeout
114
+ githubtakeout <github username>
115
+ ```
116
+
117
+ ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
118
+
119
+ ```
120
+ git clone git@github.com:cgoldberg/githubtakeout.git
121
+ cd ./githubtakeout
122
+ python3 -m venv venv
123
+ source venv/bin/activate
124
+ pip install .
125
+ githubtakeout <github username>
108
126
  ```
@@ -1,6 +1,6 @@
1
1
  # githubtakeout
2
2
 
3
- ## Archive public Git Repos and Gists from GitHub
3
+ ## Archive Git Repos and Gists from GitHub
4
4
 
5
5
  ---
6
6
 
@@ -14,11 +14,11 @@
14
14
  ## About:
15
15
 
16
16
  `githubtakeout` is a data export tool for archiving Git repositories hosted on GitHub.
17
- It clones a user's public repos and creates an archive of each.
17
+ It clones a user's repos and creates an archive of each.
18
18
 
19
- By default, it doesn't save commit history or branches (`.git` directory), or Gist
20
- repositories (both can be enabled with command line options). It also doesn't access
21
- private repositories or secret gists.
19
+ It supports public/private repos and public/secret gists. By default, it doesn't save
20
+ commit history or branches (`.git` directory), or Gist repositories (both can be enabled
21
+ with command line options).
22
22
 
23
23
  When you run the program, archives of your repos will be saved in a directory named
24
24
  `backups` inside your current working directory, unless a different location is specified
@@ -35,38 +35,32 @@ as tarballs (`.tar.gz`) using the `--format=tar` option.
35
35
  - GitPython
36
36
  - PyGithub
37
37
 
38
- ## Installing and Running:
38
+ ## Installation:
39
39
 
40
- ### Install from PyPI:
40
+ Install from [PyPI](https://pypi.org/project/githubtakeout):
41
41
 
42
42
  ```
43
43
  pip install githubtakeout
44
44
  ```
45
45
 
46
- ### Create/Activate Virtual Environment, Install from PyPI, Run:
46
+ ## Authentication:
47
47
 
48
- ```
49
- python3 -m venv venv
50
- source venv/bin/activate
51
- pip install githubtakeout
52
- githubtakeout <github username>
53
- ```
48
+ By default, `githubtakeout` will only retrieve an account's public repos. To access
49
+ private repos and secret gists, you need to authenticate.
54
50
 
55
- ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
51
+ First, you must create a [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)
52
+ on Github (either a fine-grained or classic personal access token). Once you have a token, you can set the `GITHUB_TOKEN` environment variable:
56
53
 
57
54
  ```
58
- git clone git@github.com:cgoldberg/githubtakeout.git
59
- cd ./githubtakeout
60
- python3 -m venv venv
61
- source venv/bin/activate
62
- pip install .
63
- githubtakeout <github username>
55
+ $ export GITHUB_TOKEN=<auth token created on GitHub>
64
56
  ```
65
57
 
66
- ### Usage:
58
+ If you prefer to be prompted for your token each time you run the program, use the `--token` argument.
59
+
60
+ ### CLI Options:
67
61
 
68
62
  ```
69
- usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] username
63
+ usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] [--token] username
70
64
 
71
65
  positional arguments:
72
66
  username GitHub username
@@ -78,4 +72,28 @@ options:
78
72
  --gists include gists
79
73
  --history include commit history and branches (.git directory)
80
74
  --list list repos only
75
+ --token prompt for auth toke
76
+ ```
77
+
78
+
79
+ ## Usage Examples:
80
+
81
+ ### Create/Activate Virtual Environment, Install from PyPI, Run:
82
+
83
+ ```
84
+ python3 -m venv venv
85
+ source venv/bin/activate
86
+ pip install githubtakeout
87
+ githubtakeout <github username>
88
+ ```
89
+
90
+ ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
91
+
92
+ ```
93
+ git clone git@github.com:cgoldberg/githubtakeout.git
94
+ cd ./githubtakeout
95
+ python3 -m venv venv
96
+ source venv/bin/activate
97
+ pip install .
98
+ githubtakeout <github username>
81
99
  ```
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: githubtakeout
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Archive public Git Repos and Gists from GitHub
5
5
  Author: Corey Goldberg
6
6
  Maintainer: Corey Goldberg
@@ -27,7 +27,7 @@ Dynamic: license-file
27
27
 
28
28
  # githubtakeout
29
29
 
30
- ## Archive public Git Repos and Gists from GitHub
30
+ ## Archive Git Repos and Gists from GitHub
31
31
 
32
32
  ---
33
33
 
@@ -41,11 +41,11 @@ Dynamic: license-file
41
41
  ## About:
42
42
 
43
43
  `githubtakeout` is a data export tool for archiving Git repositories hosted on GitHub.
44
- It clones a user's public repos and creates an archive of each.
44
+ It clones a user's repos and creates an archive of each.
45
45
 
46
- By default, it doesn't save commit history or branches (`.git` directory), or Gist
47
- repositories (both can be enabled with command line options). It also doesn't access
48
- private repositories or secret gists.
46
+ It supports public/private repos and public/secret gists. By default, it doesn't save
47
+ commit history or branches (`.git` directory), or Gist repositories (both can be enabled
48
+ with command line options).
49
49
 
50
50
  When you run the program, archives of your repos will be saved in a directory named
51
51
  `backups` inside your current working directory, unless a different location is specified
@@ -62,38 +62,32 @@ as tarballs (`.tar.gz`) using the `--format=tar` option.
62
62
  - GitPython
63
63
  - PyGithub
64
64
 
65
- ## Installing and Running:
65
+ ## Installation:
66
66
 
67
- ### Install from PyPI:
67
+ Install from [PyPI](https://pypi.org/project/githubtakeout):
68
68
 
69
69
  ```
70
70
  pip install githubtakeout
71
71
  ```
72
72
 
73
- ### Create/Activate Virtual Environment, Install from PyPI, Run:
73
+ ## Authentication:
74
74
 
75
- ```
76
- python3 -m venv venv
77
- source venv/bin/activate
78
- pip install githubtakeout
79
- githubtakeout <github username>
80
- ```
75
+ By default, `githubtakeout` will only retrieve an account's public repos. To access
76
+ private repos and secret gists, you need to authenticate.
81
77
 
82
- ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
78
+ First, you must create a [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)
79
+ on Github (either a fine-grained or classic personal access token). Once you have a token, you can set the `GITHUB_TOKEN` environment variable:
83
80
 
84
81
  ```
85
- git clone git@github.com:cgoldberg/githubtakeout.git
86
- cd ./githubtakeout
87
- python3 -m venv venv
88
- source venv/bin/activate
89
- pip install .
90
- githubtakeout <github username>
82
+ $ export GITHUB_TOKEN=<auth token created on GitHub>
91
83
  ```
92
84
 
93
- ### Usage:
85
+ If you prefer to be prompted for your token each time you run the program, use the `--token` argument.
86
+
87
+ ### CLI Options:
94
88
 
95
89
  ```
96
- usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] username
90
+ usage: githubtakeout [-h] [--dir DIR] [--format FORMAT] [--gists] [--history] [--list] [--token] username
97
91
 
98
92
  positional arguments:
99
93
  username GitHub username
@@ -105,4 +99,28 @@ options:
105
99
  --gists include gists
106
100
  --history include commit history and branches (.git directory)
107
101
  --list list repos only
102
+ --token prompt for auth toke
103
+ ```
104
+
105
+
106
+ ## Usage Examples:
107
+
108
+ ### Create/Activate Virtual Environment, Install from PyPI, Run:
109
+
110
+ ```
111
+ python3 -m venv venv
112
+ source venv/bin/activate
113
+ pip install githubtakeout
114
+ githubtakeout <github username>
115
+ ```
116
+
117
+ ### Clone Repo, Create/Activate Virtual Environment, Install from Source, Run:
118
+
119
+ ```
120
+ git clone git@github.com:cgoldberg/githubtakeout.git
121
+ cd ./githubtakeout
122
+ python3 -m venv venv
123
+ source venv/bin/activate
124
+ pip install .
125
+ githubtakeout <github username>
108
126
  ```
@@ -1,10 +1,12 @@
1
1
  #!/usr/bin/env python3
2
2
 
3
3
  import argparse
4
+ import getpass
4
5
  import logging
5
6
  import os
6
7
  import shutil
7
8
  import tarfile
9
+ import urllib
8
10
  import zipfile
9
11
 
10
12
  from pathlib import Path
@@ -18,6 +20,17 @@ logging.basicConfig(level=logging.INFO, format='%(message)s')
18
20
  logger = logging.getLogger(__name__)
19
21
 
20
22
 
23
+ def add_creds(url, username, token):
24
+ if token is None:
25
+ new_url = url
26
+ else:
27
+ username = urllib.parse.quote(username)
28
+ url_parts = urllib.parse.urlparse(url)
29
+ netloc = f'{username}:{token}@{url_parts.netloc}'
30
+ new_url = urllib.parse.urlunparse(url_parts._replace(netloc=netloc))
31
+ return new_url
32
+
33
+
21
34
  def archive(local_repo_dir, archive_format='zip', is_gist=False):
22
35
  if archive_format not in ('tar', 'zip'):
23
36
  raise ValueError(f'{archive_format} is not a valid archive format')
@@ -46,7 +59,8 @@ def clone_and_archive_repo(repo_url, local_repo_dir, archive_format, include_his
46
59
  shutil.rmtree(local_repo_dir)
47
60
  except FileNotFoundError:
48
61
  pass
49
- logger.info(f'cloning repo: {repo_url} to {local_repo_dir}')
62
+ repo_path = urllib.parse.urlparse(repo_url).path
63
+ logger.info(f'cloning repo: {repo_path} to {local_repo_dir}')
50
64
  start = default_timer()
51
65
  try:
52
66
  if include_history:
@@ -73,48 +87,88 @@ def clone_and_archive_repo(repo_url, local_repo_dir, archive_format, include_his
73
87
  pass
74
88
  logger.info('')
75
89
 
76
- def run(username, base_dir, archive_format, include_gists, include_history, list):
90
+
91
+ def get_user(username, prompt_token):
92
+ if prompt_token:
93
+ token = getpass.getpass('Token:')
94
+ if not token:
95
+ print(f'Error: auth token cannot be empty')
96
+ exit(1)
97
+ auth = github.Auth.Token(token)
98
+ gh = github.Github(auth=auth)
99
+ else:
100
+ token = os.getenv('GITHUB_TOKEN')
101
+ if token is not None:
102
+ auth = github.Auth.Token(token)
103
+ gh = github.Github(auth=auth)
104
+ else:
105
+ gh = github.Github()
106
+ if token is None:
107
+ try:
108
+ user = gh.get_user(username)
109
+ except github.GithubException as e:
110
+ if e.data['status'] == '404':
111
+ print(f'Error: user "{username}" not found on GitHub')
112
+ else:
113
+ raise e
114
+ exit(1)
115
+ repos = user.get_repos()
116
+ else:
117
+ # you need to be authenticated and then call the API
118
+ # with no username to get private repos
119
+ user = gh.get_user()
120
+ repos = user.get_repos(affiliation='owner')
121
+ try:
122
+ # this just makes an API request so we can exit if unauthorized
123
+ repos.totalCount
124
+ except github.GithubException as e:
125
+ if e.data['status'] == '401':
126
+ print(f'Error: invalid auth token for user "{username}" on GitHub')
127
+ else:
128
+ raise e
129
+ exit(1)
130
+ gists = user.get_gists()
131
+ return user, repos, gists, token
132
+
133
+
134
+ def run(username, base_dir, archive_format, include_gists, include_history, list, prompt_token):
77
135
  working_dir = os.path.join(base_dir, 'backups')
78
- gh = github.Github()
79
- try:
80
- user = gh.get_user(username)
81
- except github.GithubException:
82
- print(f'Error: user "{username}" not found on GitHub')
83
- exit(1)
84
- repos = user.get_repos()
85
- num_repos = len([1 for _ in repos])
136
+ user, repos, gists, token = get_user(username, prompt_token)
137
+ num_repos = repos.totalCount
86
138
  if not list:
87
139
  logger.info(f'creating archives in: {working_dir}\n')
88
140
  logger.info(f'found {num_repos} repos for user "{username}"\n')
89
141
  for repo in repos:
90
142
  local_repo_dir = os.path.join(working_dir, repo.name)
143
+ url = add_creds(repo.clone_url, username, token)
91
144
  if list:
92
- logger.info(repo.clone_url)
145
+ logger.info(url)
93
146
  else:
94
147
  clone_and_archive_repo(
95
- repo.clone_url,
148
+ url,
96
149
  local_repo_dir,
97
150
  archive_format,
98
151
  include_history
99
152
  )
100
153
  if include_gists:
101
- gists = user.get_gists()
102
- num_gists = len([1 for _ in gists])
154
+ num_gists = gists.totalCount
103
155
  logger.info('')
104
156
  logger.info(f'found {num_gists} gists for user "{username}"\n')
105
157
  for gist in gists:
106
158
  local_repo_dir = os.path.join(working_dir, gist.id)
159
+ url = add_creds(gist.git_pull_url, username, token)
107
160
  if list:
108
- logger.info(gist.git_pull_url)
161
+ logger.info(url)
109
162
  else:
110
163
  clone_and_archive_repo(
111
- gist.git_pull_url,
164
+ url,
112
165
  local_repo_dir,
113
166
  archive_format,
114
167
  include_history,
115
168
  is_gist=True
116
169
  )
117
170
 
171
+
118
172
  def main():
119
173
  parser = argparse.ArgumentParser()
120
174
  parser.add_argument(
@@ -149,6 +203,12 @@ def main():
149
203
  action='store_true',
150
204
  help='list repos only'
151
205
  )
206
+ parser.add_argument(
207
+ '--token',
208
+ default=False,
209
+ action='store_true',
210
+ help='prompt for auth token'
211
+ )
152
212
  args = parser.parse_args()
153
213
  run(
154
214
  username=args.username,
@@ -156,7 +216,8 @@ def main():
156
216
  archive_format=args.format,
157
217
  include_gists=args.gists,
158
218
  include_history=args.history,
159
- list=args.list
219
+ list=args.list,
220
+ prompt_token=args.token
160
221
  )
161
222
 
162
223
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "githubtakeout"
7
- version = "0.1.0"
7
+ version = "0.1.1"
8
8
  description = "Archive public Git Repos and Gists from GitHub"
9
9
  license = "MIT"
10
10
  license-files = ["LICENSE"]
File without changes
File without changes