# Path: fabsetup/fabutils.py
# def run(*args, **kwargs):
#     func, kwargs = _func(kwargs)
#     return func(*args, **kwargs)
# 
# def subtask(*args, **kwargs):
#     '''Decorator which prints out the name of the decorated function on
#     execution.
#     '''
#     depth = kwargs.get('depth', 2)
#     prefix = kwargs.get('prefix', '\n' + '#' * depth + ' ')
#     tail = kwargs.get('tail', '\n')
#     doc1 = kwargs.get('doc1', False)
#     color = kwargs.get('color', cyan)
# 
#     def real_decorator(func):
#         if doc1:
#             return print_full_name(color=color, prefix=prefix,
#                                    tail=tail)(print_doc1(func))
#         return print_full_name(color=color, prefix=prefix, tail=tail)(func)
# 
#     invoked = bool(not args or kwargs)
#     if not invoked:
#         # invoke decorator function which returns the wrapper function
#         return real_decorator(func=args[0])
#     return real_decorator
# 
# def task(func, *args, **kwargs):
#     '''Composition of decorator functions for inherent self-documentation on
#     task execution.
# 
#     On execution, each task prints out its name and its first docstring line.
#     '''
#     prefix = '\n# '
#     tail = '\n'
#     return fabric.api.task(
#         print_full_name(color=magenta,
#                         prefix=prefix,
#                         tail=tail)(print_doc1(func)),
#         *args,
#         **kwargs)
# 
# def suggest_localhost(func):
#     '''Decorator: prompt user for value of env.host_string with default to
#     'localhost' when env.host_string is empty.
# 
#     Modification of decorator function fabric.network.needs_host
#     '''
#     from fabric.network import handle_prompt_abort, to_dict
# 
#     @wraps(func)
#     def host_prompting_wrapper(*args, **kwargs):
#         while not env.get('host_string', False):
#             handle_prompt_abort("the target host connection string")
#             host_string = raw_input("No hosts found. Please specify "
#                                     "host string for connection [localhost]: ")
#             if host_string == '':
#                 host_string = 'localhost'
#             env.update(to_dict(host_string))
#         return func(*args, **kwargs)
# 
#     host_prompting_wrapper.undecorated = func
# 
#     return host_prompting_wrapper
# 
# def print_msg(msg):
#     if msg is not None:
#         print(cyan(flo('{msg}')))
# 
# Path: fabsetup/utils.py
# def flo(string):
#     '''Return the string given by param formatted with the callers locals.'''
#     callers_locals = {}
#     frame = inspect.currentframe()
#     try:
#         outerframe = frame.f_back
#         callers_locals = outerframe.f_locals
#     finally:
#         del frame
#     return string.format(**callers_locals)


import filecmp
from os.path import isfile, expanduser
from fabsetup.fabutils import run, subtask, task, suggest_localhost, print_msg
from fabsetup.utils import flo


def _compile(inst_dir):
    run(flo('cd {inst_dir}  &&  gcc dumpasn1.c -o dumpasn1'))


@subtask
def compile(inst_dir):
    installed = flo('{inst_dir}/dumpasn1.c.1')
    latest = flo('{inst_dir}/dumpasn1.c')
    if isfile(expanduser(installed)):
        # wget has downloaded the latest version and moved the existing
        # (installed) version to dumpasn1.c.1
        if filecmp.cmp(expanduser(latest), expanduser(installed)):
            print_msg('no new version of dumpasn1.c (skip)')
        else:
            print_msg('new version of dumpasn1.c  ->  compile it')
            _compile(inst_dir)
        run(flo('rm {installed}'))
    else:
        print_msg('compile dumpasn1.c')
        _compile(inst_dir)


@subtask
def install(inst_dir):
    run(flo('ln -snf dumpasn1-tool/dumpasn1      ~/bin/dumpasn1'))
    run(flo('ln -snf dumpasn1-tool/dumpasn1.cfg  ~/bin/dumpasn1.cfg'))



# Path: fabsetup/fabutils.py
# def run(*args, **kwargs):
#     func, kwargs = _func(kwargs)
#     return func(*args, **kwargs)
# 
# def subtask(*args, **kwargs):
#     '''Decorator which prints out the name of the decorated function on
#     execution.
#     '''
#     depth = kwargs.get('depth', 2)
#     prefix = kwargs.get('prefix', '\n' + '#' * depth + ' ')
#     tail = kwargs.get('tail', '\n')
#     doc1 = kwargs.get('doc1', False)
#     color = kwargs.get('color', cyan)
# 
#     def real_decorator(func):
#         if doc1:
#             return print_full_name(color=color, prefix=prefix,
#                                    tail=tail)(print_doc1(func))
#         return print_full_name(color=color, prefix=prefix, tail=tail)(func)
# 
#     invoked = bool(not args or kwargs)
#     if not invoked:
#         # invoke decorator function which returns the wrapper function
#         return real_decorator(func=args[0])
#     return real_decorator
# 
# def task(func, *args, **kwargs):
#     '''Composition of decorator functions for inherent self-documentation on
#     task execution.
# 
#     On execution, each task prints out its name and its first docstring line.
#     '''
#     prefix = '\n# '
#     tail = '\n'
#     return fabric.api.task(
#         print_full_name(color=magenta,
#                         prefix=prefix,
#                         tail=tail)(print_doc1(func)),
#         *args,
#         **kwargs)
# 
# def suggest_localhost(func):
#     '''Decorator: prompt user for value of env.host_string with default to
#     'localhost' when env.host_string is empty.
# 
#     Modification of decorator function fabric.network.needs_host
#     '''
#     from fabric.network import handle_prompt_abort, to_dict
# 
#     @wraps(func)
#     def host_prompting_wrapper(*args, **kwargs):
#         while not env.get('host_string', False):
#             handle_prompt_abort("the target host connection string")
#             host_string = raw_input("No hosts found. Please specify "
#                                     "host string for connection [localhost]: ")
#             if host_string == '':
#                 host_string = 'localhost'
#             env.update(to_dict(host_string))
#         return func(*args, **kwargs)
# 
#     host_prompting_wrapper.undecorated = func
# 
#     return host_prompting_wrapper
# 
# def print_msg(msg):
#     if msg is not None:
#         print(cyan(flo('{msg}')))
# 
# Path: fabsetup/utils.py
# def flo(string):
#     '''Return the string given by param formatted with the callers locals.'''
#     callers_locals = {}
#     frame = inspect.currentframe()
#     try:
#         outerframe = frame.f_back
#         callers_locals = outerframe.f_locals
#     finally:
#         del frame
#     return string.format(**callers_locals)


# Path: fabsetup/fabfile/setup/dumpasn1.py
import filecmp
from os.path import isfile, expanduser
from fabsetup.fabutils import run, subtask, task, suggest_localhost, print_msg
from fabsetup.utils import flo



def _compile(inst_dir):
    run(flo('cd {inst_dir}  &&  gcc dumpasn1.c -o dumpasn1'))


@subtask
def compile(inst_dir):
    installed = flo('{inst_dir}/dumpasn1.c.1')
    latest = flo('{inst_dir}/dumpasn1.c')
    if isfile(expanduser(installed)):
        # wget has downloaded the latest version and moved the existing
        # (installed) version to dumpasn1.c.1
        if filecmp.cmp(expanduser(latest), expanduser(installed)):
            print_msg('no new version of dumpasn1.c (skip)')
        else:
            print_msg('new version of dumpasn1.c  ->  compile it')
            _compile(inst_dir)
        run(flo('rm {installed}'))
    else:
        print_msg('compile dumpasn1.c')
        _compile(inst_dir)


@subtask
def install(inst_dir):
    run(flo('ln -snf dumpasn1-tool/dumpasn1      ~/bin/dumpasn1'))
    run(flo('ln -snf dumpasn1-tool/dumpasn1.cfg  ~/bin/dumpasn1.cfg'))



@subtask