Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init(self, force=False):
fn_manifest = os.path.join(self._wd, self.FN_MANIFEST)
# Create the workspace directory if it did not exist yet.
try:
_mkdir_p(self._wd)
except OSError:
logger.error("Error occured while trying to create "
"workspace directory for job '{}'.".format(self))
raise
try:
# Ensure to create the binary to write before file creation
blob = json.dumps(self._statepoint, indent=2)
try:
# Open the file for writing only if it does not exist yet.
with open(fn_manifest, 'w' if force else 'x') as file:
file.write(blob)
except (IOError, OSError) as error:
if error.errno not in (errno.EEXIST, errno.EACCES):
raise
except Exception as error:
# Attempt to delete the file on error, to prevent corruption.
try:
os.remove(fn_manifest)
except Exception: # ignore all errors here
pass
raise error
else:
def read_sp_manifest_file(path):
# Must use forward slashes, not os.path.sep.
fn_manifest = path + '/' + project.Job.FN_MANIFEST
if fn_manifest in names:
return json.loads(zipfile.read(fn_manifest).decode())
def _check_manifest(self):
"Check whether the manifest file, if it exists, is correct."
fn_manifest = os.path.join(self._wd, self.FN_MANIFEST)
try:
with open(fn_manifest, 'rb') as file:
assert calc_id(json.loads(file.read().decode())) == self._id
except IOError as error:
if error.errno != errno.ENOENT:
raise error
except (AssertionError, ValueError):
raise JobsCorruptedError([self._id])
def __setitem__(self, _id, doc, _trust=False):
self._assert_open()
if not isinstance(_id, str):
raise TypeError("The primary key must be of type str!")
doc.setdefault(self._primary_key, _id)
if _id != doc[self._primary_key]:
raise ValueError("Primary key mismatch!")
if _trust:
self._docs[_id] = doc
else:
try:
doc_ = json.loads(json.dumps(doc))
except TypeError as error:
raise TypeError(
"Serialization of document '{}' failed with error: {}".format(doc, error))
self._docs[_id] = self._validate_doc(doc_)
self._dirty.add(_id)
self._requires_flush = True
def _calculate_hash(cls, doc, dirpath, fn):
blob = json.dumps(doc, sort_keys=True)
m = hashlib.md5()
m.update(dirpath.encode('utf-8'))
m.update(fn.encode('utf-8'))
m.update(blob.encode('utf-8'))
return m.hexdigest()
def _with_message(query, file):
print("Interpreted filter arguments as '{}'.".format(json.dumps(query)), file=file)
return query
def _parse_json(q):
try:
return json.loads(q)
except json.JSONDecodeError:
_print_err("Failed to parse query argument. "
"Ensure that '{}' is valid JSON!".format(q))
raise
def _load(self):
assert self._filename is not None
if _BUFFERED_MODE > 0:
if self._filename in _JSONDICT_BUFFER:
# Load from buffer:
blob = _JSONDICT_BUFFER[self._filename]
else:
# Load from disk and store in buffer
blob = self._load_from_disk()
_store_in_buffer(self._filename, blob, store_hash=True)
else:
# Just load from disk
blob = self._load_from_disk()
return dict() if blob is None else json.loads(blob.decode())
def _get_statepoint_from_workspace(self, jobid):
"Attempt to read the statepoint from the workspace."
fn_manifest = os.path.join(self._wd, jobid, self.Job.FN_MANIFEST)
try:
with open(fn_manifest, 'rb') as manifest:
return json.loads(manifest.read().decode())
except (IOError, ValueError) as error:
if os.path.isdir(os.path.join(self._wd, jobid)):
logger.error(
"Error while trying to access state "
"point manifest file of job '{}': '{}'.".format(jobid, error))
raise JobsCorruptedError([jobid])
raise KeyError(jobid)