diff --git a/src/pysme/atmosphere/savfile.py b/src/pysme/atmosphere/savfile.py index fadba8aa..d4fef44d 100644 --- a/src/pysme/atmosphere/savfile.py +++ b/src/pysme/atmosphere/savfile.py @@ -1,3 +1,4 @@ +import os from os.path import basename from tempfile import NamedTemporaryFile @@ -147,9 +148,20 @@ def __new__(cls, filename, source=None, lfs=None): # Store in cache cls._cache = self # And also replace the IDL file with a numpy file in the cache + # We have to use a try except block, as this will crash with + # permissions denied on windows, when trying to copy an open file + # here the temporary file + # Therefore we close the file, after copying and then delete it manually if lfs is not None: - with NamedTemporaryFile() as named: - self.save(named) + try: + with NamedTemporaryFile(delete=False) as named: + self.save(named) + named.flush() lfs.move_to_cache(named.name, key=lfs.get_url(self.source)) + finally: + try: + os.remove(named.name) + except: + pass return self diff --git a/src/pysme/large_file_storage.py b/src/pysme/large_file_storage.py index b7fdd955..a76ac01c 100644 --- a/src/pysme/large_file_storage.py +++ b/src/pysme/large_file_storage.py @@ -11,7 +11,7 @@ import json import logging import os -from os.path import basename, join +from os.path import basename from pathlib import Path from tempfile import NamedTemporaryFile @@ -102,24 +102,34 @@ def get(self, key): def _unpack(self, fname, key, url): logger.debug("Unpacking data file %s", key) - with gzip.open(fname, "rb") as f_in: - with NamedTemporaryFile("wb") as f_out: - with tqdm( - # total=f_in.size, - desc="Unpack", - unit="B", - unit_scale=True, - unit_divisor=1024, - ) as t: - fobj = CallbackIOWrapper(t.update, f_in, "read") - while True: - chunk = fobj.read(1024) - if not chunk: - break - f_out.write(chunk) - f_out.flush() - t.reset() - import_file_to_cache(url, f_out.name, pkgname="pysme") + # We have to use a try except block, as this will crash with + # permissions denied on windows, when trying to copy an open file + # here the temporary file + # Therefore we close the file, after copying and then delete it manually + try: + with gzip.open(fname, "rb") as f_in: + with NamedTemporaryFile("wb", delete=False) as f_out: + with tqdm( + # total=f_in.size, + desc="Unpack", + unit="B", + unit_scale=True, + unit_divisor=1024, + ) as t: + fobj = CallbackIOWrapper(t.update, f_in, "read") + while True: + chunk = fobj.read(1024) + if not chunk: + break + f_out.write(chunk) + f_out.flush() + t.reset() + import_file_to_cache(url, f_out.name, pkgname="pysme") + finally: + try: + os.remove(f_out.name) + except: + pass def get_url(self, key): key = str(key) @@ -132,13 +142,13 @@ def get_url(self, key): f"File {key} does not exist and is not tracked by the Large File system" ) else: - return str(key) + return Path(key).as_uri() else: - return str(self.current / key) + return (self.current / key).as_uri() # Otherwise get it from the cache or online if necessary newest = self.pointers[key] - url = join(self.server, newest) + url = self.server + "/" + newest return url def clean_cache(self): diff --git a/src/pysme/libtools.py b/src/pysme/libtools.py index 8a83652a..e59af2da 100644 --- a/src/pysme/libtools.py +++ b/src/pysme/libtools.py @@ -91,6 +91,6 @@ def load_library(libfile=None): def get_full_datadir(): - localdir = realpath(dirname(__file__)) + localdir = dirname(__file__) datadir = join(localdir, "share/libsme/") return datadir diff --git a/src/pysme/sme_synth.py b/src/pysme/sme_synth.py index 66dbc36d..e0930575 100644 --- a/src/pysme/sme_synth.py +++ b/src/pysme/sme_synth.py @@ -1,6 +1,7 @@ """ Wrapper for sme_synth.so C library """ import logging import os +from os.path import normpath import numpy as np @@ -181,6 +182,7 @@ def SetLibraryPath(self, libpath=None): """Set the path to the library""" if libpath is None: libpath = get_full_datadir() + libpath = normpath(libpath) + os.sep self.lib.SetLibraryPath(libpath, type="string", state=self.state) def InputWaveRange(self, wfirst, wlast):