# Path: cloudify/proxy/server.py
# class CtxProxy(object):
# class HTTPCtxProxy(CtxProxy):
#         class BottleServerAdapter(bottle.ServerAdapter):
#                 class Server(WSGIServer):
#                 class Handler(WSGIRequestHandler):
# class ZMQCtxProxy(CtxProxy):
# class UnixCtxProxy(ZMQCtxProxy):
# class TCPCtxProxy(ZMQCtxProxy):
# class StubCtxProxy(object):
# class PathDictAccess(object):
#     def __init__(self, ctx, socket_url):
#     def process(self, request):
#     def close(self):
#     def __init__(self, ctx, port=None):
#     def _start_server(self):
#             def run(self, app):
#                     def handle_error(self, request, client_address):
#                     def address_string(self):
#                     def log_request(*args, **kwargs):
#         def serve():
#     def close(self):
#     def _request_handler(self):
#     def __init__(self, ctx, socket_url):
#     def poll_and_process(self, timeout=1):
#     def close(self):
#     def __init__(self, ctx, socket_path=None):
#     def __init__(self, ctx, ip='127.0.0.1', port=None):
#     def close(self):
# def process_ctx_request(ctx, args):
# def _desugar_attr(obj, attr):
#     def __init__(self, obj):
#     def set(self, prop_path, value):
#     def get(self, prop_path):
#     def _get_object_by_path(self, prop_path, fail_on_missing=True):
#     def _get_parent_obj_prop_name_by_path(self, prop_path):
#     def _raise_illegal(prop_path):
# def get_unused_port():
# 
# Path: cloudify/lru_cache.py
# def lru_cache(maxsize=100, on_purge=None):
#     """Least-recently-used cache decorator.
# 
#     Arguments to the cached function must be hashable.
#     Clear the cache with f.clear().
#     """
#     maxqueue = maxsize * 10
# 
#     def decorating_function(user_function):
#         cache = {}
#         queue = collections.deque()
#         refcount = collections.defaultdict(int)
#         sentinel = object()
#         kwd_mark = object()
# 
#         # lookup optimizations (ugly but fast)
#         queue_append, queue_popleft = queue.append, queue.popleft
#         queue_appendleft, queue_pop = queue.appendleft, queue.pop
# 
#         @functools.wraps(user_function)
#         def wrapper(*args, **kwargs):
#             # cache key records both positional and keyword args
#             key = args
#             if kwargs:
#                 key += (kwd_mark,) + tuple(sorted(kwargs.items()))
# 
#             # record recent use of this key
#             queue_append(key)
#             refcount[key] += 1
# 
#             # get cache entry or compute if not found
#             try:
#                 result = cache[key]
#             except KeyError:
#                 result = user_function(*args, **kwargs)
#                 cache[key] = result
# 
#                 # purge least recently used cache entry
#                 if len(cache) > maxsize:
#                     key = queue_popleft()
#                     refcount[key] -= 1
#                     while refcount[key]:
#                         key = queue_popleft()
#                         refcount[key] -= 1
#                     if on_purge:
#                         on_purge(cache[key])
#                     del cache[key], refcount[key]
# 
#             # periodically compact the queue by eliminating duplicate keys
#             # while preserving order of most recent access
#             if len(queue) > maxqueue:
#                 refcount.clear()
#                 queue_appendleft(sentinel)
#                 for key in ifilterfalse(refcount.__contains__,
#                                         iter(queue_pop, sentinel)):
#                     queue_appendleft(key)
#                     refcount[key] = 1
#             return result
# 
#         def clear():
#             if on_purge:
#                 for value in cache.itervalues():
#                     on_purge(value)
#             cache.clear()
#             queue.clear()
#             refcount.clear()
# 
#         wrapper._cache = cache
#         wrapper.clear = clear
#         return wrapper
#     return decorating_function


import functools
import json
import logging
import logging.handlers
import os
import random
import threading
import tempfile
import zmq
from celery import bootsteps
from celery.bin import Option
from celery.utils.log import get_logger
from cloudify.proxy import server
from cloudify.lru_cache import lru_cache
    close = _stop_logging_server
    shutdown = _stop_logging_server


class ZMQLoggingServer(object):

    def __init__(self, logdir, socket_url, cache_size):
        self.closed = False
        self.zmq_context = zmq.Context(io_threads=1)
        self.socket = self.zmq_context.socket(zmq.PULL)
        self.socket.bind(socket_url)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)
        self.logdir = logdir

        # on the management server, log files are handled by logrotate
        # with copytruncate so we use the simple FileHandler.
        # on agent hosts, we want to rotate the logs using python's
        # RotatingFileHandler.
        if os.environ.get('MGMTWORKER_HOME'):
            self.handler_func = logging.FileHandler
        else:
            self.handler_func = functools.partial(
                logging.handlers.RotatingFileHandler,
                maxBytes=LOGFILE_SIZE_BYTES,
                backupCount=LOGFILE_BACKUP_COUNT)

        # wrap the _get_handler method with an lru cache decorator
        # so we only keep the last 'cache_size' used handlers in in turn
        # have at most 'cache_size' file descriptors open

# Path: cloudify/proxy/server.py
# class CtxProxy(object):
# class HTTPCtxProxy(CtxProxy):
#         class BottleServerAdapter(bottle.ServerAdapter):
#                 class Server(WSGIServer):
#                 class Handler(WSGIRequestHandler):
# class ZMQCtxProxy(CtxProxy):
# class UnixCtxProxy(ZMQCtxProxy):
# class TCPCtxProxy(ZMQCtxProxy):
# class StubCtxProxy(object):
# class PathDictAccess(object):
#     def __init__(self, ctx, socket_url):
#     def process(self, request):
#     def close(self):
#     def __init__(self, ctx, port=None):
#     def _start_server(self):
#             def run(self, app):
#                     def handle_error(self, request, client_address):
#                     def address_string(self):
#                     def log_request(*args, **kwargs):
#         def serve():
#     def close(self):
#     def _request_handler(self):
#     def __init__(self, ctx, socket_url):
#     def poll_and_process(self, timeout=1):
#     def close(self):
#     def __init__(self, ctx, socket_path=None):
#     def __init__(self, ctx, ip='127.0.0.1', port=None):
#     def close(self):
# def process_ctx_request(ctx, args):
# def _desugar_attr(obj, attr):
#     def __init__(self, obj):
#     def set(self, prop_path, value):
#     def get(self, prop_path):
#     def _get_object_by_path(self, prop_path, fail_on_missing=True):
#     def _get_parent_obj_prop_name_by_path(self, prop_path):
#     def _raise_illegal(prop_path):
# def get_unused_port():
# 
# Path: cloudify/lru_cache.py
# def lru_cache(maxsize=100, on_purge=None):
#     """Least-recently-used cache decorator.
# 
#     Arguments to the cached function must be hashable.
#     Clear the cache with f.clear().
#     """
#     maxqueue = maxsize * 10
# 
#     def decorating_function(user_function):
#         cache = {}
#         queue = collections.deque()
#         refcount = collections.defaultdict(int)
#         sentinel = object()
#         kwd_mark = object()
# 
#         # lookup optimizations (ugly but fast)
#         queue_append, queue_popleft = queue.append, queue.popleft
#         queue_appendleft, queue_pop = queue.appendleft, queue.pop
# 
#         @functools.wraps(user_function)
#         def wrapper(*args, **kwargs):
#             # cache key records both positional and keyword args
#             key = args
#             if kwargs:
#                 key += (kwd_mark,) + tuple(sorted(kwargs.items()))
# 
#             # record recent use of this key
#             queue_append(key)
#             refcount[key] += 1
# 
#             # get cache entry or compute if not found
#             try:
#                 result = cache[key]
#             except KeyError:
#                 result = user_function(*args, **kwargs)
#                 cache[key] = result
# 
#                 # purge least recently used cache entry
#                 if len(cache) > maxsize:
#                     key = queue_popleft()
#                     refcount[key] -= 1
#                     while refcount[key]:
#                         key = queue_popleft()
#                         refcount[key] -= 1
#                     if on_purge:
#                         on_purge(cache[key])
#                     del cache[key], refcount[key]
# 
#             # periodically compact the queue by eliminating duplicate keys
#             # while preserving order of most recent access
#             if len(queue) > maxqueue:
#                 refcount.clear()
#                 queue_appendleft(sentinel)
#                 for key in ifilterfalse(refcount.__contains__,
#                                         iter(queue_pop, sentinel)):
#                     queue_appendleft(key)
#                     refcount[key] = 1
#             return result
# 
#         def clear():
#             if on_purge:
#                 for value in cache.itervalues():
#                     on_purge(value)
#             cache.clear()
#             queue.clear()
#             refcount.clear()
# 
#         wrapper._cache = cache
#         wrapper.clear = clear
#         return wrapper
#     return decorating_function


# Path: cloudify/celery/logging_server.py
import functools
import json
import logging
import logging.handlers
import os
import random
import threading
import tempfile
import zmq
from celery import bootsteps
from celery.bin import Option
from celery.utils.log import get_logger
from cloudify.proxy import server
from cloudify.lru_cache import lru_cache

    close = _stop_logging_server
    shutdown = _stop_logging_server


class ZMQLoggingServer(object):

    def __init__(self, logdir, socket_url, cache_size):
        self.closed = False
        self.zmq_context = zmq.Context(io_threads=1)
        self.socket = self.zmq_context.socket(zmq.PULL)
        self.socket.bind(socket_url)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)
        self.logdir = logdir

        # on the management server, log files are handled by logrotate
        # with copytruncate so we use the simple FileHandler.
        # on agent hosts, we want to rotate the logs using python's
        # RotatingFileHandler.
        if os.environ.get('MGMTWORKER_HOME'):
            self.handler_func = logging.FileHandler
        else:
            self.handler_func = functools.partial(
                logging.handlers.RotatingFileHandler,
                maxBytes=LOGFILE_SIZE_BYTES,
                backupCount=LOGFILE_BACKUP_COUNT)

        # wrap the _get_handler method with an lru cache decorator
        # so we only keep the last 'cache_size' used handlers in in turn
        # have at most 'cache_size' file descriptors open

        cache_decorator = lru_cache(maxsize=cache_size,