Davanum Srinivas 3ebedf84f0 Deal with underscores in names in edit-constraints
Problem showed up with django_openstack_auth job that installs from
source. The change to devstack to verify LIBS_FROM_GIT kicked in
(Iffef2007f99a0e932b68c4c897ebbfb748cac2b4) and starting failing the
job. The problem was that the edit_constraint was not fixing the
django-openstack-auth line in upperconstraints.txt as the setup.cfg
had name = django_openstack_auth. Note that when things finally get
installed setup_tools' safe_name kicks in to standardize names and for
example glance_store will show up as glance-store in pip freeze. So we
should be able to treat this situation better and allow constraints to
have the safe names (using dashes).

Co-Authored-By: Robert Collins <rbtcollins@hp.com>
Change-Id: Ibaa22657a2cf2c0ad96dbd0b9bc43cdafe6a1d56
2015-09-17 10:12:11 +12:00

76 lines
2.4 KiB
Python

# 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):
key = requirement.canonical_name(name)
if not replacement:
reqs.pop(key, None)
else:
reqs[key] = [
(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, permit_urls=True)
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