def build(cwd=None, root=None): """Build a tree from the current working directory or explicit root. :param cwd: current working directory :param root: path to root of the working copy :raises: :class:`~doorstop.common.DoorstopError` when the tree cannot be built :return: new :class:`~doorstop.core.tree.Tree` """ documents = [] # Find the root of the working copy cwd = cwd or os.getcwd() root = root or vcs.find_root(cwd) # Find all documents in the working copy log.info("looking for documents in {}...".format(root)) _document_from_path(root, root, documents) for dirpath, dirnames, _filenames in os.walk(root): for dirname in dirnames: path = os.path.join(dirpath, dirname) _document_from_path(path, root, documents) # Build the tree if not documents: log.info("no documents found in: {}".format(root)) log.info("building tree...") tree = Tree.from_list(documents, root=root) log.info("built tree: {}".format(tree)) return tree
def test_from_list(self): """Verify a tree can be created from a list.""" a = MockDocumentSkip(EMPTY) a.prefix = 'A' b = MockDocumentSkip(EMPTY) b.prefix = 'B' b.parent = 'A' c = MockDocumentSkip(EMPTY) c.prefix = 'C' c.parent = 'B' docs = [a, b, c] tree = Tree.from_list(docs) self.assertEqual(3, len(tree)) self.assertTrue(tree.validate())
def build(cwd=None, root=None, request_next_number=None) -> Tree: """Build a tree from the current working directory or explicit root. :param cwd: current working directory :param root: path to root of the working copy :param request_next_number: server method to get a document's next number :raises: :class:`~doorstop.common.DoorstopError` when the tree cannot be built :return: new :class:`~doorstop.core.tree.Tree` """ documents: List[Document] = [] # Find the root of the working copy cwd = cwd or os.getcwd() root = root or vcs.find_root(cwd) # Find all documents in the working copy log.info("looking for documents in {}...".format(root)) skip_file_name = '.doorstop.skip-all' if not os.path.isfile(os.path.join(root, skip_file_name)): _document_from_path(root, root, documents) exclude_dirnames = {'.git'} if not os.path.isfile(os.path.join(root, skip_file_name)): for dirpath, dirnames, _ in os.walk(root, topdown=True): whilelist_dirnames = [] for dirname in dirnames: if dirname in exclude_dirnames: continue path = os.path.join(dirpath, dirname) if os.path.isfile(os.path.join(path, skip_file_name)): continue whilelist_dirnames.append(dirname) _document_from_path(path, root, documents) dirnames[:] = whilelist_dirnames # Build the tree if not documents: log.info("no documents found in: {}".format(root)) log.info("building tree...") tree = Tree.from_list(documents, root=root) tree.request_next_number = request_next_number if len(tree): # pylint: disable=len-as-condition log.info("built tree: {}".format(tree)) else: log.info("tree is empty") return tree
def build(cwd=None, root=None, request_next_number=None): """Build a tree from the current working directory or explicit root. :param cwd: current working directory :param root: path to root of the working copy :param request_next_number: server method to get a document's next number :raises: :class:`~doorstop.common.DoorstopError` when the tree cannot be built :return: new :class:`~doorstop.core.tree.Tree` """ documents = [] # Find the root of the working copy cwd = cwd or os.getcwd() root = root or vcs.find_root(cwd) # Find all documents in the working copy log.info("looking for documents in {}...".format(root)) _document_from_path(root, root, documents) for dirpath, dirnames, _ in os.walk(root): for dirname in dirnames: path = os.path.join(dirpath, dirname) _document_from_path(path, root, documents) # Build the tree if not documents: log.info("no documents found in: {}".format(root)) log.info("building tree...") tree = Tree.from_list(documents, root=root) tree.request_next_number = request_next_number if len(tree): # pylint: disable=len-as-condition log.info("built tree: {}".format(tree)) else: log.info("tree is empty") return tree