mirror of
https://github.com/flatcar/scripts.git
synced 2025-08-09 22:16:58 +02:00
loman: Add support for adding elements from default.xml
This is needed in order to do the right thing when adding projects which require a revision attribute. BUG=6811 TEST=Updated unittests and verified they pass. Verified that I can add chromeos-kernel-next and revision gets set. Change-Id: I12818dad464094cd50098170da3fcf38b1e32f17 Review URL: http://codereview.chromium.org/3400019
This commit is contained in:
parent
aa5504fdec
commit
ee49ac0ec5
94
bin/loman.py
94
bin/loman.py
@ -24,6 +24,17 @@ def _FindRepoDir():
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _ReadManifest(manifest, err_not_found=False):
|
||||||
|
if os.path.isfile(manifest):
|
||||||
|
ptree = LocalManifest(open(manifest).read())
|
||||||
|
elif err_not_found:
|
||||||
|
Die('Manifest file, %s, not found' % manifest)
|
||||||
|
else:
|
||||||
|
ptree = LocalManifest()
|
||||||
|
ptree.Parse()
|
||||||
|
return ptree
|
||||||
|
|
||||||
|
|
||||||
class LocalManifest:
|
class LocalManifest:
|
||||||
"""Class which provides an abstraction for manipulating the local manifest."""
|
"""Class which provides an abstraction for manipulating the local manifest."""
|
||||||
|
|
||||||
@ -34,63 +45,92 @@ class LocalManifest:
|
|||||||
"""Parse the manifest."""
|
"""Parse the manifest."""
|
||||||
self._root = ElementTree.fromstring(self._text)
|
self._root = ElementTree.fromstring(self._text)
|
||||||
|
|
||||||
def AddWorkonProject(self, name, path):
|
def AddProjectElement(self, element, workon='False'):
|
||||||
"""Add a new workon project if it is not already in the manifest.
|
"""Add a new project element to the manifest tree.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True on success.
|
True on success.
|
||||||
"""
|
"""
|
||||||
|
name = element.attrib['name']
|
||||||
|
path = element.attrib['path']
|
||||||
for project in self._root.findall('project'):
|
for project in self._root.findall('project'):
|
||||||
if project.attrib['path'] == path or project.attrib['name'] == name:
|
if project.attrib['path'] == path or project.attrib['name'] == name:
|
||||||
if project.attrib['path'] == path and project.attrib['name'] == name:
|
if project.attrib['path'] == path and project.attrib['name'] == name:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
self._AddProject(name, path, workon='True')
|
element.attrib['workon'] = workon
|
||||||
return True
|
|
||||||
|
|
||||||
def _AddProject(self, name, path, workon='False'):
|
|
||||||
element = ElementTree.Element('project', name=name, path=path,
|
|
||||||
workon=workon)
|
|
||||||
element.tail = '\n'
|
element.tail = '\n'
|
||||||
self._root.append(element)
|
self._root.append(element)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def AddProject(self, name, path, workon='False'):
|
||||||
|
"""Add a workon project if it is not already in the manifest.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True on success.
|
||||||
|
"""
|
||||||
|
element = ElementTree.Element('project', name=name, path=path)
|
||||||
|
return self.AddProjectElement(element, workon=workon)
|
||||||
|
|
||||||
|
def AddWorkonProjectElement(self, element):
|
||||||
|
return self.AddProjectElement(element, workon='True')
|
||||||
|
|
||||||
|
def AddWorkonProject(self, name, path):
|
||||||
|
return self.AddProject(name, path, workon='True')
|
||||||
|
|
||||||
|
def GetProject(self, name):
|
||||||
|
"""Accessor method for getting a project node from the manifest tree.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
project element node from ElementTree, otherwise, None
|
||||||
|
"""
|
||||||
|
|
||||||
|
for project in self._root.findall('project'):
|
||||||
|
if project.attrib['name'] == name:
|
||||||
|
return project
|
||||||
|
return None
|
||||||
|
|
||||||
def ToString(self):
|
def ToString(self):
|
||||||
return ElementTree.tostring(self._root, encoding='UTF-8')
|
return ElementTree.tostring(self._root, encoding='UTF-8')
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
usage = 'usage: %prog add [options] <name> <path>'
|
repo_dir = _FindRepoDir()
|
||||||
|
if not repo_dir:
|
||||||
|
Die("Unable to find repo dir.")
|
||||||
|
|
||||||
|
usage = 'usage: %prog add [options] <name>'
|
||||||
parser = optparse.OptionParser(usage=usage)
|
parser = optparse.OptionParser(usage=usage)
|
||||||
parser.add_option('-w', '--workon', action='store_true', dest='workon',
|
parser.add_option('-w', '--workon', action='store_true', dest='workon',
|
||||||
default=False, help='Is this a workon package?')
|
default=False, help='Is this a workon package?')
|
||||||
parser.add_option('-f', '--file', dest='manifest',
|
parser.add_option('-f', '--file', dest='local_manifest',
|
||||||
|
default='%s/local_manifest.xml' % repo_dir,
|
||||||
help='Non-default manifest file to read.')
|
help='Non-default manifest file to read.')
|
||||||
|
parser.add_option('-d', '--default', dest='full_manifest',
|
||||||
|
default='%s/manifests/full.xml' % repo_dir,
|
||||||
|
help='Default manifest file to read.')
|
||||||
(options, args) = parser.parse_args(argv[2:])
|
(options, args) = parser.parse_args(argv[2:])
|
||||||
if len(args) < 2:
|
if len(args) < 1:
|
||||||
parser.error('Not enough arguments')
|
parser.error('Not enough arguments')
|
||||||
if argv[1] not in ['add']:
|
if argv[1] not in ['add']:
|
||||||
parser.error('Unsupported command: %s.' % argv[1])
|
parser.error('Unsupported command: %s.' % argv[1])
|
||||||
if not options.workon:
|
if not options.workon:
|
||||||
parser.error('Adding of non-workon projects is currently unsupported.')
|
parser.error('Adding of non-workon projects is currently unsupported.')
|
||||||
(name, path) = (args[0], args[1])
|
name = args[0]
|
||||||
|
|
||||||
|
local_tree = _ReadManifest(options.local_manifest)
|
||||||
|
full_tree = _ReadManifest(options.full_manifest)
|
||||||
|
|
||||||
|
project_element = full_tree.GetProject(name)
|
||||||
|
if project_element == None:
|
||||||
|
Die('No project named, %s, in the default manifest.' % name)
|
||||||
|
success = local_tree.AddWorkonProjectElement(project_element)
|
||||||
|
if not success:
|
||||||
|
Die('name "%s" already exits with a different path.' % name)
|
||||||
|
|
||||||
repo_dir = _FindRepoDir()
|
|
||||||
if not repo_dir:
|
|
||||||
Die("Unable to find repo dir.")
|
|
||||||
local_manifest = options.manifest or \
|
|
||||||
os.path.join(_FindRepoDir(), 'local_manifest.xml')
|
|
||||||
if os.path.isfile(local_manifest):
|
|
||||||
ptree = LocalManifest(open(local_manifest).read())
|
|
||||||
else:
|
|
||||||
ptree = LocalManifest()
|
|
||||||
ptree.Parse()
|
|
||||||
if not ptree.AddWorkonProject(name, path):
|
|
||||||
Die('Path "%s" or name "%s" already exits in the manifest.' %
|
|
||||||
(path, name))
|
|
||||||
try:
|
try:
|
||||||
print >> open(local_manifest, 'w'), ptree.ToString()
|
print >> open(options.local_manifest, 'w'), local_tree.ToString()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
Die('Error writing to manifest: %s' % e)
|
Die('Error writing to manifest: %s' % e)
|
||||||
|
|
||||||
|
@ -38,6 +38,13 @@ class LocalManifestTest(unittest.TestCase):
|
|||||||
ptree.Parse()
|
ptree.Parse()
|
||||||
self.assertEqual(ptree.ToString(), self.utf8 + self.tiny_manifest)
|
self.assertEqual(ptree.ToString(), self.utf8 + self.tiny_manifest)
|
||||||
|
|
||||||
|
def testGetProject(self):
|
||||||
|
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
||||||
|
ptree.Parse()
|
||||||
|
ptree.AddProject('foo', 'path/to/foo')
|
||||||
|
project = ptree.GetProject('foo')
|
||||||
|
self.assertEqual(project.attrib['name'], 'foo')
|
||||||
|
|
||||||
def testAddNew(self):
|
def testAddNew(self):
|
||||||
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
||||||
ptree.Parse()
|
ptree.Parse()
|
||||||
@ -48,6 +55,19 @@ class LocalManifestTest(unittest.TestCase):
|
|||||||
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
||||||
'</manifest>')
|
'</manifest>')
|
||||||
|
|
||||||
|
def testAddNewElement(self):
|
||||||
|
dtree = loman.LocalManifest('<manifest>\n</manifest>')
|
||||||
|
dtree.Parse()
|
||||||
|
dtree.AddProject('foo', 'path/to/foo')
|
||||||
|
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
||||||
|
ptree.Parse()
|
||||||
|
ptree.AddWorkonProjectElement(dtree.GetProject('foo'))
|
||||||
|
self.assertEqual(
|
||||||
|
ptree.ToString(),
|
||||||
|
self.utf8 + '<manifest>\n'
|
||||||
|
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
||||||
|
'</manifest>')
|
||||||
|
|
||||||
def testAddDup(self):
|
def testAddDup(self):
|
||||||
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
||||||
ptree.Parse()
|
ptree.Parse()
|
||||||
@ -83,30 +103,69 @@ class MainTest(unittest.TestCase):
|
|||||||
self.assertTrue(sys.stderr.getvalue().endswith(err_msg))
|
self.assertTrue(sys.stderr.getvalue().endswith(err_msg))
|
||||||
|
|
||||||
def testSimpleAdd(self):
|
def testSimpleAdd(self):
|
||||||
|
default = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> default, '<manifest>\n' \
|
||||||
|
'<project name="foo" path="path/to/foo" />\n' \
|
||||||
|
'</manifest>\n'
|
||||||
|
default.flush()
|
||||||
|
os.fsync(default.fileno())
|
||||||
temp = tempfile.NamedTemporaryFile('w')
|
temp = tempfile.NamedTemporaryFile('w')
|
||||||
print >> temp, '<manifest>\n</manifest>'
|
print >> temp, '<manifest>\n</manifest>'
|
||||||
temp.flush()
|
temp.flush()
|
||||||
os.fsync(temp.fileno())
|
os.fsync(temp.fileno())
|
||||||
loman.main(['loman', 'add', '--workon', '-f',
|
loman.main(['loman', 'add', '--workon', '-f', temp.name,
|
||||||
temp.name, 'foo', 'path/to/foo'])
|
'-d', default.name, 'foo'])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
open(temp.name, 'r').read(),
|
open(temp.name, 'r').read(),
|
||||||
self.utf8 + '<manifest>\n'
|
self.utf8 + '<manifest>\n'
|
||||||
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
||||||
'</manifest>\n')
|
'</manifest>\n')
|
||||||
|
|
||||||
def testAddDup(self):
|
def testIgnoredPath(self):
|
||||||
|
default = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> default, '<manifest>\n' \
|
||||||
|
'<project name="foo" path="path/to/foo" />\n' \
|
||||||
|
'</manifest>\n'
|
||||||
|
default.flush()
|
||||||
|
os.fsync(default.fileno())
|
||||||
temp = tempfile.NamedTemporaryFile('w')
|
temp = tempfile.NamedTemporaryFile('w')
|
||||||
print >> temp, '<manifest>\n</manifest>'
|
print >> temp, '<manifest>\n</manifest>'
|
||||||
temp.flush()
|
temp.flush()
|
||||||
os.fsync(temp.fileno())
|
os.fsync(temp.fileno())
|
||||||
loman.main(['loman', 'add', '--workon', '-f',
|
loman.main(['loman', 'add', '--workon', '-f', temp.name,
|
||||||
temp.name, 'foo', 'path/to/foo'])
|
'-d', default.name, 'foo'])
|
||||||
loman.main(['loman', 'add', '--workon', '-f',
|
|
||||||
temp.name, 'foo', 'path/to/foo'])
|
def testAddDup(self):
|
||||||
|
default = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> default, '<manifest>\n' \
|
||||||
|
'<project name="foo" path="path/to/foo" />\n' \
|
||||||
|
'</manifest>\n'
|
||||||
|
default.flush()
|
||||||
|
os.fsync(default.fileno())
|
||||||
|
temp = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> temp, '<manifest>\n' \
|
||||||
|
'<project name="foo" path="bad/path/to/foo" />\n' \
|
||||||
|
'</manifest>\n'
|
||||||
|
temp.flush()
|
||||||
|
os.fsync(temp.fileno())
|
||||||
self.assertRaises(SystemExit, loman.main,
|
self.assertRaises(SystemExit, loman.main,
|
||||||
['loman', 'add', '--workon', '-f',
|
['loman', 'add', '--workon', '-f', temp.name,
|
||||||
temp.name, 'foo', 'path/foo'])
|
'-d', default.name, 'foo'])
|
||||||
|
|
||||||
|
def testAddNonexistant(self):
|
||||||
|
default = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> default, '<manifest>\n' \
|
||||||
|
'<project name="foo" path="path/to/foo" />\n' \
|
||||||
|
'</manifest>\n'
|
||||||
|
default.flush()
|
||||||
|
os.fsync(default.fileno())
|
||||||
|
temp = tempfile.NamedTemporaryFile('w')
|
||||||
|
print >> temp, '<manifest>\n</manifest>'
|
||||||
|
temp.flush()
|
||||||
|
os.fsync(temp.fileno())
|
||||||
|
self.assertRaises(SystemExit, loman.main,
|
||||||
|
['loman', 'add', '--workon', '-f', temp.name,
|
||||||
|
'-d', default.name, 'bar'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user