openstack-ansible/scripts/ansible-role-requirements-editer.py
Jesse Pretorius fac5030571 Update source-branch-updater to work with IRR's
This updated the sources-branch-updater convenience script which
is used to update SHA's and ensure that all carried files are
appropriately included in the update.

The patch includes the following changes:

 - Automatically submit updates of applicable files to all the
   independent repositories.

 - Automatically update the ansible-role-requirements.yml file
   with the latest SHA from the independent repositories or the
   latest tag from any repositories that are not part of OSA.
   This functionality is disabled by design for the master branch.

 - Adds ansible-role-requirements-editer.py which provides the
   ability to edit the ansible-role-requirements.yml file
   appropriately and output a consistent result.

Change-Id: I67fadeb43cc65a557d0380a12eb3e94335097d0d
2016-04-14 14:52:20 +01:00

105 lines
2.9 KiB
Python
Executable File

#!/usr/bin/env python
#
# 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 editer',
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, "r+") 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()