"""Module that contains shared functions to the test classes
"""

import os
from os.path import join, dirname, isdir
import sys
import shutil
import subprocess

# Consts
APP = "update-conf.py"
TESTS_DIR = dirname(__file__)
APP_DIR = join(TESTS_DIR, os.pardir)

# Consts to be used by the test classes
SNIPPETS_DIR = join(TESTS_DIR, "snippets")
TMP_DIR = join(TESTS_DIR, "tmp")
CONF_FILE = join(TESTS_DIR, "config", "update-conf.py.conf")
RESULTS_DIR = join(TESTS_DIR, "expected_results")
SCRIPT = join(APP_DIR, APP)


def import_app():
    """Load the app script as a module

    This function is necessary for two reasons:

    - The script is not in the python path;
    - The Python "import" statement can't load modules that contains the "-"
      and "." characters in its name.

    See: http://stackoverflow.com/questions/7583652/python-module-with-a-dash-
             or-hyphen-in-its-name
         http://stackoverflow.com/questions/678236/how-to-get-the-filename-
             without-the-extension-from-a-path-in-python
    """
    sys.path.append(APP_DIR)
    app_module = APP.split(".")[0]

    return __import__(app_module)


def clean_tmp():
    """Clean the TMP_DIR directory
    """
    if isdir(TMP_DIR):
        shutil.rmtree(TMP_DIR)
    os.mkdir(TMP_DIR)

"""Module that contains shared functions to the test classes
"""

import os
from os.path import join, abspath, dirname, isdir
import sys
import shutil
import subprocess

# Consts
APP = "update-conf.py"
TESTS_DIR = abspath(dirname(__file__))
APP_DIR = join(TESTS_DIR, os.pardir)

# Consts to be used by the test classes
SNIPPETS_DIR = join(TESTS_DIR, "snippets")
TMP_DIR = join(TESTS_DIR, "tmp")
CONF_FILE = join(TESTS_DIR, "config", "update-conf.py.conf")
RESULTS_DIR = join(TESTS_DIR, "expected_results")
SCRIPT = join(APP_DIR, APP)


def import_app():
    """Load the app script as a module

    This function is necessary for two reasons:

    - The script is not in the python path;
    - The Python "import" statement can't load modules that contains the "-"
      and "." characters in its name.

    See: http://stackoverflow.com/questions/7583652/python-module-with-a-dash-
             or-hyphen-in-its-name
         http://stackoverflow.com/questions/678236/how-to-get-the-filename-
             without-the-extension-from-a-path-in-python
    """
    sys.path.append(APP_DIR)
    app_module = APP.split(".")[0]

    return __import__(app_module)


def clean_tmp():
    """Clean the TMP_DIR directory
    """
    if isdir(TMP_DIR):
        shutil.rmtree(TMP_DIR)
    os.mkdir(TMP_DIR)

FIX when getting local dir we should use always use abspath