执行repo init时如何忽略manifest文件格式错误的annotation标签?
遇到这个问题确实挺头疼的——不想改动旧的manifest文件,但repo的解析器又严格要求<annotation>标签必须带name和value属性。这里有两个可行的方案,看你更倾向哪种:
方案一:临时修改本地manifest文件(无需改动repo或远程manifest)
这个方法是先把远程的manifest下载到本地,临时修正annotation格式后再执行repo init,完全不影响原文件:
下载远程manifest到本地:
wget https://your-manifest-repo-url/manifest.xml -O local_manifest.xml替换成你实际的manifest地址。如果是git仓库里的manifest,也可以先clone仓库再取出对应文件。
用sed批量修正annotation标签:
sed -i 's/<annotation version="\([0-9.]*\)" \/>/<annotation name="version" value="\1" \/>/g' local_manifest.xml这条命令会把所有
<annotation version="x.x.x" />格式的标签替换成符合repo要求的<annotation name="version" value="x.x.x" />。使用本地修改后的manifest执行repo init:
repo init -u file://$(pwd)/local_manifest.xml这样repo就会用本地修正后的文件完成初始化,远程的旧manifest完全不受影响。
方案二:修改repo源码兼容旧格式(适合频繁使用旧manifest的场景)
如果你经常需要处理这类旧manifest,可以直接修改repo的解析逻辑,让它兼容<annotation version="x.x.x" />的格式:
找到repo的
manifest.py文件,通常路径是~/.repo/repo/manifest.py(如果是系统级安装的repo,路径可能在/usr/share/repo/下)。找到处理annotation标签的代码段,大概是这样的:
name = node.getAttribute('name') value = node.getAttribute('value') if not name or not value: self._error('no name in <annotation> within %s' % self.manifestFile)修改成兼容旧格式的逻辑:
name = node.getAttribute('name') value = node.getAttribute('value') # 兼容旧的version属性格式 if not name and not value: name = 'version' value = node.getAttribute('version') if not name or not value: self._error('no name in <annotation> within %s' % self.manifestFile)保存文件后,再执行
repo init就不会报错了——repo现在会自动识别旧格式的annotation标签。
这两个方案都能解决你的问题,选哪个看你是偶尔处理一次,还是经常需要和这类旧manifest打交道。
内容的提问来源于stack exchange,提问作者rodee

