Skip to content

Commit e7a0a5b

Browse files
authored
Merge pull request #4 from lsissoko/master
Options flags for branch and private repo credentials
2 parents e434dfc + 3052e8d commit e7a0a5b

File tree

2 files changed

+66
-27
lines changed

2 files changed

+66
-27
lines changed

README.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
Downloads git sub dir
22

33
##Usage
4-
5-
6-
python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>
7-
8-
<RECURSIVE> is a boolen `True` or `False`. Default is `True`.
4+
python get_git_sub_dir.py user/repo <options>
5+
python get_git_sub_dir.py user/private_repo --private <options>
96

7+
##Options Flags:
8+
- `--private`: the repo is private (default is `False`, username and password will be requested)
9+
- `-r`: recursive download (default is `True`)
10+
- `-p`: filepath
11+
- `-b`: branch
1012

1113
##Example
1214

13-
Lets download the docs from twitter bootstrap https://github.com/twbs/bootstrap/tree/master/docs
15+
Let's download the docs from twitter bootstrap https://github.com/twbs/bootstrap/tree/master/docs
1416

15-
python get_git_sub_dir.py twbs/bootstrap/docs
17+
python get_git_sub_dir.py twbs/bootstrap -p docs
1618

1719
If we don't want it to be recursive
1820

19-
python get_git_sub_dir.py twbs/bootstrap/docs False
21+
python get_git_sub_dir.py twbs/bootstrap -p docs -r False
22+
23+
If we want a specific file
24+
25+
python get_git_sub_dir.py twbs/bootstrap -p docs/examples/blog/index.html
26+
27+
If we want to download from a specific branch (say `fix-15534`)
28+
29+
python get_git_sub_dir.py twbs/bootstrap -p docs/examples/blog/index.html -b fix-15534

get_git_sub_dir.py

+48-19
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,78 @@
33
import urllib2
44
import sys
55
import os
6+
import optparse
67

78
GITHUB_REPOS_API_BASE_URL = 'https://api.github.com/repos/'
9+
USERNAME = ""
10+
PASSWORD = ""
811

9-
def write_file(item, dir_name):
12+
13+
def read_url(url, private=False):
14+
if private:
15+
request = urllib2.Request(url)
16+
base64string = base64.encodestring(
17+
'%s:%s' % (USERNAME, PASSWORD)).replace('\n', '')
18+
request.add_header("Authorization", "Basic %s" % base64string)
19+
return urllib2.urlopen(request).read()
20+
else:
21+
return urllib2.urlopen(url).read()
22+
23+
24+
def write_file(item, dir_name, private=False):
1025
name = item['name']
11-
res = urllib2.urlopen(item['url']).read()
26+
res = read_url(item['url'], private)
1227
coded_string = json.loads(res)['content']
1328
contents = base64.b64decode(coded_string)
1429
print os.path.join(dir_name, name)
15-
f = open(os.path.join(dir_name, name), 'w')
16-
f.write(contents)
17-
f.close()
30+
with open(os.path.join(dir_name, name), 'w') as f:
31+
f.write(contents)
1832

19-
def write_files(url, dir_name, recursive=True):
2033

34+
def write_files(url, dir_name, recursive=True, private=False):
2135
print 'url', url
2236
os.makedirs(dir_name)
23-
github_dir = json.loads(urllib2.urlopen(url).read())
37+
38+
github_dir = json.loads(read_url(url, private))
2439
for item in github_dir:
2540
if item['type'] == 'file':
26-
write_file(item, dir_name)
41+
write_file(item, dir_name, private)
2742
elif item['type'] == 'dir':
28-
write_files(item['url'], dir_name=os.path.join(dir_name, item['name']))
43+
write_files(item['url'], dir_name=os.path.join(
44+
dir_name, item['name']))
2945

3046

3147
if __name__ == '__main__':
32-
args = dict(enumerate(sys.argv))
33-
path = 'mfbx9da4/blog/server'
34-
path = args[1]
48+
parser = optparse.OptionParser()
49+
parser.add_option("--private", action="store_true", default=False)
50+
parser.add_option("-r", action="store")
51+
parser.add_option("-p", action="store")
52+
parser.add_option("-b", action="store")
53+
54+
options, args = parser.parse_args()
55+
56+
path = args[0]
3557
path = path.split('/')
36-
58+
3759
new_dir_name = path[-1]
3860
if os.path.exists(new_dir_name):
3961
raise 'Directory', new_dir_name, 'already exists'
40-
62+
4163
# use contents api
42-
path.insert(2, 'contents')
64+
path.append("contents")
65+
if options.p:
66+
path.append(options.p) # filepath
67+
if options.b:
68+
path.append("?ref=" + options.b) # git branch
4369
path = '/'.join(path)
44-
45-
recursive = eval(args.get(2)) if args.get(2) else True
46-
write_files(GITHUB_REPOS_API_BASE_URL + path, new_dir_name, recursive=recursive)
4770

71+
recursive = eval(options.r) if options.r else True
4872

73+
private = True if options.private else False
4974

75+
if private:
76+
USERNAME = raw_input("username: ")
77+
PASSWORD = raw_input("password: ")
5078

51-
79+
write_files(GITHUB_REPOS_API_BASE_URL + path,
80+
new_dir_name, recursive=recursive, private=private)

0 commit comments

Comments
 (0)