forked from FabriceSalvaire/CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.py
More file actions
211 lines (156 loc) · 6.7 KB
/
Copy pathPlatform.py
File metadata and controls
211 lines (156 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
####################################################################################################
#
# CodeReview - A Code Review GUI
# Copyright (C) 2015 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program. If
# not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
import os
import platform
import sys
from PyQt5 import QtCore, QtWidgets
####################################################################################################
from CodeReview.Tools.EnumFactory import EnumFactory
from CodeReview.Math.Functions import rint
####################################################################################################
platform_enum = EnumFactory('PlatformEnum', ('linux', 'windows', 'macosx'))
####################################################################################################
class Platform(object):
##############################################
def __init__(self):
self.python_version = platform.python_version()
self.qt_version = QtCore.QT_VERSION_STR
self.pyqt_version = QtCore.PYQT_VERSION_STR
self.os = self._get_os()
self.node = platform.node()
self.distribution = ' '.join(platform.dist())
self.machine = platform.machine()
self.architecture = platform.architecture()[0]
# CPU
self.cpu = self._get_cpu()
self.number_of_cores = self._get_number_of_cores()
self.cpu_khz = self._get_cpu_khz()
self.cpu_mhz = rint(self._get_cpu_khz()/float(1000))
# RAM
self.memory_size_kb = self._get_memory_size_kb()
self.memory_size_mb = rint(self.memory_size_kb/float(1024))
# Screen
try:
application = QtWidgets.QApplication.instance()
self.desktop = application.desktop()
self.number_of_screens = self.desktop.screenCount()
except:
self.desktop = None
self.number_of_screens = 0
self.screens = []
for i in range(self.number_of_screens):
self.screens.append(Screen(self, i))
##############################################
def _get_os(self):
if os.name in 'nt':
return platform_enum.windows
elif sys.platform in 'linux2':
return platform_enum.linux
else:
raise RuntimeError('unknown platform')
##############################################
def _get_cpu(self):
if self.os == platform_enum.linux:
with open('/proc/cpuinfo', 'rt') as cpuinfo:
for line in cpuinfo:
if 'model name' in line:
s = line.split(':')[1]
return s.strip().rstrip()
elif self.os == platform_enum.windows:
raise NotImplementedError
##############################################
def _get_number_of_cores(self):
if self.os == platform_enum.linux:
number_of_cores = 0
with open('/proc/cpuinfo', 'rt') as cpuinfo:
for line in cpuinfo:
if 'processor' in line:
number_of_cores += 1
return number_of_cores
elif self.os == platform_enum.windows:
return int(os.getenv('NUMBER_OF_PROCESSORS'))
##############################################
def _get_cpu_khz(self):
if self.os == platform_enum.linux:
with open('/proc/cpuinfo', 'rt') as cpuinfo:
for line in cpuinfo:
if 'cpu MHz' in line:
s = line.split(':')[1]
return int(1000 * float(s))
if self.os == platform_enum.windows:
raise NotImplementedError
##############################################
def _get_memory_size_kb(self):
if self.os == platform_enum.linux:
with open('/proc/meminfo', 'rt') as cpuinfo:
for line in cpuinfo:
if 'MemTotal' in line:
s = line.split(':')[1][:-3]
return int(s)
if self.os == platform_enum.windows:
raise NotImplementedError
##############################################
def __str__(self):
message_template = '''
Platform %(node)s
Hardware:
Machine: %(machine)s
Architecture: %(architecture)s
CPU: %(cpu)s
Number of Cores: %(number_of_cores)u
CPU Frequence: %(cpu_mhz)u MHz
Memory: %(memory_size_mb)u MB
Number of Screens: %(number_of_screens)u
'''
message = message_template % self.__dict__
for screen in self.screens:
message += str(screen)
message_template = '''
Software Versions:
OS: %(os)s
Distribution: %(distribution)s
Python: %(python_version)s
Qt: %(qt_version)s
PyQt: %(pyqt_version)s
'''
message += message_template % self.__dict__
return message
####################################################################################################
class Screen(object):
##############################################
def __init__(self, platform_obj, screen_id):
self.screen_id = screen_id
qt_screen_geometry = platform_obj.desktop.screenGeometry(screen_id)
self.screen_width, self.screen_height = qt_screen_geometry.width(), qt_screen_geometry.height()
widget = platform_obj.desktop.screen(screen_id)
self.dpi = widget.physicalDpiX(), widget.physicalDpiY()
# qt_available_geometry = self.desktop.availableGeometry(screen_id)
##############################################
def __str__(self):
message_template = """
Screen %(screen_id)u
geometry %(screen_width)ux%(screen_height)u px
resolution %(dpi)s dpi
"""
return message_template % self.__dict__
####################################################################################################
#
# End
#
####################################################################################################