OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """This module allows adding and deleting of projects to the local manifest.""" |
| 8 |
| 9 import sys |
| 10 import optparse |
| 11 import os |
| 12 import xml.etree.ElementTree as ElementTree |
| 13 |
| 14 from cros_build_lib import Die |
| 15 |
| 16 |
| 17 def _FindRepoDir(): |
| 18 cwd = os.getcwd() |
| 19 while cwd != '/': |
| 20 repo_dir = os.path.join(cwd, '.repo') |
| 21 if os.path.isdir(repo_dir): |
| 22 return repo_dir |
| 23 cwd = os.path.dirname(cwd) |
| 24 return None |
| 25 |
| 26 |
| 27 class LocalManifest: |
| 28 """Class which provides an abstraction for manipulating the local manifest.""" |
| 29 |
| 30 def __init__(self, text=None): |
| 31 self._text = text or '<manifest>\n</manifest>' |
| 32 |
| 33 def Parse(self): |
| 34 """Parse the manifest.""" |
| 35 self._root = ElementTree.fromstring(self._text) |
| 36 |
| 37 def AddWorkonProject(self, name, path): |
| 38 """Add a new workon project if it is not already in the manifest. |
| 39 |
| 40 Returns: |
| 41 True on success. |
| 42 """ |
| 43 |
| 44 for project in self._root.findall('project'): |
| 45 if project.attrib['path'] == path or project.attrib['name'] == name: |
| 46 return False |
| 47 self._AddProject(name, path, workon='True') |
| 48 return True |
| 49 |
| 50 def _AddProject(self, name, path, workon='False'): |
| 51 element = ElementTree.Element('project', name=name, path=path, |
| 52 workon=workon) |
| 53 element.tail = '\n' |
| 54 self._root.append(element) |
| 55 |
| 56 def ToString(self): |
| 57 return ElementTree.tostring(self._root, encoding='UTF-8') |
| 58 |
| 59 |
| 60 def main(argv): |
| 61 usage = 'usage: %prog add [options] <name> <path>' |
| 62 parser = optparse.OptionParser(usage=usage) |
| 63 parser.add_option('-w', '--workon', action='store_true', dest='workon', |
| 64 default=False, help='Is this a workon package?') |
| 65 parser.add_option('-f', '--file', dest='manifest', |
| 66 help='Non-default manifest file to read.') |
| 67 (options, args) = parser.parse_args(argv[2:]) |
| 68 if len(args) < 2: |
| 69 parser.error('Not enough arguments') |
| 70 if argv[1] not in ['add']: |
| 71 parser.error('Unsupported command: %s.' % argv[1]) |
| 72 if not options.workon: |
| 73 parser.error('Adding of non-workon projects is currently unsupported.') |
| 74 (name, path) = (args[0], args[1]) |
| 75 |
| 76 repo_dir = _FindRepoDir() |
| 77 if not repo_dir: |
| 78 Die("Unable to find repo dir.") |
| 79 local_manifest = options.manifest or \ |
| 80 os.path.join(_FindRepoDir(), 'local_manifest.xml') |
| 81 if os.path.isfile(local_manifest): |
| 82 ptree = LocalManifest(open(local_manifest).read()) |
| 83 else: |
| 84 ptree = LocalManifest() |
| 85 ptree.Parse() |
| 86 if not ptree.AddWorkonProject(name, path): |
| 87 Die('Path "%s" or name "%s" already exits in the manifest.' % |
| 88 (path, name)) |
| 89 try: |
| 90 print >> open(local_manifest, 'w'), ptree.ToString() |
| 91 except Exception, e: |
| 92 Die('Error writing to manifest: %s' % e) |
| 93 |
| 94 |
| 95 if __name__ == '__main__': |
| 96 main(sys.argv) |
OLD | NEW |