~nilsschlupp/auto-ndiswrapper/rewrite

« back to all changes in this revision

Viewing changes to main/auto-ndis.py

  • Committer: Gabriel Joel
  • Date: 2008-06-14 18:45:36 UTC
  • Revision ID: gabrieljoel@gmail.com-20080614184536-gm6idwezas96dqfa
Improved the installation method once again, replaced all uses of commands.getoutput with a new function called getoutput that uses subprocess. General improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
__version__ = '0.0.1'
26
26
 
27
 
import os, platform, commands, sys, re, subprocess
 
27
import os, platform, sys, re, subprocess
28
28
from database import data
29
29
from ConfigParser import ConfigParser
30
30
from optparse import OptionParser
54
54
        print 'Please look for "ndiswrapper" in your distribution\'s package manager and install it or go to www.ndiswrapper.com and download the latest sources' 
55
55
        sys.exit(1)
56
56
 
57
 
if (opts.debug): ##Print the debug options enabled
 
57
##Print the debug options enabled
 
58
if (opts.debug):
58
59
        print '[DEBUG] Options:'
59
60
        print opts
60
61
        print '[DEBUG] Arguments:'
61
62
        print args
62
63
        print
63
64
 
64
 
def get_and_print_card_info():
65
 
        ##The function's name is self explanatory, but aniway this is thanks to mintwifi.py
66
 
        os.system('lspci | grep \"Network controller\" > /tmp/detected_wireless_devices')
67
 
        devices_file = open('/tmp/detected_wireless_devices')
68
 
        for device_item in devices_file.readlines():
69
 
                devicearray = device_item.split()
70
 
                device = ' '.join(devicearray[3:])
71
 
                pci_id_line = commands.getoutput('lspci -n | grep ' + devicearray[0]) 
72
 
                pci_id_array = pci_id_line.split()
73
 
                pci_id = ' '.join(pci_id_array[2:])
 
65
def getoutput(*cmd):
 
66
        """
 
67
        Like commands.getoutput, but uses subprocess.
 
68
        """
 
69
 
 
70
        myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 
71
        value  =  myproc.stdout.read()
 
72
        return value
 
73
 
 
74
def get_output_retcode(*cmd):
 
75
        
 
76
        myproc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 
77
        value  =  myproc.stdout.read()
 
78
        retcode = myproc.wait()
 
79
        return value, retcode
 
80
 
 
81
def get_print_card_info():
 
82
        """
 
83
        Prints the pci id and other information about the pci wireless cards.
 
84
        """
 
85
        output = getoutput('lspci')
 
86
        
 
87
        
 
88
 
74
89
        print '  -- ' + device 
75
90
        print '      ==> PCI ID = ' + pci_id
76
91
        
77
92
 
78
93
def check_for_internet(url, card_id):
79
 
        ##Checks if the user has an internet connection available
 
94
        """
 
95
        Pings google to see if the user has an internet conecction available.
 
96
        """
 
97
        
80
98
        print '[*] Looking if an Internet connection is available, this may take a few seconds.'
81
 
        test = commands.getoutput('ping -c 1 google.com')
 
99
        test = getoutput('ping', '-c', '1', 'google.com')
82
100
        if 'unknown host' in test:
83
101
                print '[ERROR] An Internet connection was not found'
84
102
                choice = raw_input('(C)ontinue, (R)etry, (O)ffline mode or (E)xit: ')
103
121
                print '[*] Internet connection found, continuing installation'
104
122
                return 0
105
123
 
106
 
def searchcard(card_id, database):
107
 
        ##Looks if card is in the list of supported cards
108
 
        card_id = -1    
109
 
        for item in card_id:
 
124
def searchcard(devices_list, database):
 
125
        """
 
126
        Looks if one of the ids in the devices list given as argument matches an entry in the database given as argument. 
 
127
        """
 
128
                
 
129
        for item in devices_list:
110
130
                if item in database:
111
131
                        return item
112
 
        return card_id
113
 
 
114
 
def any(iterable):
115
 
        for element in iterable:
116
 
                if element:
117
 
                        return True
118
 
        return False
 
132
        return -1
119
133
         
120
134
def remove_old_drivers():
121
 
        ##Removes existing wireless drivers
 
135
        """
 
136
        Removes existing wireless drivers.
 
137
        """
 
138
 
122
139
        print '[*] Removing Old Drivers:'
123
140
        baddrivers = yourcard_dic['blacklist']
124
 
        if baddrivers in commands.getoutput('lsmod| awk "{print $1}"'):
 
141
        if baddrivers in getoutput('lsmod| awk "{print $1}"'):
125
142
                print '   removing %s' % (baddrivers, )
126
 
                os.system('rmmod %s' % (baddrivers, ))
 
143
                subprocess.call(['rmmod', baddrivers])
127
144
        else:
128
145
                print '   drivers not loaded, nothing to remove'
129
146
 
130
147
def blacklist_drivers():
131
 
        ##Blacklists existing wireless drivers
 
148
        """
 
149
        Blacklists existing wireless drivers.
 
150
        """
 
151
 
132
152
        print '[*] Blacklisting:'
133
153
        baddrivers = yourcard_dic['blacklist']
134
154
        if any(baddrivers in line.split('#')[0] for line in open('/etc/modprobe.d/blacklist')):
140
160
                blacklist.close()
141
161
 
142
162
def print_driver_license():
143
 
        ##Checks if the user agrees with the driver's EULA
 
163
        """
 
164
        Checks if the user agrees with the driver's EULA.
 
165
        """
 
166
        
144
167
        print 'Do you agree with this proprietary driver\'s End User License Agreement? You can find it in the website of your card\'s manufacturer.'
145
168
        choice = raw_input('(Y)es or (N)o, if you don\'t agree the program will quit: ')
146
169
        
152
175
                return driver_license()
153
176
 
154
177
def create_auto_ndis_folder():
155
 
        ##Creates a folder to place the drivers
 
178
        """
 
179
        Creates a folder in which place the downloaded drivers.
 
180
        """
 
181
 
156
182
        try:
157
183
                os.makedirs('%s/%s' % (opts.tmp_dir, card_id, ))
158
184
                print 'Tmpdir created'
168
194
                        sys.exit(1)
169
195
 
170
196
def find_decompression(driver_url):
171
 
        ##Determines the right decompression program to be used to decompress the downloaded driver
 
197
        """
 
198
        Determines the right decompression program to be used with the downloaded driver.
 
199
        """
 
200
 
172
201
        decompression = 'manual'
173
202
        
174
203
        if '.exe' in driver_url or '.zip' in driver_url:
191
220
        
192
221
        if decompression != 'manual':   
193
222
                #Checks if the system has the necessary decompression program
194
 
                test = commands.getoutput(decompression)
 
223
                test = getoutput(decompression)
195
224
                if 'command not found' in test:
196
225
                        print '[ERROR] The program to extract %s files is not installed in your system' % (decompression, )
197
226
                        print 'Install a program on your system to extract %s files and then rerun the script' % (decompression, )
199
228
        return decompression
200
229
        
201
230
def fetch(url, card_id):
202
 
        ##Determine decompression method
 
231
        """
 
232
        Downloads the apropiate driver.
 
233
        """
 
234
 
203
235
        print '[*] Downloading driver now from "%s"' % (url, )
204
236
        if (opts.quiet):
205
237
                retcode = subprocess.call(['wget', url, '-qO', opts.tmp_dir/card_id/driver])
206
238
                if retcode != 0:
207
 
                        print '[ERROR]   Download unsuccessfull, please check your Internet conecction'
 
239
                        print '[ERROR] Download unsuccessfull, please check your Internet conecction'
208
240
                        sys.exit(1)
209
241
                else:
210
242
                        retcode = subprocess.call(['wget', url, '-O', opts.tmp_dir/card_id/driver])
211
243
                        if retcode != 0:
212
 
                                print '[ERROR]   Download unsuccessfull, please check your Internet conecction'
 
244
                                print '[ERROR] Download unsuccessfull, please check your Internet conecction'
213
245
                                sys.exit(1)
214
246
                print '   Download successfull'
215
247
                
216
248
def extract(decompression):
 
249
        """
 
250
        Extracts the dowloaded driver.
 
251
        """
 
252
 
217
253
        print '[*] Extracting driver'
218
254
        
219
255
        if (opts.debug):
242
278
                
243
279
                else:
244
280
                        return extract(decompression)
245
 
        else:
246
 
                manualmode(url, card_id)
247
281
 
248
282
def manualmode(url, card_id):
249
 
        ##Comes into play if the user doesn't have an internet connection or if the driver need to be fetched manually
 
283
        """
 
284
        Comes into play if the user doesn't have an internet connection or if the driver need to be fetched manually.
 
285
        """
250
286
        yourcard_dic = data[card_id]
251
287
        driver = yourcard_dic['driver']
252
288
        print 'The driver needs to be fetched manually from: %s' % (url, )
253
289
        print 'Please place the "%s" file, along with the .sys files into:' % (driver, )
254
 
        print '"%s/%s/"' % (opts.tmp_dir, card_id, )
 
290
        print '"%s/%s"' % (opts.tmp_dir, card_id, )
255
291
        while not any(driver in line for line in os.listdir('%s/%s/' % (opts.tmp_dir, card_id, ))):
256
 
        #while ((commands.getoutput("ls %s/%s/|grep %s" % (opts.tmp_dir, card_id, driver, ))) == ""):
 
292
                
257
293
                try:
258
294
                        dummy = raw_input('When you have succesfully dowloaded the driver and extracted it press <Enter>: ')
259
295
                        
260
296
                except: 
261
 
                        ##Executed when CTRL + C is pressed
 
297
                        ##Executed when CTRL + C is pressed.
262
298
                        print '\n\nCanceled!\n'
263
299
                        sys.exit(1)
264
300
        print '  Driver file found! Continuing installation.'
265
301
 
266
302
def installdriver(card_id):
267
 
        ##Installs the driver with NDISwrapper
 
303
        """
 
304
        Installs the driver with NDISwrapper.
 
305
        """
 
306
 
268
307
        driver = yourcard_dic['driver']
269
 
        Inf = commands.getoutput('find %s/%s/ -name %s' % (opts.tmp_dir, card_id, driver, ))
 
308
        inf_file = getoutput('find', opts.tmp_dir/card_id, '-name', driver)
270
309
        if ( opts.debug ):
271
 
                print '[DEBUG] Driver found at %s' % (Inf, )
272
 
        os.system('%s -i %s' % (opts.ndiswrapper_bin, Inf, ))
273
 
        os.system('%s -l' % (opts.ndiswrapper_bin, ))
274
 
        os.system('%s -ma' % (opts.ndiswrapper_bin, ))
275
 
        os.system('modprobe ndiswrapper')               
276
 
        os.system('echo ndiswrapper >> /etc/modules')
 
310
                print '[DEBUG] Driver found at %s' % (inf, )
 
311
 
 
312
        #Attempt to install driver.
 
313
        output, retcode = get_output_retcode('ndiswrapper', '-i', inf_file)
 
314
 
 
315
        #Attempt to detect errors.
 
316
        if "already" in output:
 
317
                print 'Driver is already installed!'
 
318
                sys.exit(1)
277
319
        
278
 
        print 'Installation finished'
279
 
        sys.exit(0)
 
320
        elif retcode != 0:
 
321
                print '[ERROR] Driver installation failed'
 
322
                sys.exit(1)
 
323
                        
 
324
        else:
 
325
                ##Assume driver installed successfully. Set up and reload module.
 
326
                subprocess.call(['ndiswrapper',  '-ma'])
 
327
                subprocess.call(['modprobe', '-r', 'ndiswrapper'])
 
328
                subprocess.call(['modprobe', 'ndiswrapper'])
 
329
                print 'Installation finished'
 
330
                sys.exit(1)
280
331
 
281
332
def create_log_file():
 
333
        """
 
334
        Creates a simple log file with some useful info.
 
335
        """
 
336
 
282
337
        log = open('auto-ndis-log.txt', 'w')
283
338
 
284
339
        os_info = os.uname()
285
340
        distribution = platform.dist()
286
 
        ndis_version = commands.getoutput('ndiswrapper -v')
 
341
        ndis_version = getoutput('ndiswrapper', '-v')
287
342
 
288
343
        log.write('OS and date = ')
289
344
        for item in os_info:
305
360
 
306
361
def get_card_id():
307
362
        ##Gets the id of all the pci devices
308
 
        outtext = commands.getoutput('lspci -n')
 
363
        outtext = getoutput('lspci', '-n')
 
364
 
309
365
        ##Stores the pci ids with the revision number in a list
310
366
        pci = re.findall('.{4}:.{4}\ \(rev .{2}\)',outtext)
311
367
 
312
368
        ##Gets the id of all the usb devices
313
 
        outtext2 = commands.getoutput('lsusb')
 
369
        outtext2 = getoutput('lsusb')
314
370
        ##Stores the usb ids in a list
315
371
        usb = re.findall('.{4}:.{4}',outtext2)
316
372
 
317
 
        pcicard = -1
318
 
        usbcard = -1
319
373
        pcicard = searchcard(pci, data)
320
374
        usbcard = searchcard(usb, data)
321
375
 
328
382
                return usbcard
329
383
 
330
384
        elif pcicard != -1 and usbcard != -1:
331
 
                choice = input('Setup (w)ificard or setup your (u)sbcard?: ') 
 
385
                choice = raw_input('Setup (w)ificard or setup your (u)sbcard?: ') 
332
386
                
333
 
                while choice != 'w' and choice != 'W' and choice != 'u' and choice != 'U':
 
387
                while choice != 'W' and choice != 'w' and choice != 'U' and choice != 'u':
334
388
                        print 'Please try again'
335
 
                        choice = input('Setup (w)ificard or setup your (u)sbcard?: ') 
336
 
                if choice == 'w' or chice == 'W':
 
389
                        choice = raw_input('Setup (w)ificard or setup your (u)sbcard?: ') 
 
390
                if choice == 'W' or choice == 'w':
337
391
                        return pcicard
338
 
                else:
 
392
                elif choice == 'U' or choice == 'u':
339
393
                        return usbcard
 
394
                else:
 
395
                        return get_card_id()
340
396
                
341
397
        elif pcicard == -1 and usbcard == -1:
342
398
                print 'Sorry, card not yet supported by Auto-NDISwrapper'
343
399
                print 'Save this output as it will help other people give you support'
344
 
                get_and_print_card_info()
 
400
                get_print_card_info()
345
401
                sys.exit(1)
346
402
 
347
403
card_id = get_card_id()
365
421
        print
366
422
 
367
423
print '[*] Beginning Setup Procedure:\n'
368
 
 
369
424
print_driver_license()
370
425
create_auto_ndis_folder()
371
426
create_log_file()
372
 
check_for_internet(url, card_id)
373
427
 
374
428
if decompression != 'manual':
 
429
        check_for_internet(url, card_id)
375
430
        fetch(url, card_id)
376
431
        extract(decompression)
377
432
        installdriver(card_id)