1#!/usr/bin/env python
2# Copyright (c) 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Generate local manifest in an Android repository.
7
8This is used to generate a local manifest in an Android repository. The purpose
9of the generated manifest is to remove the set of projects that exist under a
10certain path.
11"""
12
13import os
14import sys
15import xml.etree.ElementTree as ET
16
17def createLocalManifest(manifest_path, local_manifest_path, path_to_exclude):
18  manifest_tree = ET.parse(manifest_path)
19  local_manifest_root = ET.Element('manifest')
20  for project in manifest_tree.getroot().findall('project'):
21    project_path = project.get('path')
22    project_name = project.get('name')
23    exclude_project = ((project_path != None and
24                        project_path.startswith(path_to_exclude)) or
25                       (project_path == None and
26                        project_name.startswith(path_to_exclude)))
27    if not exclude_project:
28      continue
29    print 'Excluding project name="%s" path="%s"' % (project_name,
30                                                     project_path)
31    remove_project = ET.SubElement(local_manifest_root, 'remove-project')
32    remove_project.set('name', project.get('name'))
33
34  local_manifest_tree = ET.ElementTree(local_manifest_root)
35  local_manifest_dir = os.path.dirname(local_manifest_path)
36  if not os.path.exists(local_manifest_dir):
37    os.makedirs(local_manifest_dir)
38  local_manifest_tree.write(local_manifest_path,
39                            xml_declaration=True,
40                            encoding='UTF-8',
41                            method='xml')
42
43def main():
44  if len(sys.argv) < 3:
45    print 'Too few arguments.'
46    sys.exit(-1)
47
48  android_build_top = sys.argv[1]
49  path_to_exclude = sys.argv[2]
50
51  manifest_filename = 'default.xml'
52  if len(sys.argv) >= 4:
53    manifest_filename = sys.argv[3]
54
55  manifest_path = os.path.join(android_build_top, '.repo/manifests',
56                               manifest_filename)
57  local_manifest_path = os.path.join(android_build_top,
58                                     '.repo/local_manifest.xml')
59
60
61  print 'Path to exclude: %s' % path_to_exclude
62  print 'Path to manifest file: %s' % manifest_path
63  createLocalManifest(manifest_path, local_manifest_path, path_to_exclude)
64  print 'Local manifest created in: %s' % local_manifest_path
65
66if __name__ == '__main__':
67  main()
68