#!/usr/bin/env python
import os
import sys
import shutil
import logging


logging.basicConfig(level=logging.INFO)


def get_installed_path():
    paths = [p for p in sys.path if len(p) > 2]
    for path in paths:
        candidate = os.path.join(path, 'wlauto')
        if os.path.isdir(candidate):
            return candidate


if __name__ == '__main__':
    installed_path = get_installed_path()
    if installed_path:
        logging.info('Removing installed package from {}.'.format(installed_path))
        shutil.rmtree(installed_path)
    if os.path.isdir('build'):
        logging.info('Removing local build directory.')
        shutil.rmtree('build')
    logging.info('Removing *.pyc files.')
    for root, dirs, files in os.walk('wlauto'):
        for file in files:
            if file.lower().endswith('.pyc'):
                os.remove(os.path.join(root, file))

    os.system('python setup.py install')

