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

Let the user know there could've been some problems with the indexing #1083

Merged
merged 10 commits into from
Mar 5, 2021
59 changes: 46 additions & 13 deletions validphys2/src/validphys/uploadutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Tools to upload resources to remote servers.
"""
import time
import subprocess
import logging
import os
Expand Down Expand Up @@ -182,17 +183,32 @@ def get_relative_path(self, output_path=None):
return ''

def check_fit_exists(self, fit_name):
"""Check whether the fit already exists on the server."""
"""Check whether the fit already exists on the server.
Returns true if a fit exists with the same name on the server or false otherwise
"""
# Get list of the available fits on the server
l = RemoteLoader()
fits = l.downloadable_fits

if fit_name in fits:
log.error("A fit with the same name already exists on "
"the server. To overwrite this fit use the "
"--force flag, as in `vp-upload <fitname> "
"--force`.")
raise UploadError
return True
return False

def _check_existence(self, fit_name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this done again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I felt it was ugly to have basically the same code copied twice for pdfs and fits but at the same time I didn't want to remove the duplicated code that already existed.

""" Wrapper for the correct existence method """
return self.check_fit_exists(fit_name)

def check_is_indexed(self, fit_name):
""" Check whether the fit is correctly indexed in the server
"""
log.info("Checking whether %s was correctly uploaded...", fit_name)
time.sleep(3)
if self._check_existence(fit_name):
log.info("It has been correctly indexed by the server!")
else:
log.error("The object was uploaded but haven't been indexed yet by the server "
"you might want to try to upload it again to ensure it is indexed: "
scarlehoff marked this conversation as resolved.
Show resolved Hide resolved
"vp-upload %s", fit_name)

def check_fit_md5(self, output_path):
"""When ``vp-setupfit`` is successfully ran, it creates an ``md5`` from
Expand Down Expand Up @@ -248,13 +264,22 @@ def upload_output(self, output_path, force):
fit_name = output_path.name

if not force:
self.check_fit_exists(fit_name)
if self.check_fit_exists(fit_name):
log.error("A fit with the same name already exists on "
"the server. To overwrite this fit use the "
scarlehoff marked this conversation as resolved.
Show resolved Hide resolved
"--force flag, as in `vp-upload <fitname> "
"--force`.")
raise UploadError


self.check_fit_md5(output_path)

new_out, name = self.compress(output_path)
super().upload_output(new_out)

# Check whether the fit was really uploaded
self.check_is_indexed(fit_name)

shutil.rmtree(new_out)
return name.with_suffix('.tar.gz').name

Expand Down Expand Up @@ -287,11 +312,12 @@ def check_pdf_exists(self, pdf_name):
pdfs = l.downloadable_pdfs

if pdf_name in pdfs:
log.error("A PDF with the same name already exists on "
"the server. To overwrite this PDF use the "
"--force flag, as in `vp-upload <pdfname> "
"--force`.")
raise UploadError
return True
return False

def _check_existence(self, fit_name):
""" Wrapper for the correct existence method """
return self.check_pdf_exists(fit_name)

def compress(self, output_path):
"""Compress the folder and put it in a directory inside its parent."""
Expand All @@ -317,11 +343,18 @@ def upload_output(self, output_path, force):
pdf_name = output_path.name

if not force:
self.check_pdf_exists(pdf_name)
if self.check_pdf_exists(pdf_name):
log.error("A PDF with the same name already exists on "
"the server. To overwrite this PDF use the "
"--force flag, as in `vp-upload <pdfname> "
"--force`.")
raise UploadError

new_out, name = self.compress(output_path)
super(FileUploader, self).upload_output(new_out)

self.check_is_indexed(pdf_name)

shutil.rmtree(new_out)
return name.with_suffix('.tar.gz').name

Expand Down