mirror of
https://github.com/flatcar/scripts.git
synced 2025-09-24 23:21:17 +02:00
Move loman to chromite (proper location for it after all).
BUG=chromium-os:32247,chromium-os:31867,chromium-os:9914 CQ-DEPEND=CL:29250 TEST=manual validation Change-Id: I055b1dc4b1be54684ac01d3ec4b8760ed33e0aa4 Reviewed-on: https://gerrit.chromium.org/gerrit/29245 Tested-by: Brian Harring <ferringb@chromium.org> Reviewed-by: Ryan Cui <rcui@chromium.org> Commit-Ready: Brian Harring <ferringb@chromium.org>
This commit is contained in:
parent
f4dfd18b12
commit
df4a5b41ea
150
bin/loman.py
150
bin/loman.py
@ -1,150 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
||||||
# Use of this source code is governed by a BSD-style license that can be
|
|
||||||
# found in the LICENSE file.
|
|
||||||
|
|
||||||
"""This module allows adding and deleting of projects to the local manifest."""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import optparse
|
|
||||||
import os
|
|
||||||
import xml.etree.ElementTree as ElementTree
|
|
||||||
|
|
||||||
from cros_build_lib import Die, FindRepoDir
|
|
||||||
|
|
||||||
|
|
||||||
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 which provides an abstraction for manipulating the local manifest."""
|
|
||||||
|
|
||||||
def __init__(self, text=None):
|
|
||||||
self._text = text or '<manifest>\n</manifest>'
|
|
||||||
|
|
||||||
def Parse(self):
|
|
||||||
"""Parse the manifest."""
|
|
||||||
self._root = ElementTree.fromstring(self._text)
|
|
||||||
|
|
||||||
def AddProjectElement(self, element, workon='False', remote=None):
|
|
||||||
"""Add a new project element to the manifest tree.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True on success.
|
|
||||||
"""
|
|
||||||
name = element.attrib['name']
|
|
||||||
path = element.attrib['path']
|
|
||||||
for project in self._root.findall('project'):
|
|
||||||
if project.attrib['path'] == path or project.attrib['name'] == name:
|
|
||||||
if project.attrib['path'] == path and project.attrib['name'] == name:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
element.attrib['workon'] = workon
|
|
||||||
if remote is not None:
|
|
||||||
element.attrib['remote'] = remote
|
|
||||||
element.tail = '\n'
|
|
||||||
self._root.append(element)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def AddProject(self, name, path, workon='False', remote=None):
|
|
||||||
"""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, remote=remote)
|
|
||||||
|
|
||||||
def AddWorkonProjectElement(self, element):
|
|
||||||
return self.AddProjectElement(element, workon='True')
|
|
||||||
|
|
||||||
def AddWorkonProject(self, name, path):
|
|
||||||
return self.AddProject(name, path, workon='True')
|
|
||||||
|
|
||||||
def AddNonWorkonProjectElement(self, element, remote):
|
|
||||||
return self.AddProjectElement(element, workon='False', remote=remote)
|
|
||||||
|
|
||||||
def AddNonWorkonProject(self, name, path, remote):
|
|
||||||
return self.AddProject(name, path, workon='False', remote=remote)
|
|
||||||
|
|
||||||
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):
|
|
||||||
return ElementTree.tostring(self._root, encoding='UTF-8')
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
|
||||||
repo_dir = FindRepoDir()
|
|
||||||
if not repo_dir:
|
|
||||||
Die("Unable to find repo dir.")
|
|
||||||
|
|
||||||
usage = 'usage: %prog add [options] <name> <path>'
|
|
||||||
parser = optparse.OptionParser(usage=usage)
|
|
||||||
parser.add_option('-w', '--workon', action='store_true', dest='workon',
|
|
||||||
default=False, help='Is this a workon package?')
|
|
||||||
parser.add_option('-f', '--file', dest='local_manifest',
|
|
||||||
default='%s/local_manifest.xml' % repo_dir,
|
|
||||||
help='Non-default manifest file to read.')
|
|
||||||
parser.add_option('-m', '--main', dest='main_manifest',
|
|
||||||
default='%s/manifest.xml' % repo_dir,
|
|
||||||
help='Main 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.')
|
|
||||||
parser.add_option('-r', '--remote', dest='remote',
|
|
||||||
default=None)
|
|
||||||
(options, args) = parser.parse_args(argv[2:])
|
|
||||||
if len(args) < 1:
|
|
||||||
parser.error('Not enough arguments')
|
|
||||||
if argv[1] not in ['add']:
|
|
||||||
parser.error('Unsupported command: %s.' % argv[1])
|
|
||||||
if not options.workon and options.remote is None:
|
|
||||||
parser.error('Adding non-workon projects requires a remote.')
|
|
||||||
name = args[0]
|
|
||||||
|
|
||||||
local_tree = _ReadManifest(options.local_manifest)
|
|
||||||
main_tree = _ReadManifest(options.main_manifest)
|
|
||||||
full_tree = _ReadManifest(options.full_manifest)
|
|
||||||
|
|
||||||
# Only add this project to local_manifest.xml if not in manifest.xml
|
|
||||||
if options.workon:
|
|
||||||
if main_tree.GetProject(name) is None:
|
|
||||||
project_element = full_tree.GetProject(name)
|
|
||||||
if project_element is None:
|
|
||||||
Die('No project named %s, in the default manifest.' % name)
|
|
||||||
success = local_tree.AddWorkonProjectElement(project_element)
|
|
||||||
if not success:
|
|
||||||
Die('Name "%s" already exists with a different path.' % name)
|
|
||||||
else:
|
|
||||||
success = local_tree.AddNonWorkonProject(name, args[1], options.remote)
|
|
||||||
if not success:
|
|
||||||
Die('Name "%s" already exists with a different path.' % name)
|
|
||||||
|
|
||||||
try:
|
|
||||||
print >> open(options.local_manifest, 'w'), local_tree.ToString()
|
|
||||||
except Exception, e:
|
|
||||||
Die('Error writing to manifest: %s' % e)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main(sys.argv)
|
|
@ -1,198 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
||||||
# Use of this source code is governed by a BSD-style license that can be
|
|
||||||
# found in the LICENSE file.
|
|
||||||
|
|
||||||
"""Unittests for loman."""
|
|
||||||
|
|
||||||
import os
|
|
||||||
import StringIO
|
|
||||||
import sys
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import loman
|
|
||||||
|
|
||||||
_TEST_MANIFEST1 = """<manifest>
|
|
||||||
<project name="foo" path="path/to/foo" workon="True" />
|
|
||||||
</manifest>"""
|
|
||||||
|
|
||||||
class LocalManifestTest(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.utf8 = "<?xml version='1.0' encoding='UTF-8'?>\n"
|
|
||||||
self.tiny_manifest = '<manifest>\n</manifest>'
|
|
||||||
|
|
||||||
def testSimpleParse(self):
|
|
||||||
ptree = loman.LocalManifest()
|
|
||||||
ptree.Parse()
|
|
||||||
|
|
||||||
def testParse(self):
|
|
||||||
ptree = loman.LocalManifest(self.tiny_manifest)
|
|
||||||
ptree.Parse()
|
|
||||||
self.assertEqual(ptree.ToString(), self.utf8 + self.tiny_manifest)
|
|
||||||
|
|
||||||
def testUTF8Parse(self):
|
|
||||||
ptree = loman.LocalManifest(self.utf8 + self.tiny_manifest)
|
|
||||||
ptree.Parse()
|
|
||||||
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):
|
|
||||||
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
|
||||||
ptree.Parse()
|
|
||||||
self.assertTrue(ptree.AddWorkonProject('foo', 'path/to/foo'))
|
|
||||||
self.assertEqual(
|
|
||||||
ptree.ToString(),
|
|
||||||
self.utf8 + '<manifest>\n'
|
|
||||||
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
|
||||||
'</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):
|
|
||||||
ptree = loman.LocalManifest('<manifest>\n</manifest>')
|
|
||||||
ptree.Parse()
|
|
||||||
ptree.AddWorkonProject('foo', 'path/to/foo')
|
|
||||||
self.assertTrue(ptree.AddWorkonProject('foo', 'path/to/foo'))
|
|
||||||
self.assertTrue(not ptree.AddWorkonProject('foo', 'path/foo'))
|
|
||||||
self.assertTrue(not ptree.AddWorkonProject('foobar', 'path/to/foo'))
|
|
||||||
|
|
||||||
class MainTest(unittest.TestCase):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.utf8 = "<?xml version='1.0' encoding='UTF-8'?>\n"
|
|
||||||
self.tiny_manifest = self.utf8 + '<manifest>\n</manifest>\n'
|
|
||||||
self.stderr = sys.stderr
|
|
||||||
sys.stderr = StringIO.StringIO()
|
|
||||||
self.main = tempfile.NamedTemporaryFile('w')
|
|
||||||
print >> self.main, '%s' % self.tiny_manifest
|
|
||||||
self.main.flush()
|
|
||||||
os.fsync(self.main.fileno())
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
sys.stderr = self.stderr
|
|
||||||
|
|
||||||
def testNotEnoughArgs(self):
|
|
||||||
err_msg = 'Not enough arguments\n'
|
|
||||||
self.assertRaises(SystemExit, loman.main, ['loman'])
|
|
||||||
self.assertTrue(sys.stderr.getvalue().endswith(err_msg))
|
|
||||||
|
|
||||||
def testNotWorkon(self):
|
|
||||||
err_msg = 'Adding of non-workon projects is currently unsupported.\n'
|
|
||||||
self.assertRaises(SystemExit, loman.main, ['loman', 'add', 'foo', 'path'])
|
|
||||||
self.assertTrue(sys.stderr.getvalue().endswith(err_msg))
|
|
||||||
|
|
||||||
def testBadCommand(self):
|
|
||||||
err_msg = 'Unsupported command: bad.\n'
|
|
||||||
self.assertRaises(SystemExit, loman.main, ['loman', 'bad', 'foo', 'path'])
|
|
||||||
self.assertTrue(sys.stderr.getvalue().endswith(err_msg))
|
|
||||||
|
|
||||||
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')
|
|
||||||
print >> temp, '%s' % self.tiny_manifest
|
|
||||||
temp.flush()
|
|
||||||
os.fsync(temp.fileno())
|
|
||||||
loman.main(['loman', 'add', '--workon', '-f', temp.name,
|
|
||||||
'-m', self.main.name,
|
|
||||||
'-d', default.name, 'foo'])
|
|
||||||
self.assertEqual(
|
|
||||||
open(temp.name, 'r').read(),
|
|
||||||
self.utf8 + '<manifest>\n'
|
|
||||||
'<project name="foo" path="path/to/foo" workon="True" />\n'
|
|
||||||
'</manifest>\n')
|
|
||||||
|
|
||||||
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')
|
|
||||||
print >> temp, '<manifest>\n</manifest>'
|
|
||||||
temp.flush()
|
|
||||||
os.fsync(temp.fileno())
|
|
||||||
loman.main(['loman', 'add', '--workon', '-f', temp.name,
|
|
||||||
'-m', self.main.name,
|
|
||||||
'-d', default.name, '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,
|
|
||||||
['loman', 'add', '--workon', '-f', temp.name,
|
|
||||||
'-m', self.main.name,
|
|
||||||
'-d', default.name, 'foo'])
|
|
||||||
|
|
||||||
def testAddDupInMain(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, '%s' % self.tiny_manifest
|
|
||||||
temp.flush()
|
|
||||||
os.fsync(temp.fileno())
|
|
||||||
loman.main(['loman', 'add', '--workon', '-f', temp.name,
|
|
||||||
'-m', default.name,
|
|
||||||
'-d', default.name, 'foo'])
|
|
||||||
self.assertEqual(
|
|
||||||
open(temp.name, 'r').read(),
|
|
||||||
self.tiny_manifest)
|
|
||||||
|
|
||||||
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,
|
|
||||||
'-m', self.main.name,
|
|
||||||
'-d', default.name, 'bar'])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
Loading…
x
Reference in New Issue
Block a user