Update update_network_info
[wifi-radar.git] / wifi-radar
blobef78a52456341cf4230c2f297cb8cabcff66e108
1 #!/usr/bin/python -OO
2 # -*- coding: utf-8 -*-
4 # A utility for managing WiFi profiles on GNU/Linux.
6 # Copyright (C) 2004-2005 Ahmad Baitalmal <ahmad@baitalmal.com>
7 # Copyright (C) 2005 Nicolas Brouard <nicolas.brouard@mandrake.org>
8 # Copyright (C) 2005-2009 Brian Elliott Finley <brian@thefinleys.com>
9 # Copyright (C) 2006 David Decotigny <com.d2@free.fr>
10 # Copyright (C) 2006 Simon Gerber <gesimu@gmail.com>
11 # Copyright (C) 2006-2007 Joey Hurst <jhurst@lucubrate.org>
12 # Copyright (C) 2006, 2009 Ante Karamatic <ivoks@ubuntu.com>
13 # Copyright (C) 2009-2010,2014 Sean Robinson <seankrobinson@gmail.com>
14 # Copyright (C) 2010 Prokhor Shuchalov <p@shuchalov.ru>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; version 2 of the License.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License in LICENSE.GPL for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 # http://wifi-radar.tuxfamily.org/
31 # See CREDITS file for more contributors.
32 # See HISTORY file for, well, changes.
34 # NOTE: Remove the '-OO' from '#!/usr/bin/python -OO' in the first line to
35 # turn on console debugging.
37 from __future__ import print_function
39 try:
40 # Py2
41 import Queue as queue
42 except ImportError:
43 # Py3
44 import queue
46 import gtk
47 import logging
48 import logging.handlers
49 import os
50 import sys
51 import threading
53 import wifiradar.config
54 import wifiradar.misc as misc
55 import wifiradar.gui.g2 as ui
56 import wifiradar.gui.g2.transients as transients
59 # Where the conf file should live could be different for your distro. Please change
60 # at install time with "make install sysconfdir=/etc/wifi-radar" or similar. -BEF-
62 CONF_FILE = "/etc/wifi-radar/wifi-radar.conf"
64 os.environ['LC_MESSAGES'] = 'C'
66 # Set up a logging framework.
67 logger = logging.getLogger("wifiradar")
69 generic_formatter = logging.Formatter(
70 fmt='%(asctime)s.%(msecs)d %(name)s: %(message)s',
71 datefmt='%Y-%m-%d %H:%M:%S')
73 consoleLogHandler = logging.StreamHandler()
74 consoleLogHandler.setFormatter(generic_formatter)
75 logger.addHandler(consoleLogHandler)
77 # Load our conf file and known profiles
78 # Defaults, these may get overridden by values found in the conf file.
79 config_defaults = { # The network interface you use.
80 # Specify "auto_detect" as the interface to make wifi-radar automatically detect your wireless card.
81 'interface': "auto_detect",
82 # How long should the scan for access points last?
83 #'scan_timeout': '5',
84 # You may set this to true for cards that require a "commit" command with iwconfig
85 'commit_required': 'False',
86 # You may set this to true for cards that require the interface to be brought up first
87 'ifup_required': 'False',
88 # set the location and verbosity of the log file
89 'logfile': '/var/log/wifi-radar.log',
90 'loglevel': '50',
91 # Set the location of several important programs
92 'iwlist_command': '/sbin/iwlist',
93 'iwconfig_command': '/sbin/iwconfig',
94 'ifconfig_command': '/sbin/ifconfig',
95 'route_command': '/sbin/route',
96 'auto_profile_order': '[]',
97 'version': misc.WIFI_RADAR_VERSION }
99 config_dhcp = { # DHCP client
100 'command': '/sbin/dhcpcd',
101 # How long to wait for an IP addr from DHCP server
102 'timeout': '30',
103 # Arguments to use with DHCP client on connect
104 'args': '-D -o -i dhcp_client -t %(timeout)s',
105 # Argument to use with DHCP client on disconnect
106 'kill_args': '-k',
107 # The file where DHCP client PID is written
108 'pidfile': '/etc/dhcpc/dhcpcd-%(interface)s.pid' }
110 config_wpa = { # WPA Supplicant
111 'command': '/usr/sbin/wpa_supplicant',
112 # Arguments to use with WPA Supplicant on connect
113 'args': '-B -i %(interface)s -c %(configuration)s -D %(driver)s -P %(pidfile)s',
114 # Arguments to use with WPA Supplicant on disconnect
115 'kill_command': '',
116 # Where the WPA Supplicant config file can be found
117 'configuration': '/etc/wpa_supplicant.conf',
118 # Driver to use with WPA Supplicant
119 'driver': 'wext',
120 # The file where WPA Supplicant PID is written
121 'pidfile': '/var/run/wpa_supplicant.pid' }
123 # initialize config, with defaults
124 config = wifiradar.config.ConfigFileManager(CONF_FILE, config_defaults)
125 config.set_section("DHCP", config_dhcp)
126 config.set_section("WPA", config_wpa)
128 if not os.path.isfile(CONF_FILE):
129 config.set_bool_opt('DEFAULT', 'new_file', True)
130 else:
131 if not os.access(CONF_FILE, os.R_OK):
132 print("Can't open {}".format(CONF_FILE))
133 print("Are you root?")
134 sys.exit()
135 try:
136 config.read()
137 except (NameError, SyntaxError) as e:
138 error_message = ('A configuration file from a pre-2.0 version ' +
139 'of WiFi Radar was found at {FILE}.\n\nWiFi Radar v2.0.x does ' +
140 'not read configuration files from previous versions. ')
141 if isinstance(e, NameError):
142 error_message += ('Because {FILE} may contain information ' +
143 'that you might wish to use when configuring WiFi Radar ' +
144 '{VERSION}, rename this file and run the program again.')
145 elif isinstance(e, SyntaxError):
146 error_message += ('The old configuration file is probably ' +
147 'empty and can be removed. Rename {FILE} if you want ' +
148 'to be very careful. After removing or renaming ' +
149 '{FILE}, run this program again.')
150 error_message = error_message.format(FILE=CONF_FILE,
151 VERSION=misc.WIFI_RADAR_VERSION)
152 error_dlg = transients.ErrorDialog(None, error_message)
153 del error_dlg
154 logger.critical(error_message)
155 sys.exit()
159 ####################################################################################################
160 # Make so we can be imported
161 if __name__ == "__main__":
162 if len(sys.argv) > 1 and (sys.argv[1] == '--version' or sys.argv[1] == '-v'):
163 print("WiFi Radar version {}".format(misc.WIFI_RADAR_VERSION))
164 elif len(sys.argv) > 1 and (sys.argv[1] == '--help' or sys.argv[1] == '-h'):
165 print("WiFi Radar version {}".format(misc.WIFI_RADAR_VERSION))
166 print("For help, check man pages for wifi-radar and wifi-radar.conf,")
167 print("or visit http://wifi-radar.tuxfamily.org/")
168 else:
169 import gtk, gobject
170 gtk.gdk.threads_init()
171 apQueue = queue.Queue(100)
172 commQueue = queue.Queue(2)
174 logger.setLevel(config.get_opt_as_int('DEFAULT', 'loglevel'))
175 try:
176 fileLogHandler = logging.handlers.RotatingFileHandler(config.get_opt('DEFAULT', 'logfile'), maxBytes=64*1024, backupCount=5)
177 except IOError as e:
178 error_dlg = transients.ErrorDialog(None, "Cannot open log file for writing: %s.\n\nWiFi Radar will work, but a log file will not be recorded." % (e.strerror))
179 del error_dlg
180 else:
181 fileLogHandler.setFormatter(generic_formatter)
182 logger.addHandler(fileLogHandler)
183 if __debug__:
184 logger.setLevel(logging.INFO)
186 exit_event = threading.Event()
187 exit_event.clear()
188 threading.Thread(None, misc.scanning_thread, None, (config, apQueue, commQueue, exit_event)).start()
189 main_radar_window = ui.RadarWindow(config, apQueue, commQueue, exit_event)
190 gobject.timeout_add(500, main_radar_window.update_window)
191 main_radar_window.main()