import os
from genes.lib.traits import if_any
from genes.debian.traits import is_debian
from genes.ubuntu.traits import is_ubuntu
from subprocess import call
from functools import partial


# TODO: utilize functools partial to handle some of the above functionality
class Config:
    APT_GET = ['apt-get', '-y']
    ADD_REPO = ['add-apt-repository', '-y']
    ENV = os.environ.copy()
    ENV['DEBIAN_FRONTEND'] = "noninteractive"
    ENV_CALL = partial(call, env=ENV)
    # TODO: Split me out to key
    RECV_KEY = ['apt-key', 'adv', '--keyserver', 'hkp://pgp.mit.edu:80', '--recv-keys']


@if_any(is_debian, is_ubuntu)
def install(*packages):
    if packages:
        Config.ENV_CALL(Config.APT_GET + ['install'] + list(packages))
    else:
        # FIXME: need to output failure
        pass


# FIXME: wrap partials with if_any traits
update = partial(Config.ENV_CALL, Config.APT_GET + ['update'])
upgrade = partial(Config.ENV_CALL, Config.APT_GET + ['upgrade'])


@if_any(is_debian, is_ubuntu)
def recv_keys(*keys):
    if keys:
        Config.ENV_CALL(Config.RECV_KEY + list(keys))
    else:
        # FIXME: need to output failure
        pass


@if_any(is_debian, is_ubuntu)
def add_repo(*line_items):
    if line_items:
        # FIXME: this depends on software-properties-common; debian needs this
        Config.ENV_CALL(Config.ADD_REPO + [" ".join(line_items)])
    else:
        # FIXME: need to output failure
        pass


@if_any(is_debian, is_ubuntu)
def add_ppa(ppa):
    if ppa:
        Config.ENV_CALL(Config.ADD_REPO + [ppa])
    else:
        # FIXME: need to output failure
        pass

@@ -837,24 +837,53 @@
 ts%0Aupdate = 
+if_any(is_debian, is_ubuntu)(
 partial(Conf
@@ -923,16 +923,17 @@
 pdate'%5D)
+)
 %0Aupgrade
@@ -935,16 +935,45 @@
 grade = 
+if_any(is_debian, is_ubuntu)(
 partial(
@@ -1018,16 +1018,17 @@
 grade'%5D)
+)
 %0A%0A%0A@if_a

Add if any annotation to partials