forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.py
631 lines (518 loc) · 17.2 KB
/
platform.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
"""Module for platform-specific constants and implementations, as well as
compatibility layers to make use of the 'best' implementation available
on a platform.
"""
import os
import sys
import ctypes
import signal
import pathlib
import builtins
import platform
import functools
import subprocess
import collections
import collections.abc as cabc
import importlib.util
from xonsh.lazyasd import LazyBool, lazyobject, lazybool
# do not import any xonsh-modules here to avoid circular dependencies
FD_STDIN = 0
FD_STDOUT = 1
FD_STDERR = 2
@lazyobject
def distro():
try:
import distro as d
except ImportError:
d = None
except Exception:
raise
return d
#
# OS
#
ON_DARWIN = LazyBool(lambda: platform.system() == "Darwin", globals(), "ON_DARWIN")
"""``True`` if executed on a Darwin platform, else ``False``. """
ON_LINUX = LazyBool(lambda: platform.system() == "Linux", globals(), "ON_LINUX")
"""``True`` if executed on a Linux platform, else ``False``. """
ON_WINDOWS = LazyBool(lambda: platform.system() == "Windows", globals(), "ON_WINDOWS")
"""``True`` if executed on a native Windows platform, else ``False``. """
ON_CYGWIN = LazyBool(lambda: sys.platform == "cygwin", globals(), "ON_CYGWIN")
"""``True`` if executed on a Cygwin Windows platform, else ``False``. """
ON_MSYS = LazyBool(lambda: sys.platform == "msys", globals(), "ON_MSYS")
"""``True`` if executed on a MSYS Windows platform, else ``False``. """
ON_POSIX = LazyBool(lambda: (os.name == "posix"), globals(), "ON_POSIX")
"""``True`` if executed on a POSIX-compliant platform, else ``False``. """
ON_FREEBSD = LazyBool(
lambda: (sys.platform.startswith("freebsd")), globals(), "ON_FREEBSD"
)
"""``True`` if on a FreeBSD operating system, else ``False``."""
ON_DRAGONFLY = LazyBool(
lambda: (sys.platform.startswith("dragonfly")), globals(), "ON_DRAGONFLY"
)
"""``True`` if on a DragonFly BSD operating system, else ``False``."""
ON_NETBSD = LazyBool(
lambda: (sys.platform.startswith("netbsd")), globals(), "ON_NETBSD"
)
"""``True`` if on a NetBSD operating system, else ``False``."""
@lazybool
def ON_BSD():
"""``True`` if on a BSD operating system, else ``False``."""
return bool(ON_FREEBSD) or bool(ON_NETBSD) or bool(ON_DRAGONFLY)
@lazybool
def ON_BEOS():
"""True if we are on BeOS or Haiku."""
return sys.platform == "beos5" or sys.platform == "haiku1"
@lazybool
def ON_WSL():
"""True if we are on Windows Subsystem for Linux (WSL)"""
return "Microsoft" in platform.release()
#
# Python & packages
#
PYTHON_VERSION_INFO = sys.version_info[:3]
""" Version of Python interpreter as three-value tuple. """
@lazyobject
def PYTHON_VERSION_INFO_BYTES():
"""The python version info tuple in a canonical bytes form."""
return ".".join(map(str, sys.version_info)).encode()
ON_ANACONDA = LazyBool(
lambda: pathlib.Path(sys.prefix).joinpath("conda-meta").exists(),
globals(),
"ON_ANACONDA",
)
""" ``True`` if executed in an Anaconda instance, else ``False``. """
CAN_RESIZE_WINDOW = LazyBool(
lambda: hasattr(signal, "SIGWINCH"), globals(), "CAN_RESIZE_WINDOW"
)
"""``True`` if we can resize terminal window, as provided by the presense of
signal.SIGWINCH, else ``False``.
"""
@lazybool
def HAS_PYGMENTS():
"""``True`` if `pygments` is available, else ``False``."""
spec = importlib.util.find_spec("pygments")
return spec is not None
@functools.lru_cache(1)
def pygments_version():
"""pygments.__version__ version if available, else None."""
if HAS_PYGMENTS:
import pygments
v = pygments.__version__
else:
v = None
return v
@functools.lru_cache(1)
def pygments_version_info():
""" Returns `pygments`'s version as tuple of integers. """
if HAS_PYGMENTS:
return tuple(int(x) for x in pygments_version().strip("<>+-=.").split("."))
else:
return None
@functools.lru_cache(1)
def has_prompt_toolkit():
"""Tests if the `prompt_toolkit` is available."""
spec = importlib.util.find_spec("prompt_toolkit")
return spec is not None
@functools.lru_cache(1)
def ptk_version():
"""Returns `prompt_toolkit.__version__` if available, else ``None``."""
if has_prompt_toolkit():
import prompt_toolkit
return getattr(prompt_toolkit, "__version__", "<0.57")
else:
return None
@functools.lru_cache(1)
def ptk_version_info():
""" Returns `prompt_toolkit`'s version as tuple of integers. """
if has_prompt_toolkit():
return tuple(int(x) for x in ptk_version().strip("<>+-=.").split("."))
else:
return None
@functools.lru_cache(1)
def ptk_above_min_supported():
minimum_required_ptk_version = (1, 0)
return ptk_version_info()[:2] >= minimum_required_ptk_version
@functools.lru_cache(1)
def ptk_shell_type():
"""Returns the prompt_toolkit shell type based on the installed version."""
if ptk_version_info()[:2] < (2, 0):
return "prompt_toolkit1"
else:
return "prompt_toolkit2"
@functools.lru_cache(1)
def win_ansi_support():
if ON_WINDOWS:
try:
from prompt_toolkit.utils import is_windows_vt100_supported, is_conemu_ansi
except ImportError:
return False
return is_conemu_ansi() or is_windows_vt100_supported()
else:
return False
@functools.lru_cache(1)
def ptk_below_max_supported():
ptk_max_version_cutoff = (2, 0)
return ptk_version_info()[:2] < ptk_max_version_cutoff
@functools.lru_cache(1)
def best_shell_type():
if builtins.__xonsh__.env.get("TERM", "") == "dumb":
return "dumb"
elif ON_WINDOWS or has_prompt_toolkit():
return "prompt_toolkit"
else:
return "readline"
@functools.lru_cache(1)
def is_readline_available():
"""Checks if readline is available to import."""
spec = importlib.util.find_spec("readline")
return spec is not None
@lazyobject
def seps():
"""String of all path separators."""
s = os.path.sep
if os.path.altsep is not None:
s += os.path.altsep
return s
def pathsplit(p):
"""This is a safe version of os.path.split(), which does not work on input
without a drive.
"""
n = len(p)
while n and p[n - 1] not in seps:
n -= 1
pre = p[:n]
pre = pre.rstrip(seps) or pre
post = p[n:]
return pre, post
def pathbasename(p):
"""This is a safe version of os.path.basename(), which does not work on
input without a drive. This version does.
"""
return pathsplit(p)[-1]
@lazyobject
def expanduser():
"""Dispatches to the correct platform-dependent expanduser() function."""
if ON_WINDOWS:
return windows_expanduser
else:
return os.path.expanduser
def windows_expanduser(path):
"""A Windows-specific expanduser() function for xonsh. This is needed
since os.path.expanduser() does not check on Windows if the user actually
exists. This restricts expanding the '~' if it is not followed by a
separator. That is only '~/' and '~\' are expanded.
"""
if not path.startswith("~"):
return path
elif len(path) < 2 or path[1] in seps:
return os.path.expanduser(path)
else:
return path
# termios tc(get|set)attr indexes.
IFLAG = 0
OFLAG = 1
CFLAG = 2
LFLAG = 3
ISPEED = 4
OSPEED = 5
CC = 6
#
# Dev release info
#
@functools.lru_cache(1)
def githash():
"""Returns a tuple contains two strings: the hash and the date."""
install_base = os.path.dirname(__file__)
githash_file = "{}/dev.githash".format(install_base)
if not os.path.exists(githash_file):
return None, None
sha = None
date_ = None
try:
with open(githash_file) as f:
sha, date_ = f.read().strip().split("|")
except ValueError:
pass
return sha, date_
#
# Encoding
#
DEFAULT_ENCODING = sys.getdefaultencoding()
""" Default string encoding. """
if PYTHON_VERSION_INFO < (3, 5, 0):
class DirEntry:
def __init__(self, directory, name):
self.__path__ = pathlib.Path(directory) / name
self.name = name
self.path = str(self.__path__)
self.is_symlink = self.__path__.is_symlink
def inode(self):
return os.stat(self.path, follow_symlinks=False).st_ino
def is_dir(self, *, follow_symlinks=True):
if follow_symlinks:
return self.__path__.is_dir()
else:
return not self.__path__.is_symlink() and self.__path__.is_dir()
def is_file(self, *, follow_symlinks=True):
if follow_symlinks:
return self.__path__.is_file()
else:
return not self.__path__.is_symlink() and self.__path__.is_file()
def stat(self, *, follow_symlinks=True):
return os.stat(self.path, follow_symlinks=follow_symlinks)
def scandir(path):
""" Compatibility layer for `os.scandir` from Python 3.5+. """
return (DirEntry(path, x) for x in os.listdir(path))
else:
scandir = os.scandir
#
# Linux distro
#
@functools.lru_cache(1)
def linux_distro():
"""The id of the Linux distribution running on, possibly 'unknown'.
None on non-Linux platforms.
"""
if ON_LINUX:
if distro:
ld = distro.id()
elif PYTHON_VERSION_INFO < (3, 6, 6):
ld = platform.linux_distribution()[0] or "unknown"
elif "-ARCH-" in platform.platform():
ld = "arch" # that's the only one we need to know for now
else:
ld = "unknown"
else:
ld = None
return ld
#
# Windows
#
@functools.lru_cache(1)
def git_for_windows_path():
"""Returns the path to git for windows, if available and None otherwise."""
import winreg
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\GitForWindows")
gfwp, _ = winreg.QueryValueEx(key, "InstallPath")
except FileNotFoundError:
gfwp = None
return gfwp
@functools.lru_cache(1)
def windows_bash_command():
"""Determines the command for Bash on windows."""
# Check that bash is on path otherwise try the default directory
# used by Git for windows
wbc = "bash"
cmd_cache = builtins.__xonsh__.commands_cache
bash_on_path = cmd_cache.lazy_locate_binary("bash", ignore_alias=True)
if bash_on_path:
try:
out = subprocess.check_output(
[bash_on_path, "--version"],
stderr=subprocess.PIPE,
universal_newlines=True,
)
except subprocess.CalledProcessError:
bash_works = False
else:
# Check if Bash is from the "Windows Subsystem for Linux" (WSL)
# which can't be used by xonsh foreign-shell/completer
bash_works = out and "pc-linux-gnu" not in out.splitlines()[0]
if bash_works:
wbc = bash_on_path
else:
gfwp = git_for_windows_path()
if gfwp:
bashcmd = os.path.join(gfwp, "bin\\bash.exe")
if os.path.isfile(bashcmd):
wbc = bashcmd
return wbc
#
# Environment variables defaults
#
if ON_WINDOWS:
class OSEnvironCasePreserving(cabc.MutableMapping):
""" Case-preserving wrapper for os.environ on Windows.
It uses nt.environ to get the correct cased keys on
initialization. It also preserves the case of any variables
add after initialization.
"""
def __init__(self):
import nt
self._upperkeys = dict((k.upper(), k) for k in nt.environ)
def _sync(self):
""" Ensure that the case sensitive map of the keys are
in sync with os.environ
"""
envkeys = set(os.environ.keys())
for key in envkeys.difference(self._upperkeys):
self._upperkeys[key] = key.upper()
for key in set(self._upperkeys).difference(envkeys):
del self._upperkeys[key]
def __contains__(self, k):
self._sync()
return k.upper() in self._upperkeys
def __len__(self):
self._sync()
return len(self._upperkeys)
def __iter__(self):
self._sync()
return iter(self._upperkeys.values())
def __getitem__(self, k):
self._sync()
return os.environ[k]
def __setitem__(self, k, v):
self._sync()
self._upperkeys[k.upper()] = k
os.environ[k] = v
def __delitem__(self, k):
self._sync()
if k.upper() in self._upperkeys:
del self._upperkeys[k.upper()]
del os.environ[k]
def getkey_actual_case(self, k):
self._sync()
return self._upperkeys.get(k.upper())
@lazyobject
def os_environ():
"""This dispatches to the correct, case-sensitive version of os.environ.
This is mainly a problem for Windows. See #2024 for more details.
This can probably go away once support for Python v3.5 or v3.6 is
dropped.
"""
if ON_WINDOWS:
return OSEnvironCasePreserving()
else:
return os.environ
@functools.lru_cache(1)
def bash_command():
"""Determines the command for Bash on the current platform."""
if ON_WINDOWS:
bc = windows_bash_command()
else:
bc = "bash"
return bc
@lazyobject
def BASH_COMPLETIONS_DEFAULT():
"""A possibly empty tuple with default paths to Bash completions known for
the current platform.
"""
if ON_LINUX or ON_CYGWIN or ON_MSYS:
bcd = ("/usr/share/bash-completion/bash_completion",)
elif ON_DARWIN:
bcd = (
"/usr/local/share/bash-completion/bash_completion", # v2.x
"/usr/local/etc/bash_completion",
"/opt/local/etc/profile.d/bash_completion.sh",
"/usr/local/etc/bash_completion.d/git-completion.bash",
) # v1.x
elif ON_WINDOWS and git_for_windows_path():
bcd = (
os.path.join(
git_for_windows_path(), "usr\\share\\bash-completion\\bash_completion"
),
os.path.join(
git_for_windows_path(),
"mingw64\\share\\git\\completion\\" "git-completion.bash",
),
)
else:
bcd = ()
return bcd
@lazyobject
def PATH_DEFAULT():
if ON_LINUX or ON_CYGWIN or ON_MSYS:
if linux_distro() == "arch":
pd = (
"/usr/local/sbin",
"/usr/local/bin",
"/usr/bin",
"/usr/bin/site_perl",
"/usr/bin/vendor_perl",
"/usr/bin/core_perl",
)
else:
pd = (
os.path.expanduser("~/bin"),
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/sbin",
"/bin",
"/usr/games",
"/usr/local/games",
)
elif ON_DARWIN:
pd = ("/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin")
elif ON_WINDOWS:
import winreg
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment",
)
pd = tuple(winreg.QueryValueEx(key, "Path")[0].split(os.pathsep))
else:
pd = ()
return pd
#
# libc
#
@lazyobject
def LIBC():
"""The platform dependent libc implementation."""
global ctypes
if ON_DARWIN:
import ctypes.util
libc = ctypes.CDLL(ctypes.util.find_library("c"))
elif ON_CYGWIN:
libc = ctypes.CDLL("cygwin1.dll")
elif ON_MSYS:
libc = ctypes.CDLL("msys-2.0.dll")
elif ON_BSD:
try:
libc = ctypes.CDLL("libc.so")
except AttributeError:
libc = None
except OSError:
# OS X; can't use ctypes.util.find_library because that creates
# a new process on Linux, which is undesirable.
try:
libc = ctypes.CDLL("libc.dylib")
except OSError:
libc = None
elif ON_POSIX:
try:
libc = ctypes.CDLL("libc.so")
except AttributeError:
libc = None
except OSError:
# Debian and derivatives do the wrong thing because /usr/lib/libc.so
# is a GNU ld script rather than an ELF object. To get around this, we
# have to be more specific.
# We don't want to use ctypes.util.find_library because that creates a
# new process on Linux. We also don't want to try too hard because at
# this point we're already pretty sure this isn't Linux.
try:
libc = ctypes.CDLL("libc.so.6")
except OSError:
libc = None
if not hasattr(libc, "sysinfo"):
# Not Linux.
libc = None
elif ON_WINDOWS:
if hasattr(ctypes, "windll") and hasattr(ctypes.windll, "kernel32"):
libc = ctypes.windll.kernel32
else:
try:
# Windows CE uses the cdecl calling convention.
libc = ctypes.CDLL("coredll.lib")
except (AttributeError, OSError):
libc = None
elif ON_BEOS:
libc = ctypes.CDLL("libroot.so")
else:
libc = None
return libc