check in all bootmanager sources
[bootmanager.git] / source / utils.py
1 # Copyright (c) 2003 Intel Corporation
2 # All rights reserved.
3
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10
11 #     * Redistributions in binary form must reproduce the above
12 #       copyright notice, this list of conditions and the following
13 #       disclaimer in the documentation and/or other materials provided
14 #       with the distribution.
15
16 #     * Neither the name of the Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
33 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
34 # export regulations applicable in licensee's jurisdiction. Under
35 # CURRENT (May 2000) U.S. export regulations this software is eligible
36 # for export from the U.S. and can be downloaded by or otherwise
37 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
38 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
39 # Afghanistan and any other country to which the U.S. has embargoed
40 # goods and services.
41
42
43 import os, sys, shutil
44 import popen2
45
46 from Exceptions import *
47
48
49 def makedirs( path ):
50     """
51     from python docs for os.makedirs:
52     Throws an error exception if the leaf directory
53     already exists or cannot be created.
54
55     That is real useful. Instead, we'll create the directory, then use a
56     separate function to test for its existance.
57
58     Return 1 if the directory exists and/or has been created, a BootManagerException
59     otherwise. Does not test the writability of said directory.
60     """
61     try:
62         os.makedirs( path )
63     except OSError:
64         pass
65     try:
66         os.listdir( path )
67     except OSError:
68         raise BootManagerException, "Unable to create directory tree: %s" % path
69     
70     return 1
71
72
73
74 def removedir( path ):
75     """
76     remove a directory tree, return 1 if successful, a BootManagerException
77     if failure.
78     """
79     try:
80         os.listdir( path )
81     except OSError:
82         return 1
83
84     try:
85         shutil.rmtree( path )
86     except OSError, desc:
87         raise BootManagerException, "Unable to remove directory tree: %s" % path
88     
89     return 1
90
91
92
93 def sysexec( cmd, log= None ):
94     """
95     execute a system command, output the results to the logger
96     if log <> None
97
98     return 1 if command completed (return code of non-zero),
99     0 if failed. A BootManagerException is raised if the command
100     was unable to execute or was interrupted by the user with Ctrl+C
101     """
102     prog= popen2.Popen4( cmd, 0 )
103     if prog is None:
104         raise BootManagerException, \
105               "Unable to create instance of popen2.Popen3 " \
106               "for command: %s" % cmd
107
108     if log is not None:
109         try:
110             for line in prog.fromchild:
111                 log.write( line )
112         except KeyboardInterrupt:
113             raise BootManagerException, "Interrupted by user"
114
115     returncode= prog.wait()
116     if returncode != 0:
117         raise BootManagerException, "Running %s failed (rc=%d)" % (cmd,returncode)
118
119     prog= None
120     return 1
121
122
123 def sysexec_noerr( cmd, log= None ):
124     """
125     same as sysexec, but capture boot manager exceptions
126     """
127     try:
128         rc= 0
129         rc= sysexec( cmd, log )
130     except BootManagerException, e:
131         pass
132
133     return rc
134
135
136
137 def chdir( dir ):
138     """
139     change to a directory, return 1 if successful, a BootManagerException if failure
140     """
141     try:
142         os.chdir( dir )
143     except OSError:
144         raise BootManagerException, "Unable to change to directory: %s" % dir
145
146     return 1
147
148
149
150 def removefile( filepath ):
151     """
152     removes a file, return 1 if successful, 0 if failure
153     """
154     try:
155         os.remove( filepath )
156     except OSError:
157         raise BootManagerException, "Unable to remove file: %s" % filepath
158
159     return 1
160