ovs-test: A new tool that allows to diagnose connectivity and performance issues
[sliver-openvswitch.git] / python / ovs / util.py
1 # Copyright (c) 2010, 2011 Nicira Networks
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
21
22 def abs_file_name(dir_, file_name):
23     """If 'file_name' starts with '/', returns a copy of 'file_name'.
24     Otherwise, returns an absolute path to 'file_name' considering it relative
25     to 'dir_', which itself must be absolute.  'dir_' may be None or the empty
26     string, in which case the current working directory is used.
27
28     Returns None if 'dir_' is None and getcwd() fails.
29
30     This differs from os.path.abspath() in that it will never change the
31     meaning of a file name."""
32     if file_name.startswith('/'):
33         return file_name
34     else:
35         if dir_ is None or dir_ == "":
36             try:
37                 dir_ = os.getcwd()
38             except OSError:
39                 return None
40
41         if dir_.endswith('/'):
42             return dir_ + file_name
43         else:
44             return "%s/%s" % (dir_, file_name)