def test_get_tag(self): mdpath = os.path.join(self.test_dir, 'metadata') specpath = 'dataset-ex' ensure_path_exists(os.path.join(mdpath, specpath)) shutil.copy('hdata/dataset-ex.spec', os.path.join(mdpath, specpath) + '/dataset-ex.spec') manifestpath = os.path.join(os.path.join(mdpath, specpath), 'MANIFEST.yaml') yaml_save(files_mock, manifestpath) config['mlgit_path'] = self.test_dir m = Metadata(specpath, mdpath, config, DATASETS) r = Repository(config, DATASETS) r.init() tag_list = ['computer__images__dataset-ex__1'] with mock.patch('ml_git.metadata.Metadata.list_tags', return_value=tag_list): target_tag = m.get_tag(specpath, -1) self.assertEqual(target_tag, tag_list[0]) clear(m.path)
def _checkout(self, tag, samples, options): dataset = options['with_dataset'] labels = options['with_labels'] retries = options['retry'] force_get = options['force'] bare = options['bare'] version = options['version'] repo_type = self.__repo_type try: cache_path = get_cache_path(self.__config, repo_type) metadata_path = get_metadata_path(self.__config, repo_type) objects_path = get_objects_path(self.__config, repo_type) refs_path = get_refs_path(self.__config, repo_type) if not re.search(RGX_TAG_FORMAT, tag): metadata_path = get_metadata_path(self.__config, repo_type) metadata = Metadata(tag, metadata_path, self.__config, repo_type) tag = metadata.get_tag(tag, version) if not tag: return None, None elif not self._tag_exists(tag): return None, None categories_path, spec_name, _ = spec_parse(tag) root_path = get_root_path() ws_path = os.path.join(root_path, os.sep.join([repo_type, categories_path])) except Exception as e: log.error(e, class_name=LOCAL_REPOSITORY_CLASS_NAME) return None, None ref = Refs(refs_path, spec_name, repo_type) cur_tag, _ = ref.branch() if cur_tag == tag: log.info('already at tag [%s]' % tag, class_name=REPOSITORY_CLASS_NAME) return None, None local_rep = LocalRepository(self.__config, objects_path, repo_type) # check if no data left untracked/uncommitted. otherwise, stop. if not force_get and local_rep.exist_local_changes(spec_name) is True: return None, None try: self._checkout_ref(tag) except Exception: log.error('Unable to checkout to %s' % tag, class_name=REPOSITORY_CLASS_NAME) return None, None dataset_tag, labels_tag = self._get_related_tags( categories_path, dataset, labels, metadata_path, repo_type, spec_name) fetch_success = self._fetch(tag, samples, retries, bare) if not fetch_success: objs = Objects('', objects_path) objs.fsck(remove_corrupted=True) self._checkout_ref() return None, None ensure_path_exists(ws_path) try: spec_index_path = os.path.join( get_index_metadata_path(self.__config, repo_type), spec_name) except Exception: return self._delete_spec_and_readme(spec_index_path, spec_name) try: r = LocalRepository(self.__config, objects_path, repo_type) r.checkout(cache_path, metadata_path, ws_path, tag, samples, bare) except OSError as e: self._checkout_ref() if e.errno == errno.ENOSPC: log.error( 'There is not enough space in the disk. Remove some files and try again.', class_name=REPOSITORY_CLASS_NAME) else: log.error( 'An error occurred while creating the files into workspace: %s \n.' % e, class_name=REPOSITORY_CLASS_NAME) return None, None except Exception as e: self._checkout_ref() log.error( 'An error occurred while creating the files into workspace: %s \n.' % e, class_name=REPOSITORY_CLASS_NAME) return None, None m = Metadata('', metadata_path, self.__config, repo_type) sha = m.sha_from_tag(tag) ref.update_head(tag, sha) # restore to master/head self._checkout_ref() return dataset_tag, labels_tag