Sophie

Sophie

distrib > Fedora > 13 > i386 > media > updates > by-pkgid > 8dee252679e9438b109053e1715c1da4 > files > 2

synce-hal-0.15-1.fc13.i686.rpm

#!/usr/bin/env python

import sys
import subprocess
import logging, logging.handlers
import ConfigParser

# defaults
config_file = '/etc/synce-hal.conf'
log_level = logging.WARNING


def remove_device(device_ip, iface, device_file):

    udi = "synce_bluetooth_device_"+device_ip.replace(".","_")

    cmd_list = ["hal-device", "--remove", udi]

    logger.debug("removing bluetooth device from hal:")
    logger.debug(cmd_list)

    try:
        proc = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output_text = proc.communicate()[0]
    except Exception,e:
        logger.error("failure running hal-device: %s" % e)
        return False

    rc = proc.returncode

    # success returns 13
    if rc != 13:
        logger.error("failed to remove bluetooth device in hal, return code %d, %s", (rc, output_text))
        return False

    return True


def ProcessConfig():

    global log_level

    logger = logging.getLogger("synce-bt-ipdown")

    config = ConfigParser.ConfigParser()
    try:
        config.read(config_file)
    except Exception,e:
        logger.warning("failed to parse config file %s: %s" % (config_file, e))
        return False

    if config.has_option('general', 'loglevel'):
        loglevel_txt = config.get('general', 'loglevel').lower()
        if loglevel_txt == 'critical':
            log_level = logging.CRITICAL
        elif loglevel_txt == 'error':
            log_level = logging.ERROR
        elif loglevel_txt == 'warning':
            log_level = logging.WARNING
        elif loglevel_txt == 'info':
            log_level = logging.INFO
        elif loglevel_txt == 'debug':
            log_level = logging.DEBUG
        else:
            logger.warning("found invalid loglevel in config file %s: %s" % (config_file, loglevel_txt))

        logger.setLevel(log_level)

    return True


if __name__ == '__main__':

    log_facility = logging.handlers.SysLogHandler.LOG_DAEMON

    logging.basicConfig(level=logging.WARNING,
                        format='%(asctime)s %(name)s %(levelname)s : %(message)s')
    logger = logging.getLogger("synce-bt-ipdown")
    sys_log = logging.handlers.SysLogHandler("/dev/log", log_facility)
    syslog_form = logging.Formatter('%(name)s[%(process)d] %(levelname)s : %(message)s')
    sys_log.setFormatter(syslog_form)
    logger.addHandler(sys_log)

    iface = sys.argv[1]
    devfile = sys.argv[2]
    speed = sys.argv[3]
    local_ip = sys.argv[4]
    device_ip = sys.argv[5]
    ipparam = sys.argv[6]

    if ipparam != "synce-bt":
        sys.exit(0)

    ProcessConfig()

    if remove_device(device_ip, iface, devfile) == False:
        sys.exit(1)

    sys.exit(0)