openstack-ansible/scripts/release-yaml-file-prep.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

139 lines
4.1 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 ansible-role-requirements.yml content from the CLI and output
yaml content to stdout to be used when submitting release requests."""
from __future__ import print_function
from cStringIO import StringIO
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
import argparse
import re
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 = {"version": 0, "projects": 1, "repo": 2, "hash": 3}.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)
# sourced from
# http://stackoverflow.com/questions/25108581/python-yaml-dump-bad-indentation
def yaml_dump(dump, indentSize=2):
stream = StringIO(dump)
out = StringIO()
pat = re.compile('(\s*)([^:]*)(:*)')
last = None
prefix = 0
for s in stream:
indent, key, colon = pat.match(s).groups()
if indent == "" and key[0] != '-':
prefix = 0
if last:
if len(last[0]) == len(indent) and last[2] == ':':
if all([
not last[1].startswith('-'),
s.strip().startswith('-')]):
prefix += indentSize
out.write(" " * prefix + s)
last = indent, key, colon
return out.getvalue()
def main():
"""Run the main application."""
# Setup argument parsing
parser = argparse.ArgumentParser(
description='release.yml producer',
epilog='Licensed "Apache 2.0"')
parser.add_argument(
'-f',
'--file',
help='<Required> ansible-role-requirements.yml file location',
default='ansible-role-requirements.yml'
)
parser.add_argument(
'-v',
'--version',
help='<Required> The release version to include in the output',
required=True
)
# 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)
# Prepare the vars for output
version = args.version
projects = []
# Prepare the regex match
regex = re.compile('^.*openstack/(ansible-hardening|openstack-ansible.*)$')
# Loop through the list of roles
for role_data in reqs:
# Only add OpenStack repositories to the release
if regex.match(role_data['src']):
# Prepare the repo release dict
repo_release = {}
# Figure out the repo from the git source
repo = urlparse(role_data['src']).path.lstrip('/')
# Assemble the dict
repo_release['repo'] = repo
repo_release['hash'] = role_data['version']
# Add the dict to the projects list
projects.append(repo_release.copy())
# Put the yaml content together
releases = {'releases': [{'version': version, 'projects': projects}]}
# Product the YAML output for the resulting releases data
output = yaml.dump(releases, default_flow_style=False)
# Print the output, formatted as expected
print(yaml_dump(output))
if __name__ == "__main__":
main()