openstack-ansible/scripts/ansible-role-requirements-editor.py
Jesse Pretorius e8abf19647 Fix sources-branch-updater and add docs
Problems resolved:

- Errors relating to 'etc' folders which did not exist in the
  target repositories have been eliminated.

- Some repositories were not being excluded from evaluation
  for file/template updates. All appropriate exclusions are
  now implemented.

- If an OSA role repository did not have the required folder
  to copy rootwrap files into, the copy would fail and the
  rootwrap files therefore be left out of the patch.

- When executed on Mac OS X, python2 is not found.

- Documentation is added to describe how to use the script
  to handle periodic SHA bumps.

Change-Id: I628cd2c3156d118c17ca2f90d4ccf0bc5a080bbf
2018-01-15 12:29:19 +00:00

105 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python2.7
#
# Copyright 2016, Rackspace US, Inc.
#
# 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.
#
# (c) 2016, Jesse Pretorius <jesse.pretorius@rackspace.co.uk>
#
"""Read/write ansible-role-requirements.yml content from the CLI."""
from __future__ import print_function
import argparse
import yaml
# To ensure that the dicts are always output in the same order
# we setup a representation for dict objects and register it
# with the yaml class.
def represent_dict(self, data):
def key_function((key, value)):
# Prioritizes certain keys when sorting.
prio = {"model": 0, "pk": 1, "fields": 2}.get(key, 99)
return (prio, key)
items = data.items()
items.sort(key=key_function)
return self.represent_mapping(u'tag:yaml.org,2002:map', items)
yaml.add_representer(dict, represent_dict)
def main():
"""Run the main application."""
# Setup argument parsing
parser = argparse.ArgumentParser(
description='ansible-role-requirements.yml CLI editor',
epilog='Licensed "Apache 2.0"')
parser.add_argument(
'-f',
'--file',
help='<Required> ansible-role-requirements.yml file location',
required=True
)
parser.add_argument(
'-n',
'--name',
help='<Required> The name of the Ansible role to edit',
required=True
)
parser.add_argument(
'-v',
'--version',
help='<Required> The version to set for the Ansible role',
required=True
)
parser.add_argument(
'-s',
'--src',
help='<Optional> The source URL to set for the Ansible role',
required=False
)
# Parse arguments
args = parser.parse_args()
# Read the ansible-role-requirements.yml file into memory
with open(args.file, "r") as role_req_file:
reqs = yaml.safe_load(role_req_file)
# Loop through the list to find the applicable role
for role_data in reqs:
if role_data['name'] == args.name:
# Change the specified role data
role_data['version'] = args.version
if args.src:
role_data['src'] = args.src
# Write out the resulting file
with open(args.file, "w") as role_req_file:
try:
yaml.dump(reqs, role_req_file, default_flow_style=False)
except yaml.YAMLError as exc:
print(exc)
if __name__ == "__main__":
main()