summaryrefslogtreecommitdiffstats
path: root/filters/ccs2ccsflat.py
blob: 889e006ac6b0ca8d036adb243cecf8e2155ced3d (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
# -*- coding: UTF-8 -*-
# Copyright 2013 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2 (a copy included | http://gnu.org/licenses/gpl-2.0.txt)

from os.path import pardir
from subprocess import Popen, PIPE
import logging

from clufter.filter import Filter, FilterError
from clufter.utils import which

log = logging.getLogger(__name__)
# XXX
CCS_FLATTEN = which('ccs_flatten', pardir) or ''


@Filter.deco('ccs', 'ccsflat')
def ccs2ccsflat(self, in_obj, verify=False):
    # XXX currently ccs_flatten does not handle stdin (tempfile.mkstemp?)
    if verify:
        in_obj.verify()
    in_file = in_obj('file')
    try:
        command = [CCS_FLATTEN, in_file]
        log.info("running `{0}'".format(' '.join(command)))
        proc = Popen(command, stdout=PIPE, stderr=PIPE)
    except OSError:
        raise FilterError(self, "ccs_flatten binary seems unavailable")
    out, err = proc.communicate()
    if proc.returncode != 0:
        raise FilterError(self, "ccs_flatten exit code: {0}\n\t{1}",
                          proc.returncode, err)
    elif out == '':
        # "No resource trees defined; nothing to do"
        with file(in_file, 'r') as f:
            out = f.read()
    return ('bytestring', out)