Changeset 17c79627d5d4c95f2428c5e361f8f06b64257983

Show
Ignore:
Timestamp:
05/31/10 10:20:30 (2 years ago)
Author:
Frederic Lepied <frederic.lepied@…>
Children:
9462a1ee61cb7c69c57720d39360f2d0765578a5
Parents:
7c3da85ea3081cd215b42b3ed64f951232d808ec
git-committer:
Frederic Lepied <frederic.lepied@…> (05/31/10 10:20:30)
Message:

added docstrings and a few unit tests

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pxe/common.py

    r5a52bd0 r17c7962  
    55# Author          : Frederic Lepied 
    66# Created On      : Sun Feb  1 19:03:58 2009 
    7 # Purpose         : common functions used by scripts and web logic. 
    87#--------------------------------------------------------------- 
     8 
     9"""Common functions used by scripts and web logic.""" 
    910 
    1011import sys 
     
    1213import re 
    1314import settings 
    14 from models import * 
     15from pxe.models import * 
    1516 
    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) 
    1819 
    1920def ip_to_mac(ip): 
     21    """Lookup a MAC address from an IPV4 address on a live Linux system.""" 
    2022    for line in open('/proc/net/arp').readlines(): 
    2123        if line.find(ip) != -1: 
    22             res = MAC_REGEXP.search(line) 
     24            res = _MAC_REGEXP.search(line) 
    2325            if res: 
    2426                return res.group(1) 
     
    2628 
    2729def mac_to_ip(mac): 
     30    """Lookup an IPV4 address from a MAC address on a live Linux system.""" 
    2831    mac = mac.upper() 
    2932    for line in open('/proc/net/arp').readlines(): 
    3033        if line.find(mac) != -1: 
    31             res = IP_REGEXP.search(line) 
     34            res = _IP_REGEXP.search(line) 
    3235            if res: 
    3336                return res.group(1) 
     
    3538 
    3639def get_mac(request): 
     40    """Returns a MAC address corresponding to the IPV4 address of the remote host of the request.""" 
    3741    mac = ip_to_mac(request.META['REMOTE_ADDR']) 
    3842    if not mac: 
     
    4246     
    4347def error(str): 
     48    """Print an error and exit with a return code of 1.""" 
    4449    sys.stderr.write(str + '\n') 
    4550    sys.exit(1) 
    4651 
    4752def 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""" 
    4954    ss = s.replace('-', '') 
    5055    sss = ss.replace(':', '') 
     
    5560 
    5661def mac2filename(m): 
     62    """Returns a filename using the naming convention used by PXE: 01-<mac address separated by - >.""" 
    5763    s = '01-' 
    5864    for i in range(0, 10, 2): 
     
    6268 
    6369def mac2path(mac): 
     70    """Returns the path of the MAC or IPV4 mask in the file system.""" 
    6471    if len(mac) == 12: 
    6572        return '%s/%s' % (settings.PXE_ROOT, mac2filename(mac)) 
     
    6875     
    6976def create_symlink(src, dst): 
     77    """Creates a symlink removing the destination if it exists before.""" 
    7078    if os.path.exists(dst): 
    7179        os.unlink(dst) 
     
    7381     
    7482def set_next_boot(system, name, abort=True): 
     83    """Sets next boot symlinks in the PXE and stores the value in the database.""" 
    7584    try: 
    7685        boot_name = BootName.objects.get(name=name) 
     
    96105 
    97106def 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.""" 
    99108    l = ip.split('.') 
    100109    s = '' 
     
    103112    return s 
    104113 
     114if __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 
    105137# common.py ends here