#! /usr/bin/python
"""
Quick install for my apps.
"""
import sys
import os
from os import system, path
import shutil
import subprocess
MODE_INSTALL = 1
MODE_UPGRADE = 2
MODE_UNINSTALL = 3
IMODE_USER = 1
IMODE_DEVELOPER = 2
DIM = "\033[2m"
BACK_BLUE = "\033[44m"
BACK_BRIGHT_BLACK = "\033[100m"
FORE_BRIGHT_WHITE = "\033[1;37m"
BACK_BRIGHT_WHITE = "\033[107m"
FORE_BRIGHT_RED = "\033[1;31m"
FORE_BLACK = "\033[30m"
RESET = "\033[0m"
BOX = RESET + BACK_BRIGHT_WHITE + FORE_BLACK
my_version = "0.0.0.13"
console_width = max( 10, shutil.get_terminal_size()[0] - 1 )
class UserExitError( Exception ):
pass
class BadConfigError( Exception ):
pass
def main() -> None:
"""
Exit codes: 0 = good, 1 = user error, 2 = program error
:return:
"""
code = 2
try:
code = __main()
except KeyboardInterrupt:
code = 1
except:
raise
finally:
__print( RESET + "EXIT CODE {}".format( code ) )
exit( code )
def __main() -> int:
from_bitbucket, from_pypi, get_pip, python_interpreter = __query_command_line()
for n in range( 100 ):
print()
if get_pip:
__install_pip( python_interpreter )
return 0
__print( "************************************************************************" )
__print( "* THIS SCRIPT WILL INSTALL ALL BITBUCKET.MJR129.TOOLS AND DEPENDENCIES *" )
__print( "************************************************************************" )
__query_runtime_version()
__query_unicode_support()
__query_colour_support()
python_interpreter = __query_python_interpreter( python_interpreter )
__check_python_interpreter( python_interpreter )
__print_todo_list( from_bitbucket, from_pypi )
mode = __query_installation_mode()
imode = __query_install_mode( mode )
if imode == IMODE_USER:
from_pypi.extend( from_bitbucket )
from_bitbucket = []
if imode == IMODE_DEVELOPER:
user_name = __query_user_name()
dir_name = __query_target_directory( mode )
else:
user_name = None
dir_name = None
__query_summary( dir_name, from_bitbucket, from_pypi, user_name, mode, imode )
__install_git( from_pypi, mode, python_interpreter )
__install_bitbucket( dir_name, from_bitbucket, mode, python_interpreter, user_name )
return 0
def __install_git( from_pypi, mode, python_interpreter ):
if not from_pypi:
return
__print_top()
__print_line( "CONFIRM PIP COMMANDS, ENTER 芦Y禄ES (DEFAULT) | 芦A禄LWAYS | 芦S禄KIP | 芦C禄ANCEL" )
__print_bottom()
for VARIABLE in from_pypi:
__print_top()
__print_line( "INSTALL 芦{}禄 VIA PIP".format( VARIABLE ) )
if mode == MODE_INSTALL:
__run( "USR", "sudo {} -m pip install {}".format( python_interpreter, VARIABLE ) )
elif mode == MODE_UPGRADE:
__run( "US2", "sudo {} -m pip install {} --upgrade --force-reinstall".format( python_interpreter, VARIABLE ) )
elif mode == MODE_UNINSTALL:
__run( "US3", "sudo {} -m pip uninstall {}".format( python_interpreter, VARIABLE ) )
__print_bottom()
def __install_bitbucket( dir_name, from_bitbucket, mode, python_interpreter, user_name ):
if not from_bitbucket:
return
__print_top()
__print_line( "CONFIRM GIT COMMANDS, ENTER 芦Y禄ES (DEFAULT) | 芦A禄LWAYS | 芦S禄KIP | 芦C禄ANCEL" )
__print_bottom()
for VARIABLE in from_bitbucket:
target_dir = path.join( dir_name, VARIABLE )
if mode == MODE_INSTALL:
pass
elif mode == MODE_UPGRADE:
__print_top()
__print_line( "REMOVE THE PREVIOUS INSTALL OF 芦{}禄".format( VARIABLE ) )
__run( "REM", "sudo {} -m pip uninstall {}".format( python_interpreter, VARIABLE ) )
shutil.rmtree( target_dir )
elif mode == MODE_UNINSTALL:
__print_top()
__print_line( "REMOVE THE PREVIOUS INSTALL OF 芦{}禄".format( VARIABLE ) )
__run( "REM", "sudo {} -m pip uninstall {}".format( python_interpreter, VARIABLE ) )
shutil.rmtree( target_dir )
continue
__print_top()
__print_line( "DOWNLOAD 芦{}禄 FROM BITBUCKET".format( VARIABLE ) )
os.chdir( dir_name )
if not __run( "CLO", "git clone https://{}@bitbucket.org/mjr129/{}.git".format( user_name, VARIABLE ) ):
continue
os.chdir( target_dir )
__print_top()
__print_line( "INSTALL 芦{}禄 FROM LOCAL FOLDER".format( VARIABLE ) )
__run( "DEV", "sudo {} -m pip install -e .".format( python_interpreter ) )
__print_bottom()
def __install_pip( python_interpreter ):
print( "OBTAINING PIP..." )
os.system( "wget https://bootstrap.pypa.io/get-pip.py" )
os.system( "{} get-pip.py".format( python_interpreter ) )
os.remove( "get-pip.py" )
def __query_command_line():
from_pypi = ["setuptools", "PyQt5", "py2neo", "sip", "six", "typing-inspect", "numpy", "neo4j-driver", "jsonpickle", "ete3", "bitarray", "uniprot", "typing", "py-flags", "colorama", "keyring", "biopython"]
from_bitbucket = ["stringcoercion", "neocommand", "intermake", "mhelper", "editorium", "bio42", "progressivecsv", "cluster_searcher"]
python_interpreter = "python3"
cmd_mode = 0
get_pip = False
for arg in sys.argv[1:]: # type:str
if arg == "--only":
from_pypi.clear()
from_bitbucket.clear()
elif arg == "--pip" or arg == "--pypi":
cmd_mode = 1
elif arg == "--git" or arg == "--bitbucket":
cmd_mode = 2
elif arg == "--python":
cmd_mode = 3
elif arg == "--get-pip":
get_pip = True
elif cmd_mode == 1:
from_pypi.append( arg[1:] )
elif cmd_mode == 2:
from_bitbucket.append( arg[1:] )
elif cmd_mode == 3:
python_interpreter = arg
cmd_mode = 0
else:
__print( "BAD COMMAND LINE" )
raise BadConfigError()
return from_bitbucket, from_pypi, get_pip, python_interpreter
def __query_runtime_version():
__print( "CHECKING PYTHON VERSION..." )
version = sys.version_info
if version[0] != 3 and version[1] < 6:
__print( "CHECK FAILED. YOU ARE USING PYTHON VERSION {}.{}, BUT THIS APPLICATION REQUIRES VERSION 3.6.".format( version[0], version[1] ) )
raise BadConfigError()
__print( "* NO ERROR " + ".".join( str( x ) for x in version ) )
def __query_unicode_support():
__print( "CHECKING UNICODE SUPPORT..." )
try:
print( "馃榿\r \r", end = "" )
__print( "* NO ERROR 馃榿" )
except UnicodeEncodeError:
__print( "CHECK FAILED. YOU ARE NOT USING A UNICODE TERMINAL OR UNICODE IS MISCONFIGURED." )
__print( "PLEASE SWITCH TO A UNICODE TERMINAL OR ENABLE UNICODE." )
__print( "PROGRAM EXITED - NOT SUPPORTED" )
raise BadConfigError()
def __query_colour_support():
__print( "CHECKING COLOUR SUPPORT..." )
colorterm = os.environ.get( "COLORTERM" )
if colorterm is not None:
__print( "* \033[38;2;" + str( 0 ) + ";" + str( 255 ) + ";" + str( 128 ) + "m" + "NO ERROR" + RESET )
else:
__print( "* \033[38;2;" + str( 255 ) + ";" + str( 0 ) + ";" + str( 0 ) + "m" + "24 BIT RED" + RESET )
__print( "* " + "\033[31m" + "SYSTEM RED" + RESET )
__print( "CHECK FAILED: YOU ARE NOT USING A TRUE COLOUR TERMINAL OR TRUE COLOUR IS MISCONFIGURED." )
__print( "PLEASE SWITCH A TRUE COLOUR TERMINAL OR ENABLE 'COLORTERM'." )
__print( "IF YOU ARE USING SSH THIS ASSERTION MAY BE IN ERROR'." )
__print( "** ENTER 'I'GNORE OR 'E'XIT" )
query = input( "QUERY: " ).lower()
if query not in ("i", "ignore"):
__print( "USER EXITED - BAD RES