def _serialize(self, fmt="xml", mn_client=None): assert fmt in ALLOWABLE_PACKAGE_SERIALIZATIONS if mn_client is None: mn_client = utils.get_d1_mn_client() if not self._prepare_urls(mn_client): return self._generate_resmap(mn_client.base_url) if self.resmap.serializer is not None: self.resmap.serializer = None serializer = foresite.RdfLibSerializer(fmt) self.resmap.register_serialization(serializer) doc = self.resmap.get_serialization() return doc.data
def _create_object(self, item, mn_client=None, **kwargs): """ Create an object in DataONE. """ path = utils.expand_path(item.fname) # cli_util.assert_file_exists(path) assert os.path.exists(path) if "format_id" in kwargs: del kwargs["format_id"] sysmeta = utils.create_sysmeta_from_path(item.pid, path, format_id=item.format_id, **kwargs) if mn_client is None: mn_client = utils.get_d1_mn_client() with open(path, "r") as f: try: result = mn_client.create(item.pid, f, sysmeta) print 'Created object "%s"' % item.pid return result except DataONEException as e: raise Exception("Unable to create Science Object on Member Node\n{0}".format(e.friendly_format()))
def _prepare_urls(self, mn_client=None): """ Walk through the objects make sure that everything can be serialized. """ if mn_client is None: mn_client = utils.get_d1_mn_client() if self.scimeta and not self.scimeta.url: if not self._check_item(self.scimeta): return False elif not self.scimeta.url: self.scimeta.url = utils.create_resolve_url_for_pid(mn_client.base_url, self.scimeta.pid) if self.scidata_dict: for scidata in self.scidata_dict.values(): if not self._check_item(scidata): return False elif not scidata.url: scidata.url = utils.create_resolve_url_for_pid(mn_client.base_url, scidata.pid) return True
def save(self, mn_client=None, cn_client=None, **kwargs): """ Save this object referred to by this pid. """ if self.pid is None: raise Exception("Missing pid") if mn_client is None: mn_client = utils.get_d1_mn_client() if cn_client is None: cn_client = utils.get_d1_cn_client() pkg_xml = self._serialize("xml", mn_client) if not pkg_xml: raise Exception("Couldn't serialize object.") algorithm = configuration.checksum_alg hash_fcn = util.get_checksum_calculator_by_dataone_designator(algorithm) hash_fcn.update(pkg_xml) checksum = hash_fcn.hexdigest() sysmeta = utils.create_system_metadata(self.pid, len(pkg_xml), checksum, algorithm, RDFXML_FORMATID, **kwargs) flo = StringIO.StringIO(pkg_xml) # Save all the objects. if self.scimeta and self.scimeta.dirty: self._create_or_update(mn_client, cn_client, self.scimeta) for scidata_pid in self.scidata_dict.keys(): scidata = self.scidata_dict.get(scidata_pid) if scidata and scidata.dirty: self._create_or_update(mn_client, cn_client, scidata) response = mn_client.create(pid=self.pid, obj=flo, sysmeta=sysmeta) if response is None: return None else: self.original_pid = self.pid if self.scimeta: self.scimeta.dirty = False if self.scidata_dict: for scidata in self.scidata_dict.values(): scidata.dirty = False return response.value()
def run_pkg_test(): configuration.mn_url = "https://mn-demo-9.test.dataone.org/knb/d1/mn" configuration.cn_url = "https://cn-stage-2.test.dataone.org/cn" cn_client = utils.get_d1_cn_client() mn_client = utils.get_d1_mn_client() pid = "dakoop_test_pkg603" meta_pid = "dakoop_cadwsap003" meta_fname = "/vistrails/local_packages/dataone/package_data/cadwsap-s2910001-001.xml" meta_format = "FGDC-STD-001-1998" data_list = [ ( "dakoop_cadwsap_main003", "/vistrails/local_packages/dataone/package_data/cadwsap-s2910001-001-main.csv", "text/csv", ), ( "dakoop_cadwsap_vuln003", "/vistrails/local_packages/dataone/package_data/cadwsap-s2910001-001-vuln.csv", "text/csv", ), ] pkg = DataPackage(pid) sysmeta_kwargs = { "format_id": meta_format, "submitter": "dakoop", "owner": "dakoop", "orig_mn": configuration.mn_url, "auth_mn": configuration.mn_url, } pkg.scimeta_add(meta_pid, meta_fname, **sysmeta_kwargs) for (data_pid, data_fname, data_format) in data_list: sysmeta_kwargs["format_id"] = data_format pkg.scidata_add(data_pid, data_fname, **sysmeta_kwargs) del sysmeta_kwargs["format_id"] pkg.save(mn_client, cn_client, **sysmeta_kwargs)