# Path: confduino/hwpacklist.py
# def hwpack_dir():
#     """return hardware packages root path."""
#     x = arduino_path() / 'hardware'
#     assert x.exists(), x
#     return x
# 
# Path: confduino/util.py
# def tmpdir(directory=None, suffix=''):
#     x = tempfile.mkdtemp(suffix=suffix, prefix='confduino_', dir=directory)
#     return path(x)
# 
# def download(url):
#     log.debug('downloading %s', url)
#     f, _ = urllib.urlretrieve(url)
#     log.debug('downloaded file: %s', f)
#     return f
# 
# def clean_dir(root):
#     '''remove .* and _* files and directories under root'''
#     for x in root.walkdirs('.*', errors='ignore'):
#         x.rmtree()
#     for x in root.walkdirs('_*', errors='ignore'):
#         x.rmtree()
# 
#     for x in root.walkfiles('.*', errors='ignore'):
#         x.remove()
#     for x in root.walkfiles('_*', errors='ignore'):
#         x.remove()


from confduino.hwpacklist import hwpack_dir
from confduino.util import tmpdir, download, clean_dir
from entrypoint2 import entrypoint
from path import Path as path
from pyunpack import Archive
import logging

log = logging.getLogger(__name__)


def find_hwpack_dir(root):
    """search for hwpack dir under root."""
    root = path(root)
    log.debug('files in dir: %s', root)
    for x in root.walkfiles():
        log.debug('  %s', x)

    hwpack_dir = None
    for h in (root.walkfiles('boards.txt')):
        assert not hwpack_dir
        hwpack_dir = h.parent
        log.debug('found hwpack: %s', hwpack_dir)


# Path: confduino/hwpacklist.py
# def hwpack_dir():
#     """return hardware packages root path."""
#     x = arduino_path() / 'hardware'
#     assert x.exists(), x
#     return x
# 
# Path: confduino/util.py
# def tmpdir(directory=None, suffix=''):
#     x = tempfile.mkdtemp(suffix=suffix, prefix='confduino_', dir=directory)
#     return path(x)
# 
# def download(url):
#     log.debug('downloading %s', url)
#     f, _ = urllib.urlretrieve(url)
#     log.debug('downloaded file: %s', f)
#     return f
# 
# def clean_dir(root):
#     '''remove .* and _* files and directories under root'''
#     for x in root.walkdirs('.*', errors='ignore'):
#         x.rmtree()
#     for x in root.walkdirs('_*', errors='ignore'):
#         x.rmtree()
# 
#     for x in root.walkfiles('.*', errors='ignore'):
#         x.remove()
#     for x in root.walkfiles('_*', errors='ignore'):
#         x.remove()


# Path: confduino/hwpackinstall.py
from confduino.hwpacklist import hwpack_dir
from confduino.util import tmpdir, download, clean_dir
from entrypoint2 import entrypoint
from path import Path as path
from pyunpack import Archive
import logging


log = logging.getLogger(__name__)


def find_hwpack_dir(root):
    """search for hwpack dir under root."""
    root = path(root)
    log.debug('files in dir: %s', root)
    for x in root.walkfiles():
        log.debug('  %s', x)

    hwpack_dir = None
    for h in (root.walkfiles('boards.txt')):
        assert not hwpack_dir
        hwpack_dir = h.parent
        log.debug('found hwpack: %s', hwpack_dir)


    assert hwpack_dir