deps-to-manifest.py 4.74 KB
Newer Older
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#!/usr/bin/python
# Copyright 2016 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Convert gclient's DEPS file to repo's manifest xml file."""

from __future__ import print_function

import argparse
import os
import sys


REMOTES = {
    'chromium': 'https://chromium.googlesource.com/',
    'github': 'https://github.com/',
}
REVIEWS = {
    'chromium': 'https://chromium-review.googlesource.com',
}

MANIFEST_HEAD = """<?xml version='1.0' encoding='UTF-8'?>
<!-- AUTOGENERATED BY %(prog)s; DO NOT EDIT -->
<manifest>

  <default revision='refs/heads/master'
           remote='chromium'
           sync-c='true'
           sync-j='8' />
"""

MANIFEST_REMOTE = """
  <remote  name='%(name)s'
           fetch='%(fetch)s'
           review='%(review)s' />
"""

MANIFEST_PROJECT = """
  <project path='%(path)s'
           name='%(name)s'
           revision='%(revision)s'
           remote='%(remote)s' />
"""

MANIFEST_TAIL = """
</manifest>
"""


def ConvertDepsToManifest(deps, manifest):
  """Convert the |deps| file to the |manifest|."""
  # Load the DEPS file data.
  ctx = {}
  execfile(deps, ctx)

  new_contents = ''

  # Write out the common header.
  data = {
      'prog': os.path.basename(__file__),
  }
  new_contents += MANIFEST_HEAD % data

  # Write out the <remote> sections.
  for name, fetch in REMOTES.items():
    data = {
        'name': name,
        'fetch': fetch,
        'review': REVIEWS.get(name, ''),
    }
    new_contents += MANIFEST_REMOTE % data

  # Write out the main repo itself.
  data = {
      'path': 'src',
      'name': 'breakpad/breakpad',
      'revision': 'refs/heads/master',
      'remote': 'chromium',
  }
  new_contents += MANIFEST_PROJECT % data

  # Write out the <project> sections.
  for path, url in ctx['deps'].items():
    for name, fetch in REMOTES.items():
      if url.startswith(fetch):
        remote = name
        break
    else:
      raise ValueError('Unknown DEPS remote: %s: %s' % (path, url))

    # The DEPS url will look like:
    # https://chromium.googlesource.com/external/gyp/@e8ab0833a42691cd2
    remote_path, rev = url.split('@')
    remote_path = remote_path[len(fetch):]

    # If it's not a revision, assume it's a tag.  Repo wants full ref names.
    if len(rev) != 40:
      rev = 'refs/tags/%s' % rev

    data = {
        'path': path,
        'name': remote_path,
        'revision': rev,
        'remote': remote,
    }
    new_contents += MANIFEST_PROJECT % data

  # Write out the common footer.
  new_contents += MANIFEST_TAIL

  # See if the manifest has actually changed contents to avoid thrashing.
  try:
    old_contents = open(manifest).read()
  except IOError:
    # In case the file doesn't exist yet.
    old_contents = ''
  if old_contents != new_contents:
    print('Updating %s due to changed %s' % (manifest, deps))
    with open(manifest, 'w') as fp:
      fp.write(new_contents)


def GetParser():
  """Return a CLI parser."""
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument('deps',
                      help='The DEPS file to convert')
  parser.add_argument('manifest',
                      help='The manifest xml to generate')
  return parser


def main(argv):
  """The main func!"""
  parser = GetParser()
  opts = parser.parse_args(argv)
  ConvertDepsToManifest(opts.deps, opts.manifest)


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))