def test_dir_node_iter(self): nodes = [ DirNode('docs'), DirNode('tests'), FileNode('bar'), FileNode('foo'), FileNode('readme.txt'), FileNode('setup.py'), ] dirnode = DirNode('', nodes=nodes) for node in dirnode: node == dirnode.get_node(node.path)
def test_node_state(self): """ Without link to changeset nodes should raise NodeError. """ node = FileNode('anything') self.assertRaises(NodeError, getattr, node, 'state') node = DirNode('anything') self.assertRaises(NodeError, getattr, node, 'state')
def test_is_dir(self): node = Node('any_dir', NodeKind.DIR) self.assertTrue(node.is_dir()) node = DirNode('any_dir') self.assertTrue(node.is_dir()) self.assertRaises(NodeError, getattr, node, 'content')
def test_is_dir(self): node = Node('any_dir', NodeKind.DIR) assert node.is_dir() node = DirNode('any_dir') assert node.is_dir() with pytest.raises( AttributeError): # Note: this used to raise NodeError getattr(node, 'content')
def test_node_state(self): """ Without link to changeset nodes should raise NodeError. """ node = FileNode('anything') with pytest.raises(NodeError): getattr(node, 'state') node = DirNode('anything') with pytest.raises(NodeError): getattr(node, 'state')
def get_nodes(self, path): """ Returns combined ``DirNode`` and ``FileNode`` objects list representing state of changeset at the given ``path``. If node at the given ``path`` is not instance of ``DirNode``, ChangesetError would be raised. """ if self._get_kind(path) != NodeKind.DIR: raise ChangesetError("Directory does not exist for revision %s at " " '%s'" % (self.revision, path)) path = path.rstrip('/') id = self._get_id_for_path(path) tree = self.repository._repo[id] dirnodes = [] filenodes = [] als = self.repository.alias for name, stat, id in tree.items(): obj_path = safe_str(name) if path != '': obj_path = '/'.join((path, obj_path)) if objects.S_ISGITLINK(stat): root_tree = self.repository._repo[self._tree_id] cf = ConfigFile.from_file( BytesIO( self.repository._repo.get_object( root_tree[b'.gitmodules'][1]).data)) url = ascii_str(cf.get(('submodule', obj_path), 'url')) dirnodes.append( SubModuleNode(obj_path, url=url, changeset=ascii_str(id), alias=als)) continue obj = self.repository._repo.get_object(id) if obj_path not in self._stat_modes: self._stat_modes[obj_path] = stat if isinstance(obj, objects.Tree): dirnodes.append(DirNode(obj_path, changeset=self)) elif isinstance(obj, objects.Blob): filenodes.append(FileNode(obj_path, changeset=self, mode=stat)) else: raise ChangesetError("Requested object should be Tree " "or Blob, is %r" % type(obj)) nodes = dirnodes + filenodes for node in nodes: if node.path not in self.nodes: self.nodes[node.path] = node nodes.sort() return nodes
def get_node(self, path): """ Returns ``Node`` object from the given ``path``. If there is no node at the given ``path``, ``ChangesetError`` would be raised. """ path = path.rstrip('/') if path not in self.nodes: try: id_ = self._get_id_for_path(path) except ChangesetError: raise NodeDoesNotExistError( "Cannot find one of parents' " "directories for a given path: %s" % path) stat = self._stat_modes.get(path) if stat and objects.S_ISGITLINK(stat): tree = self.repository._repo[self._tree_id] cf = ConfigFile.from_file( BytesIO( self.repository._repo.get_object( tree[b'.gitmodules'][1]).data)) url = ascii_str(cf.get(('submodule', path), 'url')) node = SubModuleNode(path, url=url, changeset=ascii_str(id_), alias=self.repository.alias) else: obj = self.repository._repo.get_object(id_) if isinstance(obj, objects.Tree): if path == '': node = RootNode(changeset=self) else: node = DirNode(path, changeset=self) node._tree = obj elif isinstance(obj, objects.Blob): node = FileNode(path, changeset=self) node._blob = obj else: raise NodeDoesNotExistError( "There is no file nor directory " "at the given path: '%s' at revision %s" % (path, self.short_id)) # cache node self.nodes[path] = node return self.nodes[path]
def get_node(self, path): """ Returns ``Node`` object from the given ``path``. If there is no node at the given ``path``, ``ChangesetError`` would be raised. """ path = path.rstrip('/') if path not in self.nodes: if path in self._file_paths: node = FileNode(path, changeset=self) elif path in self._dir_paths or path in self._dir_paths: if path == '': node = RootNode(changeset=self) else: node = DirNode(path, changeset=self) else: raise NodeDoesNotExistError( "There is no file nor directory " "at the given path: '%s' at revision %s" % (path, self.short_id)) # cache node self.nodes[path] = node return self.nodes[path]
def get_node(self, path): if isinstance(path, unicode): path = path.encode('utf-8') path = self._fix_path(path) if not path in self.nodes: try: id_ = self._get_id_for_path(path) except ChangesetError: raise NodeDoesNotExistError( "Cannot find one of parents' " "directories for a given path: %s" % path) _GL = lambda m: m and objects.S_ISGITLINK(m) if _GL(self._stat_modes.get(path)): node = SubModuleNode(path, url=None, changeset=id_, alias=self.repository.alias) else: obj = self.repository._repo.get_object(id_) if isinstance(obj, objects.Tree): if path == '': node = RootNode(changeset=self) else: node = DirNode(path, changeset=self) node._tree = obj elif isinstance(obj, objects.Blob): node = FileNode(path, changeset=self) node._blob = obj else: raise NodeDoesNotExistError( "There is no file nor directory " "at the given path '%s' at revision %s" % (path, self.short_id)) # cache node self.nodes[path] = node return self.nodes[path]
def get_nodes(self, path): """ Returns combined ``DirNode`` and ``FileNode`` objects list representing state of changeset at the given ``path``. If node at the given ``path`` is not instance of ``DirNode``, ChangesetError would be raised. """ if self._get_kind(path) != NodeKind.DIR: raise ChangesetError("Directory does not exist for revision %s at " " '%s'" % (self.revision, path)) path = self._fix_path(path) filenodes = [ FileNode(f, changeset=self) for f in self._file_paths if os.path.dirname(f) == path ] dirs = path == '' and '' or [ d for d in self._dir_paths if d and posixpath.dirname(d) == path ] dirnodes = [ DirNode(d, changeset=self) for d in dirs if os.path.dirname(d) == path ] als = self.repository.alias for k, vals in self._extract_submodules().iteritems(): #vals = url,rev,type loc = vals[0] cs = vals[1] dirnodes.append(SubModuleNode(k, url=loc, changeset=cs, alias=als)) nodes = dirnodes + filenodes # cache nodes for node in nodes: self.nodes[node.path] = node nodes.sort() return nodes
def get_nodes(self, path): if self._get_kind(path) != NodeKind.DIR: raise ChangesetError("Directory does not exist for revision %s at " " '%s'" % (self.revision, path)) path = self._fix_path(path) id = self._get_id_for_path(path) tree = self.repository._repo[id] dirnodes = [] filenodes = [] als = self.repository.alias for name, stat, id in tree.iteritems(): if objects.S_ISGITLINK(stat): dirnodes.append( SubModuleNode(name, url=None, changeset=id, alias=als)) continue obj = self.repository._repo.get_object(id) if path != '': obj_path = '/'.join((path, name)) else: obj_path = name if obj_path not in self._stat_modes: self._stat_modes[obj_path] = stat if isinstance(obj, objects.Tree): dirnodes.append(DirNode(obj_path, changeset=self)) elif isinstance(obj, objects.Blob): filenodes.append(FileNode(obj_path, changeset=self, mode=stat)) else: raise ChangesetError("Requested object should be Tree " "or Blob, is %r" % type(obj)) nodes = dirnodes + filenodes for node in nodes: if not node.path in self.nodes: self.nodes[node.path] = node nodes.sort() return nodes