import subprocess, fileinput, pprint, re, os, sys, json, pypandoc, yaml from atlassian import Confluence from json2html import * def prepareMarkdown(sourcePath): fileContent = pypandoc.convert_file(sourcePath, "html") # Alter document to confluence standard so we can tell if the page needs updating # Strip trailing whitespace fileContent = fileContent.strip() # Remove ids since confluence file comparison fails with them fileContent = re.sub(r" id=\"[\w-]*\"", "", fileContent) # Replace new lines with space fileContent = re.sub(r"\n", " ", fileContent) # Remove markdown comments fileContent = re.sub(r"", "", fileContent) # Custom character handling fileContent = fileContent.replace('ó', u'ó') fileContent = fileContent.replace('’', '’') return fileContent def prepareJSON(jsonFileContent): jsonDict = json.loads(jsonFileContent) htmlTable = json2html.convert(jsonDict) htmlTable = htmlTable.strip() # Remove ids since confluence file comparison fails with them htmlTable = re.sub(r" id=\"[\w-]*\"", "", htmlTable) # Replace new lines with space htmlTable = re.sub(r"\n", " ", htmlTable) # Remove markdown comments htmlTable = re.sub(r"", "", htmlTable) # Custom character handling htmlTable = htmlTable.replace('ó', u'ó') htmlTable = htmlTable.replace('’', '’') return htmlTable pp = pprint.PrettyPrinter(indent=2) configuration = yaml.load(open("pythonDocUpload.yml", "r").read()) print(configuration) confluence = Confluence( url=configuration.get('confluenceUrl'), username=configuration.get('confluenceUser'), password=configuration.get('confluencePass')) hostsDir = sys.argv[1] + "/hosts" puppetDir = sys.argv[1] + "/puppet/modules/ndn" docsDir = sys.argv[1] + "/docs" errors = "" # Create hosts page hostsPageId = confluence.get_page_id(configuration.get('spaceName'), "hosts") if hostsPageId == None: print("Creating hosts page") hostsPageId = confluence.create_page(configuration.get('spaceName'), "hosts", '').get("id") # Create manifests pages for dirname in os.listdir(hostsDir): print(dirname) # Create individual host page and manifests dirPath = hostsDir + "/" + dirname landingContent = "" if os.path.isfile(dirPath + "/manifest.json"): landingContent += prepareJSON(open(dirPath + "/manifest.json", "r").read()) if os.path.isfile(dirPath + "/README.md"): landingContent += prepareMarkdown(dirPath + "/README.md") dirPageReturn = confluence.update_or_create(hostsPageId, dirname, landingContent) dirPageId = confluence.get_page_id(configuration.get('spaceName'), dirname) manifestsPageReturn = confluence.update_or_create(dirPageId, "manifests - " + dirname, 'placeholder') for filename in os.listdir(dirPath + "/manifests"): if filename.endswith(".pp"): try: subprocess.run(["puppet", "strings", "generate", "--format", "markdown", dirPath + "/" + filename, "--out", "temp.md"]) fileContent = prepareMarkdown("temp.md") pageReturn = confluence.update_or_create(manifestsPageReturn.get("id"), filename + " - " + dirname, fileContent) except Exception as e: print("Error processing manifest file: " + dirPath + "/" + filename) errors += dirPath + "/manifests/" + filename + " MESSAGE: " + str(e) + "\n" dirPageId = confluence.get_page_id(configuration.get('spaceName'), "ndn") if dirPageId == None: dirPageId = confluence.create_page(configuration.get('spaceName'), "ndn", '').get("id") modulesPageReturn = confluence.update_or_create(dirPageId, "puppet/modules/ndn", "") manifestsDir = puppetDir + "/manifests" if os.path.isdir(manifestsDir): for manifestFile in os.listdir(manifestsDir): try: if manifestFile.endswith(".pp"): subprocess.run(["puppet", "strings", "generate", "--format", "markdown", manifestsDir + "/" + manifestFile, "--out", "temp.md"]) fileContent = prepareMarkdown("temp.md") pp.pprint("page: " + manifestFile + " - " + dirname) pageReturn = confluence.update_or_create(modulesPageReturn.get("id"), manifestFile + " - " + dirname, fileContent) except Exception as e: print("Error processing puppet manifest: " + dirname + "/" + manifestFile) errors += manifestsDir + "/" + manifestFile + " MESSAGE: " + str(e) + "\n" docsPageId = confluence.get_page_id(configuration.get('spaceName'), "docs") if docsPageId == None: print("Creating docs page") docsPageId = confluence.create_page(configuration.get('spaceName'), "docs", '').get("id") for docsName in os.listdir(docsDir): try: fileContent = prepareMarkdown(docsDir + "/" + docsName) pp.pprint("page: " + docsDir + " - " + docsName) pageReturn = confluence.update_or_create(docsPageId, docsName + " - docs", fileContent) except Exception as e: print("Error processing docs file: " + docsDir + "/" + docsName) errors += docsDir + "/" + docsName + " MESSAGE: " + str(e) + "\n" if errors != "": print("Errors encountered while handling following docs: ") print(errors) print("------- DONE -------")