setup script 2.1.0: added uninstallation option, other minor enhancements

This commit is contained in:
k4yt3x 2020-05-16 07:04:44 -04:00
parent 0b67ec879d
commit 192c6ef38b

View File

@ -4,7 +4,7 @@
Name: Video2X Setup Script Name: Video2X Setup Script
Creator: K4YT3X Creator: K4YT3X
Date Created: November 28, 2018 Date Created: November 28, 2018
Last Modified: May 12, 2020 Last Modified: May 16, 2020
Editor: BrianPetkovsek Editor: BrianPetkovsek
Editor: SAT3LL Editor: SAT3LL
@ -27,6 +27,7 @@ import argparse
import contextlib import contextlib
import os import os
import pathlib import pathlib
import platform
import re import re
import shutil import shutil
import subprocess import subprocess
@ -38,12 +39,11 @@ import traceback
import urllib import urllib
import zipfile import zipfile
# Requests doesn't come with windows, therefore # Some libraries don't come with default Python installation.
# it will be installed as a dependency and imported # Therefore, they will be installed during the Python dependency
# later in the script. # installation step and imported later in the script.
# import requests
VERSION = '2.0.1' SETUP_VERSION = '2.1.0'
# global static variables # global static variables
LOCALAPPDATA = pathlib.Path(os.getenv('localappdata')) LOCALAPPDATA = pathlib.Path(os.getenv('localappdata'))
@ -59,20 +59,17 @@ DRIVER_OPTIONS = ['all',
def parse_arguments(): def parse_arguments():
"""Processes CLI arguments """ parse command line arguments
""" """
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--driver', help='driver to download and configure', choices=DRIVER_OPTIONS, default='all')
# video options parser.add_argument('-u', '--uninstall', help='uninstall Video2X dependencies from default location', action='store_true')
general_options = parser.add_argument_group('General Options')
general_options.add_argument('-d', '--driver', help='driver to download and configure', action='store', choices=DRIVER_OPTIONS, default='all')
# parse arguments # parse arguments
return parser.parse_args() return parser.parse_args()
class Video2xSetup: class Video2xSetup:
""" Install dependencies for video2x video enlarger """ install dependencies for video2x video enlarger
This library is meant to be executed as a stand-alone This library is meant to be executed as a stand-alone
script. All files will be installed under %LOCALAPPDATA%\\video2x. script. All files will be installed under %LOCALAPPDATA%\\video2x.
@ -330,7 +327,9 @@ def pip_install(file):
return subprocess.run([sys.executable, '-m', 'pip', 'install', '-U', '-r', file]).returncode return subprocess.run([sys.executable, '-m', 'pip', 'install', '-U', '-r', file]).returncode
if __name__ == '__main__': if __name__ != '__main__':
raise ImportError('video2x_setup cannot be imported')
try: try:
# set default exit code # set default exit code
EXIT_CODE = 0 EXIT_CODE = 0
@ -339,7 +338,7 @@ if __name__ == '__main__':
start_time = time.time() start_time = time.time()
# check platform # check platform
if sys.platform != 'win32': if platform.system() != 'Windows':
print('This script is currently only compatible with Windows') print('This script is currently only compatible with Windows')
EXIT_CODE = 1 EXIT_CODE = 1
sys.exit(1) sys.exit(1)
@ -347,8 +346,22 @@ if __name__ == '__main__':
# parse command line arguments # parse command line arguments
args = parse_arguments() args = parse_arguments()
print('Video2X Setup Script') print('Video2X Setup Script')
print(f'Version: {VERSION}') print(f'Version: {SETUP_VERSION}')
# uninstall video2x dependencies
if args.uninstall:
if input('Are you sure you want to uninstall all Video2X dependencies? [y/N]: ').lower() == 'y':
try:
print(f'Removing: {LOCALAPPDATA / "video2x"}')
shutil.rmtree(LOCALAPPDATA / 'video2x')
print('Successfully uninstalled all dependencies')
except FileNotFoundError:
print(f'Dependency folder does not exist: {LOCALAPPDATA / "video2x"}')
else:
print('Uninstallation aborted')
# run installation
else:
# do not install pip modules if script # do not install pip modules if script
# is packaged in exe format # is packaged in exe format
download_python_modules = True download_python_modules = True
@ -356,12 +369,15 @@ if __name__ == '__main__':
print('\nScript is packaged as exe, skipping pip module download') print('\nScript is packaged as exe, skipping pip module download')
download_python_modules = False download_python_modules = False
# create setup install instance and run installer
setup = Video2xSetup(args.driver, download_python_modules) setup = Video2xSetup(args.driver, download_python_modules)
setup.run() setup.run()
print('\nScript finished successfully') print('\nScript finished successfully')
except SystemExit: # let SystemExit signals pass through
pass except SystemExit as e:
raise e
# if PermissionError is raised # if PermissionError is raised
# user needs to run this with higher privilege # user needs to run this with higher privilege
@ -391,5 +407,11 @@ if __name__ == '__main__':
finally: finally:
print('Script finished') print('Script finished')
print(f'Time taken: {timedelta(seconds=round(time.time() - start_time))}') print(f'Time taken: {timedelta(seconds=round(time.time() - start_time))}')
# if the program is launched as an Windows PE file
# it might be launched from double clicking
# pause the window before it closes automatically
if sys.argv[0].endswith('.exe'):
input('Press [ENTER] to exit script') input('Press [ENTER] to exit script')
sys.exit(EXIT_CODE) sys.exit(EXIT_CODE)