<commit_before>#   Copyright 2011 Red Hat, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import logging
import os
import os.path
from imgfac.ApplicationConfiguration import ApplicationConfiguration
from threading import BoundedSemaphore

class ReservationManager(object):
    """ TODO: Docstring for ReservationManager """
    instance = None

    DEFAULT_MINIMUM = 21474836480

    MIN_PORT = 1025
    MAX_PORT = 65535

    ### Properties
    def default_minimum():
        """The property default_minimum"""
        def fget(self):
            return self._default_minimum
        def fset(self, value):
            self._default_minimum = value
        return locals()
    default_minimum = property(**default_minimum())

    @property
    def available_space(self):
        """Dictionary of mount points and bytes available."""
        space = dict()
        for path in self._mounts.keys():
            space.update({path:self.available_space_for_path(path)})
        return space

    @property
    def reservations(self):
        """Dictionary of filepaths and number of bytes reserved for each."""
        reservations = dict()
        for key in self._mounts.keys():
            reservations.update(self._mounts[key]['reservations'])
        return reservations

    @property
    def queues(self):
        """The property queues"""
        return self._queues.keys()
    ### END Properties

    def __new__(cls, *p, **k):
        if cls.instance is None:
            i = super(ReservationManager, cls).__new__(cls, *p, **k)
            # initialize here, not in __init__()
            i.log = logging.getLogger('%s.%s' % (__name__, i.__class__.__name__))
            i.default_minimum = cls.DEFAULT_MINIMUM
            i._mounts = dict()
            i.appconfig = ApplicationConfiguration().configuration
            i._queues = dict(local=BoundedSemaphore(i.appconfig.get('max_concurrent_local_sessions', 1)),
                             ec2=BoundedSemaphore(i.appconfig.get('max_concurrent_ec2_sessions', 1)))
            i._named_locks = { } 
            i._named_locks_lock = BoundedSemaphore()
            i._listen_port = cls.MIN_PORT<commit_msg>Make listen_port initial values less likely to conflict<commit_after>#   Copyright 2011 Red Hat, Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import logging
import os
import os.path
from imgfac.ApplicationConfiguration import ApplicationConfiguration
from threading import BoundedSemaphore

class ReservationManager(object):
    """ TODO: Docstring for ReservationManager """
    instance = None

    DEFAULT_MINIMUM = 21474836480

    MIN_PORT = 1025
    MAX_PORT = 65535

    ### Properties
    def default_minimum():
        """The property default_minimum"""
        def fget(self):
            return self._default_minimum
        def fset(self, value):
            self._default_minimum = value
        return locals()
    default_minimum = property(**default_minimum())

    @property
    def available_space(self):
        """Dictionary of mount points and bytes available."""
        space = dict()
        for path in self._mounts.keys():
            space.update({path:self.available_space_for_path(path)})
        return space

    @property
    def reservations(self):
        """Dictionary of filepaths and number of bytes reserved for each."""
        reservations = dict()
        for key in self._mounts.keys():
            reservations.update(self._mounts[key]['reservations'])
        return reservations

    @property
    def queues(self):
        """The property queues"""
        return self._queues.keys()
    ### END Properties

    def __new__(cls, *p, **k):
        if cls.instance is None:
            i = super(ReservationManager, cls).__new__(cls, *p, **k)
            # initialize here, not in __init__()
            i.log = logging.getLogger('%s.%s' % (__name__, i.__class__.__name__))
            i.default_minimum = cls.DEFAULT_MINIMUM
            i._mounts = dict()
            i.appconfig = ApplicationConfiguration().configuration
            i._queues = dict(local=BoundedSemaphore(i.appconfig.get('max_concurrent_local_sessions', 1)),
                             ec2=BoundedSemaphore(i.appconfig.get('max_concurrent_ec2_sessions', 1)))
            i._named_locks = { } 
            i._named_locks_lock = BoundedSemaphore()
            # Initialize based on PID to prevent conflicts from multiple CLI runs on the same machine
            # TODO: This is TinMan/Oz specific - move it to the plugin
            i._listen_port = cls.MIN_PORT + (os.getpid() % (cls.MAX_PORT - cls.MIN_PORT))
{'fraction_non_alphanumeric': 0.09277984743657974, 'fraction_numerical': 0.011353556856483945, 'mean_word_length': 2.723910171730515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6316797', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1504, 'n_words': 474}