Add edit-constraints command.

This allows shell editing of a constraints file, which we need to do
in devstack when installing a constrained library from git.

Change-Id: Ibdbfc786b5d2f56d2e41d329c8766bef7b2e6854
This commit is contained in:
Robert Collins 2015-06-25 21:50:40 +12:00
parent 3aa37f7ea4
commit 893490c958
3 changed files with 139 additions and 0 deletions

@ -0,0 +1,73 @@
# 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 optparse
import os.path
import sys
import textwrap
from openstack_requirements import requirement
def edit(reqs, name, replacement):
if not replacement:
reqs.pop(name, None)
else:
reqs[name] = [(requirement.Requirement('', '', '', replacement), '')]
result = []
for entries in reqs.values():
for entry, _ in entries:
result.append(entry)
return requirement.Requirements(sorted(result))
# -- untested UI glue from here down.
def _validate_options(options, args):
"""Check that options and arguments are valid.
:param options: The optparse options for this program.
:param args: The args for this program.
"""
if len(args) < 2:
raise Exception("No enough arguments given")
if not os.path.exists(args[0]):
raise Exception(
"Constraints file %(con)s not found."
% dict(con=args[0]))
def main(argv=None, stdout=None):
parser = optparse.OptionParser(
usage="%prog [options] constraintpath name replacement",
epilog=textwrap.dedent("""\
Replaces any entries of "name" in the constraints file with
"replacement". If "name" is not present, it is added to the end of
the file. If "replacement" is missing or empty, remove "name" from
the file.
"""))
options, args = parser.parse_args(argv)
if stdout is None:
stdout = sys.stdout
_validate_options(options, args)
args = args + [""]
content = open(args[0], 'rt').read()
reqs = requirement.parse(content)
out_reqs = edit(reqs, args[1], args[2])
out = requirement.to_content(out_reqs, prefix=False)
with open(args[0] + '.tmp', 'wt') as f:
f.write(out)
os.rename(args[0] + '.tmp', args[0])
return 0

@ -0,0 +1,65 @@
# 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 io
import os
import fixtures
import testscenarios
import testtools
from openstack_requirements.cmds import edit_constraint as edit
from openstack_requirements import requirement
load_tests = testscenarios.load_tests_apply_scenarios
class SmokeTest(testtools.TestCase):
def test_make_url(self):
stdout = io.StringIO()
tmpdir = self.useFixture(fixtures.TempDir()).path
constraints_path = os.path.join(tmpdir, 'name.txt')
with open(constraints_path, 'wt') as f:
f.write('bar===1\nfoo===1.0.2\nquux==3\n')
rv = edit.main(
[constraints_path, 'foo', '--', '-e /path/to/foo'], stdout)
self.assertEqual(0, rv)
content = open(constraints_path, 'rt').read()
self.assertEqual('-e /path/to/foo\nbar===1\nquux==3\n', content)
class TestEdit(testtools.TestCase):
def test_add(self):
reqs = {}
res = edit.edit(reqs, 'foo', 'foo==1.2')
self.assertEqual(requirement.Requirements(
[requirement.Requirement('', '', '', 'foo==1.2')]), res)
def test_delete(self):
reqs = requirement.parse('foo==1.2\n')
res = edit.edit(reqs, 'foo', '')
self.assertEqual(requirement.Requirements([]), res)
def test_replace(self):
reqs = requirement.parse('foo==1.2\n')
res = edit.edit(reqs, 'foo', 'foo==1.3')
self.assertEqual(requirement.Requirements(
[requirement.Requirement('', '', '', 'foo==1.3')]), res)
def test_replace_many(self):
reqs = requirement.parse('foo==1.2;p\nfoo==1.3;q')
res = edit.edit(reqs, 'foo', 'foo==1.3')
self.assertEqual(requirement.Requirements(
[requirement.Requirement('', '', '', 'foo==1.3')]), res)

@ -23,5 +23,6 @@ packages =
[entry_points]
console_scripts =
edit-constraints = openstack_requirements.cmds.edit_constraint:main
generate-constraints = openstack_requirements.cmds.generate:main
update-requirements = openstack_requirements.cmds.update:main