示例#1
0
文件: mudur.py 项目: Tayyib/uludag
def startNetwork(bus):
    import dbus
    # Remote mount required?
    need_remount = remoteMount(dry_run=True)
    obj = bus.get_object("tr.org.pardus.comar", "/", introspect=False)
    for script in obj.listModelApplications("Net.Link", dbus_interface="tr.org.pardus.comar"):
        db = pardus.iniutils.iniDB(os.path.join("/etc/network", script))
        for profile in db.listDB():
            if db.getDB(profile).get("state", "down") == "up":
                device = db.getDB(profile).get("device", None)
                if not device:
                    continue
                device = device.rsplit("_")[-1]
                try:
                    ui.info(_("Bringing up interface %s") % device)
                    obj = bus.get_object("tr.org.pardus.comar", "/package/%s" % script, introspect=False)
                    if need_remount:
                        obj.setState(profile, "up", dbus_interface="tr.org.pardus.comar.Net.Link")
                    else:
                        obj.setState(profile, "up", dbus_interface="tr.org.pardus.comar.Net.Link", ignore_reply=True)
                except dbus.DBusException:
                    ui.error(_("Unable to bring up interface %s") % device)
    if need_remount:
        if waitNet():
            remoteMount()
        else:
            ui.error(_("No network connection, skipping remote mount."))
示例#2
0
文件: mudur.py 项目: Tayyib/uludag
def startNetwork():
    """Sets up network connections if any."""
    import dbus
    import comar

    # Remote mount required?
    need_remount = mountRemoteFileSystems(dry_run=True)

    link = comar.Link()

    def ifUp(package, name, info):
        ifname = info["device_id"].split("_")[-1]
        ui.info((_("Bringing up %s") + ' (%s)') % (ui.colorize("light", ifname), ui.colorize("cyan", name)))
        if need_remount:
            try:
                link.Network.Link[package].setState(name, "up")
            except dbus.DBusException:
                ui.error((_("Unable to bring up %s") + ' (%s)') % (ifname, name))
                return False
        else:
            link.Network.Link[package].setState(name, "up", quiet=True)
        return True

    def ifDown(package, name):
        try:
            link.Network.Link[package].setState(name, "down", quiet=True)
        except dbus.DBusException:
            pass

    def getConnections(package):
        connections = {}
        try:
            for name in link.Network.Link[package].connections():
                connections[name] = link.Network.Link[package].connectionInfo(name)
        except dbus.DBusException:
            pass
        return connections

    try:
        packages = list(link.Network.Link)
    except dbus.DBusException:
        packages = []

    for package in packages:
        try:
            linkInfo = link.Network.Link[package].linkInfo()
        except dbus.DBusException:
            break
        if linkInfo["type"] == "net":
            for name, info in getConnections(package).iteritems():
                if info.get("state", "down").startswith("up"):
                    ifUp(package, name, info)
                else:
                    ifDown(package, name)
        elif linkInfo["type"] == "wifi":
            # Scan remote access points
            devices = {}
            try:
                for deviceId in link.Network.Link[package].deviceList():
                    devices[deviceId] = []
                    for point in link.Network.Link[package].scanRemote(deviceId):
                        devices[deviceId].append(unicode(point["remote"]))
            except dbus.DBusException:
                break
            # Try to connect last connected profile
            skip = False
            for name, info in getConnections(package).iteritems():
                if info.get("state", "down").startswith("up") and info.get("device_id", None) in devices and info["remote"] in devices[info["device_id"]]:
                    ifUp(package, name, info)
                    skip = True
                    break
            # There's no last connected profile, try to connect other profiles
            if not skip:
                # Reset connection states
                for name, info in getConnections(package).iteritems():
                    ifDown(package, name)
                # Try to connect other profiles
                for name, info in getConnections(package).iteritems():
                    if info.get("device_id", None) in devices and info["remote"] in devices[info["device_id"]]:
                        ifUp(package, name, info)
                        break

    if need_remount:
        from pardus.netutils import waitNet
        if waitNet():
            mountRemoteFileSystems()
        else:
            ui.error(_("No network connection, skipping remote mount."))