Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / python / runtime.py
1 # -*- test-case-name: twisted.python.test.test_runtime -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5
6 # System imports
7 import os
8 import sys
9 import time
10 import imp
11
12
13 def shortPythonVersion():
14     hv = sys.hexversion
15     major = (hv & 0xff000000L) >> 24
16     minor = (hv & 0x00ff0000L) >> 16
17     teeny = (hv & 0x0000ff00L) >> 8
18     return "%s.%s.%s" % (major,minor,teeny)
19
20 knownPlatforms = {
21     'nt': 'win32',
22     'ce': 'win32',
23     'posix': 'posix',
24     'java': 'java',
25     'org.python.modules.os': 'java',
26     }
27
28 _timeFunctions = {
29     #'win32': time.clock,
30     'win32': time.time,
31     }
32
33 class Platform:
34     """Gives us information about the platform we're running on"""
35
36     type = knownPlatforms.get(os.name)
37     seconds = staticmethod(_timeFunctions.get(type, time.time))
38     _platform = sys.platform
39
40     def __init__(self, name=None, platform=None):
41         if name is not None:
42             self.type = knownPlatforms.get(name)
43             self.seconds = _timeFunctions.get(self.type, time.time)
44         if platform is not None:
45             self._platform = platform
46
47
48     def isKnown(self):
49         """Do we know about this platform?"""
50         return self.type != None
51
52
53     def getType(self):
54         """Return 'posix', 'win32' or 'java'"""
55         return self.type
56
57
58     def isMacOSX(self):
59         """Check if current platform is Mac OS X.
60
61         @return: C{True} if the current platform has been detected as OS X
62         @rtype: C{bool}
63         """
64         return self._platform == "darwin"
65
66
67     def isWinNT(self):
68         """Are we running in Windows NT?"""
69         if self.getType() == 'win32':
70             import _winreg
71             try:
72                 k=_winreg.OpenKeyEx(_winreg.HKEY_LOCAL_MACHINE,
73                                     r'Software\Microsoft\Windows NT\CurrentVersion')
74                 _winreg.QueryValueEx(k, 'SystemRoot')
75                 return 1
76             except WindowsError:
77                 return 0
78         # not windows NT
79         return 0
80
81
82     def isWindows(self):
83         return self.getType() == 'win32'
84
85
86     def isVista(self):
87         """
88         Check if current platform is Windows Vista or Windows Server 2008.
89
90         @return: C{True} if the current platform has been detected as Vista
91         @rtype: C{bool}
92         """
93         if getattr(sys, "getwindowsversion", None) is not None:
94             return sys.getwindowsversion()[0] == 6
95         else:
96             return False
97
98
99     def isLinux(self):
100         """
101         Check if current platform is Linux.
102
103         @return: C{True} if the current platform has been detected as Linux.
104         @rtype: C{bool}
105         """
106         return self._platform.startswith("linux")
107
108
109     def supportsThreads(self):
110         """Can threads be created?
111         """
112         try:
113             return imp.find_module('thread')[0] is None
114         except ImportError:
115             return False
116
117
118     def supportsINotify(self):
119         """
120         Return C{True} if we can use the inotify API on this platform.
121
122         @since: 10.1
123         """
124         try:
125             from twisted.python._inotify import INotifyError, init
126         except ImportError:
127             return False
128         try:
129             os.close(init())
130         except INotifyError:
131             return False
132         return True
133
134
135 platform = Platform()
136 platformType = platform.getType()
137 seconds = platform.seconds