Skip to content
Snippets Groups Projects
Commit 192ab3ea authored by aknecht2's avatar aknecht2
Browse files

Fixed issues with circular dependncy in utils / db.py

parent 69ec65f8
No related branches found
No related tags found
1 merge request!33Resolve "MongoDB auth should be optional"
......@@ -11,7 +11,20 @@ import chipathlon.conf
from pprint import pprint
import hashlib
from chipathlon.utils import progress
import bson
def download_from_gridfs(host, gridfs_id, local_path, username=None, password=None, retries=3, overwrite=True, checkmd5=False):
mdb = chipathlon.db.MongoDB(host, username, password)
if not os.path.isfile(local_path) or overwrite:
for i in range(0, retries):
print "Attempt #%s, downloading file with ID '%s' to '%s'" % (i + 1, gridfs_id, local_path)
if mdb.fetch_from_gridfs(bson.objectid.ObjectId(gridfs_id), localpath, checkmd5):
return True
else:
print "Download attempt #%s from GridFS failed, retrying..." % (i + 1)
else:
print "File already exists, skipping download.\n"
return False
class MongoDB(object):
"""
......
......@@ -3,8 +3,6 @@ import hashlib
import urllib2
import os
import traceback
import chipathlon.db
import bson.objectid
def progress(current, end, length=20):
percent = float(current) / end
......@@ -50,21 +48,6 @@ def downloadFile(url, localpath, urltype="http://", retries=3, overwrite=True, c
print "File already exists, skipping download.\n"
return
def downloadFromGridFS(hostname, username, password, gridfs_id, localpath, retries=3, overwrite=True, checkmd5=False):
success = False
mdb = chipathlon.db.MongoDB(hostname, username, password)
if not os.path.isfile(localpath) or overwrite:
for i in range(0, retries):
print "Attempt #%s, downloading file with ID '%s' to '%s'" % (i + 1, gridfs_id, localpath)
if mdb.fetch_from_gridfs(bson.objectid.ObjectId(gridfs_id), localpath, checkmd5):
success = True
break
else:
print "Download attempt #%s from GridFS failed, retrying..." % (i + 1)
else:
print "File already exists, skipping download.\n"
return success
# http://pythoncentral.io/how-to-check-if-a-string-is-a-number-in-python-including-unicode/
def is_number(s):
try:
......
#!/usr/bin/env python
import chipathlon.utils
import chipathlon.db
import argparse
parser = argparse.ArgumentParser(description="Download target file from GridFS.")
......@@ -9,17 +9,17 @@ parser.add_argument("-p", "--password", dest="password", help="Database password
parser.add_argument("-i", "--id", dest="gridfs_id", required=True, help="GridFS ID.")
parser.add_argument("-d", "--destination", dest="destination", required=True, help="Local path to file destination.")
parser.add_argument("-r", "--retries", dest="retries", default=3, type=int, help="Number of retries.")
parser.add_argument("-n", "--overwrite", dest="overwrite", default=True, action="store_false", help="Don't overwrite local file if it exists.")
parser.add_argument("-n", "--no-overwrite", dest="overwrite", default=True, action="store_false", help="Don't overwrite local file if it exists.")
parser.add_argument("-c", "--checkmd5", dest="checkmd5", action="store_true", help="Check md5 value of downloaded file against database value.")
args = parser.parse_args()
chipathlon.utils.downloadFromGridFS(
chipathlon.db.download_from_gridfs(
args.host,
args.username,
args.password,
args.gridfs_id,
args.destination,
args.retries,
args.overwrite,
args.checkmd5
username=args.username,
password=args.password,
retries=args.retries,
overwrite=args.overwrite,
checkmd5=args.checkmd5
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment