Created
December 5, 2019 17:53
-
-
Save cidrblock/40a540c54abb6f8767f7da7a85c84a94 to your computer and use it in GitHub Desktop.
Make a disposition CSV for ansible content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from ansible.parsing.plugin_docs import read_docstring | |
from pprint import pprint | |
import csv | |
import yaml | |
path = "./ansible/" | |
interesting_directories = [ | |
path + "lib/ansible/plugins", | |
path + "lib/ansible/modules", | |
] | |
def get_contents(minimal): | |
contents = [] | |
for directory_name, _subdirList, fileList in os.walk(path): | |
for interesting_directory in interesting_directories: | |
if directory_name.startswith(interesting_directory): | |
parts = directory_name.split("/") | |
for fname in fileList: | |
if fname not in ["__init__.py"]: | |
content = { | |
"description": None, | |
"topic": None, | |
"topic_subdirectory": None, | |
"supported_by": None, | |
"status": None, | |
"in_minimal": False | |
} | |
content["filename"] = fname | |
content["full_path"] = directory_name + "/" + fname | |
docstring = read_docstring( | |
content["full_path"], verbose=True, ignore_errors=True | |
) | |
if docstring: | |
if "doc" in docstring and docstring["doc"]: | |
content["description"] = docstring["doc"].get("short_description", None) | |
if "metadata" in docstring and docstring["metadata"]: | |
content["supported_by"] = docstring["metadata"].get("supported_by", None) | |
content["status"] = " ".join(docstring["metadata"].get("status", [])) | |
if parts[4] == "plugins": | |
if len(parts) == 5: | |
content["content_type"] = "plugin_dir_root" | |
min_type = 'plugin_dir_root' | |
min_name = fname | |
else: | |
if parts[5] != 'doc_fragments': | |
content["content_type"] = parts[5] + ' plugin' | |
else: | |
content["content_type"] = parts[5] | |
min_type = parts[5] | |
min_name = fname | |
elif parts[4] == "modules": | |
min_type = 'modules' | |
min_name = '/'.join(parts[5:]) + '/' + fname | |
if len(parts) == 5: | |
content["content_type"] = "module_dir_root" | |
else: | |
content["content_type"] = "module" | |
content["topic"] = parts[5] | |
if len(parts) > 6: | |
content["topic_subdirectory"] = parts[6] | |
if min_type in minimal['_core']: | |
if min_name in minimal['_core'][min_type]: | |
content['in_minimal'] = True | |
contents.append(content) | |
return contents | |
def write_csv(contents): | |
csv_columns = list(contents[0].keys()) | |
csv_file = "content_dispostion.csv" | |
try: | |
with open(csv_file, "w") as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=csv_columns) | |
writer.writeheader() | |
for data in contents: | |
writer.writerow(data) | |
except IOError: | |
print("I/O error") | |
def load_minimal(): | |
with open("./collection_migration/scenarios/minimal/ansible.yml", 'r') as stream: | |
try: | |
data = yaml.safe_load(stream) | |
except yaml.YAMLError as exc: | |
print(exc) | |
return data | |
minimal = load_minimal() | |
contents = get_contents(minimal) | |
write_csv(contents) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment