3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
10 import os, sys, shutil
17 from Exceptions import *
22 from python docs for os.makedirs:
23 Throws an error exception if the leaf directory
24 already exists or cannot be created.
26 That is real useful. Instead, we'll create the directory, then use a
27 separate function to test for its existance.
29 Return 1 if the directory exists and/or has been created, a BootManagerException
30 otherwise. Does not test the writability of said directory.
39 raise BootManagerException, "Unable to create directory tree: %s" % path
45 def removedir( path ):
47 remove a directory tree, return 1 if successful, a BootManagerException
58 raise BootManagerException, "Unable to remove directory tree: %s" % path
64 def sysexec( cmd, log= None ):
66 execute a system command, output the results to the logger
69 return 1 if command completed (return code of non-zero),
70 0 if failed. A BootManagerException is raised if the command
71 was unable to execute or was interrupted by the user with Ctrl+C
73 prog= popen2.Popen4( cmd, 0 )
75 raise BootManagerException, \
76 "Unable to create instance of popen2.Popen3 " \
77 "for command: %s" % cmd
81 for line in prog.fromchild:
83 except KeyboardInterrupt:
84 raise BootManagerException, "Interrupted by user"
86 returncode= prog.wait()
88 raise BootManagerException, "Running %s failed (rc=%d)" % (cmd,returncode)
94 def sysexec_noerr( cmd, log= None ):
96 same as sysexec, but capture boot manager exceptions
100 rc= sysexec( cmd, log )
101 except BootManagerException, e:
110 change to a directory, return 1 if successful, a BootManagerException if failure
115 raise BootManagerException, "Unable to change to directory: %s" % dir
121 def removefile( filepath ):
123 removes a file, return 1 if successful, 0 if failure
126 os.remove( filepath )
128 raise BootManagerException, "Unable to remove file: %s" % filepath
134 # from: http://forums.devshed.com/archive/t-51149/
135 # Ethernet-card-address-Through-Python-or-C
138 return "%02x" % (ord(n))
140 def get_mac_from_interface(ifname):
142 given a device name, like eth0, return its mac_address.
143 return None if the device doesn't exist.
146 SIOCGIFHWADDR = 0x8927 # magic number
148 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
149 ifname = string.strip(ifname)
150 ifr = ifname + '\0'*(32-len(ifname))
153 r= fcntl.ioctl(s.fileno(),SIOCGIFHWADDR,ifr)
154 addr = map(hexy,r[18:24])
155 ret = (':'.join(map(str, addr)))