Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return MD5 of existing files on open #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions restful-tango/tangoREST.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(self):
self.found_dir = self.create(0, "Found directory")
self.made_dir = self.create(0, "Created directory")
self.file_uploaded = self.create(0, "Uploaded file")
self.file_exists = self.create(0, "File exists")
self.job_added = self.create(0, "Job added")
self.obtained_info = self.create(0, "Found info successfully")
self.obtained_jobs = self.create(0, "Found list of jobs")
Expand Down Expand Up @@ -102,18 +101,19 @@ def getOutPath(self, key, courselab):
labPath = self.getDirPath(key, courselab)
return "%s/%s" % (labPath, self.OUTPUT_FOLDER)

def checkFileExists(self, directory, filename, fileMD5):
""" checkFileExists - Checks if a file exists in a
directory
def computeMD5(self, directory):
""" computeMD5 - Computes the MD5 hash of given files in the
given directory
"""
result = {}
for elem in os.listdir(directory):
if elem == filename:
try:
body = open("%s/%s" % (directory, elem)).read()
md5hash = hashlib.md5(body).hexdigest()
return md5hash == fileMD5
except IOError:
continue
try:
body = open("%s/%s" % (directory, elem)).read()
md5hash = hashlib.md5(body).hexdigest()
result[elem] = md5hash
except IOError:
continue
return result

def createTangoMachine(self, image, vmms=Config.VMMS_NAME,
vmObj={'cores': 1, 'memory': 512}):
Expand Down Expand Up @@ -244,7 +244,7 @@ def open(self, key, courselab):
self.log.info(
"Found directory for (%s, %s)" % (key, courselab))
statusObj = self.status.found_dir
statusObj['files'] = {}
statusObj['files'] = self.computeMD5(labPath)
return statusObj
else:
outputPath = self.getOutPath(key, courselab)
Expand All @@ -262,20 +262,14 @@ def open(self, key, courselab):
return self.status.wrong_key

def upload(self, key, courselab, file, body):
""" upload - Upload file as an input file in key-courselab if the
same file doesn't exist already
""" upload - Upload file as an input file in key-courselab
"""
self.log.debug("Received upload request(%s, %s, %s)" %
(key, courselab, file))
if (self.validateKey(key)):
labPath = self.getDirPath(key, courselab)
try:
if os.path.exists(labPath):
fileMD5 = hashlib.md5(body).hexdigest()
if self.checkFileExists(labPath, file, fileMD5):
self.log.info(
"File (%s, %s, %s) exists" % (key, courselab, file))
return self.status.file_exists
absPath = "%s/%s" % (labPath, file)
fh = open(absPath, "wt")
fh.write(body)
Expand Down