1 # Copyright (c) 2010, 2011, 2012 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 PROGRAM_NAME = os.path.basename(sys.argv[0])
23 def abs_file_name(dir_, file_name):
24 """If 'file_name' starts with '/', returns a copy of 'file_name'.
25 Otherwise, returns an absolute path to 'file_name' considering it relative
26 to 'dir_', which itself must be absolute. 'dir_' may be None or the empty
27 string, in which case the current working directory is used.
29 Returns None if 'dir_' is None and getcwd() fails.
31 This differs from os.path.abspath() in that it will never change the
32 meaning of a file name."""
33 if file_name.startswith('/'):
36 if dir_ is None or dir_ == "":
42 if dir_.endswith('/'):
43 return dir_ + file_name
45 return "%s/%s" % (dir_, file_name)
48 def ovs_retval_to_string(retval):
49 """Many OVS functions return an int which is one of:
52 - EOF: end of file (not necessarily an error; depends on the function
55 Returns the appropriate human-readable string."""
60 return os.strerror(retval)
63 return "***unknown return value: %s***" % retval
66 def ovs_error(err_no, message, vlog=None):
67 """Prints 'message' on stderr and emits an ERROR level log message to
68 'vlog' if supplied. If 'err_no' is nonzero, then it is formatted with
69 ovs_retval_to_string() and appended to the message inside parentheses.
71 'message' should not end with a new-line, because this function will add
74 err_msg = "%s: %s" % (PROGRAM_NAME, message)
76 err_msg += " (%s)" % ovs_retval_to_string(err_no)
78 sys.stderr.write("%s\n" % err_msg)
83 def ovs_fatal(*args, **kwargs):
84 """Prints 'message' on stderr and emits an ERROR level log message to
85 'vlog' if supplied. If 'err_no' is nonzero, then it is formatted with
86 ovs_retval_to_string() and appended to the message inside parentheses.
87 Then, terminates with exit code 1 (indicating a failure).
89 'message' should not end with a new-line, because this function will add
92 ovs_error(*args, **kwargs)