# 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']

# 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']