1#!/usr/bin/python
2# Copyright 2016 Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Convert gclient's DEPS file to repo's manifest xml file."""
32
33from __future__ import print_function
34
35import argparse
36import os
37import sys
38
39
40REMOTES = {
41    'chromium': 'https://chromium.googlesource.com/',
42    'github': 'https://github.com/',
43}
44REVIEWS = {
45    'chromium': 'https://chromium-review.googlesource.com',
46}
47
48MANIFEST_HEAD = """<?xml version='1.0' encoding='UTF-8'?>
49<!-- AUTOGENERATED BY %(prog)s; DO NOT EDIT -->
50<manifest>
51
52  <default revision='refs/heads/master'
53           remote='chromium'
54           sync-c='true'
55           sync-j='8' />
56"""
57
58MANIFEST_REMOTE = """
59  <remote  name='%(name)s'
60           fetch='%(fetch)s'
61           review='%(review)s' />
62"""
63
64MANIFEST_PROJECT = """
65  <project path='%(path)s'
66           name='%(name)s'
67           revision='%(revision)s'
68           remote='%(remote)s' />
69"""
70
71MANIFEST_TAIL = """
72</manifest>
73"""
74
75
76def ConvertDepsToManifest(deps, manifest):
77  """Convert the |deps| file to the |manifest|."""
78  # Load the DEPS file data.
79  ctx = {}
80  execfile(deps, ctx)
81
82  new_contents = ''
83
84  # Write out the common header.
85  data = {
86      'prog': os.path.basename(__file__),
87  }
88  new_contents += MANIFEST_HEAD % data
89
90  # Write out the <remote> sections.
91  for name, fetch in REMOTES.items():
92    data = {
93        'name': name,
94        'fetch': fetch,
95        'review': REVIEWS.get(name, ''),
96    }
97    new_contents += MANIFEST_REMOTE % data
98
99  # Write out the main repo itself.
100  data = {
101      'path': 'src',
102      'name': 'breakpad/breakpad',
103      'revision': 'refs/heads/master',
104      'remote': 'chromium',
105  }
106  new_contents += MANIFEST_PROJECT % data
107
108  # Write out the <project> sections.
109  for path, url in ctx['deps'].items():
110    for name, fetch in REMOTES.items():
111      if url.startswith(fetch):
112        remote = name
113        break
114    else:
115      raise ValueError('Unknown DEPS remote: %s: %s' % (path, url))
116
117    # The DEPS url will look like:
118    # https://chromium.googlesource.com/external/gyp/@e8ab0833a42691cd2
119    remote_path, rev = url.split('@')
120    remote_path = remote_path[len(fetch):]
121
122    # If it's not a revision, assume it's a tag.  Repo wants full ref names.
123    if len(rev) != 40:
124      rev = 'refs/tags/%s' % rev
125
126    data = {
127        'path': path,
128        'name': remote_path,
129        'revision': rev,
130        'remote': remote,
131    }
132    new_contents += MANIFEST_PROJECT % data
133
134  # Write out the common footer.
135  new_contents += MANIFEST_TAIL
136
137  # See if the manifest has actually changed contents to avoid thrashing.
138  try:
139    old_contents = open(manifest).read()
140  except IOError:
141    # In case the file doesn't exist yet.
142    old_contents = ''
143  if old_contents != new_contents:
144    print('Updating %s due to changed %s' % (manifest, deps))
145    with open(manifest, 'w') as fp:
146      fp.write(new_contents)
147
148
149def GetParser():
150  """Return a CLI parser."""
151  parser = argparse.ArgumentParser(description=__doc__)
152  parser.add_argument('deps',
153                      help='The DEPS file to convert')
154  parser.add_argument('manifest',
155                      help='The manifest xml to generate')
156  return parser
157
158
159def main(argv):
160  """The main func!"""
161  parser = GetParser()
162  opts = parser.parse_args(argv)
163  ConvertDepsToManifest(opts.deps, opts.manifest)
164
165
166if __name__ == '__main__':
167  sys.exit(main(sys.argv[1:]))
168