########################################################################### # # Copyright (c) 2019-2019 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file update_paths.py # # @date 18-Jun-2020 # @author Peter Lucia # # @brief This script sets up squish qt to use the testsuites virtualenv # ############################################################################ from configparser import ConfigParser, Error as ConfigParserError import os import shutil import sys import argparse from argparse import RawTextHelpFormatter def update_paths(venv_dir, backup_path, squish_qt_install_dir="/opt/squishqt"): # 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.") filepath = os.path.join(squish_qt_install_dir, "etc/paths.ini") # 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\"" } } 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:{0}:@(SQUISH_PREFIX)/ruby/lib:@(SQUISH_PREFIX)/tcl/lib\"".format( os.path.join(venv_dir, "bin")) } } 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()