Skip to content

Commit f6efe3e

Browse files
author
Lamine Sissoko
committed
adding command line options for filepath, branch, and recursive
1 parent e434dfc commit f6efe3e

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

get_git_sub_dir.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import urllib2
44
import sys
55
import os
6+
import optparse
67

78
GITHUB_REPOS_API_BASE_URL = 'https://api.github.com/repos/'
89

10+
911
def write_file(item, dir_name):
1012
name = item['name']
1113
res = urllib2.urlopen(item['url']).read()
@@ -16,6 +18,7 @@ def write_file(item, dir_name):
1618
f.write(contents)
1719
f.close()
1820

21+
1922
def write_files(url, dir_name, recursive=True):
2023

2124
print 'url', url
@@ -25,27 +28,35 @@ def write_files(url, dir_name, recursive=True):
2528
if item['type'] == 'file':
2629
write_file(item, dir_name)
2730
elif item['type'] == 'dir':
28-
write_files(item['url'], dir_name=os.path.join(dir_name, item['name']))
31+
write_files(item['url'], dir_name=os.path.join(
32+
dir_name, item['name']))
2933

3034

3135
if __name__ == '__main__':
32-
args = dict(enumerate(sys.argv))
36+
parser = optparse.OptionParser()
37+
parser.add_option("-r", action="store")
38+
parser.add_option("-p", action="store")
39+
parser.add_option("-b", action="store")
40+
41+
options, args = parser.parse_args()
42+
3343
path = 'mfbx9da4/blog/server'
34-
path = args[1]
44+
path = args[0]
3545
path = path.split('/')
36-
46+
3747
new_dir_name = path[-1]
3848
if os.path.exists(new_dir_name):
3949
raise 'Directory', new_dir_name, 'already exists'
40-
50+
4151
# use contents api
42-
path.insert(2, 'contents')
52+
path.append("contents")
53+
if options.p:
54+
path.append(options.p) # filepath
55+
if options.b:
56+
path.append("?ref=" + options.b) # git branch
4357
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)
47-
48-
4958

59+
recursive = eval(options.r) if options.r else True
5060

51-
61+
write_files(GITHUB_REPOS_API_BASE_URL + path,
62+
new_dir_name, recursive=recursive)

0 commit comments

Comments
 (0)