<commit_before># -*- coding: utf-8 -*-
#    Copyright 2015 Mirantis, 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 errno
import os
import shutil
import tempfile

from solar.core.log import log
from solar.core.transports.ssh import SSHRunTransport
from solar.core.transports.ssh import SSHSyncTransport
from solar import errors
from solar import utils


tempfile.gettempdir()

SOLAR_TEMP_LOCAL_LOCATION = os.path.join(tempfile.tempdir, 'solar_local')


class BaseHandler(object):

    def __init__(self, resources, handlers=None):
        self.resources = resources
        if handlers is None:
            self.transport_sync = SSHSyncTransport()
            self.transport_run = SSHRunTransport()
        else:
            self.transport_run = handlers['run']()
            self.transport_sync = handlers['sync']()
        self.transport_sync.bind_with(self.transport_run)
        self.transport_run.bind_with(self.transport_sync)

    def verify_run_result(self, cmd, result):
        rc, out, err = result
        log.debug('CMD %r RC %s OUT %s ERR %s', cmd, rc, out, err)
        if rc:
            message = 'CMD %r failed RC %s ERR %s' % (cmd, rc, err)
            raise errors.SolarError(message)

    def __enter__(self):
        return self

    def __exit__(self, exc, value, traceback):
        return


class TempFileHandler(BaseHandler):

    def __init__(self, resources, handlers=None):
        super(TempFileHandler, self).__init__(resources, handlers)
        self.dst = None

    def __enter__(self):
        try:
            self.dst = tempfile.mkdtemp(dir=SOLAR_TEMP_LOCAL_LOCATION)
        except OSError as ex:
            if ex.errno == errno.ENOENT:
                os.makedirs(SOLAR_TEMP_LOCAL_LOCATION)
                self.dst = tempfile.mkdtemp(dir=SOLAR_TEMP_LOCAL_LOCATION)
            else:
                raise
        self.dirs = {}
        for resource in self.resources:
            resource_dir = tempfile.mkdtemp(suffix=resource.name, dir=self.dst)
            self.dirs[resource.name] = resource_dir
        return self

    def __exit__(self, type, value, traceback):
        log.debug(self.dst)
        return
        shutil.rmtree(self.dst)

    def _compile_action_file(self, resource, action):<commit_msg>Fixed OSError during concurrent TempFileHandler init<commit_after># -*- coding: utf-8 -*-
#    Copyright 2015 Mirantis, 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 errno
import os
import shutil
import tempfile

from solar.core.log import log
from solar.core.transports.ssh import SSHRunTransport
from solar.core.transports.ssh import SSHSyncTransport
from solar import errors
from solar import utils


tempfile.gettempdir()

SOLAR_TEMP_LOCAL_LOCATION = os.path.join(tempfile.tempdir, 'solar_local')


class BaseHandler(object):

    def __init__(self, resources, handlers=None):
        self.resources = resources
        if handlers is None:
            self.transport_sync = SSHSyncTransport()
            self.transport_run = SSHRunTransport()
        else:
            self.transport_run = handlers['run']()
            self.transport_sync = handlers['sync']()
        self.transport_sync.bind_with(self.transport_run)
        self.transport_run.bind_with(self.transport_sync)

    def verify_run_result(self, cmd, result):
        rc, out, err = result
        log.debug('CMD %r RC %s OUT %s ERR %s', cmd, rc, out, err)
        if rc:
            message = 'CMD %r failed RC %s ERR %s' % (cmd, rc, err)
            raise errors.SolarError(message)

    def __enter__(self):
        return self

    def __exit__(self, exc, value, traceback):
        return


class TempFileHandler(BaseHandler):

    def __init__(self, resources, handlers=None):
        super(TempFileHandler, self).__init__(resources, handlers)
        self.dst = None

    def __enter__(self):
        try:
            self.dst = tempfile.mkdtemp(dir=SOLAR_TEMP_LOCAL_LOCATION)
        except OSError as ex:
            if ex.errno == errno.ENOENT:
                # TODO: we have race condition there
                # could be easily solved with lockfile
                # but it's yet another extra dependency
                # this solution is "hacky"
                try:
                    os.makedirs(SOLAR_TEMP_LOCAL_LOCATION)
                except OSError as ex:
                    if ex.errno == errno.EEXIST:
                        pass
                    else:
                        raise
                self.dst = tempfile.mkdtemp(dir=SOLAR_TEMP_LOCAL_LOCATION)
            else:
                raise
        self.dirs = {}
        for resource in self.resources:
            resource_dir = tempfile.mkdtemp(suffix=resource.name, dir=self.dst)
            self.dirs[resource.name] = resource_dir
        return self

    def __exit__(self, type, value, traceback):
        log.debug(self.dst)
        return
        shutil.rmtree(self.dst)

    def _compile_action_file(self, resource, action):
{'fraction_non_alphanumeric': 0.08434335048978914, 'fraction_numerical': 0.0029885439149925285, 'mean_word_length': 2.4541284403669725, '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': '5740500', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1680, 'n_words': 511}