|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing Operating System related utility functions. |
|
8 """ |
|
9 |
|
10 import contextlib |
|
11 import ctypes |
|
12 import getpass |
|
13 import os |
|
14 import sys |
|
15 |
|
16 with contextlib.suppress(ImportError): |
|
17 import pwd # only available on Unix systems |
|
18 |
|
19 |
|
20 ############################################################################### |
|
21 ## functions for platform handling |
|
22 ############################################################################### |
|
23 |
|
24 |
|
25 def isWindowsPlatform(): |
|
26 """ |
|
27 Function to check, if this is a Windows platform. |
|
28 |
|
29 @return flag indicating Windows platform |
|
30 @rtype bool |
|
31 """ |
|
32 return sys.platform.startswith(("win", "cygwin")) |
|
33 |
|
34 |
|
35 def isMacPlatform(): |
|
36 """ |
|
37 Function to check, if this is a Mac platform. |
|
38 |
|
39 @return flag indicating Mac platform |
|
40 @rtype bool |
|
41 """ |
|
42 return sys.platform == "darwin" |
|
43 |
|
44 |
|
45 def isLinuxPlatform(): |
|
46 """ |
|
47 Function to check, if this is a Linux platform. |
|
48 |
|
49 @return flag indicating Linux platform |
|
50 @rtype bool |
|
51 """ |
|
52 return sys.platform.startswith("linux") |
|
53 |
|
54 |
|
55 ############################################################################### |
|
56 ## functions for user handling |
|
57 ############################################################################### |
|
58 |
|
59 |
|
60 def getUserName(): |
|
61 """ |
|
62 Function to get the user name. |
|
63 |
|
64 @return user name (string) |
|
65 """ |
|
66 user = getpass.getuser() |
|
67 |
|
68 if isWindowsPlatform() and not user: |
|
69 return win32_GetUserName() |
|
70 |
|
71 return user |
|
72 |
|
73 |
|
74 def getRealName(): |
|
75 """ |
|
76 Function to get the real name of the user. |
|
77 |
|
78 @return real name of the user (string) |
|
79 """ |
|
80 if isWindowsPlatform(): |
|
81 return win32_getRealName() |
|
82 else: |
|
83 user = getpass.getuser() |
|
84 return pwd.getpwnam(user).pw_gecos |
|
85 |
|
86 |
|
87 def getHomeDir(): |
|
88 """ |
|
89 Function to get a users home directory. |
|
90 |
|
91 @return home directory (string) |
|
92 """ |
|
93 return os.path.expanduser("~") |
|
94 |
|
95 |
|
96 ############################################################################### |
|
97 ## functions for environment handling |
|
98 ############################################################################### |
|
99 |
|
100 |
|
101 def getEnvironmentEntry(key, default=None): |
|
102 """ |
|
103 Module function to get an environment entry. |
|
104 |
|
105 @param key key of the requested environment entry (string) |
|
106 @param default value to be returned, if the environment doesn't contain |
|
107 the requested entry (string) |
|
108 @return the requested entry or the default value, if the entry wasn't |
|
109 found (string or None) |
|
110 """ |
|
111 if key in os.environ: |
|
112 entryKey = key |
|
113 elif isWindowsPlatform() and key.lower() in os.environ: |
|
114 entryKey = key.lower() |
|
115 else: |
|
116 return default |
|
117 |
|
118 return os.environ[entryKey].strip() |
|
119 |
|
120 |
|
121 def hasEnvironmentEntry(key): |
|
122 """ |
|
123 Module function to check, if the environment contains an entry. |
|
124 |
|
125 @param key key of the requested environment entry |
|
126 @type str |
|
127 @return flag indicating the presence of the requested entry |
|
128 @rtype bool |
|
129 """ |
|
130 return key in os.environ or (isWindowsPlatform() and key.lower() in os.environ) |
|
131 |
|
132 |
|
133 ############################################################################### |
|
134 ## posix compatibility functions below |
|
135 ############################################################################### |
|
136 |
|
137 # None right now |
|
138 |
|
139 ############################################################################### |
|
140 ## win32 compatibility functions below |
|
141 ############################################################################### |
|
142 |
|
143 |
|
144 def win32_Kill(pid): |
|
145 """ |
|
146 Function to provide an os.kill equivalent for Win32. |
|
147 |
|
148 @param pid process id (integer) |
|
149 @return result of the kill (boolean) |
|
150 """ |
|
151 import win32api # __IGNORE_WARNING_I102__ |
|
152 |
|
153 handle = win32api.OpenProcess(1, 0, pid) |
|
154 return 0 != win32api.TerminateProcess(handle, 0) |
|
155 |
|
156 |
|
157 def win32_GetUserName(): |
|
158 """ |
|
159 Function to get the user name under Win32. |
|
160 |
|
161 @return user name (string) |
|
162 """ |
|
163 try: |
|
164 import win32api # __IGNORE_WARNING_I10__ |
|
165 |
|
166 return win32api.GetUserName() |
|
167 except ImportError: |
|
168 try: |
|
169 u = getEnvironmentEntry("USERNAME") |
|
170 except KeyError: |
|
171 u = getEnvironmentEntry("username", None) |
|
172 return u |
|
173 |
|
174 |
|
175 def win32_getRealName(): |
|
176 """ |
|
177 Function to get the user's real name (aka. display name) under Win32. |
|
178 |
|
179 @return real name of the current user (string) |
|
180 """ |
|
181 GetUserNameEx = ctypes.windll.secur32.GetUserNameExW |
|
182 NameDisplay = 3 |
|
183 |
|
184 size = ctypes.pointer(ctypes.c_ulong(0)) |
|
185 GetUserNameEx(NameDisplay, None, size) |
|
186 |
|
187 nameBuffer = ctypes.create_unicode_buffer(size.contents.value) |
|
188 GetUserNameEx(NameDisplay, nameBuffer, size) |
|
189 return nameBuffer.value |