def test_eq_property(self, lhs: VID, rhs: VID) -> None: # Comparing two VIDs should be the same as comparing their # version numbers. self.assertEqual(lhs == rhs, [lhs.major(), lhs.minor()] == [rhs.major(), rhs.minor()])
def __iter__(self) -> Iterator[LIDVID]: for dir in self.fs.walk.dirs(): parts = fs.path.parts(dir) if parts[-1].startswith("v$"): vid_part = str(parts[-1][2:]) lid_parts = [str(p) for p in parts[1:-1]] yield LIDVID.create_from_lid_and_vid( LID.create_from_parts(lid_parts), VID(vid_part))
def visit_bundle(self, bundle: Bundle, post: bool) -> None: if post: first_bundle = LIDVID(bundle.lidvid).vid() == VID("1.0") if first_bundle: self._create_context_collection(bundle) self._create_schema_collection(bundle) else: context_collection_lid = (LIDVID( bundle.lidvid).lid().extend_lid("context")) context_collection_lidvid = LIDVID.create_from_lid_and_vid( context_collection_lid, VID("1.0")) bundle_db.create_context_collection( str(context_collection_lidvid), bundle.lidvid) changes_dict.set(context_collection_lid, VID("1.0"), False) bundle_db.create_bundle_collection_link( str(bundle_lidvid), str(context_collection_lidvid)) self._post_visit_bundle(bundle)
def _run(self) -> None: working_dir: str = self.working_dir() archive_dir: str = self.archive_dir() archive_primary_deltas_dir: str = self.archive_primary_deltas_dir() if os.path.isdir(self.deliverable_dir()): raise ValueError( f"{self.deliverable_dir()} cannot exist for PopulateDatabase.") changes_path = os.path.join(working_dir, CHANGES_DICT_NAME) changes_dict = read_changes_dict(changes_path) bundle_lid = LID.create_from_parts([self._bundle_segment]) first_round = changes_dict.vid(bundle_lid) == VID("1.0") schema_collection_lid = LID.create_from_parts( [self._bundle_segment, "schema"]) changes_dict.set(schema_collection_lid, VID("1.0"), first_round) write_changes_dict(changes_dict, changes_path) db_filepath = os.path.join(working_dir, _BUNDLE_DB_NAME) db_exists = os.path.isfile(db_filepath) db = create_bundle_db_from_os_filepath(db_filepath) with make_osfs(archive_dir) as archive_osfs, make_version_view( archive_osfs, self._bundle_segment) as version_view, make_sv_deltas( version_view, archive_primary_deltas_dir) as sv_deltas: if not db_exists: db.create_tables() documents_dir = f"/{self._bundle_segment}$/document$/phase2$" docs = set(sv_deltas.listdir(documents_dir)) # Pass this to create citation info db in _populate_citation_info info_param: Tuple = (sv_deltas, documents_dir, docs) bundle_lidvid = _populate_bundle(changes_dict, db) _populate_collections(changes_dict, db) _populate_products(changes_dict, db, sv_deltas) _populate_target_identification(changes_dict, db, sv_deltas) _populate_citation_info(changes_dict, db, info_param) if not db: raise ValueError("db doesn't exist.") if not os.path.isfile(db_filepath): raise ValueError(f"{db_filepath} is not a file.")
def _populate_schema_collection(db: BundleDB, bundle_lidvid: str) -> None: # TODO We're assuming here that there will only ever be one schema # collection. I'm not sure that's true. lid = LIDVID(bundle_lidvid).lid().extend_lid("schema") new_lidvid = LIDVID.create_from_lid_and_vid(lid, VID("1.0")) collection_lidvid = str(new_lidvid) db.create_schema_collection(collection_lidvid, bundle_lidvid) # TODO Hardcoded here. Is this what we want to do? for lidvid in [DISP_LIDVID, HST_LIDVID, PDS4_LIDVID]: db.create_schema_product(lidvid)
def _next_vid(mv: Multiversioned, lid: LID, changed: bool) -> VID: # TODO If you want to allow minor changes, here is where you # decide which VID to use. Set a test here, then add a # parameter that decides between major and minor and thread it # up through the call stack. latest_lidvid = mv.latest_lidvid(lid) if latest_lidvid is None: return VID("1.0") elif changed: return latest_lidvid.vid().next_major_vid() else: return latest_lidvid.vid()
def __init__(self, lidvid_str: str) -> None: """ Create a LIDVID object from a string, raising an exception if the LIDVID string is malformed. """ segs = lidvid_str.split("::") if len(segs) != 2: raise ValueError(f"The number of {lidvid_str} segments: " + f"{len(segs)}, exepct 2.") self._lidvid = lidvid_str self._lid = LID(segs[0]) self._vid = VID(segs[1])
def read_changes_dict(changes_path: str) -> CHANGES_DICT: changes_dict = dict() with open(changes_path, "r") as f: for line in f: parts = line.strip().split() if parts: if len(parts) != 3: raise ValueError( f"Length of parts {parts} for changes_dict is not 3.") lid, vid, changed = parts if changed not in ["False", "True"]: raise ValueError( f"{changed} from parts has unexpected value.") changes_dict[LID(lid)] = (VID(vid), changed == "True") return ChangesDict(changes_dict)
def _munge_lidvid(product_lidvid: str, suffix: str, new_basename: str) -> str: bundle_id, collection_id, product_id = LIDVID(product_lidvid).lid().parts() # TODO This is a hack collection_type = get_collection_type(suffix=suffix) first_underscore_idx = collection_id.index("_") new_collection_id = (collection_type + collection_id[first_underscore_idx:-3] + suffix.lower()) # TODO This is a hack new_product_id = new_basename[0:9] new_lid = LID.create_from_parts( [bundle_id, new_collection_id, new_product_id]) # TODO This is a hack. Fix it. vid = VID("1.0") new_lidvid = LIDVID.create_from_lid_and_vid(new_lid, vid) return str(new_lidvid)
def test_init(self) -> None: with self.assertRaises(Exception): VID("foo") with self.assertRaises(Exception): VID("0.0") with self.assertRaises(Exception): VID("0.0.0") with self.assertRaises(Exception): VID("5.") with self.assertRaises(Exception): VID(".5") with self.assertRaises(Exception): VID("0.01") # test fields v = VID("3.14159265") self.assertEqual(3, v._major) self.assertEqual(14159265, v._minor)
def test_create_from_lid_and_vid(self) -> None: lid = LID("urn:nasa:pds:ssc01.hirespc.cruise:browse") vid = VID("2.5") lidvid = LIDVID.create_from_lid_and_vid(lid, vid) self.assertEqual( LIDVID("urn:nasa:pds:ssc01.hirespc.cruise:browse::2.5"), lidvid)
def test_vid(self) -> None: self.assertEqual(VID("666.0"), LIDVID("urn:nasa:pds:b:c:p::666.0").vid()) self.assertEqual(VID("3.14159"), LIDVID("urn:nasa:pds:b:c:p::3.14159").vid())
def make_sub_lidvid(seg: str, vid_part: str) -> LIDVID: lid_parts = lidvid.lid().parts() lid_parts.append(seg) return LIDVID.create_from_lid_and_vid( LID.create_from_parts(lid_parts), VID(vid_part))
def next_minor_lidvid(self, lid: LID) -> LIDVID: latest = self.latest_lidvid(lid) if latest: return latest.next_minor_lidvid() else: return LIDVID.create_from_lid_and_vid(lid, VID("1.0"))
def test_str_roundtrip_property(self, vid_str: str) -> None: """ Creating a VID from a string and turning it back into a string should result in the same string. """ self.assertEqual(vid_str, str(VID(vid_str)))
def test_str(self) -> None: self.assertEqual("2.3", str(VID("2.3")))
def test_cmp(self) -> None: self.assertTrue(VID("2.3") == VID("2.3")) self.assertTrue(VID("2.3") != VID("2.4")) self.assertTrue(VID("2.3") < VID("3.2")) self.assertTrue(VID("2.3") > VID("2.2"))
def test_next_minor_vid_property(self, vid: VID) -> None: next_vid = vid.next_minor_vid() # The major version should not change self.assertEqual(next_vid.major(), vid.major()) # and the minor should increment self.assertEqual(next_vid.minor(), vid.minor() + 1)
def test_next_minor_vid(self) -> None: self.assertEqual(VID("2.1"), VID("2.0").next_minor_vid()) self.assertEqual(VID("2.10"), VID("2.9").next_minor_vid())
def test_next_major_vid(self) -> None: self.assertEqual(VID("3.0"), VID("2.9").next_major_vid())
def test_repr(self) -> None: self.assertEqual("VID('2.3')", repr(VID("2.3")))