# Path: dput/command.py
# class AbstractCommand(object):
#     """
#     Abstract base class for all concrete dcut command implementations.
#     """
# 
#     __metaclass__ = abc.ABCMeta
# 
#     def __init__(self, interface):
#         self.cmd_name = None
#         self.cmd_purpose = None
#         self.interface = interface
# 
#     @abc.abstractmethod
#     def register(self, parser, **kwargs):
#         pass
# 
#     @abc.abstractmethod
#     def produce(self, fh, args):
#         pass
# 
#     @abc.abstractmethod
#     def validate(self, args):
#         pass
# 
#     @abc.abstractmethod
#     def name_and_purpose(self):
#         pass
# 
#     @abc.abstractmethod
#     def generate_commands_name(self, profile):
#         pass
# 
# Path: dput/exceptions.py
# class DcutError(Exception):
#     pass
# 
# Path: dput/core.py
# CONFIG_LOCATIONS = {
#     "/usr/share/dput-ng/": 30,
#     "/etc/dput.d/": 20,
#     os.path.expanduser("~/.dput.d"): 10,
#     "skel/": 100
# }
# DPUT_CONFIG_LOCATIONS = {
#     "/etc/dput.cf": 15,
#     os.path.expanduser("~/.dput.cf"): 5
# }
# SCHEMA_DIRS = [
#     "/usr/share/dput-ng/schemas",
#     "skel/schemas"
# ]
# def _write_upload_log(logfile, full_log):
# def _enable_debugging(level):
# def mangle_sys():
# def maybe_print_traceback(debug_level, stack):
# def get_local_username():
# 
# Path: dput/util.py
# def run_command(command, env=None):
#     """
#     Run a synchronized command. The argument must be a list of arguments.
#     Returns a triple (stdout, stderr, exit_status)
# 
#     If there was a problem to start the supplied command, (None, None, -1) is
#     returned
#     """
#     if not isinstance(command, list):
#         command = shlex.split(command)
#     try:
#         pipe = subprocess.Popen(command,
#                                 env=env,
#                                 shell=False,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.PIPE)
#     except OSError as e:
#         logger.error("Could not execute %s: %s" % (" ".join(command), e))
#         return (None, None, -1)
#     (output, stderr) = pipe.communicate()
#     output = output.decode('utf-8', errors='replace')
#     stderr = stderr.decode('utf-8', errors='replace')
#     #if pipe.returncode != 0:
#     #   error("Command %s returned failure: %s" % (" ".join(command), stderr))
#     return (output, stderr, pipe.returncode)


import time
import os.path
from dput.command import AbstractCommand
from dput.exceptions import DcutError
from dput.core import logger, get_local_username
from dput.util import run_command
        # I HATE embedded functions. But OTOH this function is not usable
        # somewhere else, so...
        def pretty_print_list(tuples):
            fingerprints = ""
            for entry in tuples:
                fingerprints += "\n- %s (%s)" % entry
            return fingerprints

        # I don't mind embedded functions  ;3
        def flatten(it):
            return [ item for nested in it for item in nested ]

        # TODO: Validate input. Packages must exist (i.e. be not NEW)
        cmd =[
            "gpg", "--no-options",
            "--no-auto-check-trustdb", "--no-default-keyring",
            "--list-key", "--with-colons", "--fingerprint"
        ] + flatten(([ "--keyring", keyring] for keyring in KEYRINGS)) + [ args.dm ]

        (out, err, exit_status) = run_command(cmd)
        if exit_status != 0:
            logger.warning("")
            logger.warning("There was an error looking up the DM's key")
            logger.warning("")
            logger.warning(" dput-ng uses the DM keyring in /usr/share/keyrings/")
            logger.warning(" as the keyring to pull full fingerprints from.")
            logger.warning("")
            logger.warning(" Please ensure your keyring is up to date:")
            logger.warning("")
            logger.warning("   sudo apt-get install debian-keyring")

# Path: dput/command.py
# class AbstractCommand(object):
#     """
#     Abstract base class for all concrete dcut command implementations.
#     """
# 
#     __metaclass__ = abc.ABCMeta
# 
#     def __init__(self, interface):
#         self.cmd_name = None
#         self.cmd_purpose = None
#         self.interface = interface
# 
#     @abc.abstractmethod
#     def register(self, parser, **kwargs):
#         pass
# 
#     @abc.abstractmethod
#     def produce(self, fh, args):
#         pass
# 
#     @abc.abstractmethod
#     def validate(self, args):
#         pass
# 
#     @abc.abstractmethod
#     def name_and_purpose(self):
#         pass
# 
#     @abc.abstractmethod
#     def generate_commands_name(self, profile):
#         pass
# 
# Path: dput/exceptions.py
# class DcutError(Exception):
#     pass
# 
# Path: dput/core.py
# CONFIG_LOCATIONS = {
#     "/usr/share/dput-ng/": 30,
#     "/etc/dput.d/": 20,
#     os.path.expanduser("~/.dput.d"): 10,
#     "skel/": 100
# }
# DPUT_CONFIG_LOCATIONS = {
#     "/etc/dput.cf": 15,
#     os.path.expanduser("~/.dput.cf"): 5
# }
# SCHEMA_DIRS = [
#     "/usr/share/dput-ng/schemas",
#     "skel/schemas"
# ]
# def _write_upload_log(logfile, full_log):
# def _enable_debugging(level):
# def mangle_sys():
# def maybe_print_traceback(debug_level, stack):
# def get_local_username():
# 
# Path: dput/util.py
# def run_command(command, env=None):
#     """
#     Run a synchronized command. The argument must be a list of arguments.
#     Returns a triple (stdout, stderr, exit_status)
# 
#     If there was a problem to start the supplied command, (None, None, -1) is
#     returned
#     """
#     if not isinstance(command, list):
#         command = shlex.split(command)
#     try:
#         pipe = subprocess.Popen(command,
#                                 env=env,
#                                 shell=False,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.PIPE)
#     except OSError as e:
#         logger.error("Could not execute %s: %s" % (" ".join(command), e))
#         return (None, None, -1)
#     (output, stderr) = pipe.communicate()
#     output = output.decode('utf-8', errors='replace')
#     stderr = stderr.decode('utf-8', errors='replace')
#     #if pipe.returncode != 0:
#     #   error("Command %s returned failure: %s" % (" ".join(command), stderr))
#     return (output, stderr, pipe.returncode)


# Path: dput/commands/dm.py
import time
import os.path
from dput.command import AbstractCommand
from dput.exceptions import DcutError
from dput.core import logger, get_local_username
from dput.util import run_command

        # I HATE embedded functions. But OTOH this function is not usable
        # somewhere else, so...
        def pretty_print_list(tuples):
            fingerprints = ""
            for entry in tuples:
                fingerprints += "\n- %s (%s)" % entry
            return fingerprints

        # I don't mind embedded functions  ;3
        def flatten(it):
            return [ item for nested in it for item in nested ]

        # TODO: Validate input. Packages must exist (i.e. be not NEW)
        cmd =[
            "gpg", "--no-options",
            "--no-auto-check-trustdb", "--no-default-keyring",
            "--list-key", "--with-colons", "--fingerprint"
        ] + flatten(([ "--keyring", keyring] for keyring in KEYRINGS)) + [ args.dm ]

        (out, err, exit_status) = run_command(cmd)
        if exit_status != 0:
            logger.warning("")
            logger.warning("There was an error looking up the DM's key")
            logger.warning("")
            logger.warning(" dput-ng uses the DM keyring in /usr/share/keyrings/")
            logger.warning(" as the keyring to pull full fingerprints from.")
            logger.warning("")
            logger.warning(" Please ensure your keyring is up to date:")
            logger.warning("")
            logger.warning("   sudo apt-get install debian-keyring")

            logger.warning("")