Index: tools/update_paths.py =================================================================== diff -u -re1408993175c73512ea26437708e51c62fa43af2 -r361b58c60372d8758dc43b153b92e6f1dd59fa1a --- tools/update_paths.py (.../update_paths.py) (revision e1408993175c73512ea26437708e51c62fa43af2) +++ tools/update_paths.py (.../update_paths.py) (revision 361b58c60372d8758dc43b153b92e6f1dd59fa1a) @@ -13,47 +13,137 @@ # @brief This script sets up squish qt to use the testsuites virtualenv # ############################################################################ -from configparser import ConfigParser +from configparser import ConfigParser, Error as ConfigParserError import os import shutil +import sys +import argparse +from argparse import RawTextHelpFormatter -SQUISH_QT_INSTALL_DIR="/opt/squishqt" # this script must be run as root if squishqt is installed by root -filepath = os.path.join(SQUISH_QT_INSTALL_DIR, "etc/paths.ini") -current_file_dir = os.path.abspath(os.path.join(os.path.dirname(__file__))) -i = 0 -while os.path.exists(os.path.join(current_file_dir, "paths.ini.BAK{0}".format(i))): - i += 1 -backup_path = os.path.join(current_file_dir, "paths.ini.BAK{0}".format(i)) -parent_dir = os.path.abspath(os.path.join(current_file_dir, "..")) -venv_dir = os.path.join(parent_dir, "venv") +def update_paths(venv_dir, backup_path, squish_qt_install_dir="/opt/squishqt"): -paths_original = { - "Paths": { - "Scripting/PythonHome" : "\"@(SQUISH_PREFIX)/python\"", - "LibraryPath" : "\"@(SQUISH_PREFIX)/lib:@(SQUISH_PREFIX)/perl/lib/perl5/5.22.0/x86_64-linux/CORE:@(SQUISH_PREFIX)/python/lib:@(SQUISH_PREFIX)/ruby/lib:@(SQUISH_PREFIX)/tcl/lib\"" - } -} + # this script must be run as root since squishqt is installed by root + if os.geteuid() != 0: + sys.exit("This script must be run as root.") -paths_new = { - "Paths": { - "Scripting/PythonHome" : "\"{0}\"".format(venv_dir), - "LibraryPath" : "\"@(SQUISH_PREFIX)/lib:@(SQUISH_PREFIX)/perl/lib/perl5/5.22.0/x86_64-linux/CORE:@(SQUISH_PREFIX)/ruby/lib:@(SQUISH_PREFIX)/tcl/lib\"" - } -} + filepath = os.path.join(squish_qt_install_dir, "etc/paths.ini") -try: - shutil.copyfile(filepath, backup_path) - config = ConfigParser() - config.optionxform=str # ensure case sensitive - config.read(filepath) - config["Paths"]["Scripting/PythonHome"] = paths_new["Paths"]["Scripting/PythonHome"] - config["Paths"]["LibraryPath"] = paths_new["Paths"]["LibraryPath"] + # For debugging + paths_original = { + "Paths": { + "Scripting/PythonHome" : "\"@(SQUISH_PREFIX)/python\"", + "LibraryPath" : "\"@(SQUISH_PREFIX)/lib:@(SQUISH_PREFIX)/perl/lib/perl5/5.22.0/x86_64-linux/CORE:@(SQUISH_PREFIX)/python/lib:@(SQUISH_PREFIX)/ruby/lib:@(SQUISH_PREFIX)/tcl/lib\"" + } + } - with open(filepath, 'w') as f: - config.write(f) + paths_new = { + "Paths": { + "Scripting/PythonHome" : "\"{0}\"".format(venv_dir), + "LibraryPath" : "\"@(SQUISH_PREFIX)/lib:@(SQUISH_PREFIX)/perl/lib/perl5/5.22.0/x86_64-linux/CORE:@(SQUISH_PREFIX)/ruby/lib:@(SQUISH_PREFIX)/tcl/lib\"" + } + } - print("Success.") -except Exception as e: - print("Error, are the paths correct?") + try: + + shutil.copyfile(filepath, backup_path) + config = ConfigParser() + config.optionxform=str # ensure case sensitive + config.read(filepath) + config["Paths"]["Scripting/PythonHome"] = paths_new["Paths"]["Scripting/PythonHome"] + config["Paths"]["LibraryPath"] = paths_new["Paths"]["LibraryPath"] + + with open(filepath, 'w') as f: + config.write(f) + + print(f"squishqt installation: {squish_qt_install_dir} ") + print(f"virtualenv: {venv_dir}") + print(f"paths.ini backup location: {backup_path}") + print("Success! Please restart SquishQt to begin using the packages installed in the virtualenv.") + except shutil.SameFileError: + print(f"Error backing up {filepath}. The backup file is the same as the original file. ") + except ConfigParserError: + print(f"Error updating the paths.ini file.") + except Exception: + print("Non-configuration related or backup file related error occurred.") + + +def dir_path(path): + if os.path.isdir(path): + return path + return ValueError + + +def file_path(path): + if not os.path.exists(path): + return path + return ValueError + + +if __name__ == '__main__': + current_file_dir = os.path.abspath(os.path.join(os.path.dirname(__file__))) + i = 0 + while os.path.exists(os.path.join(current_file_dir, "paths.ini.BAK{0}".format(i))): + i += 1 + backup_path = os.path.join(current_file_dir, "paths.ini.BAK{0}".format(i)) + parent_dir = os.path.abspath(os.path.join(current_file_dir, "..")) + venv_dir = os.path.join(parent_dir, "venv") + + help_url="https://kb.froglogic.com/squish/howto/changing-python-installation-used-squish-binary-packages/" + support_email="support@froglogic.com" + diality_compatible_squishqt_versions = [ + "squish-for-qt-6.6-20200708-0922 (Python 3.6.8)", + "squish-for-qt-6.5-20191031-1443 (Python 3.6.4)" + ] + diality_incompatible_squishqt_versions = [ + "squish-for-qt-6.6.0 (Python 3.8)", + "squish-for-qt-6.5.1 (Python 2.7)" + ] + diality_python = "3.6.8" + + parser = argparse.ArgumentParser(description="Updates paths.ini in the squishqt installation directory. \n" \ + "Note that if the squishqt minor python version is different \n" \ + "from the virtualenv and / or system minor python version, squishqt \n" \ + "may not support the change made by this script. \n" \ + "For more information see {0} \n" \ + "Or contact {1}\n\n" \ + "Diality currently uses Python version 3.6.8. To use this script with a virtualenv running \n" \ + "Python {2}, please only run this script with the following compatible squishqt versions: \n\n" + "Compatible SquishQt Versions: \n" \ + "{3}\n\n" \ + "Incompatible SquishQt Versions: \n" \ + "{4}\n".format( + help_url, + support_email, + diality_python, + "\n".join(diality_compatible_squishqt_versions), + "\n".join(diality_incompatible_squishqt_versions)), + formatter_class=RawTextHelpFormatter) + + parser.add_argument("--squishqt", + help="Full path to squishqt installation directory", + required=False, + type=dir_path, + default="/opt/squishqt") + parser.add_argument("--venv", + required=False, + help="Full path to virtual environment directory", + type=dir_path, + default=venv_dir) + parser.add_argument("--backup", + required=False, + help="Full file path for where to backup the existing paths.ini file", + type=file_path, + default=backup_path) + + args = parser.parse_args() + if ValueError not in [getattr(args, arg) for arg in vars(args)]: + update_paths(args.venv, args.backup, args.squishqt) + else: + print("Invalid arguments found.") + print("Please check the squishqt and venv directories exists and the backup file path does not already exist.") + parser.print_help() + + +