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,25 +327,41 @@ 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__':
try: raise ImportError('video2x_setup cannot be imported')
# set default exit code
EXIT_CODE = 0
# get start time try:
start_time = time.time() # set default exit code
EXIT_CODE = 0
# check platform # get start time
if sys.platform != 'win32': start_time = time.time()
print('This script is currently only compatible with Windows')
EXIT_CODE = 1
sys.exit(1)
# parse command line arguments # check platform
args = parse_arguments() if platform.system() != 'Windows':
print('Video2X Setup Script') print('This script is currently only compatible with Windows')
print(f'Version: {VERSION}') EXIT_CODE = 1
sys.exit(1)
# parse command line arguments
args = parse_arguments()
print('Video2X Setup Script')
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,40 +369,49 @@ 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
except PermissionError: except PermissionError:
traceback.print_exc() traceback.print_exc()
print('You might have insufficient privilege for this script to run') print('You might have insufficient privilege for this script to run')
print('Try running this script with Administrator privileges') print('Try running this script with Administrator privileges')
EXIT_CODE = 1 EXIT_CODE = 1
# for any exception in the script # for any exception in the script
except Exception:
traceback.print_exc()
print('An error has occurred')
print('Video2X Automatic Setup has failed')
# in case of a failure, try cleaning up temp files
try:
setup._cleanup()
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
print('An error has occurred') print('An error occurred while trying to cleanup files')
print('Video2X Automatic Setup has failed')
# in case of a failure, try cleaning up temp files EXIT_CODE = 1
try:
setup._cleanup()
except Exception:
traceback.print_exc()
print('An error occurred while trying to cleanup files')
EXIT_CODE = 1 # regardless if script finishes successfully or not
# print script execution summary
finally:
print('Script finished')
print(f'Time taken: {timedelta(seconds=round(time.time() - start_time))}')
# regardless if script finishes successfully or not # if the program is launched as an Windows PE file
# print script execution summary # it might be launched from double clicking
finally: # pause the window before it closes automatically
print('Script finished') if sys.argv[0].endswith('.exe'):
print(f'Time taken: {timedelta(seconds=round(time.time() - start_time))}')
input('Press [ENTER] to exit script') input('Press [ENTER] to exit script')
sys.exit(EXIT_CODE)
sys.exit(EXIT_CODE)