]> git.proxmox.com Git - mirror_ovs.git/blob - python/ovs/util.py
Global replace of Nicira Networks.
[mirror_ovs.git] / python / ovs / util.py
1 # Copyright (c) 2010, 2011, 2012 Nicira, Inc.
2 #
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:
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import os
16 import os.path
17 import sys
18
19 PROGRAM_NAME = os.path.basename(sys.argv[0])
20 EOF = -1
21
22
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.
28
29 Returns None if 'dir_' is None and getcwd() fails.
30
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('/'):
34 return file_name
35 else:
36 if dir_ is None or dir_ == "":
37 try:
38 dir_ = os.getcwd()
39 except OSError:
40 return None
41
42 if dir_.endswith('/'):
43 return dir_ + file_name
44 else:
45 return "%s/%s" % (dir_, file_name)
46
47
48 def ovs_retval_to_string(retval):
49 """Many OVS functions return an int which is one of:
50 - 0: no error yet
51 - >0: errno value
52 - EOF: end of file (not necessarily an error; depends on the function
53 called)
54
55 Returns the appropriate human-readable string."""
56
57 if not retval:
58 return ""
59 if retval > 0:
60 return os.strerror(retval)
61 if retval == EOF:
62 return "End of file"
63 return "***unknown return value: %s***" % retval
64
65
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.
70
71 'message' should not end with a new-line, because this function will add
72 one itself."""
73
74 err_msg = "%s: %s" % (PROGRAM_NAME, message)
75 if err_no:
76 err_msg += " (%s)" % ovs_retval_to_string(err_no)
77
78 sys.stderr.write("%s\n" % err_msg)
79 if vlog:
80 vlog.err(err_msg)
81
82
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).
88
89 'message' should not end with a new-line, because this function will add
90 one itself."""
91
92 ovs_error(*args, **kwargs)
93 sys.exit(1)