Skip to content

Commit b31f7dc

Browse files
committed
CWLProv, add self-check
1 parent d69f5c2 commit b31f7dc

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

cwltool/provenance.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,13 +964,14 @@ def __init__(self, temp_prefix_ro="tmp", orcid=None, full_name=None):
964964
self.orcid = _valid_orcid(orcid)
965965
self.full_name = full_name or None
966966
self.folder = os.path.abspath(tempfile.mkdtemp(prefix=temp_prefix_ro)) # type: Optional[Text]
967+
self.final_location = None # type: Optional[Text]
967968
# map of filename "data/de/alsdklkas": 12398123 bytes
968969
self.bagged_size = {} # type: Dict
969970
self.tagfiles = set() # type: Set
970971
self._file_provenance = {} # type: Dict
971-
self._external_aggregates = [] # type: List[Dict]
972-
self.annotations = [] # type: List[Dict]
973-
self._content_types = {} # type: Dict[Text,str]
972+
self._external_aggregates = [] # type: List[Dict]
973+
self.annotations = [] # type: List[Dict]
974+
self._content_types = {} # type: Dict[Text,str]
974975

975976
# These should be replaced by generate_prov_doc when workflow/run IDs are known:
976977
self.engine_uuid = "urn:uuid:%s" % uuid.uuid4()
@@ -986,9 +987,16 @@ def __init__(self, temp_prefix_ro="tmp", orcid=None, full_name=None):
986987
_logger.debug(u"[provenance] Temporary research object: %s",
987988
self.folder)
988989

990+
def self_check(self): # type: () -> None
991+
"""Raises ValueError if this RO is closed."""
992+
if not self.folder:
993+
raise ValueError(
994+
"This ResearchObject has already been closed and is not "
995+
"available for futher manipulation.")
996+
989997
def __str__(self):
990998
return "ResearchObject <%s> in <%s>" % (
991-
self.ro_uuid, self.folder)
999+
self.ro_uuid, self.folder or self.final_location)
9921000

9931001
def _initialize(self):
9941002
# type: (...) -> None
@@ -1018,6 +1026,7 @@ def _finalize(self):
10181026
def user_provenance(self, document):
10191027
# type: (ProvDocument) -> None
10201028
"""Add the user provenance."""
1029+
self.self_check()
10211030
(username, fullname) = _whoami()
10221031

10231032
if not self.full_name:
@@ -1049,6 +1058,7 @@ def user_provenance(self, document):
10491058
def write_bag_file(self, path, encoding=ENCODING):
10501059
# type: (Text, Optional[str]) -> IO
10511060
"""Write the bag file into our research object."""
1061+
self.self_check()
10521062
# For some reason below throws BlockingIOError
10531063
#fp = BufferedWriter(WritableBagFile(self, path))
10541064
bag_file = cast(IO, WritableBagFile(self, path))
@@ -1062,6 +1072,7 @@ def write_bag_file(self, path, encoding=ENCODING):
10621072
def add_tagfile(self, path, when=None):
10631073
# type: (Text, datetime.datetime) -> None
10641074
"""Add tag files to our research object."""
1075+
self.self_check()
10651076
checksums = {}
10661077
# Read file to calculate its checksum
10671078
if os.path.isdir(path):
@@ -1215,6 +1226,7 @@ def guess_mediatype(rel_path):
12151226

12161227
def add_uri(self, uri, when=None):
12171228
# type: (str, Optional[datetime.datetime]) -> Dict
1229+
self.self_check()
12181230
aggr = self._self_made(when=when)
12191231
aggr["uri"] = uri
12201232
self._external_aggregates.append(aggr)
@@ -1223,6 +1235,7 @@ def add_uri(self, uri, when=None):
12231235
def add_annotation(self, about, content, motivated_by="oa:describing"):
12241236
# type: (str, List[str], str) -> str
12251237
"""Cheap URI relativize for current directory and /."""
1238+
self.self_check()
12261239
curr = self.base_uri + METADATA + "/"
12271240
content = [c.replace(curr, "").replace(self.base_uri, "../")
12281241
for c in content]
@@ -1342,6 +1355,7 @@ def _write_bag_info(self):
13421355

13431356
def generate_snapshot(self, prov_dep):
13441357
# type: (MutableMapping[Text, Any]) -> None
1358+
self.self_check()
13451359
"""Copy all of the CWL files to the snapshot/ directory."""
13461360
assert self.folder
13471361
for key, value in prov_dep.items():
@@ -1374,6 +1388,7 @@ def generate_snapshot(self, prov_dep):
13741388

13751389
def packed_workflow(self, packed): # type: (Text) -> None
13761390
"""Pack CWL description to generate re-runnable CWL object in RO."""
1391+
self.self_check()
13771392
rel_path = posixpath.join(_posix_path(WORKFLOW), "packed.cwl")
13781393
# Write as binary
13791394
with self.write_bag_file(rel_path, encoding=None) as write_pack:
@@ -1383,12 +1398,14 @@ def packed_workflow(self, packed): # type: (Text) -> None
13831398

13841399
def has_data_file(self, sha1hash):
13851400
# type: (str) -> bool
1401+
self.self_check()
13861402
assert self.folder
13871403
folder = os.path.join(self.folder, DATA, sha1hash[0:2])
13881404
return os.path.isfile(os.path.join(folder, sha1hash))
13891405

13901406
def add_data_file(self, from_fp, when=None, content_type=None):
13911407
# type: (IO, Optional[datetime.datetime], Optional[str]) -> Text
1408+
self.self_check()
13921409
"""Copy inputs to data/ folder."""
13931410
with tempfile.NamedTemporaryFile(
13941411
prefix=self.temp_prefix, delete=False) as tmp:
@@ -1439,6 +1456,7 @@ def _self_made(self, when=None):
14391456
def add_to_manifest(self, rel_path, checksums):
14401457
# type: (Text, Dict[str,str]) -> None
14411458
"""Add files to the research object manifest."""
1459+
self.self_check()
14421460
if posixpath.isabs(rel_path):
14431461
raise ValueError("rel_path must be relative: %s" % rel_path)
14441462

@@ -1608,6 +1626,7 @@ def close(self, save_to=None):
16081626
assert self.folder
16091627
shutil.move(self.folder, save_to)
16101628
_logger.info(u"[provenance] Research Object saved to %s", save_to)
1629+
self.final_location = save_to
16111630
# Forget our temporary folder, which should no longer exists
16121631
# This makes later close() a no-op
16131632
self.folder = None

0 commit comments

Comments
 (0)