def _revert(self, package): global _STORE # we do not need a "transaction" here, because an interrupted revert # can be easily continued later (in the worst case after a repair # of the project wc) st = self._status(package) if st == '?': msg = "cannot revert untracked package: %s" % package raise ValueError(msg) elif st == 'A': path = os.path.join(self.path, package) store = os.path.join(path, _STORE) if os.path.islink(store): os.unlink(store) # TODO: refactor this code path (and the on in _remove_wc_dir) # into a new function in the util module store = wc_pkg_data_filename(self.path, package) if os.path.exists(store): shutil.rmtree(store) self._packages.remove(package) self._packages.write() return # just revert the package pkg = self.package(package) if pkg is None: path = os.path.join(self.path, package) storedir = wc_pkg_data_filename(self.path, package) wc_init(path, ext_storedir=storedir) # now the package can be obtained pkg = self.package(package) pkg.revert() if st != ' ': self._packages.set(package, ' ') self._packages.write()
def test_wc_init1(self): """simple init wc""" path = self.fixture_file('init') self._not_exists(path, '.osc') wc_init(path) self._exists(path, '.osc') storedir = self.fixture_file('init', '.osc') self.assertFalse(os.path.islink(storedir)) self.assertTrue(os.path.isdir(storedir)) self.assertEqual(sorted(os.listdir(storedir)), ['_version', 'data'])
def test_wc_init2(self): """simple init wc external, empty storedir""" path = self.fixture_file('init') # we do not have to remove storedir later (cleanup happens # after each testcase) storedir = mkdtemp(dir=self._tmp_dir) storedir_lnk = self.fixture_file(path, '.osc') self._not_exists(path, '.osc') wc_init(path, ext_storedir=storedir) self._exists(path, '.osc') self.assertTrue(os.path.islink(storedir_lnk)) self.assertTrue(os.path.isdir(storedir_lnk)) self.assertEqual(sorted(os.listdir(storedir)), ['_version', 'data'])
def test_wc_init3(self): """init wc external, non-empty storedir""" path = self.fixture_file('init') storedir = self.fixture_file('storedir_non_empty') storedir_lnk = self.fixture_file(path, '.osc') self._not_exists(path, '.osc') wc_init(path, ext_storedir=storedir) self._exists(path, '.osc') self.assertTrue(os.path.islink(storedir_lnk)) self.assertTrue(os.path.isdir(storedir_lnk)) contents = ['_apiurl', '_files', '_package', '_project', '_version', 'data'] self.assertEqual(sorted(os.listdir(storedir)), contents)
def init(path, project, apiurl, *args, **kwargs): """Initializes a directory as a project working copy. path is a path to a directory, project is the name of the project and apiurl is the apiurl. *args and **kwargs are additional arguments for the Project's __init__ method. """ wc_init(path) wc_write_project(path, project) wc_write_apiurl(path, apiurl) wc_write_packages(path, '<packages/>') return Project(path, *args, **kwargs)
def test_wc_init3(self): """init wc external, non-empty storedir""" path = self.fixture_file('init') storedir = self.fixture_file('storedir_non_empty') storedir_lnk = self.fixture_file(path, '.osc') self._not_exists(path, '.osc') wc_init(path, ext_storedir=storedir) self._exists(path, '.osc') self.assertTrue(os.path.islink(storedir_lnk)) self.assertTrue(os.path.isdir(storedir_lnk)) contents = [ '_apiurl', '_files', '_package', '_project', '_version', 'data' ] self.assertEqual(sorted(os.listdir(storedir)), contents)
def repair(path, ext_storedir=None, revision='latest', **kwargs): """Repair a working copy. path is the path to the package working copy. Keyword arguments: project -- name of the project (default: '') package -- name of the package (default: '') apiurl -- apiurl is the apiurl (default: '') revision -- the revision of the package (default: 'latest') ext_storedir -- path to the storedir (default: None) """ global _PKG_DATA if not os.path.exists(_storedir(path)): wc_init(path, ext_storedir=ext_storedir) missing, xml_data, pkg_data = Package.wc_check(path) for filename in ('_project', '_package', '_apiurl'): if filename not in missing: continue key = filename[1:] if key not in kwargs: raise ValueError("%s argument required" % key) meth_name = 'wc_write_' + key globals()[meth_name](path, kwargs[key]) project = wc_read_project(path) package = wc_read_package(path) apiurl = wc_read_apiurl(path) if '_files' in missing or xml_data: spkg = SourcePackage(project, package) directory = spkg.list(rev=revision, apiurl=apiurl) xml_data = etree.tostring(directory, pretty_print=True) wc_write_files(path, xml_data) if '_version' in missing: wc_write_version(path) if _PKG_DATA in missing: os.mkdir(wc_pkg_data_filename(path, '')) files = wc_read_files(path) # check again - only pkg_data left missing, xml_data, pkg_data = Package.wc_check(path) for filename in pkg_data: fname = wc_pkg_data_filename(path, filename) f = files.find(filename).file() f.write_to(fname) # clean unused storefiles store = wc_pkg_data_filename(path, '') for filename in os.listdir(store): if files.find(filename) is None: os.unlink(os.path.join(store, filename))
def init(path, project, package, apiurl, ext_storedir=None, **kwargs): """Initializes a directory as a package working copy. path is a path to a directory, project is the name of the project, package is the name of the package and apiurl is the apiurl. Keyword arguments: ext_storedir -- path to the storedir (default: None). If not specified a "flat" package is created, otherwise path/.osc is a symlink to storedir. kwargs -- optional keyword args which are passed to Package's __init__ method """ wc_init(path, ext_storedir=ext_storedir) wc_write_project(path, project) wc_write_package(path, package) wc_write_apiurl(path, apiurl) wc_write_files(path, '<directory/>') return Package(path, **kwargs)
def test_wc_init6(self): """init wc (create new directory)""" path = self.fixture_file('init_nonexistent') self.assertFalse(os.path.exists(path)) wc_init(path) self.assertTrue(os.path.exists(path))