Esempio n. 1
0
def api_root():
    # domogik global version
    global_version = sys.modules["domogik"].__version__
    # domogik src version
    domogik_path = os.path.dirname(domogik.rest.__file__)
    subp = Popen("cd %s ; hg branch | xargs hg log -l1 --template '{branch}.{rev} - {date|isodate}' -b" % domogik_path, shell=True, stdout=PIPE, stderr=PIPE)
    (stdout, stderr) = subp.communicate()
    # if hg id has no error, we are using source  repository
    if subp.returncode == 0:
        src_version= "%s" % (stdout)
    # else, we send dmg version
    else:
        src_version = global_version

    info = {}
    info["REST_API_version"] = urlHandler.apiversion
    info["SSL"] = urlHandler.use_ssl
    info["Domogik_version"] = global_version
    info["Sources_version"] = src_version
    info["Host"] = urlHandler.hostname

    # for compatibility with Rest API < 0.6
    info["REST_API_release"] = urlHandler.apiversion
    info["Domogik_release"] = global_version
    info["Sources_release"] = src_version

    # Configuration
    cfg = Loader('mq').load()
    mqcfg = dict(cfg[1])

    data = {"info" : info, 
           "mq": mqcfg}
    return 200, data
Esempio n. 2
0
 def __init__(self, xpl, log=None):
     '''
     Init the query system and connect it to xPL network
     :param xpl: the XplManager instance (usually self.myxpl)
     :param log: a Logger instance (usually took from self.log))
     '''
     self.log = log
     self.__myxpl = xpl
     if self.log != None: self.log.debug("Init config query instance")
     self._keys = {}
     self._listens = {}
     self._result = None
     self.parameters = ["parameter0", "parameter1"]
     self.values = ["valueon", "valueoff"]
     # Check in config file is target is forced
     cfg = Loader('domogik')
     config = cfg.load()
     conf = dict(config[1])
     if conf.has_key('query_xpl_timeout'):
         try:
             self.query_timeout = int(conf["query_xpl_timeout"])
             msg = "Set query timeout to '%s' from domogik.cfg" % self.query_timeout
             if self.log != None: self.log.debug(msg)
         except ValueError:
             #There is an error in domogik.cfg. Set it to default.
             self.query_timeout = 10
             msg = "Error in domogik.cfg. query_xpl_timeout ('%s') is not an integer." % conf[
                 "query_xpl_timeout"]
             if self.log != None: self.log.error(msg)
     else:
         #There is not option in domogik.cfg. Set it to default.
         self.query_timeout = 10
Esempio n. 3
0
 def get_data_files_directory(self):
    """
    Return the directory where a plugin developper can store data files.
    If the directory doesn't exist, try to create it.
    After that, try to create a file inside it.
    If something goes wrong, generate an explicit exception.
    """
    cfg = Loader('domogik')
    my_conf = cfg.load()
    config = dict(my_conf[1])
    path = "{0}/{1}/{2}_{3}/data/" % (self.librairies_directory, PACKAGES_DIR, "plugin", self._name)
    if os.path.exists(path):
        if not os.access(path, os.W_OK & os.X_OK):
            raise OSError("Can't write in directory %s" % path)
    else:
        try:
            os.mkdir(path, '0770')
            self.log.info(u"Create directory %s." % path)
        except:
            raise OSError("Can't create directory %s." % path)
    try:
        tmp_prefix = "write_test";
        count = 0
        filename = os.path.join(path, tmp_prefix)
        while(os.path.exists(filename)):
            filename = "{}.{}".format(os.path.join(path, tmp_prefix),count)
            count = count + 1
        f = open(filename,"w")
        f.close()
        os.remove(filename)
    except :
        raise IOError("Can't create a file in directory %s." % path)
    return path
Esempio n. 4
0
def get_libraries_directory():
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        TODO : see if there is a cleaner way to do this!
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return conf_global['libraries_path']
Esempio n. 5
0
    def __init__(self,
                 component_name,
                 domogik_prefix=True,
                 use_filename=None,
                 log_on_stdout=True):
        '''
        Get a logger with provided parameters and set config
        @param component_name : component name to log
        @param domogik_prefix : if logger name should be prefixed by 'domogik-'
        @param use_filename : if set tells the logger to use this file name (otherwise takes 'component_name')
        @param log_on_stdout : if set to True, allow to display logs in both stdout and log file
        '''
        if component_name not in self.logger:
            LEVELS = {
                'debug': logging.DEBUG,
                'info': logging.INFO,
                'warning': logging.WARNING,
                'error': logging.ERROR,
                'critical': logging.CRITICAL
            }

            cfg = Loader()
            config = cfg.load()[0]
            if use_filename is None:
                filename = "%s/%s.log" % (config['log_dir_path'],
                                          component_name)
            else:
                filename = "%s/%s.log" % (config['log_dir_path'], use_filename)
            level = config['log_level']

            if level not in LEVELS:
                raise ValueError("level must be one of 'debug','info','warning',"\
                        "'error','critical'. Check your config.")

            if domogik_prefix:
                my_logger = logging.getLogger('domogik-%s' % component_name)
            else:
                my_logger = logging.getLogger(component_name)
            # log to file
            my_logger.propagate = 0
            if not my_logger.handlers:
                hdlr = logging.FileHandler(filename)
                formatter = logging.Formatter(
                    '%(asctime)s %(name)s %(levelname)s %(message)s')
                hdlr.setFormatter(formatter)
                my_logger.addHandler(hdlr)

                # if loglevvel is set to debug (all log entries also go to stdout)
                # TODO : why looking about level=debug ? to clean ?
                if log_on_stdout and level == 'debug' and component_name.find(
                        'sqlalchemy') == -1:
                    dhdlr = logging.StreamHandler(sys.stdout)
                    dhdlr.setFormatter(formatter)
                    my_logger.addHandler(dhdlr)

            my_logger.setLevel(LEVELS[level])
            self.logger[component_name] = my_logger
Esempio n. 6
0
    def __init__(self):
        """ Init
        """
        # set logger
        l = logger.Logger("testrunner")
        l.set_format_mode("messageOnly")
        self.log = l.get_logger()

        # read the config file
        try:
            cfg = Loader('domogik')
            config = cfg.load()
            conf = dict(config[1])

            # pid dir path
            self._libraries_path = conf['libraries_path']
            self.log.debug("Libraries path is : {0}".format(self._libraries_path))

        except:
            self.log.error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
            return


        parser = ArgumentParser(description="Launch all the tests that don't need hardware.")
	parser.add_argument("directory",
                          help="What directory to run")
        parser.add_argument("-a", 
                          "--allow-alter", 
                          dest="allow_alter",
                          action="store_true",
                          help="Launch the tests that can alter the configuration of the plugin or the setup (devices, ...)")
        parser.add_argument("-c", 
                          "--criticity", 
                          dest="criticity", 
                          help="Set the minimum level of criticity to use to filter the tests to execute. low/medium/high. Default is low.")
        self.options = parser.parse_args()
	self.testcases = {}
        self.results = {}

        # options
        self.log.info("Domogik release : {0}".format(DMG_VERSION))
        self.log.info("Running test with the folowing parameters:")
	if self.options.allow_alter:
	    self.log.info("- allow to alter the configuration or setup.")
	if self.options.criticity not in (LOW, MEDIUM, HIGH):
            self.options.criticity = LOW
	self.log.info("- criticity : {0}".format(self.options.criticity))

        # check tests folder
	self.log.info("- path {0}".format(self.options.directory))
        if not self.check_dir():
            return

        # check and load the json file
        self.log.info("- json file {0}".format(self.json_file))
	if not self.load_json():
	    return
Esempio n. 7
0
def api_root():
    """
    @api {get} /rest/ Get the status of the REST server
    @apiName getStatus
    @apiGroup Status
    @apiVersion 0.4.1

    @apiSuccess {json} result A json result with the status

    @apiSuccessExample Success-Response:
        HTTTP/1.1 200 OK
        {
            "info": {
                "REST_API_version": "0.7",
                "SSL": false,
                "Host": "igor",
                "Domogik_release": "0.4.0",
                "Domogik_version": "0.4.0",
                "REST_API_release": "0.7",
                "Sources_release": "0.4.0",
                "Sources_version": "0.4.0"
            },
            "mq": {
                "sub_port": "40412",
                "ip": "127.0.0.1",
                "req_rep_port": "40410",
                "pub_port": "40411"
            }
        }
    """
    # domogik global version
    global_version = sys.modules["domogik"].__version__
    src_version = global_version

    info = {}
    info["REST_API_version"] = app.apiversion
    info["Domogik_version"] = global_version
    info["Sources_version"] = src_version
    info["SSL"] = app.use_ssl
    info["Host"] = app.hostname

    # for compatibility with Rest API < 0.6
    info["REST_API_release"] = app.apiversion
    info["Domogik_release"] = global_version
    info["Sources_release"] = src_version

    # mq part
    mqconfig = Loader('mq', 'domogik-mq.cfg')
    config = dict(mqconfig.load()[1])
    mq = {}
    mq["sub_port"] = config["sub_port"]
    mq["ip"] = config["ip"]
    mq["req_rep_port"] = config["req_rep_port"]
    mq["pub_port"] = config["pub_port"]

    data = {"info" : info, "mq": mq}
    return 200, data
Esempio n. 8
0
    def __init__(self, server_interfaces, server_port):
        """ Initiate DbHelper, Logs and config
            Then, start HTTP server and give it initialized data
            @param server_interfaces :  interfaces of HTTP server
            @param server_port :  port of HTTP server
        """

        Plugin.__init__(self, name='admin')
        # logging initialization
        self.log.info(u"Admin Server initialisation...")
        self.log.debug(u"locale : %s %s" % locale.getdefaultlocale())

        try:
            try:
                cfg_rest = Loader('admin')
                config_rest = cfg_rest.load()
                conf_rest = dict(config_rest[1])
                self.interfaces = conf_rest['interfaces']
                self.port = conf_rest['port']
                # if rest_use_ssl = True, set here path for ssl certificate/key
                self.use_ssl = conf_rest['use_ssl']
                self.key_file = conf_rest['ssl_certificate']
                self.cert_file = conf_rest['ssl_key']
            except KeyError:
                # default parameters
                self.interfaces = server_interfaces
                self.port = server_port
                self.use_ssl = False
                self.key_file = ""
                self.cert_file = ""
                self.clean_json = False
            self.log.info(u"Configuration : interfaces:port = %s:%s" %
                          (self.interfaces, self.port))

            # get all datatypes
            cli = MQSyncReq(self.zmq)
            msg = MQMessage()
            msg.set_action('datatype.get')
            res = cli.request('manager', msg.get(), timeout=10)
            if res is not None:
                self.datatypes = res.get_data()['datatypes']
            else:
                self.datatypes = {}

# Launch server, stats
            self.log.info(u"Admin Initialisation OK")
            self.add_stop_cb(self.stop_http)
            self.server = None
            self.start_http()
            # calls the tornado.ioloop.instance().start()

            ### Component is ready
            self.ready(0)
            IOLoop.instance().start()
        except:
            self.log.error(u"%s" % self.get_exception())
Esempio n. 9
0
def get_rest_doc_path():
    """ return the REST API generated doc path
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    ### get libraries path
    path = conf['libraries_path']

    return "{0}/rest_doc_generated_during_install/".format(path)
Esempio n. 10
0
def api_datatype():
    """ return the datatypes json file
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    json_file = "{0}/datatypes.json".format(urlHandler.resources_directory)
    data = json.load(open(json_file))

    return 200, data
Esempio n. 11
0
def get_data_files_directory_for_plugin(plugin_name):
    """ This function is already defined in the Plugin class, but this one is used by the admin in application.py
        In fact, this is not really the same function as this one takes one parameter
    """
    # global config
    cfg_global = Loader('domogik')
    config_global = cfg_global.load()
    conf_global = dict(config_global[1])
    return "{0}/{1}/plugin_{2}/data".format(conf_global['libraries_path'],
                                            "domogik_packages", plugin_name)
Esempio n. 12
0
def get_rest_url():
    """ Return the REST server url (constructed from the configuration file of the host)
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    # we return the url related to the first declared interface in domogik.cfg
    intf = conf['interfaces'].split(",")[0]
    ip = get_ip_for_interfaces([intf])[0]
    return "http://{0}:{1}/".format(ip, conf['port'])
Esempio n. 13
0
def get_backup_dir():
    ### Read the configuration file
    try:
        cfg = Loader('backup')
        config = cfg.load()
        conf = dict(config[1])
        return conf['folder']

    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(
            CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 14
0
def get_packages_dir():
    ### Read the configuration file
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])
        return os.path.join(conf['libraries_path'], PACKAGES_DIR)

    except:
        print(u"Error while reading the configuration file '{0}' : {1}".format(
            CONFIG_FILE, traceback.format_exc()))
        return None
Esempio n. 15
0
    def __init__(self, xpl, log):
        cfg = Loader('mq')
        config = cfg.load()
        conf = dict(config[1])
        if conf.has_key('query_mq'):
            self.mq = True
        else:
            self.mq = False

        if self.mq:
            self.qry = QueryMQ(None, log)
        else:
            self.qry = QueryXPL(xpl, log)
Esempio n. 16
0
 def __init__(self, context, caller_id):
     if ("domogik.common.configloader" in sys.modules):
         cfg = Loader('mq').load()
         self.cfg_mq = dict(cfg[1])
         pub_addr = "tcp://{0}:{1}".format(self.cfg_mq['ip'],
                                           self.cfg_mq['pub_port'])
     else:
         ipaddr = Parameter.objects.get(key='mq-ip')
         port = Parameter.objects.get(key='mq-pub_port')
         pub_addr = "tcp://{0}:{1}".format(ipaddr.value, port.value)
     self.caller_id = caller_id
     self.s_send = context.socket(zmq.PUB)
     self.s_send.connect(pub_addr)
Esempio n. 17
0
 def get_stats_files_directory(self):
     """ Return the directory where a plugin developper can store data files
    """
     cfg = Loader('domogik')
     my_conf = cfg.load()
     config = dict(my_conf[1])
     if config.has_key('package_path'):
         path = "%s/domogik_packages/stats/%s" % (config['package_path'],
                                                  self._name)
     else:
         path = "%s/share/domogik/stats/%s" % (config['src_prefix'],
                                               self._name)
     return path
Esempio n. 18
0
 def __init__(self, context):
     """Initialize the MDPClient.
     """
     if ("domogik.common.configloader" in sys.modules):
         cfg = Loader('mq').load()
         config = dict(cfg[1])
         endpoint = "tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])
     else:
         ip = Parameter.objects.get(key='mq-ip')
         port = Parameter.objects.get(key='mq-req_rep_port')
         endpoint = "tcp://{0}:{1}".format(ip.value, port.value)
     self.socket = ZmqSocket(context, zmq.REQ)
     self.socket.connect(endpoint)         
     return
Esempio n. 19
0
def get_rest_url():
    """ Build and return the rest url
    """
    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    ### get REST ip and port
    port = conf['port']
    interfaces = conf['interfaces']
    intf = interfaces.split(',')
    # get the first ip of the first interface declared
    ip = get_ip_for_interfaces(intf)[0]

    return "http://{0}:{1}".format(ip, port)
Esempio n. 20
0
def main():
    cfg = Loader('mq')
    my_conf = cfg.load()
    config = dict(my_conf[1])

    ctx = DaemonContext()
    ctx.open()

    context = zmq.Context()
    print(("tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])))
    broker = MDPBroker(
        context, "tcp://{0}:{1}".format(config['ip'], config['req_rep_port']))
    IOLoop.instance().start()
    broker.shutdown()
Esempio n. 21
0
def test_config_files():
    # TODO : /etc/default

    ### /etc/domogik/domogik.cfg
    from domogik.common.configloader import Loader, CONFIG_FILE
    try:
        cfg = Loader('domogik')
        config = cfg.load()
        conf = dict(config[1])

        ok("Configuration file '{0}' can be read".format(CONFIG_FILE))
        return True
    except:
        error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
        solution("Make sure the Domogik installation is OK")
        return False
Esempio n. 22
0
def api_datatype():
    """
    @api {get} /datatype Retrieve all datatypes
    @apiName getDataTypes
    @apiGroup DataType

    @apiSuccess {json} result The json representation of the datatypes

    @apiSuccessExample Success-Response:
        HTTTP/1.1 200 OK
        {
            "DT_HVACVent": {
                "childs": [],
                "values": {
                    "0": "Auto",
                    "1": "Heat",
                    "2": "Cool",
                    "3": "Fan only",
                    "4": "Dry"
                }
            },
            "DT_DateTime": {
                "childs": [
                    "DT_Date",
                    "DT_Time"
                ],
                "format": "YYYY-MM-DDThh:mm:ss.s"
            },
            "DT_String": {
                "childs": [
                    "DT_Phone",
                    "DT_Hexa",
                    "DT_ColorRGBHexa"
                ],
                "maxLengh": null,
                "format": null
            },
            ...
        }
    """
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    json_file = "{0}/datatypes.json".format(urlHandler.resources_directory)
    data = json.load(open(json_file))

    return 200, data
Esempio n. 23
0
    def __init__(self, context, caller_id, category_filters):
        if ("domogik.common.configloader" in sys.modules):
            cfg = Loader('mq').load()
            self.cfg_mq = dict(cfg[1])
            sub_addr = "tcp://{0}:{1}".format(self.cfg_mq['ip'], self.cfg_mq['sub_port'])
        else:
            ipaddr = Parameter.objects.get(key='mq-ip')
            port = Parameter.objects.get(key='mq-sub_port')
            sub_addr = "tcp://{0}:{1}".format(ipaddr.value, port.value)
        self.s_recv = context.socket(zmq.SUB)
        self.caller_id = caller_id
        self.s_recv.connect(sub_addr)

        for category_filter in category_filters:
            self.s_recv.setsockopt(zmq.SUBSCRIBE, category_filter)
        if len(category_filters) == 0:
            self.s_recv.setsockopt(zmq.SUBSCRIBE, '')
Esempio n. 24
0
    def __init__(self, listitems, expr):
        '''
        @param listitems a dictionnary discribing items used in the condition
        @param expr : the condition
        '''
        XplPlugin.__init__(self, 'dbmgr')
        self._log = self.get_my_logger()
        self.listitems = listitems
        loader = Loader('trigger')
        config = loader.load()[1]
        self.__expr = expr

        #We should try/catch this bloc in case of undefined method
        #Anyway, if they're not defined, needed value will never be updated,
        #So we prefer raise an exception now
        for k in listitems:
            call = "self.build%slistener(listitems[k])" % k.lower()
            eval(call)
Esempio n. 25
0
def main():
    """
       Main loop for the forwarder
    """
    ctx = DaemonContext()
    ctx.open()

    cfg = Loader('mq').load()
    config = dict(cfg[1])
    log = logger.Logger('mq_forwarder').get_logger()
    log.info("Starting the forwarder")

    try:
        context = zmq.Context(1)

        # Socket facing emitters
        frontend = context.socket(zmq.SUB)
        # Forwarder subscribes to the emitter *pub* port
        sub_addr = "tcp://{0}:{1}".format(\
                   config['ip'], config['pub_port'])
        frontend.bind(sub_addr)
        log.info("Waiting for messages on {0}".format(sub_addr))
        # We want to get all messages from emitters
        frontend.setsockopt(zmq.SUBSCRIBE, "")

        # Socket facing receivers
        backend = context.socket(zmq.PUB)
        # Forwarder publishes to the receiver *sub* port
        pub_addr = "tcp://{0}:{1}".format(\
                   config['ip'], config['sub_port'])
        backend.bind(pub_addr)
        log.info("Sending messages to {0}".format(pub_addr))

        log.info("Forwarding messages...")
        zmq.device(zmq.FORWARDER, frontend, backend)
    except Exception as exp:
        log.error(exp)
        log.error("Bringing down ZMQ device")
        raise Exception("Error with forwarder device")
    finally:
        frontend.close()
        backend.close()
        context.term()
        log.info("Forwarder stopped")
Esempio n. 26
0
    def __init__(self, log=None, params=None):
        AbstractAction.__init__(self, log)
        self.set_description("Make the butler say something.")

        ### Butler configuration elements
        try:
            cfg = Loader('butler')
            config = cfg.load()
            conf = dict(config[1])

            self.butler_name = conf['name']
            self.butler_sex = conf['sex']
        except:
            self._log.error(u"ButlerACtion init : error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))

        ### MQ
        self._mq_name = "butler"
        self.zmq = zmq.Context()
        self.pub = MQPub(self.zmq, self._mq_name)
Esempio n. 27
0
def rest():

    cfg = Loader('rest')
    config = cfg.load()
    conf = dict(config[1])
    ### get REST ip and port
    port = conf['port']                          
    interfaces = conf['interfaces']                          
    intf = interfaces.split(',')
    print intf
    # get the first ip of the first interface declared
    ip = get_ip_for_interfaces(intf)[0]


    r = requests.get("http://{0}:{1}/map".format(ip, port))

    return render_template('rest.html',
        mactive="rest",
        urls = r.json(),
        )
Esempio n. 28
0
def load_plugin_library(name, element):
    '''
    Function to call in the bin part of each plugin to load the appropriate library :  from the sources in development mode and from packages folder in package mode
    To do a : from foo import bar
    Do      : bar = load_plugin_library("foo", "bar")
    '''

    # Read config files to get mode
    cfg = Loader('domogik')
    config = cfg.load()
    conf = dict(config[1])
    if conf.has_key('package_path'):
        print("Load library from packages")
        sys.path.append(conf["package_path"])
        lib_path = "packages.xpl.lib." + name
    else:
        print("Load library from sources")
        lib_path = "domogik.xpl.lib." + name
    imp = __import__(lib_path, globals(), locals(), element)
    ret = "imp." + element
    return eval(ret)
Esempio n. 29
0
 def __init__(self, context, service):
     """Initialize the MDPClient.
     """
     if ("domogik.common.configloader" in sys.modules):
         cfg = Loader('mq').load()
         confi = dict(cfg[1])
         self.endpoint = "tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])
     else:
         ip = Parameter.objects.get(key='mq-ip')
         port = Parameter.objects.get(key='mq-req_rep_port')
         self.endpoint = "tcp://{0}:{1}".format(ip.value, port.value)
     socket = ZmqSocket(context, zmq.REQ)
     ioloop = IOLoop.instance()
     self.service = service
     self.stream = ZMQStream(socket, ioloop)
     self.stream.on_recv(self._on_message)
     self.can_send = True
     self._proto_prefix = [ PROTO_VERSION, service]
     self._tmo = None
     self.timed_out = False
     socket.connect(self.endpoint)
     return
Esempio n. 30
0
 def get_data_files_directory(self):
     """
    Return the directory where a plugin developper can store data files.
    If the directory doesn't exist, try to create it.
    After that, try to create a file inside it.
    If something goes wrong, generate an explicit exception.
    """
     cfg = Loader('domogik')
     my_conf = cfg.load()
     config = dict(my_conf[1])
     if config.has_key('package_path'):
         path = "%s/domogik_packages/data/%s" % (config['package_path'],
                                                 self._name)
     else:
         path = "%s/share/domogik/data/%s" % (config['src_prefix'],
                                              self._name)
     if os.path.exists(path):
         if not os.access(path, os.W_OK & os.X_OK):
             raise OSError("Can't write in directory %s" % path)
     else:
         try:
             os.mkdir(path, 0770)
             self.log.info("Create directory %s." % path)
         except:
             raise OSError("Can't create directory %s." % path)
     try:
         tmp_prefix = "write_test"
         count = 0
         filename = os.path.join(path, tmp_prefix)
         while (os.path.exists(filename)):
             filename = "{}.{}".format(os.path.join(path, tmp_prefix),
                                       count)
             count = count + 1
         f = open(filename, "w")
         f.close()
         os.remove(filename)
     except:
         raise IOError("Can't create a file in directory %s." % path)
     return path