FIX: Remove undistributable CHEMKIN files
[freefoam.git] / data / python / FreeFOAM / compat.py
blob64510c707d208f5b6e1096fdd5c1c08493c84514
1 #-------------------------------------------------------------------------------
2 # ______ _ ____ __ __
3 # | ____| _| |_ / __ \ /\ | \/ |
4 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
5 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
6 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
7 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
9 # FreeFOAM: The Cross-Platform CFD Toolkit
11 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
12 # Gerber van der Graaf <gerber_graaf@users.sf.net>
13 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software
14 # Foundation; All Rights Reserved
15 #-------------------------------------------------------------------------------
16 # License
17 # This file is part of FreeFOAM.
19 # FreeFOAM is free software: you can redistribute it and/or modify it
20 # under the terms of the GNU General Public License as published by the
21 # Free Software Foundation, either version 3 of the License, or (at your
22 # option) any later version.
24 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
25 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 # for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with FreeFOAM. If not, see <http://www.gnu.org/licenses/>.
32 # Some of the below functions have been backported from Python. They are
33 # licensed under the Python Software Foundation License version 2. The full
34 # license text is available at http://docs.python.org/license.html. Notably,
35 # the functions _nt_relpath and _posix_relpath are adapted versions of
36 # ntpath.relpath and posixpath.relpath, respectively and are copyrighted to
37 # the Python Software Foundation. _check_call and _check_output were modified
38 # from subprocess.check_call and subprocess.check_output which are
39 # copyrighted to Peter Astrand <astrand@lysator.liu.se>.
41 # Description
42 # Compatibility functions that make Python 3.x code work with 2.x
43 # interpreters.
45 #-------------------------------------------------------------------------------
47 import sys as _sys
48 import os.path as _op
49 import os as _os
50 import subprocess as _subprocess
52 def echo(*args, **kwargs):
53 """print() replacement which does not depend on the Python version"""
54 if 'sep' in kwargs:
55 sep = kwargs['sep']
56 else:
57 sep = ' '
58 if 'end' in kwargs:
59 end = kwargs['end']
60 else:
61 end = '\n'
62 if 'file' in kwargs:
63 file = kwargs['file']
64 else:
65 file = _sys.stdout
66 file.write(str(sep).join(map(str, args)))
67 end = str(end)
68 if len(end):
69 file.write(end)
71 # check whether Python version is < 3.0
72 if _sys.hexversion < 0x030000f0:
73 # replace the standard functions
74 import itertools
75 filter = itertools.ifilter
76 map = itertools.imap
77 zip = itertools.izip
78 repr = unicode
79 range = xrange
80 import HTMLParser
81 else:
82 callable = None
83 xrange = None
84 import html.parser as HTMLParser
86 # check whether Python version is < 2.6
87 if _sys.hexversion < 0x020600f0:
88 def _posix_relpath(path, start=_os.curdir):
89 """Return a relative version of a path, POSIX version"""
91 if not path:
92 raise ValueError("no path specified")
94 start_list = _op.abspath(start).split(_op.sep)
95 path_list = _op.abspath(path).split(_op.sep)
97 # Work out how much of the filepath is shared by start and path.
98 i = len(_op.commonprefix([start_list, path_list]))
100 rel_list = [_op.pardir] * (len(start_list)-i) + path_list[i:]
101 if not rel_list:
102 return _os.curdir
103 return _op.join(*rel_list)
105 def _nt_relpath(path, start=_os.curdir):
106 """Return a relative version of a path, Windows version"""
108 if not path:
109 raise ValueError("no path specified")
110 start_list = _op.abspath(start).split(_op.sep)
111 path_list = _op.abspath(path).split(_op.sep)
112 if start_list[0].lower() != path_list[0].lower():
113 unc_path, rest = _op.splitunc(path)
114 unc_start, rest = _op.splitunc(start)
115 if bool(unc_path) ^ bool(unc_start):
116 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
117 % (path, start))
118 else:
119 raise ValueError("path is on drive %s, start on drive %s"
120 % (path_list[0], start_list[0]))
121 # Work out how much of the filepath is shared by start and path.
122 for i in range(min(len(start_list), len(path_list))):
123 if start_list[i].lower() != path_list[i].lower():
124 break
125 else:
126 i += 1
128 rel_list = [_op.pardir] * (len(start_list)-i) + path_list[i:]
129 if not rel_list:
130 return _os.curdir
131 return _op.join(*rel_list)
133 # provide os.path.relpath
134 if _os.name == 'posix':
135 _op.relpath = _posix_relpath
136 elif _os.name == 'nt':
137 _op.relpath = _nt_relpath
138 else:
139 raise Exception('No implementation of os.path.relpath available')
141 # provide md5/hashlib compatibility
142 import md5 as _md5
143 md5 = _md5.new
144 else:
145 import hashlib as _hashlib
146 md5 = lambda x: _hashlib.md5(x.encode())
148 # check whether Python version is < 2.5
149 if _sys.hexversion < 0x020500f0:
150 # there is no BaseException class
151 BaseException = Exception
153 # there is no subprocess.check_call() function
154 def _check_call(*popernargs, **kwargs):
155 retcode = _subprocess.call(*popenargs, **kwargs)
156 if retcode:
157 cmd = kwargs.get("args")
158 if cmd is None:
159 cmd = popenargs[0]
160 raise _subprocess.CalledProcessError(retcode, cmd)
161 return 0
162 _subprocess.check_call = _check_call
164 # check whether Python version is < 2.7
165 if _sys.hexversion < 0x020700f0:
166 # there is no subprocess.check_output() function
167 def _check_output(*popenargs, **kwargs):
168 if 'stdout' in kwargs:
169 raise ValueError(
170 'stdout argument not allowed, it will be overridden.')
171 process = _subprocess.Popen(
172 stdout=_subprocess.PIPE, *popenargs, **kwargs)
173 output, unused_err = process.communicate()
174 retcode = process.poll()
175 if retcode:
176 cmd = kwargs.get("args")
177 if cmd is None:
178 cmd = popenargs[0]
179 raise _subprocess.CalledProcessError(retcode, cmd, output=output)
180 return output
181 _subprocess.check_output = _check_output
183 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file