chromacs: Use repo tool for finding manifest files.

Instead of finding and including manifest files ourselves, we now
leverage the repo tool's manifest command to handle manifest file
finding and including.

BUG=chromium:1115170
TEST=Used chromacs with parsed output from repo manifest command.

Change-Id: If1020405368b1c18ac7b5d3f3a7e5a759433ede0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2349240
Tested-by: Aaron Massey <aaronmassey@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Commit-Queue: Aaron Massey <aaronmassey@chromium.org>
diff --git a/contrib/emacs/gerrit.el b/contrib/emacs/gerrit.el
index 87106d7..27d5cc1 100644
--- a/contrib/emacs/gerrit.el
+++ b/contrib/emacs/gerrit.el
@@ -12,11 +12,6 @@
   "The system path to the repo project root."
   :type 'string)
 
-(defcustom gerrit-repo-manifest
-  nil
-  "The system path path to repo manifest file."
-  :type 'string)
-
 (defcustom gerrit-git-cookies
   (expand-file-name ".gitcookies" "~/")
   "Path to gitcookies associated with your Gerrit account."
@@ -36,12 +31,6 @@
   "The executable used to parse the repo manifest as an alist.")
 
 
-(defvar gerrit-repo-manifest
-  nil
-  "The system path path to repo manifest file.
-Default is repo_root_path/.repo/manifests/default.xml")
-
-
 (defvar gerrit--change-to-host
   (make-hash-table :test 'equal)
   "Map showing => host.
@@ -50,12 +39,6 @@
 
 (defun gerrit-init ()
   "Initialize Repo Gerrit state."
-  (unless gerrit-repo-manifest
-    (setq gerrit-repo-manifest
-          (expand-file-name
-           ".repo/manifests/default.xml"
-           gerrit-repo-root)))
-
   ;; Authenticate using cURL with gitcookies.
   ;; Use let to shadow dynamic scoped var to avoid
   ;; side effects with other users of request.el
@@ -64,7 +47,7 @@
 
     (gerrit--init-global-comment-map)
     (gerrit--init-global-repo-project-path-map gerrit--manifest-parser
-                                               gerrit-repo-manifest)))
+                                               gerrit-repo-root)))
 
 
 (defvar gerrit--change-to-filepath-comments nil
@@ -141,9 +124,9 @@
 
 
 (cl-defun gerrit--project-branch-pair-to-path-map (path-to-manifest-parser-exec
-                                                   abs-path-to-manifest)
+                                                   path-to-repo-root)
   "Return map (project . dest-branch) => path-from-repo-root.
-Parses the manifest given manifest file using the given parser executable.
+Parses the repo manifest using the given parser executable.
 Assumes that stdout of parser is a Lisp alist of the form:
 ((project . dest-branch) . path-from-repo-root)."
   (let (parsed-alist
@@ -157,8 +140,8 @@
                                nil
                                tmp-buffer-name
                                nil
-                               abs-path-to-manifest))
-      (message "Error parsing manifest file investigate %s" tmp-buffer-name)
+                               path-to-repo-root))
+      (message "Error parsing manifest investigate %s" tmp-buffer-name)
       (cl-return-from gerrit--project-branch-pair-to-path-map nil))
 
     (save-excursion
@@ -174,7 +157,7 @@
 
 
 (defun gerrit--init-global-repo-project-path-map (path-to-manifest-parser-exec
-                                                  abs-path-to-manifest)
+                                                  path-to-repo-root)
   "Initializes `gerrit--project-branch-pair-to-projectpath`.
 This function is idempotent."
   ;; Here we use Python expat sax parser as it's considerably faster.
@@ -182,12 +165,12 @@
     (setf gerrit--project-branch-pair-to-projectpath
           (gerrit--project-branch-pair-to-path-map
            path-to-manifest-parser-exec
-           abs-path-to-manifest))))
+           path-to-repo-root))))
 
 
 (defun gerrit--get-abs-path-to-file (filepath-from-project-git-root
                                      project-branch-pair
-                                     abs-path-to-repo-root)
+                                     path-to-repo-root)
   "Returns full system path of the first argument.
 `gerrit--project-branch-pair-to-projectpath` must be initialized."
   (expand-file-name
@@ -197,7 +180,7 @@
      (gethash (cons (gethash "project" project-branch-pair)
                     (gethash "branch" project-branch-pair))
               gerrit--project-branch-pair-to-projectpath)
-     abs-path-to-repo-root))))
+     path-to-repo-root))))
 
 
 (provide 'gerrit)
diff --git a/contrib/emacs/manifest_parser.py b/contrib/emacs/manifest_parser.py
index 29147e7..d424419 100755
--- a/contrib/emacs/manifest_parser.py
+++ b/contrib/emacs/manifest_parser.py
@@ -4,25 +4,27 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-"""This is a utility script for parsing repo manifest files used by gerrit.el"""
+"""This is a utility script for parsing the repo manifest used by gerrit.el"""
 
 import xml.parsers.expat as xml
 import sys
 import argparse
 import pathlib
+import subprocess
 
-def parse_manifest_projects_to_lisp_alist(manifest_path):
-    """Parse manifest xml to Lisp alist.
+def parse_manifest_projects_to_lisp_alist(repo_root_path):
+    """Parse repo manifest to Lisp alist.
 
     Any project without a dest-branch attribute is skipped.
 
     Args:
-        manifest_path: The path to a trusted repo manifest file to parse project elements.
+        repo_root_path: The path to a repo root.
 
     Returns:
         Lisp readable alist with elements of the form ((name . dest-branch) . path)
 
     Raises:
+        CalledProcessError: The repo tool threw an error getting the manifest.
         ExpatError: An error occured when attempting to parse.
     """
 
@@ -52,21 +54,26 @@
 
     p = xml.ParserCreate()
     p.StartElementHandler = _project_elem_handler
-    with open(manifest_path, 'rb') as manifest_fd:
-        p.ParseFile(manifest_fd)
-        return '({})'.format(''.join(assoc_list_entries))
+
+    repo_cmd = ['repo', '--no-pager', 'manifest']
+    repo_cmd_result = subprocess.run(repo_cmd,
+                                     cwd=repo_root_path.expanduser().resolve(),
+                                     capture_output=True,
+                                     check=True)
+    p.Parse(repo_cmd_result.stdout)
+    return '({})'.format(''.join(assoc_list_entries))
 
 
 def main(argv):
     """main."""
     arg_parser = argparse.ArgumentParser()
-    arg_parser.add_argument('repo_manifest_path',
+    arg_parser.add_argument('repo_root_path',
                             type=pathlib.Path,
-                            help='System path to repo manifest xml file.')
+                            help='System path to repo root.')
     args = arg_parser.parse_args(argv)
 
     try:
-        print(parse_manifest_projects_to_lisp_alist(args.repo_manifest_path))
+        print(parse_manifest_projects_to_lisp_alist(args.repo_root_path))
         return 0
 
     except xml.ExpatError as err: