33import urllib2
44import sys
55import os
6+ import optparse
67
78GITHUB_REPOS_API_BASE_URL = 'https://api.github.com/repos/'
89
10+
911def 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+
1922def 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
3135if __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