-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-get.py
executable file
·53 lines (43 loc) · 1.55 KB
/
gh-get.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# gh-get.py
#
"""Download a single file from a GitHub repository."""
import shutil
from os.path import expanduser
from posixpath import basename
import requests
import yaml
GITHUB_CONFIG = '~/.config/hub'
ENDPOINT = 'https://api.github.com/repos/{user}/{repo}/contents/{path}'
API_TYPE = 'application/vnd.github.v4.raw'
def main(args=None):
try:
user, repo, path = args.pop(0).split('/', 2)
except (IndexError, ValueError):
return "Usage: gh-get.py <gh-user>/<repo>/<path/to/file> [<dest>]"
else:
destfn = args.pop(0) if args else basename(path)
try:
with open(expanduser(GITHUB_CONFIG)) as fp:
hub_config = yaml.load(fp.read())
access_token = hub_config['github.com'][0]['oauth_token']
except OSError as exc:
return "Could not read configuration file '{}': {}".format(
GITHUB_CONFIG, exc)
except (IndexError, KeyError):
return "Access token not found in configuration file."
headers = dict(Authorization='token {}'.format(access_token),
Accept=API_TYPE)
url = ENDPOINT.format(path=path, repo=repo, user=user)
try:
with requests.get(url, headers=headers, stream=True) as req:
req.raise_for_status()
with open(destfn, 'wb') as fp:
shutil.copyfileobj(req.raw, fp)
except (requests.HTTPError, OSError) as exc:
return "Error downloading file: {}".format(exc)
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv[1:]) or 0)