Exemplo n.º 1
0
class ConfluenceManager:
    def __init__(self, credentials, path):
        self.remote = Confluence(url=credentials[0],
                                 username=credentials[1],
                                 password=credentials[2])

        def converter_to_local_key(key):
            if isinstance(key, tuple):
                return 'name_' + key[0] + key[1]
            else:
                return 'id_' + key

        self.cached = CachedDataProvider(
            self, path, converter_to_local_key=converter_to_local_key)
        self.simplified = CachedDataProvider(
            ConfluenceSimplifiedTextProvider(self.cached),
            path + '.simplified',
            converter_to_local_key=converter_to_local_key)

    def extract_url(self, raw_page):
        return self.remote.url + raw_page['_links']['webui']

    def extract_title(self, raw_page):
        return raw_page['title']

    def __get_page_id(self, id_):
        if isinstance(id_, tuple):
            id_ = self.remote.get_page_id(id_[0], id_[1])
        return id_

    def get_page(self, id_, full_info=True):
        id_ = self.__get_page_id(id_)

        expand = 'children.page.id'
        if full_info:
            expand = 'version,body.storage,children.page.id'

        raw = self.remote.get_page_by_id(id_, expand=expand)
        return raw

    def get_simple_text(self, id_):
        return self.simplified[id_]

    def get_page_tree_ids(self, id_):
        page = self.get_page(id_, full_info=False)
        ret = [page['id']]
        children_ids = [r['id'] for r in page['children']['page']['results']]
        for id_ in children_ids:
            ret += self.get_page_tree_ids(id_)
        return ret

    def __getitem__(self, key):
        if isinstance(key, tuple):
            remote_key = self.remote.get_page_id(key[0], key[1])
        else:
            remote_key = key

        ret = self.remote.get_page_by_id(
            remote_key, expand='version,body.storage,children.page.id')
        return ret
# coding=utf-8
from atlassian import Confluence
"""This example shows how to export pages"""

confluence = Confluence(
    url="https://test.atlassian.net/wiki",
    username="******",
    password="******",
    api_version="cloud",
)

if __name__ == "__main__":
    space = "TEST"
    page_title = "Test"
    page_id = confluence.get_page_id(space, page_title)
    content = confluence.export_page(page_id)
    with open(page_title + ".pdf", "wb") as pdf_file:
        pdf_file.write(content)
        pdf_file.close()
        print("Completed")
    """<table>
                <tr>
                    <th>Project Key</th>
                    <th>Project Name</th>
                    <th>Leader</th>
                    <th>Email</th>
                </tr>"""
]

for data in jira.project_leaders():
    log.info(
        "{project_key} leader is {lead_name} <{lead_email}>".format(**data))
    row = """<tr>
                <td>{project_key}</td>
                <td>{project_name}</td>
                <td>{lead_name}</td>
                <td><a href="mailto:{lead_email}">{lead_email}</a></td>
             </tr>"""
    html.append(row.format(**data))

html.append("</table><p></p><p></p>")

status = confluence.create_page(
    space="DEMO",
    parent_id=confluence.get_page_id("DEMO", "demo"),
    title="Jira Administrators",
    body="\r\n".join(html),
)

print(status)
Exemplo n.º 4
0
    password='******')

html = ["""<table>
                <tr>
                    <th>Project Key</th>
                    <th>Project Name</th>
                    <th>Leader</th>
                    <th>Email</th>
                </tr>"""]


for data in jira.project_leaders():
    log.info('{project_key} leader is {lead_name} <{lead_email}>'.format(**data))
    row = """<tr>
                <td>{project_key}</td>
                <td>{project_name}</td>
                <td>{lead_name}</td>
                <td><a href="mailto:{lead_email}">{lead_email}</a></td>
             </tr>"""
    html.append(row.format(**data))

html.append('</table><p></p><p></p>')

status = confluence.create_page(
    space='DEMO',
    parent_id=confluence.get_page_id('DEMO', 'demo'),
    title='Jira Administrators',
    body='\r\n'.join(html))

print(status)
Exemplo n.º 5
0
from atlassian import Confluence

# połączenie z confluence
confluence = Confluence(url='http://conflenecobawroc.corp.capgemini.com',
                        username='******',
                        password='******')

# status = confluence.create_page(
#     space='Coba',
#     title='147',
#     body='TESTOWY',
#     parent_id=3702829,
#     type='page')
#
# print(status)

page_id = confluence.get_page_id('COBA', 'OneNote')
print(page_id)
# coding: utf8
from atlassian import Confluence

"""This example shows how to export pages"""

confluence = Confluence(
    url='https://test.atlassian.net/wiki',
    username='******',
    password='******',
    api_version='cloud'
)

if __name__ == '__main__':
    space = 'TEST'
    page_title = 'Test'
    page_id = confluence.get_page_id(space, page_title)
    content = confluence.export_page(page_id)
    with open(page_title + ".pdf", 'wb') as pdf_file:
        pdf_file.write(content)
        pdf_file.close()
        print("Completed")
Exemplo n.º 7
0
confluenceSpace = c["space"]
confluencePage = c["page"]

try:
    confluence = Confluence(url=confluenceURL,
                            username=confluenceUser,
                            password=confluencePass)
except Exception as e:
    print("Uh oh, can't connect to confluence at " + confluenceURL)
    print(e)

print(
    "########################################################################")
print("Looking for page \"" + confluencePage + "\" in space " +
      confluenceSpace)
pageID = confluence.get_page_id(confluenceSpace, confluencePage)
if pageID is None:
    print("Page not found: " + confluencePage + " in space " + confluenceSpace)
    exit()
else:
    print("Found page : " + confluencePage + " pageID: " + str(pageID))

print(
    "########################################################################")
print("Getting page by ID: " + str(pageID))
contents = confluence.get_page_by_id(pageID,
                                     expand="body.storage,version",
                                     status="current")
print("ID           : " + str(contents["id"]))
print("Status       : " + contents["status"])
print("Title        : " + contents["title"])
Exemplo n.º 8
0
class ConfluencePublisher():
    def __init__(
        self,
        url,
        username,
        apiToken,
        pageTitlePrefix,
        markdownDir,
        dbPath,
        space,
        parentPageId,
        forceUpdate=False,
        forceDelete=False,
        skipUpdate=False,
    ):
        self.api = Confluence(url=url, username=username, password=apiToken)
        self.pageTitlePrefix = pageTitlePrefix
        self.markdownDir = markdownDir
        self.kv = KeyValue(dbPath)
        self.space = space
        self.parentPageId = parentPageId
        self.forceUpdate = forceUpdate
        self.forceDelete = forceDelete
        self.skipUpdate = skipUpdate
        self.confluenceRenderer = ConfluenceRenderer(url)
        self.metadataPlugin = MetadataPlugin()
        self.renderer = mistune.create_markdown(
            renderer=self.confluenceRenderer,
            plugins=[
                'strikethrough', 'footnotes', 'table', 'url',
                self.metadataPlugin.plugin_metadata
            ])

        # Hack to allow metadata plugin to work (See mistune/block_parser.py)
        self.renderer.block.rules.remove('thematic_break')

    def __getFileContent(self, filepath):
        file = open(filepath, mode='r')
        content = file.read()
        file.close()
        return content

    def __updatePage(self, space, parentId, filepath, autoindex=False):

        if autoindex:
            markdown = ''
        else:
            markdown = self.__getFileContent(filepath)

        metadata = self.kv.load(filepath)

        currentTitle = metadata['title']
        currentHash = metadata['sha256']
        hash = self.kv.sha256(markdown)

        # --- Render (BEGIN)
        self.metadataPlugin.stack['title'] = None

        if autoindex:
            body = self.confluenceRenderer.generate_autoindex()
        else:
            body = self.renderer(markdown)

        if self.metadataPlugin.stack['title'] is None:
            if autoindex:
                title = 'Folder ' + os.path.basename(os.path.dirname(filepath))
            else:
                title = os.path.basename(filepath)
        else:
            title = self.metadataPlugin.stack['title']

        title = self.pageTitlePrefix + title
        # >>> Removed: + " [" + self.kv.sha256(filepath)[-6:] + "]"
        # --- Render (END)

        if currentTitle and currentTitle != title:
            print('REN => Title: ' + title)
            pageId = self.api.get_page_id(space, currentTitle)
            self.api.update_page(pageId, title, body)

        if currentHash != hash or self.forceUpdate:
            if autoindex:
                print('IDX => Title: ' + title)
            else:
                print('UPD => Title: ' + title)

            if self.api.update_or_create(parent_id=parentId,
                                         title=title,
                                         body=body,
                                         representation='storage'):
                id = self.api.get_page_id(space, title)
                self.kv.save(filepath, {
                    'id': id,
                    'title': title,
                    'sha256': hash
                })
                return id
            else:
                return None
        else:
            print('SKP => Title: ' + title)
            return self.api.get_page_id(space, title)

    def __deleteAttachment(self, filepath):
        metadata = self.kv.load(filepath)
        filename = os.path.basename(filepath)
        if metadata['id']:
            try:
                print('DEL Att. => Title: ' + filename)
                # https://confluence.atlassian.com/confkb/confluence-rest-api-lacks-delete-method-for-attachments-715361922.html
                # self.api.delete_attachment_by_id(metadata['id'], 1)
                self.api.remove_content(metadata['id'])
            except (HTTPError, ApiError):
                pass

    def __updateAttachment(self, space, pageId, filepath):
        filename = os.path.basename(filepath)

        print('UPD Att. => Title: ' + filename)
        results = self.api.attach_file(filepath,
                                       name=filename,
                                       page_id=pageId,
                                       space=space)
        id = results['id'] if 'id' in results else results['results'][0]['id']
        self.kv.save(filepath, {'id': id, 'title': filename, 'sha256': None})
        return id

    def __publishRecursive(self, space, parentId, path):
        # File: _index.md
        indexParentId = parentId
        indexPath = path + os.sep + '_index.md'
        if os.path.isfile(indexPath):
            # Use local _index.md file
            indexParentId = self.__updatePage(space, parentId, indexPath)
        else:
            # Autoindex simulate _index.md in Confluence if missing locally
            # Except for (root) parentPageId because missing in markdownDir!
            if parentId != self.parentPageId:
                indexParentId = self.__updatePage(space, parentId, indexPath,
                                                  True)

        # Directories: */
        for f in os.scandir(path):
            if f.is_dir():
                self.__publishRecursive(space, indexParentId, f.path)

        # Files: *.* (Except _index.md)
        for f in os.scandir(path):
            if f.is_file():
                if f.path.endswith(".md"):
                    if not f.path.endswith(os.sep + '_index.md'):
                        self.__updatePage(space, indexParentId, f.path)
                else:
                    self.__deleteAttachment(f.path)
                    self.__updateAttachment(space, indexParentId, f.path)

    def delete(self):
        for filepath in sorted(self.kv.keys()):
            metadata = self.kv.load(filepath)

            # Page has Sub-pages (Childs)?
            indexWithChilds = False
            if filepath.endswith('_index.md'):
                childs = 0
                if os.path.isdir(os.path.dirname(filepath)):
                    for f in os.scandir(os.path.dirname(filepath)):
                        if f.path.endswith(".md") and \
                           not f.path.endswith('_index.md'):
                            childs = childs + 1
                indexWithChilds = childs > 0

            if self.forceDelete \
               or (not os.path.isfile(filepath) and not indexWithChilds):
                print('DEL => Id: ' + metadata['id'] + ', Title: ' +
                      metadata['title'])
                if filepath.endswith(".md"):
                    try:
                        if self.api.get_page_by_id(metadata['id']):
                            self.api.remove_page(metadata['id'])
                    except HTTPError as ex:
                        code = ex.response.status_code
                        if code != 404:
                            print("DEL Pag. (Error):" + str(code))
                        else:
                            pass
                else:
                    self.__deleteAttachment(filepath)

                self.kv.remove(filepath)

    def publish(self):
        self.__publishRecursive(self.space, self.parentPageId,
                                self.markdownDir)
Exemplo n.º 9
0
class Readme2Confluence:
    """
    Main class that will create the Confluence page from Markdown
    """
    def __init__(self, url, username, password, space):
        self.confluence = Confluence(url, username=username, password=password)
        spaces = self.confluence.get_all_spaces(start=0, limit=500)
        if any(s for s in spaces if s["name"] == space):
            self.space = space
        else:
            raise ValueError("{} is not valid Confluence Space".format(space))

    def get_page_id(self, title):
        """
        Retrieve the ID of a page using it's title
        This is basically a small wrapper around confluence.get_page_id().
        The main reason for the wrapper is to better handle pages that aren't found
        by raising an exception
        """
        page_id = self.confluence.get_page_id(self.space, title)
        if page_id is None:
            raise ValueError("{} is not a valid page title in {}".format(
                title, self.space))
        return page_id

    def get_page_html(self, title):
        """
        Retrieve a confluence page and return it as a
        HTML (string)
        """
        page_id = self.get_page_id(title)
        return self.confluence.get_page_by_id(page_id)

    def send2confluence(self,
                        title,
                        parent_title=None,
                        md_file=None,
                        md_text=""):
        """
        Method that creates the confluence page.
        Page will either be created or updated depending on what is
        required.
        """
        if md_file is None:
            body = md2html(md_text)
        else:
            body = md2html(get_md_text(md_file))

        parent_id = None if parent_title is None else self.get_page_id(
            parent_title)
        page_id = self.confluence.get_page_id(self.space, title)

        if page_id is None:
            ret = self.confluence.create_page(self.space,
                                              title,
                                              body,
                                              parent_id=parent_id,
                                              type='page',
                                              representation='storage')
        else:
            ret = self.confluence.update_page(page_id,
                                              title,
                                              body,
                                              parent_id=parent_id,
                                              type='page',
                                              representation='storage')
        return ret
Exemplo n.º 10
0
class ConfluencePage:

    dre = re.compile(r'(\d+)')

    def __init__(self, config_file, timestamp):
        self.timestamp = timestamp
        self.page = None

        config = configparser.ConfigParser()
        config.read(config_file)

        if "confluence" not in config.sections():
            print("No section 'confluence' found")
            exit(1)

        self.url = config.get("confluence", "url")
        self.username = config.get("confluence", "username")
        self.password = config.get("confluence", "password")
        self.page_id = config.get("confluence", "pageId")
        self.templateFile = config.get("confluence", "templateFile")
        title = config.get("confluence", "title")
        space = config.get("confluence", "space")

        if self.url is None or self.username is None or self.password is None:
            print("Config file missing configurations")
            exit(1)

        if self.page_id is None and (title is None or space is None):
            print("pageId or title and space missing")
            exit(1)

        self.confluence = Confluence(url=self.url,
                                     username=self.username,
                                     password=self.password)

        if self.page_id is None:
            if not self.confluence.page_exists(space, title):
                print(("Page Space: '%s' Title: '%s' does not exist" %
                       (space, title)))
                exit(1)

            self.page_id = self.confluence.get_page_id(space, title)

    def generate_page(self, library, projects):
        # Generate page - latest version on develop
        project_versions_body = self.confluence_generate_last_versions(
            projects, library)

        # Generate page - which project depends on each library version
        lib_versions_body = self.confluence_generate_library_versions(library)

        template = open(self.templateFile, "r").read()
        self.page = template.format(timestamp=self.timestamp,
                                    lib_versions_body=lib_versions_body,
                                    app_versions_body=project_versions_body)

    def confluence_generate_last_versions(self, projects, library):
        app_versions_body = ""

        for project in projects:
            project_versions_sorted = sorted(
                project.versions,
                key=lambda version: [
                    int(s) if s.isdigit() else s.lower()
                    for s in re.split(self.dre, version.name)
                ],
                reverse=True)
            if len(project_versions_sorted) <= 0:
                project_version = "Unknown"
                library_version = "Not using %s" % library.name
            else:
                latest_version = project_versions_sorted[0]
                project_version = latest_version.name
                library_version = latest_version.library_version if latest_version.library_version is not None else "Not using %s" % library.name

            app_versions_body += "<tr><td>{project_name}</td><td style=\"text-align: center;\"><strong>{project_version}</strong></td><td style=\"text-align: center;\"><strong>{library_version}</strong></td></tr>" \
                .format(project_name=project.name, project_version=project_version, library_version=library_version)

        return app_versions_body

    def confluence_generate_library_versions(self, library):
        sorted_versions = sorted(library.versions,
                                 key=lambda version: [
                                     int(s) if s.isdigit() else s.lower()
                                     for s in re.split(self.dre, version.name)
                                 ],
                                 reverse=True)

        # TODO add links to release notes

        versions_body = ""
        for version in sorted_versions:
            if len(version.dependency_in) == 0:
                versions_body += "<tr><th><strong>{lib_name}</strong></th><td><strong>Not in use</strong></td><td></td></tr>\n" \
                    .format(lib_name=version.name)
            else:
                versions_body += "<tr><th rowspan=\"{num_of_dep}\"><strong>{lib_name}</strong></th><td>{dep_name}</td><td colspan=\"1\" style=\"text-align: center;\">{dep_rev}</td></tr>\n" \
                    .format(lib_name=version.name, num_of_dep=len(version.dependency_in), dep_name=version.dependency_in[0][0],
                            dep_rev=version.dependency_in[0][1].name)
                for dep in version.dependency_in[1:]:
                    versions_body += "<tr><td>{dep_name}</td><td colspan=\"1\" style=\"text-align: center;\">{dep_rev}</td></tr>" \
                        .format(dep_name=dep[0], dep_rev=dep[1].name)

        return versions_body

    def publish_page(self):
        if self.page is not None:
            print((self.confluence.update_page(self.page_id,
                                               "Test",
                                               self.page,
                                               type='page',
                                               representation='storage')))
Exemplo n.º 11
0
class ConfluencePublisher():

    def __init__(
            self, url, username, api_token,
            page_title_prefix, markdown_dir, db_path, space, parent_pageid,
            force_update=False, force_delete=False, skip_update=False,
            verbose=False):

        self.api = Confluence(url=url, username=username, password=api_token)
        self.page_title_prefix = page_title_prefix
        self.markdown_dir = markdown_dir
        self.kv = KeyValue(db_path)
        self.space = space
        self.parent_pageid = parent_pageid
        self.force_update = force_update
        self.force_delete = force_delete
        self.skip_update = skip_update
        self.confluence_renderer = ConfluenceRenderer(verbose)
        self.renderer = mistune.create_markdown(
            renderer=self.confluence_renderer,
            plugins=[
                plugin_front_matter,
                DirectiveInclude(),
                HugoRefLinkPlugin(self.markdown_dir),
                'strikethrough',
                'footnotes',
                'table',
                'url',
                Admonition(),
                plugin_html_comment,
            ]
        )

    def __update_page(self, space, parentid, filepath, autoindex=False):

        metadata = self.kv.load(filepath)

        current_title = metadata['title']
        current_hash = metadata['sha256']
        sha_hash = get_file_sha256(filepath)

        # --- Render (BEGIN)

        body = ''
        state = {'front_matter': {}}

        if autoindex:
            body = generate_autoindex()
            state['front_matter']['title'] = \
                os.path.basename(os.path.dirname(filepath)).title()
        else:
            if filepath.endswith("_index.md"):
                body = generate_autoindex()
            body += self.renderer.read(filepath, state)

        title = '{}{}'.format(self.page_title_prefix,
                              state['front_matter']['title'])

        # --- Render (END)

        if current_title and current_title != title:
            print('REN => Title: ' + title)
            confluence_page_id = self.api.get_page_id(space, current_title)
            self.api.update_page(confluence_page_id, title, body)

        if current_hash != sha_hash or self.force_update:
            if autoindex:
                print('IDX => Title: ' + title)
            else:
                print('UPD => Title: ' + title)

            if self.api.update_or_create(
                parent_id=parentid,
                title=title,
                body=body,
                representation='storage'
            ):
                confluence_page_id = self.api.get_page_id(space, title)
                self.kv.save(filepath,
                             {'id': confluence_page_id, 'title': title, 'sha256': sha_hash})
                return confluence_page_id

            return None
        else:
            print('SKP => Title: ' + title)
            return self.api.get_page_id(space, title)

    def __delete_attachment(self, filepath):
        metadata = self.kv.load(filepath)
        filename = os.path.basename(filepath)
        if metadata['id']:
            try:
                print('DEL Att. => Title: ' + filename)
                # https://confluence.atlassian.com/confkb/confluence-rest-api-lacks-delete-method-for-attachments-715361922.html
                # self.api.delete_attachment_by_id(metadata['id'], 1)
                self.api.remove_content(metadata['id'])
            except (HTTPError, ApiError):
                pass

    def __update_attachment(self, space, pageid, filepath):
        filename = os.path.basename(filepath)

        print('UPD Att. => Title: ' + filename)
        results = self.api.attach_file(filepath,
                                       name=filename,
                                       page_id=pageid,
                                       space=space)
        confluence_page_id = results['id'] if 'id' in results else results['results'][0]['id']
        self.kv.save(filepath, {'id': confluence_page_id,
                                'title': filename, 'sha256': None})
        return confluence_page_id

    def __publish_recursive(self, space, parentid, path, root=False):
        # File: _index.md
        index_parentid = parentid
        index_path = path + os.sep + '_index.md'
        if not root:
            if os.path.isfile(index_path):
                # Use local _index.md file
                index_parentid = self.__update_page(
                    space, parentid, index_path)
            else:
                # Autoindex simulate _index.md in Confluence if missing locally
                index_parentid = self.__update_page(
                    space, parentid, index_path, True)

        # Directories: */
        for f in os.scandir(path):
            if f.is_dir():
                self.__publish_recursive(space, index_parentid, f.path)

        # Files: *.* (Except _index.md)
        for f in os.scandir(path):
            if f.is_file():
                if f.path.endswith(".md"):
                    if not f.path.endswith(os.sep + '_index.md'):
                        self.__update_page(space, index_parentid, f.path)
                else:
                    self.__delete_attachment(f.path)
                    self.__update_attachment(space, index_parentid, f.path)

    def delete(self):
        for filepath in sorted(self.kv.keys()):
            metadata = self.kv.load(filepath)

            # Page has Sub-pages (Childs)?
            index_with_childs = False
            if filepath.endswith('_index.md'):
                childs = 0
                if os.path.isdir(os.path.dirname(filepath)):
                    for f in os.scandir(os.path.dirname(filepath)):
                        if f.path.endswith(".md") and \
                           not f.path.endswith('_index.md'):
                            childs = childs + 1
                index_with_childs = childs > 0

            if self.force_delete \
               or (not os.path.isfile(filepath) and not index_with_childs):
                print('DEL => Id: '
                      + metadata['id'] + ', Title: ' + metadata['title'])
                if filepath.endswith(".md"):
                    try:
                        if self.api.get_page_by_id(metadata['id']):
                            self.api.remove_page(metadata['id'])
                    except HTTPError as ex:
                        code = ex.response.status_code
                        if code != 404:
                            print("DEL Pag. (Error):" + str(code))
                        else:
                            pass
                else:
                    self.__delete_attachment(filepath)

                self.kv.remove(filepath)

    def publish(self):
        self.__publish_recursive(
            self.space, self.parent_pageid, self.markdown_dir, root=True)
Exemplo n.º 12
0
def main():

    # create logging file handler
    logger = logging.getLogger("Perf_reporter")
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler("perf_report.log")
    sh = logging.StreamHandler(sys.stdout)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    sh.setFormatter(formatter)
    logger.addHandler(fh)
    logger.addHandler(sh)

    logger.info("Reporter started")

    settings_path = 'settings.ini'
    bank_list = []
    # creating settings.ini if not exists, reading all settings
    if os.path.exists(settings_path):
        db_host = config.get_setting(settings_path, 'Settings', 'db_host')
        db_port = config.get_setting(settings_path, 'Settings', 'db_port')
        db_name = config.get_setting(settings_path, 'Settings', 'db_name')
        db_start = config.get_setting(settings_path, 'Settings', 'db_start')
        db_end = config.get_setting(settings_path, 'Settings', 'db_end')
        db_info = config.get_setting(settings_path, 'Settings', 'db_info')
        confluence_auth = (config.get_setting(settings_path, 'Settings', 'confluence_user'),
                           config.get_setting(settings_path, 'Settings', 'confluence_password'))
        confluence_url = config.get_setting(settings_path, 'Settings', 'confluence_url')
        confluence_space = config.get_setting(settings_path, 'Settings', 'confluence_space')
        confluence_page_id = config.get_setting(settings_path, 'Settings', 'confluence_page_id')
        confluence_html_to_post = config.get_setting(settings_path, 'Settings', 'confluence_html_to_post')
        confluence_path_to_post = config.get_setting(settings_path, 'Settings', 'confluence_path_to_post')
        grafana_url = config.get_setting(settings_path, 'Settings', 'grafana_url')
        garafana_dashboard_uri = config.get_setting(settings_path, 'Settings', 'garafana_dashboard_uri')
        grafana_org_id = config.get_setting(settings_path, 'Settings', 'grafana_org_id')
        grafana_panel_id_list = config.get_setting(settings_path, 'Settings', 'grafana_panel_id_list')
        grafana_panel_width = config.get_setting(settings_path, 'Settings', 'grafana_panel_width')
        grafana_panel_height = config.get_setting(settings_path, 'Settings', 'grafana_panel_height')
        logger.info(db_info)
    else:
        config.create_config('settings.ini')
        db_host = config.get_setting(settings_path, 'Settings', 'db_host')
        db_port = config.get_setting(settings_path, 'Settings', 'db_port')
        db_name = config.get_setting(settings_path, 'Settings', 'db_name')
        db_start = config.get_setting(settings_path, 'Settings', 'db_start')
        db_end = config.get_setting(settings_path, 'Settings', 'db_end')
        db_info = config.get_setting(settings_path, 'Settings', 'db_info')
        confluence_auth = (config.get_setting(settings_path, 'Settings', 'confluence_user'),
                           config.get_setting(settings_path, 'Settings', 'confluence_password'))
        confluence_url = config.get_setting(settings_path, 'Settings', 'confluence_url')
        confluence_space = config.get_setting(settings_path, 'Settings', 'confluence_space')
        confluence_page_id = config.get_setting(settings_path, 'Settings', 'confluence_page_id')
        confluence_html_to_post = config.get_setting(settings_path, 'Settings', 'confluence_html_to_post')
        confluence_path_to_post = config.get_setting(settings_path, 'Settings', 'confluence_path_to_post')
        grafana_url = config.get_setting(settings_path, 'Settings', 'grafana_url')
        garafana_dashboard_uri = config.get_setting(settings_path, 'Settings', 'garafana_dashboard_uri')
        grafana_org_id = config.get_setting(settings_path, 'Settings', 'grafana_org_id')
        grafana_panel_id_list = config.get_setting(settings_path, 'Settings', 'grafana_panel_id_list')
        grafana_panel_width = config.get_setting(settings_path, 'Settings', 'grafana_panel_width')
        grafana_panel_height = config.get_setting(settings_path, 'Settings', 'grafana_panel_height')
        logger.info(db_info)

    # command line arguments parser settings
    parser = argparse.ArgumentParser(description='Report builder requires relative template path for '
                                                 'report generation.')
    parser.add_argument(
                        "template_path",
                        metavar='1',
                        type=str,
                        help='file name or template file path')
    args = parser.parse_args()
    logger.debug(parser.parse_args())
    template_path = parser.parse_args().template_path
    db_connection_string = db_query.set_db_connection_string(host=db_host, port=db_port, name=db_name)
    logger.debug("db_connection_string = " + db_connection_string)
    test_time = db_query.set_time_string(start=db_start, end=db_end)
    logger.debug("test_time = " + test_time)
    all_bank_list = db_query.get_bank_list(db_conn=db_connection_string,
                                           test_time=test_time)
    for b in all_bank_list:
        if db_query.check_bank_query(db_conn=db_connection_string,
                                     test_time=test_time,
                                     bank=b):
            bank_list.append(b)
        else:
            continue

    logger.debug(bank_list)
    last_line_list = csv_processing.csv_read_last_line(read_path=template_path)
    csv_report_file_name = csv_processing.csv_file_name(template_path=template_path)
    html_report_file_name = html_processing.html_file_name(template_path=template_path)
    csv_headers = csv_processing.csv_read(read_path=template_path)
    csv_processing.csv_add(data=csv_headers, start_index=0, end_index=1, write_path=csv_report_file_name)

    logger.info("Start info retrieving")

    for bank in sorted(bank_list):
        service_list = []
        target_service_list = []
        found_t_service = []
        all_service_list = db_query.get_service_list(db_conn=db_connection_string,
                                                     test_time=test_time,
                                                     bank=bank)
        for s in all_service_list:
            if db_query.check_service_query(db_conn=db_connection_string,
                                            test_time=test_time,
                                            service=s,
                                            bank=bank):
                service_list.append(s)
            else:
                continue

        all_target_service_list = db_query.get_target_service_list(db_conn=db_connection_string,
                                                                   test_time=test_time,
                                                                   bank=bank)
        for ts in all_target_service_list:
            if db_query.check_target_service_query(db_conn=db_connection_string,
                                                   test_time=test_time,
                                                   t_service=ts,
                                                   bank=bank):
                target_service_list.append(ts)
            else:
                continue
        logger.debug(bank_list, service_list, target_service_list)
        for service in sorted(service_list):
            calculated_list = []
            logger.info("BANK: " + bank)
            logger.info("SERVICE: " + service)
            db_query_list = csv_processing.csv_read(read_path=template_path)
            service_result_list = db_query.query_db(db_conn=db_connection_string,
                                                    q_list=db_query_list,
                                                    service=service,
                                                    test_time=test_time)
            for target_service in sorted(target_service_list):
                if service in target_service:
                    found_t_service.append(target_service)
                    for t in found_t_service:
                        target_service_result_list = db_query.query_db_with_target_services(
                                                                                        db_conn=db_connection_string,
                                                                                        q_ts_list=last_line_list,
                                                                                        bank=bank,
                                                                                        service=service,
                                                                                        target_service=t,
                                                                                        test_time=test_time)
                        logger.debug(target_service_result_list)
                        service_result_list.append(target_service_result_list)
                        logger.debug("OVERALL RESULTS" + str(service_result_list))
                        calculated_list = process_table_data(list_to_p=service_result_list,
                                                             bank=bank,
                                                             service=service,
                                                             target_service=target_service)
                        logger.debug(calculated_list)
                    # service_result_list = []
                    found_t_service = []
            csv_processing.csv_add(data=calculated_list,
                                   start_index=1,
                                   end_index=len(calculated_list),
                                   write_path=csv_report_file_name)
            html_processing.html_add(calculated_list, html_report_file_name)
    list_for_html = csv_processing.csv_read(read_path=csv_report_file_name)
    html_processing.html_write(html_data=list_for_html, report_file_name=html_report_file_name)

    # image retrieving
    logger.info("Image retrieving started")

    time_for_url_grafana = images_processing.set_time_string(start=db_start, end=db_end)
    grafana_panel_width = ast.literal_eval(grafana_panel_width)
    grafana_panel_height = ast.literal_eval(grafana_panel_height)
    grafana_panel_id_list = grafana_panel_id_list.split(',')
    i_count = 99
    for i in range(len(grafana_panel_id_list)):
        images_processing.retrieve_pics(url=grafana_url,
                                        uri=garafana_dashboard_uri,
                                        org_id=grafana_org_id,
                                        test_time=time_for_url_grafana,
                                        id=grafana_panel_id_list[i],
                                        width=grafana_panel_width[grafana_panel_id_list[i]],
                                        height=grafana_panel_height[grafana_panel_id_list[i]],
                                        template_path=template_path,
                                        i_count=i_count)
        i_count -= 1
    logger.info("Image retrieving done!")

    # confluence posting
    confluence = Confluence(url=confluence_url, username=confluence_auth[0], password=confluence_auth[1])
    descr = os.path.splitext(template_path)[0].split('/')[1]
    test_start = db_start.strip('\'')
    test_end_time = db_end.strip('\'').split(" ")[1]
    page_title = "{} {} - {}".format(descr, test_start, test_end_time)
    files = []
    confluence.create_page(space=confluence_space,
                           title=page_title,
                           body=confluence_html_to_post,
                           parent_id=confluence_page_id)

    new_page_id = confluence.get_page_id(space=confluence_space, title=page_title)

    for (dirpath, dirnames, filenames) in walk(confluence_path_to_post):
        files.extend(filenames)
        break

    description = "<p> TEST SUMMARY" + confluence_html_to_post + "</p>"

    page_body = description

    test_duration = "<p> Test duration: " + str(datetime.strptime(db_end, "'%Y-%m-%d %H:%M:%S'") -
                                               datetime.strptime(db_start, "'%Y-%m-%d %H:%M:%S'")) + "</p>"
    page_body += test_duration

    logger.info("Start confluence report attachment")

    for file in sorted(files, reverse=True):
        if str(os.path.splitext(file)[1]) == '.png':
            response = confluence_processing.upload_attachment(auth=confluence_auth,
                                                               page_id=new_page_id,
                                                               file_path=confluence_path_to_post+file)
            base_url, filename, container_id, modification_date, attachment_id = confluence_processing.\
                get_attachments_info(response)
            image_tag = confluence_processing.prepare_html_for_image(base_url=base_url,
                                                                     filename=filename,
                                                                     container_id=container_id,
                                                                     modification_date=modification_date,
                                                                     attachment_id=attachment_id,
                                                                     height="300",
                                                                     width="825")
            image_tag = "<p>" + image_tag + "</p>"
            page_body += image_tag
        elif str(os.path.splitext(file)[1]) == '.html':
            with open(confluence_path_to_post + file, 'r') as fr:
                html_table = fr.read()
                page_body += html_table
        else:
            continue

    logger.info("Start confluence update")
    # confluence.update_page(new_page_id, title=page_title, body=page_body, minor_edit=True)
    confluence_processing.write_data_storage(auth=confluence_auth, html=page_body, page_id=new_page_id)
    logger.info("Report completed")
Exemplo n.º 13
0
        execute_command()
        exit()

    elif comp == False:
        print("There's been change in reporev, proceeding code")
        command = "rm -fr " + file_to_cmp
        execute_command()

# first run
elif len(all_files) == 0:
    print("No files detected,proceeding to update the code")

confluence = Confluence(url="https://confluence.waves.com:8443",
                        username=username,
                        password=password)
pageid = confluence.get_page_id('SWDO', env)

final = [
    ['Revision', 'Date', 'Description'],
]

mylist = list()

myDict = dict()

tree = ET.parse(new_file)
root = tree.getroot()
for elem in root.iter():
    if elem.tag == "logentry":
        revision = elem.attrib['revision']
        myDict['revision'] = revision
Exemplo n.º 14
0
                              expand='description.plain,homepage')
if "statusCode" in status:
    if status["statusCode"] == 404:
        print("Space not found: " + confluenceSpace)
    else:
        print("Unknown error: " + status["statusCode"])
        print(status)
        exit(8)
else:
    print("Found Space : " + confluenceSpace + " Name: " + status["name"])

print(
    "########################################################################")
pageTitle = "My Test Page"
print("Looking for page \"" + pageTitle + "\" in space " + confluenceSpace)
pageID = confluence.get_page_id(confluenceSpace, pageTitle)
if pageID is None:
    print("Page not found: " + pageTitle + " in space " + confluenceSpace)
else:
    print("Found page : " + pageTitle + " pageID: " + str(pageID))

print(
    "########################################################################")
pageTitle = "My New Page " + str(random.randint(0, 100))
print("Looking for page \"" + pageTitle + "\" in space " + confluenceSpace)
pageID = confluence.get_page_id(confluenceSpace, pageTitle)
if pageID is None:
    print("Page not found: " + pageTitle + " in space " + confluenceSpace)
    status = confluence.create_page(space=confluenceSpace,title=pageTitle, \
             body='Hello<strong>World</strong>!')
    if "id" in status: