Skip to content

Commit e24d6bd

Browse files
committed
patchler işlenmiş hali.
0 parents  commit e24d6bd

5 files changed

Lines changed: 1030 additions & 0 deletions

File tree

comar/__init__.py

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (C) 2006-2009, TUBITAK/UEKAE
5+
#
6+
# This program is free software; you can redistribute it and/or modify it
7+
# under the terms of the GNU General Public License as published by the
8+
# Free Software Foundation; either version 2 of the License, or (at your
9+
# option) any later version. Please read the COPYING file.
10+
#
11+
12+
__version__ = '2.4.9'
13+
14+
import dbus
15+
import locale
16+
import os
17+
import subprocess
18+
19+
class Call:
20+
def __init__(self, link, group, class_=None, package=None, method=None):
21+
self.link = link
22+
self.group = group
23+
self.class_ = class_
24+
self.package = package
25+
self.method = method
26+
self.async = None
27+
self.quiet = False
28+
29+
if self.package:
30+
self.package = self.package.replace("-", "_")
31+
32+
def __getitem__(self, key):
33+
if not self.class_:
34+
raise KeyError, "Package should be selected after class"
35+
if not isinstance(key, basestring):
36+
raise KeyError
37+
return Call(self.link, self.group, self.class_, key)
38+
39+
def __getattr__(self, name):
40+
if self.class_:
41+
c = Call(self.link, self.group, self.class_, self.package, name)
42+
return c.call
43+
else:
44+
if name[0] < 'A' or name[0] > 'Z':
45+
raise AttributeError
46+
47+
return Call(self.link, self.group, name)
48+
49+
def __iter__(self):
50+
if self.class_:
51+
obj = self.link.bus.get_object(self.link.address, "/", introspect=False)
52+
packages = obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface)
53+
for package in packages:
54+
yield unicode(package)
55+
56+
def call(self, *args, **kwargs):
57+
self.async = kwargs.get("async", None)
58+
self.quiet = kwargs.get("quiet", False)
59+
self.timeout = kwargs.get("timeout", 120)
60+
if self.async and self.quiet:
61+
raise Exception, "async and quiet arguments can't be used together"
62+
if self.async or self.quiet:
63+
if self.package:
64+
obj = self.link.bus.get_object(self.link.address, "/package/%s" % self.package)
65+
met = getattr(obj, self.method)
66+
67+
def handleResult(*result):
68+
self.async(self.package, None, result)
69+
def handleError(exception):
70+
if "policy.auth" in exception._dbus_error_name or "Comar.PolicyKit" in exception._dbus_error_name:
71+
action = exception.get_dbus_message()
72+
if self.queryPolicyKit(action):
73+
return self.call(*args, **kwargs)
74+
self.async(self.package, exception, None)
75+
76+
if self.quiet:
77+
met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), *args)
78+
else:
79+
met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), reply_handler=handleResult, error_handler=handleError, timeout=self.timeout, *args)
80+
else:
81+
def handlePackages(packages):
82+
if self.quiet:
83+
for package in packages:
84+
obj = self.link.bus.get_object(self.link.address, "/package/%s" % package)
85+
met = getattr(obj, self.method)
86+
met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), *args)
87+
else:
88+
def handleResult(package):
89+
def handler(*result):
90+
return self.async(package, None, result)
91+
return handler
92+
def handleError(package):
93+
def handler(exception):
94+
return self.async(package, exception, None)
95+
return handler
96+
97+
for package in packages:
98+
obj = self.link.bus.get_object(self.link.address, "/package/%s" % package)
99+
met = getattr(obj, self.method)
100+
101+
met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), reply_handler=handleResult(package), error_handler=handleError(package), timeout=self.timeout, *args)
102+
103+
def handlePackError(exception):
104+
if self.quiet:
105+
pass
106+
else:
107+
raise exception
108+
109+
if self.quiet:
110+
obj = self.link.bus.get_object(self.link.address, "/", introspect=False)
111+
packages = obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface)
112+
handlePackages(packages)
113+
else:
114+
obj = self.link.bus.get_object(self.link.address, "/", introspect=False)
115+
obj.listModelApplications("%s.%s" % (self.group, self.class_), dbus_interface=self.link.interface, reply_handler=handlePackages, error_handler=handlePackError, timeout=self.timeout)
116+
else:
117+
if self.package:
118+
obj = self.link.bus.get_object(self.link.address, "/package/%s" % self.package)
119+
met = getattr(obj, self.method)
120+
try:
121+
return met(dbus_interface="%s.%s.%s" % (self.link.interface, self.group, self.class_), timeout=self.timeout, *args)
122+
except dbus.DBusException, exception:
123+
if "policy.auth" in exception._dbus_error_name or "Comar.PolicyKit" in exception._dbus_error_name:
124+
action = exception.get_dbus_message()
125+
if self.queryPolicyKit(action):
126+
return self.call(*args, **kwargs)
127+
raise dbus.DBusException, exception
128+
else:
129+
raise AttributeError, "Package name required for non-async calls."
130+
131+
def queryPolicyKit(self, action):
132+
return False
133+
134+
135+
class Link:
136+
def __init__(self, version="2", socket=None, alternate=False):
137+
self.version = str(version)
138+
self.address = "tr.org.pardus.comar"
139+
self.interface = "tr.org.pardus.comar"
140+
self.socket = socket
141+
142+
# Auto-enable agent, if X session present.
143+
self.use_agent = ("DISPLAY" in os.environ)
144+
145+
if not socket:
146+
self.bus = dbus.SystemBus()
147+
else:
148+
self.bus = dbus.bus.BusConnection(address_or_type="unix:path=%s" % socket)
149+
150+
if alternate:
151+
self.address += "2"
152+
153+
def useAgent(self, agent=True):
154+
self.use_agent = agent
155+
156+
def setLocale(self):
157+
try:
158+
code, encoding = locale.getdefaultlocale()
159+
if code:
160+
if "_" in code:
161+
code = code.split("_")[0]
162+
obj = self.bus.get_object(self.address, '/', introspect=False)
163+
obj.setLocale(code, dbus_interface=self.interface)
164+
except dbus.DBusException, exception:
165+
pass
166+
167+
def cancel(self, method="*"):
168+
try:
169+
obj = self.bus.get_object(self.address, '/', introspect=False)
170+
return obj.cancel(method, dbus_interface=self.interface)
171+
except dbus.DBusException, exception:
172+
return 0
173+
174+
def listRunning(self, all=True):
175+
methods = []
176+
try:
177+
obj = self.bus.get_object(self.address, '/', introspect=False)
178+
methods = obj.listRunning(all, dbus_interface=self.interface)
179+
except dbus.DBusException, exception:
180+
return methods
181+
for index, method in enumerate(methods):
182+
if method.startswith("%s." % self.interface):
183+
methods[index] = method[len("%s." % self.interface):]
184+
return methods
185+
186+
def listenSignals(self, model, handler):
187+
def sigHandler(*args, **kwargs):
188+
if "/package/" not in kwargs["path"]:
189+
return
190+
package = kwargs["path"].split("/package/")[1]
191+
signal = kwargs["signal"]
192+
handler(package, signal, args)
193+
self.bus.add_signal_receiver(sigHandler, dbus_interface="%s.%s" % (self.interface, model), member_keyword="signal", path_keyword="path")
194+
195+
def register(self, app, model, script, timeout=120):
196+
obj = self.bus.get_object(self.address, '/', introspect=False)
197+
obj.register(app, model, script, dbus_interface=self.interface, timeout=timeout)
198+
199+
def remove(self, app, timeout=120):
200+
obj = self.bus.get_object(self.address, '/', introspect=False)
201+
obj.remove(app, dbus_interface=self.interface, timeout=timeout)
202+
203+
def __getattr__(self, name):
204+
if name[0] < 'A' or name[0] > 'Z':
205+
raise AttributeError
206+
return Call(self, name)

comar/network.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2006-2009, TUBITAK/UEKAE
4+
#
5+
# This program is free software; you can redistribute it and/or modify it under
6+
# the terms of the GNU General Public License as published by the Free
7+
# Software Foundation; either version 2 of the License, or (at your option)
8+
# any later version.
9+
#
10+
# Please read the COPYING file.
11+
#
12+
13+
"""network module provides network management utils."""
14+
15+
import os
16+
import subprocess
17+
18+
from pisilinux import iniutils
19+
from pisilinux import netutils
20+
21+
from comar.service import startService, stopService, loadConfig
22+
23+
NET_PATH = "/etc/network"
24+
NET_STACK = "baselayout"
25+
26+
MSG_PROFILE_NAME = {
27+
"en": "You have to enter a profile name to create a connection",
28+
"tr": "Bağlantı yaratmak için profil ismi girmelisiniz",
29+
}
30+
31+
INI = iniutils.iniParser(os.path.join(NET_PATH, script()), quiet=True)
32+
33+
def listProfiles():
34+
try:
35+
return INI.listSections()
36+
except iniutils.iniParserError:
37+
return []
38+
39+
class Profile:
40+
def __init__(self, name):
41+
if not len(name):
42+
fail(_(MSG_PROFILE_NAME))
43+
self.name = name
44+
try:
45+
self.info = INI.getSection(name)
46+
except iniutils.iniParserError:
47+
self.info = {}
48+
49+
def delete(self):
50+
INI.removeSection(self.name)
51+
52+
def save(self, no_notify=False):
53+
is_new = self.name not in listProfiles()
54+
INI.setSection(self.name, self.info)
55+
if no_notify:
56+
return
57+
if is_new:
58+
notify("Network.Link", "connectionChanged", ("added", self.name))
59+
else:
60+
notify("Network.Link", "connectionChanged", ("changed", self.name))
61+
62+
class AccessPoint:
63+
def __init__(self, id=None):
64+
self.ssid = ""
65+
self.mode = ""
66+
self.mac = ""
67+
self.encryption = "none"
68+
self.qual = ""
69+
self.qual_max = "100"
70+
self.protocol = ""
71+
self.channel = ""
72+
if id:
73+
if " (" in id and id.endswith(")"):
74+
self.ssid, rest = id.split(" (", 1)
75+
self.mode, self.mac = rest.split(" ", 1)
76+
self.mac = self.mac[:-1]
77+
else:
78+
self.ssid = id
79+
80+
def id(self):
81+
d = {
82+
"remote": self.ssid,
83+
"mode": self.mode,
84+
"mac": self.mac,
85+
"encryption": self.encryption,
86+
"quality": self.qual,
87+
"quality_max": self.qual_max,
88+
"protocol": self.protocol,
89+
"channel": self.channel,
90+
}
91+
return d
92+
93+
def stopSameDevice(name):
94+
from csl import setState
95+
profile = Profile(name)
96+
device = profile.info["device"]
97+
for pn in listProfiles():
98+
if pn == name:
99+
continue
100+
pro = Profile(pn)
101+
if pro.info["device"] == device:
102+
setState(pn, "down")
103+
104+
def registerNameServers(profile, iface):
105+
name_mode = profile.info.get("name_mode", "default")
106+
name_servers = []
107+
name_domain = ""
108+
if name_mode == "auto":
109+
for server in iface.autoNameServers():
110+
name_servers.append(server)
111+
name_domain = iface.autoNameSearch()
112+
elif name_mode == "custom":
113+
for server in profile.info.get("name_server", ",").split():
114+
if server.strip():
115+
name_servers.append(server.strip())
116+
elif name_mode == "default":
117+
name_servers = call(NET_STACK, "Network.Stack", "getNameServers")
118+
call(NET_STACK, "Network.Stack", "registerNameServers", (iface.name, name_servers, name_domain))
119+
120+
def unregisterNameServers(ifname):
121+
call(NET_STACK, "Network.Stack", "unregisterNameServers", (ifname, [], ""))
122+
123+
def callScript(name, state):
124+
path = os.path.join("/etc/network/netlink.d", "%s.%s" % (name, state))
125+
if os.path.exists(path):
126+
try:
127+
subprocess.call([path])
128+
except:
129+
pass
130+
131+
def plugService(device, state, wireless=False):
132+
# Do nothing if ifplugd is missing
133+
if not os.path.exists("/usr/sbin/ifplugd"):
134+
return
135+
if state == "up":
136+
# Do nothing if device is missing
137+
if not netutils.IF(device):
138+
return
139+
# Load service configuration
140+
config = loadConfig("/etc/conf.d/ifplugd")
141+
# Get arguments
142+
if wireless:
143+
args = config.get("IFPLUGD_WLAN_ARGS", "")
144+
else:
145+
args = config.get("IFPLUGD_ARGS", "")
146+
# Start service
147+
startService(command="/usr/sbin/ifplugd",
148+
args="%s -i %s" % (args, device),
149+
pidfile="/run/ifplugd.%s.pid" % device,
150+
detach=True,
151+
donotify=False)
152+
else:
153+
# Stop service
154+
stopService(pidfile="/run/ifplugd.%s.pid" % device, donotify=False)
155+
156+
def plugCheck(device):
157+
# Return true if ifplugd is missing
158+
if not os.path.exists("/usr/sbin/ifplugstatus"):
159+
return True
160+
return subprocess.call(["/usr/sbin/ifplugstatus", "-q", device]) == 2

0 commit comments

Comments
 (0)