Skip to content

Commit

Permalink
Merge pull request #4 from lsissoko/master
Browse files Browse the repository at this point in the history
Options flags for branch and private repo credentials
  • Loading branch information
mfbx9da4 authored Jan 13, 2017
2 parents e434dfc + 3052e8d commit e7a0a5b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
Downloads git sub dir

##Usage


python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>

<RECURSIVE> is a boolen `True` or `False`. Default is `True`.
python get_git_sub_dir.py user/repo <options>
python get_git_sub_dir.py user/private_repo --private <options>

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

##Example

Lets download the docs from twitter bootstrap https://github.com/twbs/bootstrap/tree/master/docs
Let's download the docs from twitter bootstrap https://github.com/twbs/bootstrap/tree/master/docs

python get_git_sub_dir.py twbs/bootstrap/docs
python get_git_sub_dir.py twbs/bootstrap -p docs

If we don't want it to be recursive

python get_git_sub_dir.py twbs/bootstrap/docs False
python get_git_sub_dir.py twbs/bootstrap -p docs -r False

If we want a specific file

python get_git_sub_dir.py twbs/bootstrap -p docs/examples/blog/index.html

If we want to download from a specific branch (say `fix-15534`)

python get_git_sub_dir.py twbs/bootstrap -p docs/examples/blog/index.html -b fix-15534
67 changes: 48 additions & 19 deletions get_git_sub_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,78 @@
import urllib2
import sys
import os
import optparse

GITHUB_REPOS_API_BASE_URL = 'https://api.github.com/repos/'
USERNAME = ""
PASSWORD = ""

def write_file(item, dir_name):

def read_url(url, private=False):
if private:
request = urllib2.Request(url)
base64string = base64.encodestring(
'%s:%s' % (USERNAME, PASSWORD)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(request).read()
else:
return urllib2.urlopen(url).read()


def write_file(item, dir_name, private=False):
name = item['name']
res = urllib2.urlopen(item['url']).read()
res = read_url(item['url'], private)
coded_string = json.loads(res)['content']
contents = base64.b64decode(coded_string)
print os.path.join(dir_name, name)
f = open(os.path.join(dir_name, name), 'w')
f.write(contents)
f.close()
with open(os.path.join(dir_name, name), 'w') as f:
f.write(contents)

def write_files(url, dir_name, recursive=True):

def write_files(url, dir_name, recursive=True, private=False):
print 'url', url
os.makedirs(dir_name)
github_dir = json.loads(urllib2.urlopen(url).read())

github_dir = json.loads(read_url(url, private))
for item in github_dir:
if item['type'] == 'file':
write_file(item, dir_name)
write_file(item, dir_name, private)
elif item['type'] == 'dir':
write_files(item['url'], dir_name=os.path.join(dir_name, item['name']))
write_files(item['url'], dir_name=os.path.join(
dir_name, item['name']))


if __name__ == '__main__':
args = dict(enumerate(sys.argv))
path = 'mfbx9da4/blog/server'
path = args[1]
parser = optparse.OptionParser()
parser.add_option("--private", action="store_true", default=False)
parser.add_option("-r", action="store")
parser.add_option("-p", action="store")
parser.add_option("-b", action="store")

options, args = parser.parse_args()

path = args[0]
path = path.split('/')

new_dir_name = path[-1]
if os.path.exists(new_dir_name):
raise 'Directory', new_dir_name, 'already exists'

# use contents api
path.insert(2, 'contents')
path.append("contents")
if options.p:
path.append(options.p) # filepath
if options.b:
path.append("?ref=" + options.b) # git branch
path = '/'.join(path)

recursive = eval(args.get(2)) if args.get(2) else True
write_files(GITHUB_REPOS_API_BASE_URL + path, new_dir_name, recursive=recursive)

recursive = eval(options.r) if options.r else True

private = True if options.private else False

if private:
USERNAME = raw_input("username: ")
PASSWORD = raw_input("password: ")


write_files(GITHUB_REPOS_API_BASE_URL + path,
new_dir_name, recursive=recursive, private=private)

0 comments on commit e7a0a5b

Please sign in to comment.