import os
from tambo import Transport
from merfi.collector import FileCollector
import merfi
from merfi import logger


class Gpg(object):
    help_menu = 'gpg handler for signing files'
    _help = """
Signs files with gpg. Crawls a given path looking for 'Release' files (by
default)

--output      Custom filename output (vs. $filename.asc).
              Defaults to Release.gpg

Positional Arguments:

[path]        The path to crawl for signing release files. Defaults to current
              working directory
"""

    def __init__(self, argv):
        self.argv = argv
        self.default_keyfile = '/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'

    def get_path(self, arguments):
        if os.path.exists(arguments[-1]):
            return os.path.abspath(arguments[-1])
        else:
            return os.path.abspath('.')

    def parse_args(self):
        parser = Transport(self.argv)
        parser.catch_help = self._help
        parser.parse_args()
        file_output = parser.get('--output') or self.default_keyfile
        merfi.config['path'] = self.get_path(parser.arguments)
        self.sign()

    def sign(self):
        logger.info('Starting path collection, looking for files to sign')
        paths = FileCollector(merfi.config)
        if paths:
            logger.info('%s matching paths found' % len(paths))
            # FIXME: this should spit the actual verified command
            logger.info('will sign with the following commands:')
            logger.info('gpg --armor --detach-sig --output Release.gpg Release')
            logger.info('gpg --clearsign --output InRelease Release')
        else:
            logger.warning('No paths found that matched')

        for path in paths:
            if merfi.config['check']:
                new_gpg_path = path.split('Release')[0]+'Release.gpg'
                new_in_path = path.split('Release')[0]+'InRelease'
                logger.info('[CHECKMODE] signing: %s' % path)
                logger.info('[CHECKMODE] signed: %s' % new_gpg_path)
                logger.info('[CHECKMODE] signed: %s' % new_in_path)
            else:
                # XXX do actual signing here
                pass

@@ -111,16 +111,39 @@
  logger%0A
+from merfi import util%0A
 %0A%0Aclass 
@@ -537,16 +537,39 @@
 tory%0A%22%22%22
+%0A    executable = 'gpg'
 %0A%0A    de
@@ -1153,21 +1153,298 @@
    self.
-sign(
+check_dependencies()%0A        self.sign()%0A%0A    def check_dependencies(self):%0A        if not util.which(self.executable):%0A            logger.error('could not find %25s' %25 self.executable)%0A            raise RuntimeError('%25s needs to be installed and available in $PATH' %25 self.executable
 )%0A%0A    d

check for gpg dependency