def match(self, path): """Checks if path is a wc path. If path is a wc path a WCPath object is returned otherwise None. """ project_path = package_path = filename_path = None if not path: path = os.getcwd() path = os.path.normpath(path) par_dir = wc_parent(path) if wc_is_package(path): package_path = path project_path = par_dir elif wc_is_project(path): project_path = path elif par_dir is not None: if wc_is_package(par_dir): filename_path = path package_path = par_dir # check if package has a parent par_dir = wc_parent(package_path) if par_dir is not None and wc_is_project(par_dir): project_path = par_dir elif wc_is_project(par_dir): project_path = par_dir package_path = path else: return None else: return None return {self._name: WCPath(project_path, package_path, filename_path)}
def wc_resolve(self, path=''): """Try to read components from path. If path is specified it overrides the path which was passed to __init__. If all conditions are met (see class description for details) a dict is returned which contains the matches (some optional and _non_optional matches might be None). Otherwise None is returned. """ path = path or self._path if not path: return None unresolved = dict([(comp.name, None) for comp in self._components]) has_prj = 'project' in unresolved has_pkg = 'package' in unresolved and 'project' in unresolved if has_pkg: if wc_is_package(path): ret = {'apiurl': wc_read_apiurl(path), 'project': wc_read_project(path), 'package': wc_read_package(path)} unresolved.update(ret) return unresolved pkg_opt = True for comp in self._components: if comp.name == 'package': pkg_opt = comp.opt if has_prj and pkg_opt: if wc_is_project(path): ret = {'apiurl': wc_read_apiurl(path), 'project': wc_read_project(path)} unresolved.update(ret) return unresolved return None
def test27(self): """test wc_parent (package/non_existent - no parent)""" path = self.fixture_file('package', 'non_existent') self.assertFalse(os.path.exists(path)) par_dir = wc_parent(path) self.assertIsNotNone(par_dir) self.assertTrue(wc_is_package(par_dir)) self.assertEqual(wc_read_package(par_dir), 'package')
def test24(self): """test wc_parent (package/file)""" path = self.fixture_file('prj1', 'added', 'foo') self.assertTrue(os.path.isfile(path)) par_dir = wc_parent(path) self.assertIsNotNone(par_dir) self.assertTrue(wc_is_package(par_dir)) self.assertEqual(wc_read_package(par_dir), 'added')
def test30(self): """test wc_parent in non-standard hierarchy""" # currently, such a hierarchy is not created by osc2 # but we should resolve the parent with the help of # the storedir link path = self.fixture_file('prj1', 'hierarchy', 'non-standard') self.assertTrue(wc_is_package(path)) par_dir = wc_parent(path) self.assertIsNotNone(par_dir) self.assertTrue(wc_is_project(par_dir)) self.assertEqual(wc_read_project(par_dir), 'prj1')
def package_obj(self, *args, **kwargs): """Returns a Package object if possible. *args and **kwargs are optional arguments for Package's __init__ method. If no Package can be returned None is returned. """ if self.package_path is None or not wc_is_package(self.package_path): return None return Package(self.package_path, *args, **kwargs)
def test28(self): """test wc_parent cwd (package/non_existent - no parent)""" pkg_path = self.fixture_file('package') path = 'non_existent' cwd = os.getcwd() try: os.chdir(pkg_path) self.assertFalse(os.path.exists(path)) par_dir = wc_parent(path) self.assertIsNotNone(par_dir) self.assertTrue(wc_is_package(par_dir)) self.assertEqual(wc_read_package(par_dir), 'package') finally: os.chdir(cwd)
def package(self, package, *args, **kwargs): """Return a Package object for package package. None is returned if package is missing (has state '!') or if package is untracked. *args and **kwargs are additional arguments for the Package's __init__ method. """ path = os.path.join(self.path, package) st = self._status(package) if st in ('!', '?') or not wc_is_package(path): return None return Package(path, *args, **kwargs)
def add(self, package, *filenames, **kwargs): """Add a new package to the project. package is the name of the directory which will be added. A ValueError is raised if package is already tracked or if package is already an osc working copy. Also if prj/package does not exist or is no directory a ValueError is raised. If filenames are specified they are added to package. If no filenames are specified all files will be added to the package. A ValueError is raised if filenames and no_files=True is specified. Keyword arguments: no_files -- add no files (default: False) """ super(Project, self).add(package) no_files = kwargs.get('no_files', False) if filenames and no_files: raise ValueError("filenames and no_files are mutually exclusive") with wc_lock(self.path): if self._status(package) != '?': raise ValueError("package \"%s\" is already tracked" % package) pkg_path = os.path.join(self.path, package) if not os.path.isdir(pkg_path): raise ValueError("path \"%s\" is no dir" % pkg_path) elif wc_is_project(pkg_path) or wc_is_package(pkg_path): msg = ("path \"%s\" is already an initialized" "working copy" % pkg_path) raise ValueError(msg) storedir = wc_pkg_data_mkdir(self.path, package) pkg = Package.init(pkg_path, self.name, package, self.apiurl, ext_storedir=storedir) self._packages.add(package, state='A') self._packages.write() if no_files: filenames = [] elif not filenames: filenames = [f for f in os.listdir(pkg.path) if os.path.isfile(os.path.join(pkg.path, f))] for filename in filenames: pkg.add(filename)
def __init__(self, project_path, package_path, filename_path): """Constructs a new WCPath object. project_path is the path to the project wc. package_path is the path to the package wc. filename_path is the path to the wc filename. Either project_path or package_path or both aren't None. """ self.project_path = project_path self.package_path = package_path self.filename_path = filename_path self.project = self.package = self.filename = None if self.project_path is not None: self.project = wc_read_project(self.project_path) if self.package_path is not None: if wc_is_package(self.package_path): self.package = wc_read_package(self.package_path) else: self.package = os.path.basename(self.package_path) if self.filename_path is not None: self.filename = os.path.basename(self.filename_path)
def test5(self): """test wc_is_package""" path = self.fixture_file('project') self.assertFalse(wc_is_package(path))
def test6(self): """test wc_is_package""" self.assertFalse(wc_is_package('/'))
def test4(self): """test wc_is_package""" path = self.fixture_file('package') self.assertTrue(wc_is_package(path))