summaryrefslogtreecommitdiffstats
path: root/android_webview/buildbot/generate_local_manifest.py
blob: 59cb2de1dc6d1ddb87eda477e76ea2b706528199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Generate local manifest in an Android repository.

This is used to generate a local manifest in an Android repository. The purpose
of the generated manifest is to remove the set of projects that exist under a
certain path.
"""

from optparse import OptionParser
import os
import xml.etree.ElementTree as ET

def createLocalManifest(manifest_path, local_manifest_path, path_to_exclude,
                        pinned_projects=None):
  manifest_tree = ET.parse(manifest_path)
  local_manifest_root = ET.Element('manifest')

  def remove_project(project):
    remove_project = ET.SubElement(local_manifest_root, 'remove-project')
    remove_project.set('name', project.get('name'))

  def pin_project(project, revision):
    pin_project = ET.SubElement(local_manifest_root, 'project')
    pin_project.set('name', project.get('name'))
    if project.get('path') != None:
      pin_project.set('path', project.get('path'))
    pin_project.set('revision', revision)

  for project in manifest_tree.getroot().findall('project'):
    project_path = project.get('path')
    project_name = project.get('name')
    exclude_project = ((project_path != None and
                        project_path.startswith(path_to_exclude)) or
                       (project_path == None and
                        project_name.startswith(path_to_exclude)))
    if exclude_project:
      print 'Excluding project name="%s" path="%s"' % (project_name,
                                                       project_path)
      remove_project(project)
      continue

    pinned_projects = pinned_projects or []
    for pinned in pinned_projects:
      if pinned['path'] == project_path and pinned['name'] == project_name:
        remove_project(project)
        pin_project(project, pinned['revision'])
        break

  local_manifest_tree = ET.ElementTree(local_manifest_root)
  local_manifest_dir = os.path.dirname(local_manifest_path)
  if not os.path.exists(local_manifest_dir):
    os.makedirs(local_manifest_dir)
  local_manifest_tree.write(local_manifest_path,
                            xml_declaration=True,
                            encoding='UTF-8',
                            method='xml')

def main():
  usage = 'usage: %prog [options] <android_build_top> <path_to_exclude>'
  parser = OptionParser(usage=usage)
  parser.add_option('--ndk-revision', dest='ndk_revision',
                    help='pin the ndk project at a particular REVISION',
                    metavar='REVISION', default=None)
  parser.add_option('--manifest_filename', dest='manifest_filename',
                    help='name of the manifest file', default='default.xml')
  (options, args) = parser.parse_args()

  if len(args) != 2:
    parser.error('Wrong number of arguments.')

  android_build_top = args[0]
  path_to_exclude = args[1]

  manifest_filename = options.manifest_filename

  manifest_path = os.path.join(android_build_top, '.repo/manifests',
                               manifest_filename)
  local_manifest_path = os.path.join(android_build_top,
                                     '.repo/local_manifest.xml')

  pinned_projects = []
  if options.ndk_revision:
    pinned_projects = [{
        'path': 'ndk',
        'name': 'platform/ndk',
        'revision' : options.ndk_revision,
    },]

  print 'Path to exclude: %s' % path_to_exclude
  print 'Path to manifest file: %s' % manifest_path
  createLocalManifest(manifest_path, local_manifest_path, path_to_exclude,
                      pinned_projects)
  print 'Local manifest created in: %s' % local_manifest_path

if __name__ == '__main__':
  main()