2008-10-21 14:00:00 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import xml.dom.minidom
|
|
|
|
|
2009-07-03 22:26:17 +00:00
|
|
|
from git_config import GitConfig
|
|
|
|
from git_config import IsId
|
2009-06-03 23:01:11 +00:00
|
|
|
from manifest import Manifest
|
2009-07-03 22:26:17 +00:00
|
|
|
from project import RemoteSpec
|
|
|
|
from project import Project
|
|
|
|
from project import MetaProject
|
|
|
|
from project import R_HEADS
|
|
|
|
from project import HEAD
|
2008-10-21 14:00:00 +00:00
|
|
|
from error import ManifestParseError
|
|
|
|
|
|
|
|
MANIFEST_FILE_NAME = 'manifest.xml'
|
2008-10-23 23:19:27 +00:00
|
|
|
LOCAL_MANIFEST_NAME = 'local_manifest.xml'
|
2009-07-03 22:29:02 +00:00
|
|
|
R_M = 'refs/remotes/m/'
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
class _Default(object):
|
|
|
|
"""Project defaults within the manifest."""
|
|
|
|
|
2009-05-30 01:38:17 +00:00
|
|
|
revisionExpr = None
|
2008-10-21 14:00:00 +00:00
|
|
|
remote = None
|
|
|
|
|
2009-05-19 21:58:02 +00:00
|
|
|
class _XmlRemote(object):
|
|
|
|
def __init__(self,
|
|
|
|
name,
|
|
|
|
fetch=None,
|
|
|
|
review=None):
|
|
|
|
self.name = name
|
|
|
|
self.fetchUrl = fetch
|
|
|
|
self.reviewUrl = review
|
|
|
|
|
|
|
|
def ToRemoteSpec(self, projectName):
|
|
|
|
url = self.fetchUrl
|
|
|
|
while url.endswith('/'):
|
|
|
|
url = url[:-1]
|
|
|
|
url += '/%s.git' % projectName
|
|
|
|
return RemoteSpec(self.name, url, self.reviewUrl)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-06-03 23:01:11 +00:00
|
|
|
class XmlManifest(Manifest):
|
2008-10-21 14:00:00 +00:00
|
|
|
"""manages the repo configuration file"""
|
|
|
|
|
|
|
|
def __init__(self, repodir):
|
2009-06-03 23:01:11 +00:00
|
|
|
Manifest.__init__(self, repodir)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile = os.path.join(repodir, MANIFEST_FILE_NAME)
|
2008-10-21 14:00:00 +00:00
|
|
|
self.manifestProject = MetaProject(self, 'manifests',
|
2008-11-04 16:11:53 +00:00
|
|
|
gitdir = os.path.join(repodir, 'manifests.git'),
|
|
|
|
worktree = os.path.join(repodir, 'manifests'))
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
self._Unload()
|
|
|
|
|
|
|
|
def Link(self, name):
|
|
|
|
"""Update the repo metadata to use a different manifest.
|
|
|
|
"""
|
|
|
|
path = os.path.join(self.manifestProject.worktree, name)
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
raise ManifestParseError('manifest %s not found' % name)
|
|
|
|
|
2009-06-03 23:01:11 +00:00
|
|
|
old = self._manifestFile
|
2008-10-21 14:00:00 +00:00
|
|
|
try:
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile = path
|
2008-10-21 14:00:00 +00:00
|
|
|
self._Unload()
|
|
|
|
self._Load()
|
|
|
|
finally:
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile = old
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
try:
|
2009-06-03 23:01:11 +00:00
|
|
|
if os.path.exists(self._manifestFile):
|
|
|
|
os.remove(self._manifestFile)
|
|
|
|
os.symlink('manifests/%s' % name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
except OSError, e:
|
|
|
|
raise ManifestParseError('cannot link manifest %s' % name)
|
|
|
|
|
2009-03-05 18:32:38 +00:00
|
|
|
def _RemoteToXml(self, r, doc, root):
|
|
|
|
e = doc.createElement('remote')
|
|
|
|
root.appendChild(e)
|
|
|
|
e.setAttribute('name', r.name)
|
|
|
|
e.setAttribute('fetch', r.fetchUrl)
|
|
|
|
if r.reviewUrl is not None:
|
|
|
|
e.setAttribute('review', r.reviewUrl)
|
|
|
|
|
|
|
|
def Save(self, fd, peg_rev=False):
|
|
|
|
"""Write the current manifest out to the given file descriptor.
|
|
|
|
"""
|
|
|
|
doc = xml.dom.minidom.Document()
|
|
|
|
root = doc.createElement('manifest')
|
|
|
|
doc.appendChild(root)
|
|
|
|
|
|
|
|
d = self.default
|
|
|
|
sort_remotes = list(self.remotes.keys())
|
|
|
|
sort_remotes.sort()
|
|
|
|
|
|
|
|
for r in sort_remotes:
|
|
|
|
self._RemoteToXml(self.remotes[r], doc, root)
|
|
|
|
if self.remotes:
|
|
|
|
root.appendChild(doc.createTextNode(''))
|
|
|
|
|
|
|
|
have_default = False
|
|
|
|
e = doc.createElement('default')
|
|
|
|
if d.remote:
|
|
|
|
have_default = True
|
|
|
|
e.setAttribute('remote', d.remote.name)
|
2009-05-30 01:38:17 +00:00
|
|
|
if d.revisionExpr:
|
2009-03-05 18:32:38 +00:00
|
|
|
have_default = True
|
2009-05-30 01:38:17 +00:00
|
|
|
e.setAttribute('revision', d.revisionExpr)
|
2009-03-05 18:32:38 +00:00
|
|
|
if have_default:
|
|
|
|
root.appendChild(e)
|
|
|
|
root.appendChild(doc.createTextNode(''))
|
|
|
|
|
|
|
|
sort_projects = list(self.projects.keys())
|
|
|
|
sort_projects.sort()
|
|
|
|
|
|
|
|
for p in sort_projects:
|
|
|
|
p = self.projects[p]
|
|
|
|
e = doc.createElement('project')
|
|
|
|
root.appendChild(e)
|
|
|
|
e.setAttribute('name', p.name)
|
|
|
|
if p.relpath != p.name:
|
|
|
|
e.setAttribute('path', p.relpath)
|
|
|
|
if not d.remote or p.remote.name != d.remote.name:
|
|
|
|
e.setAttribute('remote', p.remote.name)
|
|
|
|
if peg_rev:
|
|
|
|
if self.IsMirror:
|
|
|
|
e.setAttribute('revision',
|
2009-05-30 01:38:17 +00:00
|
|
|
p.bare_git.rev_parse(p.revisionExpr + '^0'))
|
2009-03-05 18:32:38 +00:00
|
|
|
else:
|
|
|
|
e.setAttribute('revision',
|
|
|
|
p.work_git.rev_parse(HEAD + '^0'))
|
2009-05-30 01:38:17 +00:00
|
|
|
elif not d.revisionExpr or p.revisionExpr != d.revisionExpr:
|
|
|
|
e.setAttribute('revision', p.revisionExpr)
|
2009-03-05 18:32:38 +00:00
|
|
|
|
|
|
|
for c in p.copyfiles:
|
|
|
|
ce = doc.createElement('copyfile')
|
|
|
|
ce.setAttribute('src', c.src)
|
|
|
|
ce.setAttribute('dest', c.dest)
|
|
|
|
e.appendChild(ce)
|
|
|
|
|
|
|
|
doc.writexml(fd, '', ' ', '\n', 'UTF-8')
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
@property
|
|
|
|
def projects(self):
|
|
|
|
self._Load()
|
|
|
|
return self._projects
|
|
|
|
|
|
|
|
@property
|
|
|
|
def remotes(self):
|
|
|
|
self._Load()
|
|
|
|
return self._remotes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default(self):
|
|
|
|
self._Load()
|
|
|
|
return self._default
|
|
|
|
|
2009-07-03 23:24:57 +00:00
|
|
|
def InitBranch(self):
|
|
|
|
m = self.manifestProject
|
|
|
|
if m.CurrentBranch is None:
|
|
|
|
return m.StartBranch('default')
|
|
|
|
return True
|
|
|
|
|
2009-07-03 22:29:02 +00:00
|
|
|
def SetMRefs(self, project):
|
|
|
|
if self.branch:
|
|
|
|
project._InitAnyMRef(R_M + self.branch)
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _Unload(self):
|
|
|
|
self._loaded = False
|
|
|
|
self._projects = {}
|
|
|
|
self._remotes = {}
|
|
|
|
self._default = None
|
|
|
|
self.branch = None
|
|
|
|
|
|
|
|
def _Load(self):
|
|
|
|
if not self._loaded:
|
2008-11-04 16:22:07 +00:00
|
|
|
m = self.manifestProject
|
2009-07-04 00:24:17 +00:00
|
|
|
b = m.GetBranch(m.CurrentBranch)
|
|
|
|
if b.remote and b.remote.name:
|
|
|
|
m.remote.name = b.remote.name
|
|
|
|
b = b.merge
|
2009-06-25 23:47:30 +00:00
|
|
|
if b is not None and b.startswith(R_HEADS):
|
2008-11-04 16:22:07 +00:00
|
|
|
b = b[len(R_HEADS):]
|
|
|
|
self.branch = b
|
|
|
|
|
2008-10-23 23:19:27 +00:00
|
|
|
self._ParseManifest(True)
|
|
|
|
|
|
|
|
local = os.path.join(self.repodir, LOCAL_MANIFEST_NAME)
|
|
|
|
if os.path.exists(local):
|
|
|
|
try:
|
2009-06-03 23:01:11 +00:00
|
|
|
real = self._manifestFile
|
|
|
|
self._manifestFile = local
|
2008-10-23 23:19:27 +00:00
|
|
|
self._ParseManifest(False)
|
|
|
|
finally:
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile = real
|
2008-10-23 23:19:27 +00:00
|
|
|
|
2008-11-04 15:37:10 +00:00
|
|
|
if self.IsMirror:
|
|
|
|
self._AddMetaProjectMirror(self.repoProject)
|
|
|
|
self._AddMetaProjectMirror(self.manifestProject)
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
self._loaded = True
|
|
|
|
|
2008-10-23 23:19:27 +00:00
|
|
|
def _ParseManifest(self, is_root_file):
|
2009-06-03 23:01:11 +00:00
|
|
|
root = xml.dom.minidom.parse(self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
if not root or not root.childNodes:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"no root node in %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
config = root.childNodes[0]
|
|
|
|
if config.nodeName != 'manifest':
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"no <manifest> in %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
self._manifestFile
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
for node in config.childNodes:
|
2008-11-20 19:42:22 +00:00
|
|
|
if node.nodeName == 'remove-project':
|
|
|
|
name = self._reqatt(node, 'name')
|
|
|
|
try:
|
|
|
|
del self._projects[name]
|
|
|
|
except KeyError:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
'project %s not found' % \
|
|
|
|
(name)
|
|
|
|
|
|
|
|
for node in config.childNodes:
|
2008-10-21 14:00:00 +00:00
|
|
|
if node.nodeName == 'remote':
|
|
|
|
remote = self._ParseRemote(node)
|
|
|
|
if self._remotes.get(remote.name):
|
|
|
|
raise ManifestParseError, \
|
|
|
|
'duplicate remote %s in %s' % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(remote.name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
self._remotes[remote.name] = remote
|
|
|
|
|
|
|
|
for node in config.childNodes:
|
|
|
|
if node.nodeName == 'default':
|
|
|
|
if self._default is not None:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
'duplicate default in %s' % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
self._default = self._ParseDefault(node)
|
|
|
|
if self._default is None:
|
|
|
|
self._default = _Default()
|
|
|
|
|
|
|
|
for node in config.childNodes:
|
|
|
|
if node.nodeName == 'project':
|
|
|
|
project = self._ParseProject(node)
|
|
|
|
if self._projects.get(project.name):
|
|
|
|
raise ManifestParseError, \
|
|
|
|
'duplicate project %s in %s' % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(project.name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
self._projects[project.name] = project
|
|
|
|
|
2008-11-04 15:37:10 +00:00
|
|
|
def _AddMetaProjectMirror(self, m):
|
|
|
|
name = None
|
|
|
|
m_url = m.GetRemote(m.remote.name).url
|
|
|
|
if m_url.endswith('/.git'):
|
|
|
|
raise ManifestParseError, 'refusing to mirror %s' % m_url
|
|
|
|
|
|
|
|
if self._default and self._default.remote:
|
|
|
|
url = self._default.remote.fetchUrl
|
|
|
|
if not url.endswith('/'):
|
|
|
|
url += '/'
|
|
|
|
if m_url.startswith(url):
|
|
|
|
remote = self._default.remote
|
|
|
|
name = m_url[len(url):]
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
s = m_url.rindex('/') + 1
|
2009-05-19 21:58:02 +00:00
|
|
|
remote = _XmlRemote('origin', m_url[:s])
|
2008-11-04 15:37:10 +00:00
|
|
|
name = m_url[s:]
|
|
|
|
|
|
|
|
if name.endswith('.git'):
|
|
|
|
name = name[:-4]
|
|
|
|
|
|
|
|
if name not in self._projects:
|
|
|
|
m.PreSync()
|
|
|
|
gitdir = os.path.join(self.topdir, '%s.git' % name)
|
|
|
|
project = Project(manifest = self,
|
|
|
|
name = name,
|
2009-05-19 21:58:02 +00:00
|
|
|
remote = remote.ToRemoteSpec(name),
|
2008-11-04 15:37:10 +00:00
|
|
|
gitdir = gitdir,
|
|
|
|
worktree = None,
|
|
|
|
relpath = None,
|
2009-05-30 01:38:17 +00:00
|
|
|
revisionExpr = m.revisionExpr,
|
|
|
|
revisionId = None)
|
2008-11-04 15:37:10 +00:00
|
|
|
self._projects[project.name] = project
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _ParseRemote(self, node):
|
|
|
|
"""
|
|
|
|
reads a <remote> element from the manifest file
|
|
|
|
"""
|
|
|
|
name = self._reqatt(node, 'name')
|
|
|
|
fetch = self._reqatt(node, 'fetch')
|
|
|
|
review = node.getAttribute('review')
|
2008-11-06 18:25:35 +00:00
|
|
|
if review == '':
|
|
|
|
review = None
|
2009-05-19 21:58:02 +00:00
|
|
|
return _XmlRemote(name, fetch, review)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
def _ParseDefault(self, node):
|
|
|
|
"""
|
|
|
|
reads a <default> element from the manifest file
|
|
|
|
"""
|
|
|
|
d = _Default()
|
|
|
|
d.remote = self._get_remote(node)
|
2009-05-30 01:38:17 +00:00
|
|
|
d.revisionExpr = node.getAttribute('revision')
|
|
|
|
if d.revisionExpr == '':
|
|
|
|
d.revisionExpr = None
|
2008-10-21 14:00:00 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
def _ParseProject(self, node):
|
|
|
|
"""
|
|
|
|
reads a <project> element from the manifest file
|
|
|
|
"""
|
|
|
|
name = self._reqatt(node, 'name')
|
|
|
|
|
|
|
|
remote = self._get_remote(node)
|
|
|
|
if remote is None:
|
|
|
|
remote = self._default.remote
|
|
|
|
if remote is None:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"no remote for project %s within %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-05-30 01:38:17 +00:00
|
|
|
revisionExpr = node.getAttribute('revision')
|
|
|
|
if not revisionExpr:
|
|
|
|
revisionExpr = self._default.revisionExpr
|
|
|
|
if not revisionExpr:
|
2008-10-21 14:00:00 +00:00
|
|
|
raise ManifestParseError, \
|
|
|
|
"no revision for project %s within %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
path = node.getAttribute('path')
|
|
|
|
if not path:
|
|
|
|
path = name
|
|
|
|
if path.startswith('/'):
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"project %s path cannot be absolute in %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2008-11-04 15:37:10 +00:00
|
|
|
if self.IsMirror:
|
|
|
|
relpath = None
|
|
|
|
worktree = None
|
|
|
|
gitdir = os.path.join(self.topdir, '%s.git' % name)
|
|
|
|
else:
|
|
|
|
worktree = os.path.join(self.topdir, path)
|
|
|
|
gitdir = os.path.join(self.repodir, 'projects/%s.git' % path)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
project = Project(manifest = self,
|
|
|
|
name = name,
|
2009-05-19 21:58:02 +00:00
|
|
|
remote = remote.ToRemoteSpec(name),
|
2008-10-21 14:00:00 +00:00
|
|
|
gitdir = gitdir,
|
|
|
|
worktree = worktree,
|
|
|
|
relpath = path,
|
2009-05-30 01:38:17 +00:00
|
|
|
revisionExpr = revisionExpr,
|
|
|
|
revisionId = None)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
for n in node.childNodes:
|
2009-05-19 20:00:29 +00:00
|
|
|
if n.nodeName == 'copyfile':
|
2008-10-21 14:00:00 +00:00
|
|
|
self._ParseCopyFile(project, n)
|
|
|
|
|
|
|
|
return project
|
|
|
|
|
|
|
|
def _ParseCopyFile(self, project, node):
|
|
|
|
src = self._reqatt(node, 'src')
|
|
|
|
dest = self._reqatt(node, 'dest')
|
2008-11-04 15:37:10 +00:00
|
|
|
if not self.IsMirror:
|
|
|
|
# src is project relative;
|
|
|
|
# dest is relative to the top of the tree
|
2009-03-05 18:32:38 +00:00
|
|
|
project.AddCopyFile(src, dest, os.path.join(self.topdir, dest))
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
def _get_remote(self, node):
|
|
|
|
name = node.getAttribute('remote')
|
|
|
|
if not name:
|
|
|
|
return None
|
|
|
|
|
|
|
|
v = self._remotes.get(name)
|
|
|
|
if not v:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"remote %s not defined in %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(name, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
return v
|
|
|
|
|
|
|
|
def _reqatt(self, node, attname):
|
|
|
|
"""
|
|
|
|
reads a required attribute from the node.
|
|
|
|
"""
|
|
|
|
v = node.getAttribute(attname)
|
|
|
|
if not v:
|
|
|
|
raise ManifestParseError, \
|
|
|
|
"no %s in <%s> within %s" % \
|
2009-06-03 23:01:11 +00:00
|
|
|
(attname, node.nodeName, self._manifestFile)
|
2008-10-21 14:00:00 +00:00
|
|
|
return v
|