Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(536)

Unified Diff: bin/loman.py

Issue 3331013: loman: tool for updating local_manifest (initial commit) (Closed) Base URL: http://git.chromium.org/git/crosutils.git
Patch Set: Fixed whitespace. Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « bin/loman ('k') | bin/loman_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: bin/loman.py
diff --git a/bin/loman.py b/bin/loman.py
new file mode 100755
index 0000000000000000000000000000000000000000..7575e2904f4e37ab27024aead6649e40d3b94627
--- /dev/null
+++ b/bin/loman.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python
+
+# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""This module allows adding and deleting of projects to the local manifest."""
+
+import sys
+import optparse
+import os
+import xml.etree.ElementTree as ElementTree
+
+from cros_build_lib import Die
+
+
+def _FindRepoDir():
+ cwd = os.getcwd()
+ while cwd != '/':
+ repo_dir = os.path.join(cwd, '.repo')
+ if os.path.isdir(repo_dir):
+ return repo_dir
+ cwd = os.path.dirname(cwd)
+ return None
+
+
+class LocalManifest:
+ """Class which provides an abstraction for manipulating the local manifest."""
+
+ def __init__(self, text=None):
+ self._text = text or '<manifest>\n</manifest>'
+
+ def Parse(self):
+ """Parse the manifest."""
+ self._root = ElementTree.fromstring(self._text)
+
+ def AddWorkonProject(self, name, path):
+ """Add a new workon project if it is not already in the manifest.
+
+ Returns:
+ True on success.
+ """
+
+ for project in self._root.findall('project'):
+ if project.attrib['path'] == path or project.attrib['name'] == name:
+ return False
+ self._AddProject(name, path, workon='True')
+ return True
+
+ def _AddProject(self, name, path, workon='False'):
+ element = ElementTree.Element('project', name=name, path=path,
+ workon=workon)
+ element.tail = '\n'
+ self._root.append(element)
+
+ def ToString(self):
+ return ElementTree.tostring(self._root, encoding='UTF-8')
+
+
+def main(argv):
+ usage = 'usage: %prog add [options] <name> <path>'
+ parser = optparse.OptionParser(usage=usage)
+ parser.add_option('-w', '--workon', action='store_true', dest='workon',
+ default=False, help='Is this a workon package?')
+ parser.add_option('-f', '--file', dest='manifest',
+ help='Non-default manifest file to read.')
+ (options, args) = parser.parse_args(argv[2:])
+ if len(args) < 2:
+ parser.error('Not enough arguments')
+ if argv[1] not in ['add']:
+ parser.error('Unsupported command: %s.' % argv[1])
+ if not options.workon:
+ parser.error('Adding of non-workon projects is currently unsupported.')
+ (name, path) = (args[0], args[1])
+
+ repo_dir = _FindRepoDir()
+ if not repo_dir:
+ Die("Unable to find repo dir.")
+ local_manifest = options.manifest or \
+ os.path.join(_FindRepoDir(), 'local_manifest.xml')
+ if os.path.isfile(local_manifest):
+ ptree = LocalManifest(open(local_manifest).read())
+ else:
+ ptree = LocalManifest()
+ ptree.Parse()
+ if not ptree.AddWorkonProject(name, path):
+ Die('Path "%s" or name "%s" already exits in the manifest.' %
+ (path, name))
+ try:
+ print >> open(local_manifest, 'w'), ptree.ToString()
+ except Exception, e:
+ Die('Error writing to manifest: %s' % e)
+
+
+if __name__ == '__main__':
+ main(sys.argv)
« no previous file with comments | « bin/loman ('k') | bin/loman_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698