~stefanlsd/source-switcher/main

« back to all changes in this revision

Viewing changes to source-switcher.py

  • Committer: Stefan Lesicnik
  • Date: 2008-12-17 09:22:49 UTC
  • mfrom: (12.1.2 source-switcher-dict)
  • Revision ID: stefan@lsd.co.za-20081217092249-wzsrz30q0hha808g
merged from dict branch which uses dictionary's for lists

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import re
6
6
import netifaces
7
7
import glob
8
 
 
9
 
office="http://ecstacy.lsd.co.za:3142/"
10
 
home="http://192.168.1.2:3142/"
 
8
import string
 
9
 
 
10
from netaddr import IP, CIDR
 
11
 
 
12
networks = {}
 
13
networks["home"] = {"ip":"192.168.1.0/24","url":"http://192.168.1.2:3142/"}
 
14
networks["office"] = {"ip":"172.30.16.16/20","url":"http://ecstacy.lsd.co.za:3142/"}
11
15
 
12
16
#Things to keep in sync
13
17
sync=["/etc/apt/sources.list","/etc/apt/sources.list.d/*.list","/etc/pbuilderrc"]
 
18
#sync=["/etc/apt/sources.list"]
14
19
 
15
20
#Dont change these
16
21
location = False
19
24
for interface in netifaces.interfaces():
20
25
    iface = netifaces.ifaddresses(interface)
21
26
    if netifaces.AF_INET in iface:
22
 
        network = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
23
 
        if re.search('192\.168\.1\..', network):
24
 
            location = home
25
 
            break
26
 
        elif re.search('172\.30\.18\..', network):
27
 
            location = office
28
 
            break
29
 
 
 
27
        networkaddr = '%s/%s' % (netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'], netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask'])
 
28
        networkaddr = IP(networkaddr).cidr()
 
29
        print 'networkaddr: %s' % networkaddr        
 
30
        for name, values in networks.iteritems():
 
31
                        network = IP(values['ip']).cidr()
 
32
                        print 'network: %s' % network
 
33
                        if networkaddr in network:
 
34
                                location = networks[name]['url']
 
35
                                print 'Location \'%s\' found. Setting proxy to \'%s\'' % (name,location)
 
36
                        
30
37
#Function to hash out mirrors based on regex - may be useful for enabling/disabling src package downloads
31
38
#Not currently used
32
39
def hash():