| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """This module allows adding and deleting of projects to the local manifest.""" | 7 """This module allows adding and deleting of projects to the local manifest.""" |
| 8 | 8 |
| 9 import sys | 9 import sys |
| 10 import optparse | 10 import optparse |
| 11 import os | 11 import os |
| 12 import xml.etree.ElementTree as ElementTree | 12 import xml.etree.ElementTree as ElementTree |
| 13 | 13 |
| 14 from cros_build_lib import Die | 14 from cros_build_lib import Die |
| 15 | 15 |
| 16 | 16 |
| 17 def _FindRepoDir(): | 17 def _FindRepoDir(): |
| 18 cwd = os.getcwd() | 18 cwd = os.getcwd() |
| 19 while cwd != '/': | 19 while cwd != '/': |
| 20 repo_dir = os.path.join(cwd, '.repo') | 20 repo_dir = os.path.join(cwd, '.repo') |
| 21 if os.path.isdir(repo_dir): | 21 if os.path.isdir(repo_dir): |
| 22 return repo_dir | 22 return repo_dir |
| 23 cwd = os.path.dirname(cwd) | 23 cwd = os.path.dirname(cwd) |
| 24 return None | 24 return None |
| 25 | 25 |
| 26 | 26 |
| 27 def _ReadManifest(manifest, err_not_found=False): |
| 28 if os.path.isfile(manifest): |
| 29 ptree = LocalManifest(open(manifest).read()) |
| 30 elif err_not_found: |
| 31 Die('Manifest file, %s, not found' % manifest) |
| 32 else: |
| 33 ptree = LocalManifest() |
| 34 ptree.Parse() |
| 35 return ptree |
| 36 |
| 37 |
| 27 class LocalManifest: | 38 class LocalManifest: |
| 28 """Class which provides an abstraction for manipulating the local manifest.""" | 39 """Class which provides an abstraction for manipulating the local manifest.""" |
| 29 | 40 |
| 30 def __init__(self, text=None): | 41 def __init__(self, text=None): |
| 31 self._text = text or '<manifest>\n</manifest>' | 42 self._text = text or '<manifest>\n</manifest>' |
| 32 | 43 |
| 33 def Parse(self): | 44 def Parse(self): |
| 34 """Parse the manifest.""" | 45 """Parse the manifest.""" |
| 35 self._root = ElementTree.fromstring(self._text) | 46 self._root = ElementTree.fromstring(self._text) |
| 36 | 47 |
| 37 def AddWorkonProject(self, name, path): | 48 def AddProjectElement(self, element, workon='False'): |
| 38 """Add a new workon project if it is not already in the manifest. | 49 """Add a new project element to the manifest tree. |
| 39 | 50 |
| 40 Returns: | 51 Returns: |
| 41 True on success. | 52 True on success. |
| 42 """ | 53 """ |
| 43 | 54 name = element.attrib['name'] |
| 55 path = element.attrib['path'] |
| 44 for project in self._root.findall('project'): | 56 for project in self._root.findall('project'): |
| 45 if project.attrib['path'] == path or project.attrib['name'] == name: | 57 if project.attrib['path'] == path or project.attrib['name'] == name: |
| 46 if project.attrib['path'] == path and project.attrib['name'] == name: | 58 if project.attrib['path'] == path and project.attrib['name'] == name: |
| 47 return True | 59 return True |
| 48 else: | 60 else: |
| 49 return False | 61 return False |
| 50 self._AddProject(name, path, workon='True') | 62 element.attrib['workon'] = workon |
| 63 element.tail = '\n' |
| 64 self._root.append(element) |
| 51 return True | 65 return True |
| 52 | 66 |
| 53 def _AddProject(self, name, path, workon='False'): | 67 def AddProject(self, name, path, workon='False'): |
| 54 element = ElementTree.Element('project', name=name, path=path, | 68 """Add a workon project if it is not already in the manifest. |
| 55 workon=workon) | 69 |
| 56 element.tail = '\n' | 70 Returns: |
| 57 self._root.append(element) | 71 True on success. |
| 72 """ |
| 73 element = ElementTree.Element('project', name=name, path=path) |
| 74 return self.AddProjectElement(element, workon=workon) |
| 75 |
| 76 def AddWorkonProjectElement(self, element): |
| 77 return self.AddProjectElement(element, workon='True') |
| 78 |
| 79 def AddWorkonProject(self, name, path): |
| 80 return self.AddProject(name, path, workon='True') |
| 81 |
| 82 def GetProject(self, name): |
| 83 """Accessor method for getting a project node from the manifest tree. |
| 84 |
| 85 Returns: |
| 86 project element node from ElementTree, otherwise, None |
| 87 """ |
| 88 |
| 89 for project in self._root.findall('project'): |
| 90 if project.attrib['name'] == name: |
| 91 return project |
| 92 return None |
| 58 | 93 |
| 59 def ToString(self): | 94 def ToString(self): |
| 60 return ElementTree.tostring(self._root, encoding='UTF-8') | 95 return ElementTree.tostring(self._root, encoding='UTF-8') |
| 61 | 96 |
| 62 | 97 |
| 63 def main(argv): | 98 def main(argv): |
| 64 usage = 'usage: %prog add [options] <name> <path>' | 99 repo_dir = _FindRepoDir() |
| 100 if not repo_dir: |
| 101 Die("Unable to find repo dir.") |
| 102 |
| 103 usage = 'usage: %prog add [options] <name>' |
| 65 parser = optparse.OptionParser(usage=usage) | 104 parser = optparse.OptionParser(usage=usage) |
| 66 parser.add_option('-w', '--workon', action='store_true', dest='workon', | 105 parser.add_option('-w', '--workon', action='store_true', dest='workon', |
| 67 default=False, help='Is this a workon package?') | 106 default=False, help='Is this a workon package?') |
| 68 parser.add_option('-f', '--file', dest='manifest', | 107 parser.add_option('-f', '--file', dest='local_manifest', |
| 108 default='%s/local_manifest.xml' % repo_dir, |
| 69 help='Non-default manifest file to read.') | 109 help='Non-default manifest file to read.') |
| 110 parser.add_option('-d', '--default', dest='full_manifest', |
| 111 default='%s/manifests/full.xml' % repo_dir, |
| 112 help='Default manifest file to read.') |
| 70 (options, args) = parser.parse_args(argv[2:]) | 113 (options, args) = parser.parse_args(argv[2:]) |
| 71 if len(args) < 2: | 114 if len(args) < 1: |
| 72 parser.error('Not enough arguments') | 115 parser.error('Not enough arguments') |
| 73 if argv[1] not in ['add']: | 116 if argv[1] not in ['add']: |
| 74 parser.error('Unsupported command: %s.' % argv[1]) | 117 parser.error('Unsupported command: %s.' % argv[1]) |
| 75 if not options.workon: | 118 if not options.workon: |
| 76 parser.error('Adding of non-workon projects is currently unsupported.') | 119 parser.error('Adding of non-workon projects is currently unsupported.') |
| 77 (name, path) = (args[0], args[1]) | 120 name = args[0] |
| 78 | 121 |
| 79 repo_dir = _FindRepoDir() | 122 local_tree = _ReadManifest(options.local_manifest) |
| 80 if not repo_dir: | 123 full_tree = _ReadManifest(options.full_manifest) |
| 81 Die("Unable to find repo dir.") | 124 |
| 82 local_manifest = options.manifest or \ | 125 project_element = full_tree.GetProject(name) |
| 83 os.path.join(_FindRepoDir(), 'local_manifest.xml') | 126 if project_element == None: |
| 84 if os.path.isfile(local_manifest): | 127 Die('No project named, %s, in the default manifest.' % name) |
| 85 ptree = LocalManifest(open(local_manifest).read()) | 128 success = local_tree.AddWorkonProjectElement(project_element) |
| 86 else: | 129 if not success: |
| 87 ptree = LocalManifest() | 130 Die('name "%s" already exits with a different path.' % name) |
| 88 ptree.Parse() | 131 |
| 89 if not ptree.AddWorkonProject(name, path): | |
| 90 Die('Path "%s" or name "%s" already exits in the manifest.' % | |
| 91 (path, name)) | |
| 92 try: | 132 try: |
| 93 print >> open(local_manifest, 'w'), ptree.ToString() | 133 print >> open(options.local_manifest, 'w'), local_tree.ToString() |
| 94 except Exception, e: | 134 except Exception, e: |
| 95 Die('Error writing to manifest: %s' % e) | 135 Die('Error writing to manifest: %s' % e) |
| 96 | 136 |
| 97 | 137 |
| 98 if __name__ == '__main__': | 138 if __name__ == '__main__': |
| 99 main(sys.argv) | 139 main(sys.argv) |
| OLD | NEW |