# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_BUILT = 5
# 
# EVENT_ALREADY_BUILDING = 5
# 
# EVENT_START_BUILDING = 4
# 
# Path: henet/util.py
# def save_build_hash(directory, cache_dir):
#     if not os.path.exists(cache_dir):
#         os.makedirs(cache_dir)
#     filename = os.path.join(cache_dir, md5(directory) + '.age')
# 
#     new_hash = hash(directory)
#     with open(filename, 'w') as f:
#         f.write(new_hash)


import os
import subprocess
from functools import partial
from bottle import post, app, redirect, request
from bottle_utils.csrf import csrf_protect
from henet.events import (emit, EVENT_BUILT, EVENT_ALREADY_BUILDING,
                          EVENT_START_BUILDING)
from henet.util import save_build_hash


def _run(command, wdir):
    return subprocess.call([command], cwd=wdir, shell=True)


@post("/build", no_i18n=True)
@csrf_protect
def build():

    if app.workers.in_progress('build-pelican'):
        emit(EVENT_ALREADY_BUILDING)
        redirect('/')
        return

    cache_dir = app._config['henet']['cache_dir']
    content_dir = app._config['henet']['pelican_content_path']
    cmd = app._config['henet']['build_command']
    cmd_dir = app._config['henet'].get('build_working_dir', os.getcwd())

    client_id = request.remote_addr

    def done_building(client_id, *args):
        save_build_hash(content_dir, cache_dir)
        emit(EVENT_BUILT, client_id=client_id)

    done_building = partial(done_building, client_id)

    app.workers.apply_async('build-pelican', _run, (cmd, cmd_dir),
                            done_building)

# Path: henet/events.py
# def emit(event, **data):
#     for subscriber in _SUBSCRIBERS[event]:
#         subscriber(event, **data)
#     for subscriber in _SUBSCRIBERS[ALL_EVENTS]:
#         subscriber(event, **data)
# 
# EVENT_BUILT = 5
# 
# EVENT_ALREADY_BUILDING = 5
# 
# EVENT_START_BUILDING = 4
# 
# Path: henet/util.py
# def save_build_hash(directory, cache_dir):
#     if not os.path.exists(cache_dir):
#         os.makedirs(cache_dir)
#     filename = os.path.join(cache_dir, md5(directory) + '.age')
# 
#     new_hash = hash(directory)
#     with open(filename, 'w') as f:
#         f.write(new_hash)


# Path: henet/views/build.py
import os
import subprocess
from functools import partial
from bottle import post, app, redirect, request
from bottle_utils.csrf import csrf_protect
from henet.events import (emit, EVENT_BUILT, EVENT_ALREADY_BUILDING,
                          EVENT_START_BUILDING)
from henet.util import save_build_hash



def _run(command, wdir):
    return subprocess.call([command], cwd=wdir, shell=True)


@post("/build", no_i18n=True)
@csrf_protect
def build():

    if app.workers.in_progress('build-pelican'):
        emit(EVENT_ALREADY_BUILDING)
        redirect('/')
        return

    cache_dir = app._config['henet']['cache_dir']
    content_dir = app._config['henet']['pelican_content_path']
    cmd = app._config['henet']['build_command']
    cmd_dir = app._config['henet'].get('build_working_dir', os.getcwd())

    client_id = request.remote_addr

    def done_building(client_id, *args):
        save_build_hash(content_dir, cache_dir)
        emit(EVENT_BUILT, client_id=client_id)

    done_building = partial(done_building, client_id)

    app.workers.apply_async('build-pelican', _run, (cmd, cmd_dir),
                            done_building)

    emit(EVENT_START_BUILDING)