Changeset 17c79627d5d4c95f2428c5e361f8f06b64257983 for pxe/common.py
- Timestamp:
- 05/31/10 10:20:30 (2 years ago)
- Children:
- 9462a1ee61cb7c69c57720d39360f2d0765578a5
- Parents:
- 7c3da85ea3081cd215b42b3ed64f951232d808ec
- git-committer:
- Frederic Lepied <frederic.lepied@…> (05/31/10 10:20:30)
- Files:
-
- 1 modified
-
pxe/common.py (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pxe/common.py
r5a52bd0 r17c7962 5 5 # Author : Frederic Lepied 6 6 # Created On : Sun Feb 1 19:03:58 2009 7 # Purpose : common functions used by scripts and web logic.8 7 #--------------------------------------------------------------- 8 9 """Common functions used by scripts and web logic.""" 9 10 10 11 import sys … … 12 13 import re 13 14 import settings 14 from models import *15 from pxe.models import * 15 16 16 MAC_REGEXP = re.compile('^.*\s([0-9a-f:]+)\s.*', re.I)17 IP_REGEXP = re.compile('^([0-9.]+).*\s[0-9a-f:]+\s.*', re.I)17 _MAC_REGEXP = re.compile('^.*\s([0-9a-f:]+)\s.*', re.I) 18 _IP_REGEXP = re.compile('^([0-9.]+).*\s[0-9a-f:]+\s.*', re.I) 18 19 19 20 def ip_to_mac(ip): 21 """Lookup a MAC address from an IPV4 address on a live Linux system.""" 20 22 for line in open('/proc/net/arp').readlines(): 21 23 if line.find(ip) != -1: 22 res = MAC_REGEXP.search(line)24 res = _MAC_REGEXP.search(line) 23 25 if res: 24 26 return res.group(1) … … 26 28 27 29 def mac_to_ip(mac): 30 """Lookup an IPV4 address from a MAC address on a live Linux system.""" 28 31 mac = mac.upper() 29 32 for line in open('/proc/net/arp').readlines(): 30 33 if line.find(mac) != -1: 31 res = IP_REGEXP.search(line)34 res = _IP_REGEXP.search(line) 32 35 if res: 33 36 return res.group(1) … … 35 38 36 39 def get_mac(request): 40 """Returns a MAC address corresponding to the IPV4 address of the remote host of the request.""" 37 41 mac = ip_to_mac(request.META['REMOTE_ADDR']) 38 42 if not mac: … … 42 46 43 47 def error(str): 48 """Print an error and exit with a return code of 1.""" 44 49 sys.stderr.write(str + '\n') 45 50 sys.exit(1) 46 51 47 52 def simplify_mac(s): 48 '''Remove : or - between hexa numbers for a MAC address. Always return the address in lowercase'''53 """Remove : or - between hexa numbers for a MAC address. Always returns the address in lowercase""" 49 54 ss = s.replace('-', '') 50 55 sss = ss.replace(':', '') … … 55 60 56 61 def mac2filename(m): 62 """Returns a filename using the naming convention used by PXE: 01-<mac address separated by - >.""" 57 63 s = '01-' 58 64 for i in range(0, 10, 2): … … 62 68 63 69 def mac2path(mac): 70 """Returns the path of the MAC or IPV4 mask in the file system.""" 64 71 if len(mac) == 12: 65 72 return '%s/%s' % (settings.PXE_ROOT, mac2filename(mac)) … … 68 75 69 76 def create_symlink(src, dst): 77 """Creates a symlink removing the destination if it exists before.""" 70 78 if os.path.exists(dst): 71 79 os.unlink(dst) … … 73 81 74 82 def set_next_boot(system, name, abort=True): 83 """Sets next boot symlinks in the PXE and stores the value in the database.""" 75 84 try: 76 85 boot_name = BootName.objects.get(name=name) … … 96 105 97 106 def simplify_ip(ip): 98 '''Rertun a string containing the hexadecimal representation of an ipv4 address or a sub-part.'''107 """Returns a string containing the hexadecimal representation of an IPV4 address or a sub-part.""" 99 108 l = ip.split('.') 100 109 s = '' … … 103 112 return s 104 113 114 if __name__ == "__main__": 115 import unittest 116 117 class commonTest(unittest.TestCase): 118 119 def test1(self): 120 """simplify_ip test.""" 121 return self.assertEqual(simplify_ip('10.34'), '0A22') 122 123 def test2(self): 124 """mac2filename test.""" 125 return self.assertEqual(mac2filename('001b38710ab6'), '01-00-1b-38-71-0a-b6') 126 127 def test3(self): 128 """simplify_mac test.""" 129 return self.assertEqual(simplify_mac('00:1b:38:71:0a:b6'), '001b38710ab6') 130 131 def test4(self): 132 """simplify_mac test.""" 133 return self.assertRaises(ValueError, lambda: simplify_mac('00:1b:38:71:0a')) 134 135 unittest.main() 136 105 137 # common.py ends here
