-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.py
103 lines (92 loc) · 4.29 KB
/
version.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
#!/usr/bin/env python
# vi:sw=2:ts=2:expandtab
# Copyright (C) 2009-2021 Ian Munsie
#
# Kosh 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.
#
# Kosh 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 Kosh. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import subprocess
import importlib
import site
__version__ = 'v0.1 development' #FIXME: use git describe if from git repository
HAS_TERMUX_API = False
def import_ask_install(module, package, msg, version_check=None, uninstall=None):
try:
ret = __import__(module)
except ImportError:
print(msg)
answer = None
while answer not in ('y', 'n'):
answer = input('Install %s with pip? (y/n) ' % package).lower()
if answer == 'y':
subprocess.call([sys.executable] + "-m ensurepip --user".split())
if uninstall:
subprocess.call([sys.executable, "-m", "pip", "uninstall", uninstall])
subprocess.call([sys.executable, "-m", "pip", "install", package, "--user"])
importlib.reload(site) # Ensure site-packages paths are up to date if pip just created it
ret = __import__(module)
if version_check is not None and not version_check(ret):
print('%s version too old' % module)
answer = None
while answer not in ('y', 'n'):
answer = input('Upgrade %s with pip? (y/n) ' % package).lower()
if answer == 'y':
subprocess.call([sys.executable] + "-m ensurepip --user".split())
if uninstall:
subprocess.call([sys.executable, "-m", "pip", "uninstall", uninstall])
subprocess.call([sys.executable, "-m", "pip", "install", package, "--upgrade", "--user"])
importlib.reload(site) # Ensure site-packages paths are up to date if pip just created it
ret = __import__(module)
return ret
def checkCrypto():
def version_check(module):
return module.version_info[0] >= 3
try:
import Cryptodome
except ImportError:
if not import_ask_install('Crypto', 'pycryptodome', 'ERROR: Python crypto library not found',
version_check=version_check, uninstall='pycrypto'):
sys.exit(1)
def checkUrwid(required):
if not import_ask_install('urwid', 'urwid', 'ERROR: Python urwid library not found'):
sys.exit(1)
import urwid
if required.split('.') > urwid.__version__.split('.'):
print('ERROR: Python urwid library TOO OLD - Version %s or later is required' % required)
sys.exit(1)
def checkXClipboard():
import_ask_install('Xlib', 'xlib', 'WARNING: Python-Xlib not installed, clipboard integration will be unavailable')
def checkTermuxAPI():
global HAS_TERMUX_API
HAS_TERMUX_API = not not import_ask_install('termux', 'termux-api', 'WARNING: Python Termux-API not installed, clipboard integration will be unavailable')
# TODO: Verify termux API Android app + package are both installed
def is_wsl():
import platform
return 'microsoft-standard' in platform.uname().release
def checkWSLClipboard():
import wslclipboard
native_python_is_stub = wslclipboard.native_python_is_stub()
if native_python_is_stub is None:
# No python.exe, not even the stub to install it
print('WSL detected, but native Python unavailable - clipboard implementation will be limited. Install native python and add to path to enable advanced clipboard support.\nPress enter to continue...')
input()
if native_python_is_stub is True:
print('WSL detected, but native Python not installed - clipboard implementation will be limited.')
print('If Python is already installed via the Windows store and you are still seeing this message,')
print('open Settings ->...-> "App Execution Aliases" and try cycling the python.exe alias off and on.')
answer = None
while answer not in ('y', 'n'):
answer = input('Open Windows Store to install native Python for advanced clipboard integration? (y/n) ').lower()
if answer == 'y':
wslclipboard.attempt_install_winstore_python()