Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
from kbucket import client as kb | ||
import argparse | ||
import os | ||
|
||
parser = argparse.ArgumentParser(description='Download a file or directory from kbucket') | ||
|
||
parser.add_argument('-a', action="store_true", default=False) | ||
parser.add_argument('-b', action="store", dest="b") | ||
parser.add_argument('-c', action="store", dest="c", type=int) | ||
|
||
def print_usage(): | ||
print('Usage:') | ||
print('kbucket-download kbucket://[share-id]/[path] [output_file]') | ||
print('kbucket-download sha1://[sha1]/[path] [output_file]') | ||
|
||
def download(path,output_path,args): | ||
if os.path.exists(output_path): | ||
if not args.overwrite: | ||
print('Cannot download directory... output directory already exists. Use --overwrite flag to force.') | ||
sys.exit(-1) | ||
path0=kb.findFile(path) | ||
if not path0: | ||
print('Unable to find file: '+path) | ||
sys.exit(-1) | ||
if is_url(path0): | ||
size0=kb.getFileSize(path) | ||
if size0>args.max_file_size_mb*1024*1024: | ||
print('Ignoring large file ({} > {}): {}'.format(size0/(1024*1024),args.max_file_size_mb,output_path)) | ||
return | ||
kb.realizeFile(path,target_path=output_path) | ||
|
||
def is_url(path): | ||
return (path.startswith('http://') or path.startswith('https://')) | ||
|
||
def download_dir(path,output_path,args): | ||
try: | ||
dd=kb.readDir(path) | ||
except: | ||
print('Unable to read directory: '+path) | ||
sys.exit(-1) | ||
if os.path.exists(output_path): | ||
if not args.overwrite: | ||
print('Cannot download directory... output directory already exists. Use --overwrite flag to force.') | ||
sys.exit(-1) | ||
else: | ||
os.mkdir(output_path) | ||
for ff in dd.files: | ||
print(output_path+'/'+ff.name) | ||
download(path+'/'+ff.name,output_path+'/'+ff.name,args) | ||
for dd in dd.dirs: | ||
print(output_path+'/'+dd.name+'/') | ||
download_dir(path+'/'+dd.name,output_path+'/'+dd.name,args) | ||
|
||
if __name__== "__main__": | ||
parser = argparse.ArgumentParser(description = 'Download a file or directory from kbucket') | ||
parser.add_argument('path', help='The path of the file (or directory) on kbucket') | ||
parser.add_argument('output_path', help='The path of the destination file on the local computer') | ||
parser.add_argument('--dir', action='store_true', help='Download a directory rather than a file') | ||
parser.add_argument('--overwrite', action='store_true', help='Allow overwriting existing files') | ||
parser.add_argument('--max_file_size_mb', help='Ignore files larger than this size in megabytes', type=float, default=10) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.dir: | ||
download_dir(args.path,args.output_path,args) | ||
else: | ||
download(args.path,args.output_path,args) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters