import os
from sh import wine
import tempfile
import hashlib
import shutil
from urlparse import urlparse

template_specifications = (
    {
        "name": "ie6",
        "hash": b'[\xdfoK\x03\xdb\xcbR$\xef\xc97O\xbf^_\x8f,Fn>\xca\x80\x19\x83\xaa,^R\xe1\x0f\x92',
        "url": "https://github.com/downloads/chrissorchard/malucrawl/wineprefix_ie6.tar.gz",
        "command": r"C:/Program\ Files/Internet\ Explorer/IEXPLORE.EXE {url}"
    }
)


def verify(hash, data):
    s = hashlib.sha256()
    s.update(data)
    return hash == s.digest()


def url_filename(url):
    return os.path.basename(urlparse(url).path)


class WineBrowser():
    closed = False

    def __init__(self, template_spec):
        self.wineprefix = WineBrowser.create_wine_prefix(template_spec)
        self.wine_env = os.environ.copy().update({
            "WINEPREFIX": self.wineprefix,
            "WINEARCH": "win32"
        })
        self.template_spec = template_spec

    def execute(self, url):
        wine(self.template_spec["command"].format(url=url), _env=self.wine_env)  # really this should be run in a separate thread.

    @classmethod
    def create_wine_prefix(template_spec):
        """
        return path to temporary directory containing wine prefix matching the template_spec
        """

        #check if we already have the file.
        #if the tar.gz file does not exist, or the hash does not match download a new copy.
        #avoid repeating this on the same machine.

        temp_dir = tempfile.mkdtemp(prefix="malucrawl_prefix")
        #extract the tar.gz into the temp_dir

        return temp_dir

    def close(self):
        self.closed = True
        shutil.rmtree(self.wineprefix)

import os
from sh import wine
import tempfile
import hashlib
import shutil
from urlparse import urlparse

template_specifications = (
    {
        "name": "ie6",
        "hash": b'[\xdfoK\x03\xdb\xcbR$\xef\xc97O\xbf^_\x8f,Fn>\xca\x80\x19\x83\xaa,^R\xe1\x0f\x92',
        "url": "https://github.com/downloads/chrissorchard/malucrawl/wineprefix_ie6.tar.gz",
        "command": r"C:/Program\ Files/Internet\ Explorer/IEXPLORE.EXE {url}"
    }
)


def verify(hash, data):
    s = hashlib.sha256()
    s.update(data)
    return hash == s.digest()


def url_filename(url):
    return os.path.basename(urlparse(url).path)


class WineBrowser():
    closed = False

    def __init__(self, template_spec):
        self.wineprefix = WineBrowser.create_wine_prefix(template_spec)
        self.wine_env = os.environ.copy().update({
            "WINEPREFIX": self.wineprefix,
            "WINEARCH": "win32"
        })
        self.template_spec = template_spec

    def execute(self, url):
        wine(self.template_spec["command"].format(url=url), _env=self.wine_env)  # really this should be run in a separate thread.

    @classmethod
    def create_wine_prefix(template_spec):
        """
        return path to temporary directory containing wine prefix matching the template_spec
        """

        #todo: check if we already have the file.
        #if the tar.gz file does not exist, or the hash does not match download a new copy.
        #avoid repeating this on the same machine.

        temp_dir = tempfile.mkdtemp(prefix="malucrawl_prefix")
        #extract the tar.gz into the temp_dir

        return temp_dir

    def close(self):
        self.closed = True
        shutil.rmtree(self.wineprefix)

tag todo comments