Module EDUtilsPlatform
[hide private]
[frames] | no frames]

Source Code for Module EDUtilsPlatform

  1  # -*- coding: utf8 -*- 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    File: "$Id: EDUtilsPath.py 1484 2010-05-05 07:08:21Z svensson $" 
  7  # 
  8  #    Copyright (C) 2008-2010 European Synchrotron Radiation Facility 
  9  #                            Grenoble, France 
 10  # 
 11  #    Principal authors: Jérôme Kieffer (jerome.kieffer@esrf.fr) 
 12  #  
 13  # 
 14  #    This program is free software: you can redistribute it and/or modify 
 15  #    it under the terms of the GNU Lesser General Public License as published 
 16  #    by the Free Software Foundation, either version 3 of the License, or 
 17  #    (at your option) any later version. 
 18  # 
 19  #    This program is distributed in the hope that it will be useful, 
 20  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 21  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 22  #    GNU Lesser General Public License for more details. 
 23  # 
 24  #    You should have received a copy of the GNU General Public License 
 25  #    and the GNU Lesser General Public License  along with this program.   
 26  #    If not, see <http://www.gnu.org/licenses/>. 
 27  # 
 28  __authors__ = [ "Jérôme Kieffer" ] 
 29  __contact__ = "jerome.kieffer@esrf.fr" 
 30  __license__ = "LGPLv3+" 
 31  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 32   
 33   
 34  import sys, os, math, distutils.util, signal, subprocess 
 35  from EDThreading import Semaphore 
 36  from EDVerbose import EDVerbose 
37 38 39 -class classproperty(property):
40 - def __get__(self, obj, type_):
41 return self.fget.__get__(None, type_)()
42 - def __set__(self, obj, value):
43 cls = type(obj) 44 return self.fset.__get__(None, cls)(value)
45
46 47 -class EDUtilsPlatform(object):
48 """ 49 Static class for guessing: platform specific stuff. 50 51 """ 52 __semaphore = Semaphore() 53 __semaphore.acquire() 54 __strCmdSep = None 55 __strCmdEnv = None 56 __strEscapedLineSep = None 57 __strLineSep = os.linesep 58 __strSep = os.sep 59 __strAltSep = os.altsep 60 __strExtSep = os.extsep 61 __strPathSep = os.pathsep 62 __strName = os.name 63 __strStartScript = None 64 __PythonPlatformSize = int(round(math.log(sys.maxint + 1) / math.log(2) + 1)) 65 66 __SystemPlatform = distutils.util.get_platform() 67 if __PythonPlatformSize == 32 and __SystemPlatform.endswith("x86_64"): 68 __PythonPlatform = __SystemPlatform[:-6] + "i386" 69 else: 70 __PythonPlatform = __SystemPlatform 71 __PythonArchitecture = "lib.%s-%i.%i" % (__PythonPlatform, sys.version_info[0], sys.version_info[1]) 72 if sys.maxunicode == 65535: 73 __PythonArchitecture += "-ucs2" 74 __SystemArchitecture = "lib.%s-%i.%i" % (__SystemPlatform, sys.version_info[0], sys.version_info[1]) 75 76 if os.name == "java": # Then we are running under Jython ! 77 from java.lang import System as javasystem 78 if javasystem.getProperty("os.name").startswith("Windows"): 79 __strName = "nt" 80 else: 81 __strName = "posix" 82 83 if __strName == "nt": 84 __strCmdSep = "&" 85 __strCmdEnv = "set" 86 __strEscapedSep = "/" 87 __strEscapedLineSep = "\\r\\n" 88 __strStartScript = "@ECHO OFF" 89 elif __strName == "posix": 90 __strCmdSep = ";" 91 __strCmdEnv = "env" 92 __strEscapedSep = "\\\\" 93 __strEscapedLineSep = "\\n" 94 __strStartScript = "#!%s" % sys.executable 95 __semaphore.release() 96 97 98 99 @classmethod
100 - def getArchitecture(cls):
101 """ 102 Returns the name of the architecture including the CPU arch and the version of python for the actual running python 103 (i.e. can be i386 on a x86_64 computer) 104 @return: lib-$OS-$arch-$PyVersion 105 @rtype: python string 106 """ 107 return cls.__PythonArchitecture
108 architecture = classproperty(getArchitecture) 109 110 111 @classmethod
112 - def getSystemArchitecture(cls):
113 """ 114 Returns the name of the architecture including the CPU arch and the version of python for the actual operating system 115 @return: lib-$OS-$arch-$PyVersion 116 @rtype: python string 117 """ 118 return cls.__SystemArchitecture
119 systemArchitecture = classproperty(getSystemArchitecture) 120 121 122 @classmethod
123 - def getName(cls):
124 """ 125 Returns the name of the architecture 126 @return: os.name like architecture (posix, nt, ... but never java even under jython) 127 @rtype: python string 128 """ 129 return cls.__strName
130 name = classproperty(getName) 131 132 133 @classmethod
134 - def getCmdSep(cls):
135 """ 136 Returns the Command separator like 137 - Under Unix: cmd1;cmd2 138 - Under Windows cmd1 & cmd2 139 @return: "&" or ";" depending on the architecture 140 @rtype: python string 141 """ 142 return cls.__strCmdSep
143 cmdSep = classproperty(getCmdSep) 144 145 146 @classmethod
147 - def getCmdEnv(cls):
148 """ 149 @return: "env" or "set" to print the environment under the current operating system 150 """ 151 return cls.__strCmdEnv
152 cmdEnv = classproperty(getCmdEnv) 153 154 155 @classmethod
156 - def getEscapedSep(cls):
157 """ 158 return os.sep with "\" escaped 159 """ 160 return cls.__strEscapedSep
161 escapedSep = classproperty(getEscapedSep) 162 163 164 @classmethod
165 - def getEscapedLineSep(cls):
166 """ 167 return os.linesep with "\" escaped 168 """ 169 return cls.__strEscapedLineSep
170 escapedLinesep = classproperty(getEscapedLineSep) 171 172 173 @classmethod
174 - def getLineSep(cls):
175 """ 176 @return os.linesep 177 """ 178 return cls.__strLineSep
179 linesep = classproperty(getLineSep) 180 181 182 @classmethod
183 - def getSep(cls):
184 """ 185 @return: os.sep 186 """ 187 return cls.__strSep
188 sep = classproperty(getSep) 189 190 191 @classmethod
192 - def getStartScript(cls):
193 """ 194 @return: header for writing scripts under the current the archtecture (#!/usr/bin/python under linux) 195 """ 196 return cls.__strStartScript
197 startScript = classproperty(getStartScript) 198 199 200 @classmethod
201 - def getSize(cls):
202 """ 203 @return: the size of the environment, probably 32 or 64 bits 204 """ 205 return cls.__PythonPlatformSize
206 size = classproperty(getSize) 207 208 @classmethod
209 - def getSystemPlatform(cls):
210 """ 211 @return: linux-x86_64, if python 32bits is running under amd64 OS. 212 """ 213 return cls.__SystemPlatform
214 systemPlatform = classproperty(getSystemPlatform) 215 216 217 @classmethod
218 - def getPythonPlatform(cls):
219 """ 220 @return: linux-i386, if python 32bits is running under amd64 OS. 221 """ 222 return cls.__PythonPlatform
223 pythonPlatform = classproperty(getPythonPlatform) 224 225 226 @classmethod
227 - def getAltSep(cls):
228 """ 229 @return: os.altsep 230 """ 231 return cls.__strAltSep
232 altsep = classproperty(getAltSep) 233 234 235 @classmethod
236 - def getExtSep(cls):
237 """ 238 @return: "." as extension separator 239 """ 240 return cls.__strExtSep
241 extsep = classproperty(getExtSep) 242 243 244 @classmethod
245 - def getPathSep(cls):
246 """ 247 same as os.pathsep 248 @return "/" under linux and "\" under windows. 249 """ 250 return cls.__strPathSep
251 pathsep = classproperty(getPathSep) 252 253 @classmethod
254 - def escape(cls, _string):
255 """ 256 escape \ (i.e. "\\") under windows as \\ (i.e. "\\\\") 257 @return escaped string suitable for metaprogramming 258 """ 259 return _string.replace("\\", "\\\\")
260 261 262 @classmethod
263 - def Popen(cls, *args, **kwargs):
264 """ 265 implementation of a platform independent subprocess.Popen method 266 267 @return: subporcess.Popen instance. 268 """ 269 if os.name == "posix": #python under unix 270 kwargs["preexec_fn"] = os.setsid 271 return subprocess.Popen(*args, **kwargs)
272 273 274 275 276 @classmethod
277 - def kill(cls, _iPid):
278 """ 279 implementation of a platform independent kill method 280 281 @param _iPid: process ID 282 @type _iPid: integer 283 """ 284 EDVerbose.log("EDUtilsPlatorm.kill called on PID: %s" % _iPid) 285 if os.name == "posix": #python under unix 286 os.killpg(_iPid, signal.SIGKILL) 287 elif cls.architecture == "posix": #jython running under unix 288 os.kill(_iPid, signal.SIGKILL) 289 else: #windows, ... Nota: this only works from python2.7 under windows ! 290 EDVerbose.WARNING("Kill Called to PID= %s with signal %s" % (_iPid, signal.SIGTERM)) 291 os.kill(_iPid, signal.SIGTERM)
292