Skip to content

Instantly share code, notes, and snippets.

@cidrblock
Created May 4, 2020 22:34
Show Gist options
  • Save cidrblock/4ae405dcd798c4637904b8f67951f2de to your computer and use it in GitHub Desktop.
Save cidrblock/4ae405dcd798c4637904b8f67951f2de to your computer and use it in GitHub Desktop.
Prep for 1.0
import astor
import ast
import ruamel.yaml
import os
import re
import sys
COLPATH = "./"
COLLECTION = "arista.eos"
SUBDIRS = ("modules", "action")
LICENSE = """
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
"""
for subdir in SUBDIRS:
dirpath = "{colpath}/{collection}/plugins/{subdir}".format(
colpath=COLPATH, collection=COLLECTION, subdir=subdir
)
for filename in os.listdir(dirpath):
if filename.endswith(".py"):
filename = "{dirpath}/{filename}".format(dirpath=dirpath, filename=filename)
a = astor.code_to_ast.parse_file(filename)
module_name = ""
for b in a.body:
if hasattr(b, "targets"):
if b.targets[0].id == "ANSIBLE_METADATA":
meta = ast.Dict()
meta.keys = [
ast.Constant(s)
for s in ["metadata_version", "supported_by"]
]
meta.values = [ast.Constant(s) for s in ["1.1", "Ansible"]]
b.value = meta
if b.targets[0].id == "DOCUMENTATION":
documentation = ruamel.yaml.load(
b.value.value, ruamel.yaml.RoundTripLoader
)
module_name = documentation["module"]
documentation.pop("version_added", None)
desc_idx = [
idx
for idx, key in enumerate(documentation.keys())
if key == "description"
]
documentation.insert(
desc_idx[0] + 1, key="version_added", value="1.0.0"
)
repl = ruamel.yaml.dump(
documentation, None, ruamel.yaml.RoundTripDumper
)
b.value.value = repl
example_lines = b.value.value.splitlines()
regex = re.compile("^\s+version_added\:\s.*$")
example_lines = [
l for l in example_lines if not re.match(regex, l)
]
b.value.value = "\n".join(example_lines)
if b.targets[0].id == "EXAMPLES":
full_module_name = "{collection}.{module_name}".format(
collection=COLLECTION, module_name=module_name
)
example = ruamel.yaml.load(
b.value.value, ruamel.yaml.RoundTripLoader
)
for idx, task in enumerate(example):
example[idx] = ruamel.yaml.comments.CommentedMap(
[
(full_module_name, v)
if k == module_name
else (k, v)
for k, v in task.items()
]
)
repl = ruamel.yaml.dump(
example, None, ruamel.yaml.RoundTripDumper
)
b.value.value = repl
example_lines = b.value.value.splitlines()
for idx, line in enumerate(example_lines):
if (
line.startswith("#")
and module_name in line
and module_name
):
example_lines[idx] = line.replace(
module_name, full_module_name
)
b.value.value = "\n".join(example_lines)
filec = LICENSE + astor.to_source(a)
with open(filename, "w") as fileh:
fileh.write(filec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment