import os
import sys
import stat
import subprocess
import xbmcaddon
from xbmctorrent.common import RESOURCES_PATH
from xbmctorrent.platform import PLATFORM
from xbmctorrent.utils import url_get


def ensure_exec_perms(file_):
    st = os.stat(file_)
    os.chmod(file_, st.st_mode | stat.S_IEXEC)
    return file_

TORRENT2HTTP = xbmcaddon.Addon("script.module.torrent2http")
BIN_PATH = os.path.join(TORRENT2HTTP.getAddonInfo("path"), "bin", "%(os)s_%(arch)s" % PLATFORM)
TORRENT2HTTP_BINARY = ensure_exec_perms(os.path.join(BIN_PATH, "torrent2http%s" % (sys.platform == "win32" and ".exe" or "")))
NICE_LEVEL = 10


def find_free_port():
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', 0))
    port = s.getsockname()[1]
    s.close()
    return port

def start(**kwargs):
    args = [TORRENT2HTTP_BINARY]
    bind_address = "localhost:%d" % find_free_port()
    kwargs["bind"] = bind_address

    # Are we on ARM? Activate minimum memory mode, and nice torrent2http
    if PLATFORM["arch"] == "arm":
        args = ["/bin/nice", "-n", str(NICE_LEVEL)] + args
        kwargs["minmem"] = None

    for k, v in kwargs.items():
        args.append("--%s" % k)
        if v:
            args.append(v)

    # Needed because torrent2http is vendored with Boost and libtorrent-rasterbar
    env = os.environ.copy()
    env["LD_LIBRARY_PATH"] = BIN_PATH
    env["DYLD_LIBRARY_PATH"] = BIN_PATH

    import xbmc
    xbmc.log(repr(args))
    kwargs = {
        "cwd": BIN_PATH,
        "env": env,
    }
    if sys.platform == "win32":
        si = subprocess.STARTUPINFO()
        si.dwFlags |= 1
        si.wShowWindow = 0
        kwargs["startupinfo"] = si
    proc = subprocess.Popen(args, **kwargs)
    proc.get_bind_address = lambda: bind_address
    proc.shutdown = lambda: url_get("http://%s/shutdown" % bind_address)
    return proc

import os
import sys
import stat
import subprocess
import xbmcaddon
from xbmctorrent.common import RESOURCES_PATH
from xbmctorrent.platform import PLATFORM
from xbmctorrent.utils import url_get


def ensure_exec_perms(file_):
    st = os.stat(file_)
    os.chmod(file_, st.st_mode | stat.S_IEXEC)
    return file_

TORRENT2HTTP = xbmcaddon.Addon("script.module.torrent2http")
BIN_PATH = os.path.join(TORRENT2HTTP.getAddonInfo("path"), "bin", "%(os)s_%(arch)s" % PLATFORM)
TORRENT2HTTP_BINARY = ensure_exec_perms(os.path.join(BIN_PATH, "torrent2http%s" % (sys.platform == "win32" and ".exe" or "")))
NICE_LEVEL = 10


def find_free_port():
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', 0))
    port = s.getsockname()[1]
    s.close()
    return port

def start(**kwargs):
    args = [TORRENT2HTTP_BINARY]
    bind_address = "localhost:%d" % find_free_port()
    kwargs["bind"] = bind_address

    # Are we on ARM? Activate minimum memory mode, and nice torrent2http
    if PLATFORM["arch"] == "arm":
        args = ["nice", "-n", str(NICE_LEVEL)] + args
        kwargs["minmem"] = None

    for k, v in kwargs.items():
        args.append("--%s" % k)
        if v:
            args.append(v)

    # Needed because torrent2http is vendored with Boost and libtorrent-rasterbar
    env = os.environ.copy()
    env["LD_LIBRARY_PATH"] = BIN_PATH
    env["DYLD_LIBRARY_PATH"] = BIN_PATH

    import xbmc
    xbmc.log(repr(args))
    kwargs = {
        "cwd": BIN_PATH,
        "env": env,
    }
    if sys.platform == "win32":
        si = subprocess.STARTUPINFO()
        si.dwFlags |= 1
        si.wShowWindow = 0
        kwargs["startupinfo"] = si
    proc = subprocess.Popen(args, **kwargs)
    proc.get_bind_address = lambda: bind_address
    proc.shutdown = lambda: url_get("http://%s/shutdown" % bind_address)
    return proc

Leverage $PATH for nice. Closes #28