summaryrefslogtreecommitdiffstats
path: root/filters/ccs_artefacts.py
blob: d57d1dde27375be24ad076d3bfa66d2aadb1f947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: UTF-8 -*-
# Copyright 2015 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
"""ccs-artefacts filter"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"

from ..filter import XMLFilter


def artefact(xpath, kind='?', own=False, desc=''):
    return '''\
    <xsl:value-of select="concat('{kind}; {own}; ', {xpath}, '; {desc}', '&#xA;')"/>
'''.format(xpath=xpath, kind=kind, own='1' if own else '0', desc=desc)


def artefact_cond(cond, xpath=None, **kwargs):
    if xpath is None:
        xpath = '.'
    return '''\
    <xsl:for-each select="{cond}">
        {artefact}</xsl:for-each>
'''.format(cond=cond, artefact=artefact(xpath, **kwargs))

def artefact_cond_ra(*args, **kwargs):
    kwargs['desc'] = nested_concat('RA/${name(..)}: ' + kwargs.get('desc'))
    return artefact_cond(*args, **kwargs)


def nested_concat(*args, **kwargs):
    """Make a concat body of argument(s) of strings and ${XSLT expressions}

    Keyword arguments:
        proper_nesting: whether initial and trailing quote is put externally

    Needlessly careless: produces too many extraneous empty strings ('').
    """
    proper_nesting = kwargs.get('proper_nesting', True)
    acc, part = '', ('', '', '}' + ''.join(args))
    while part[2]:
        if part[0]:
            acc += "', '" + part[0]
        xslt = part[2][:part[2].find('}')]
        if xslt:
            acc += "', " + xslt + ", '"
        part = part[2][len(xslt)+1:].partition("${")
    acc += part[0]
    return acc if proper_nesting else acc.join("''")


@XMLFilter.deco('ccs', 'SimpleFormat')
def ccs_artefacts(flt_ctxt, in_obj):
    """Outputs CVS-like file (kind; own; id; description) for CCS artefacts

    Note that "CCS artefacts" refers to any notable file/port/system-wide user,
    etc.  Boolean flag "own" is used to distinguish the assets/something
    provided (exposed) by the cluster stack itself and the artefacts being
    merely consumed.

    Kind can be:
        A   administrative file (mostly synonym to "application-specific
                                 configuration")
        C   command (binary, optionally with full path and arguments)
        D   directory
        F   general file
        I   IP address
        L   log file
        S   sensitive file
        X   executable file (binary or script with shebang)
    (perhaps also:
        G   group
        P   port (optionally suffixed with '[{tcp,udp}]'?)
        U   user
    )
    """
    return ('bytestring', flt_ctxt.ctxt_proceed_xslt(in_obj, textmode=True))