def handle_package_by_name(self, pkg_name, **options): """ Installs a package from its name .. important:: The package name might contains version specs :param pkg_name: The package name :type pkg_name: str :param options: :type options: dict :return: The commandline output as an Types.AttributeString instance :rtype: Types.AttributeString :raise: SetupManagerException """ command_args = self.format_pip_command(pkg_name, **options) output_cmd = self.run_command(' '.join(command_args)) if output_cmd.return_code != 0: error_msg = 'Error while executing pip {0} {1}'.format( ' '.join(command_args), output_cmd) logger.error(error_msg) self.error(self.SetupManagerException, error_msg) else: print(output_cmd) return output_cmd
def __uninstall_operation(self, data_to_remove): """ Uninstall Files given in data_to_remove dictionary. :param data_to_remove: Data to remove :type data_to_remove: dict """ file_name = data_to_remove.get('name') destination = format_configuration_path( data_to_remove.get('destination')) try: # Remove installed file if path.isfile(destination): logger.info("Removing file '{0}'".format(file_name)) unlink(destination) elif path.isdir(destination): logger.info("Removing folder '{0}'".format(destination)) rmtree(destination) # Remove shortcut if any shortcut_list = data_to_remove.get('shortcut') if shortcut_list: for shortcut_args in shortcut_list: sc_name = shortcut_args.get('name') self.remove_shortcut(sc_name) except Exception as sc_exception: error_msg = "Uninstall error of {0} ! ({1})".format( file_name, sc_exception) logger.error(error_msg) self.error('SetupManagerException', error_msg)
def main(args): """ Main Entry point :param args: Command line arguments :type args: tuple :return: Execution status :rtype: int """ try: parser = ACSSetup.build_parser() setup = ACSSetup(parser.parse_args(args)) if not setup.is_os_supported: raise setup_exception(ManagerBase.ValidationException, 'This OS is not supported.') if not setup.is_python_supported: raise setup_exception(ManagerBase.ValidationException, 'Please install a supported python version.') setup.run() return_code = 0 except Exception as installation_exception: logger.error(installation_exception) return_code = 1 return return_code
def install_packages(self): """ Install the list of packages """ import pkg_resources installed_package_list = self.installed_packages.keys() for pkg in self.packages: package_name = pkg.get('name') package_version = pkg.get('version') is_already_installed = package_name in installed_package_list if is_already_installed: versions_parser = pkg_resources.parse_version installed_package_version = self.installed_packages[ package_name] package_version = installed_package_version if package_version == '' else package_version if versions_parser(package_version) > versions_parser( installed_package_version): # Reset the flag to force update/installation of the package is_already_installed = False if is_already_installed: print("The package '{0} ({1})' is already installed !".format( package_name, package_version)) else: # extracting meta_data specific to the OS meta_data = pkg.get( 'windows' if self.session.os == OsSupport.windows else 'linux', {}) # Execute pre installation scripts pre_processing_instructions = meta_data.get("pre_install") post_processing_instructions = meta_data.get("post_install") self.pre_processing(pre_processing_instructions) # Install python package out = self.handle_package_by_name(package_name, upgrade=True) if out.failed: logger.error( u'Error while installing package : {0} ({1})'.format( package_name, out)) else: is_already_up2date = bool([ hint for hint in ('requirement already up-to-date', 'requirement already satisfied') if hint in out.lower() ]) if not is_already_up2date: self.post_processing(post_processing_instructions)
def uninstall_packages(self): """ Uninstall the list of packages """ installed_package_list = self.installed_packages.keys() for pkg in self.packages: package_name = pkg.get('name') if package_name in installed_package_list: # extracting meta_data specific to the OS meta_data = pkg.get( 'windows' if self.session.os == OsSupport.windows else 'linux', {}) # Execute pre uninstalling scripts pre_processing_instructions = meta_data.get("pre_uninstall") post_processing_instructions = meta_data.get("post_uninstall") self.pre_processing(pre_processing_instructions) # Uninstall python package command_args = self.format_pip_command(package_name) out = self.run_command(' '.join(command_args)) print(out) is_ignored_errors = bool([ hint for hint in ['not installed', 'access is denied'] if hint in out.lower() ]) if out.failed and not is_ignored_errors: logger.error( u'Error while uninstalling package : {0} ({1})'.format( package_name, out)) else: if 'not installed' not in out.lower(): self.post_processing(post_processing_instructions) else: print("The package '{0}' is already uninstalled !".format( package_name))
def run(self): """ Runs the manager """ if self.data: super(FsManager, self).run() install = self.execute == 'install' for f in self.data: name = f.get('name') destination = format_configuration_path(f.get('destination')) if not destination: print("'destination' for {0} is not set !".format(name)) continue if not install: self.__uninstall_operation(f) continue artifact_location = f.get('artifacts_location') if not artifact_location: artifact_location = ManagerBase.DEFAULT_REPO_BASE source = urljoin(artifact_location, f.get('source')) if not path.isdir(destination): # Create destination folder if not exists makedirs(destination) destination = path.join(destination, name) local_artifact = self.download_file( source, local_destination=destination) if local_artifact: if self.file_extensions(local_artifact, ('.zip', '.tar', '.tar.gz')): self.unzip(local_artifact, path.dirname(destination)) unlink(local_artifact) else: self.move(local_artifact, destination) if self.session.os == OsSupport.linux: self.run_command('chown -R {1}:{1} {0}'.format( path.dirname(destination), CURRENT_USER), env=environ, cwd="", shell="/bin/bash") shortcut_list = f.get('shortcut') if shortcut_list: for shortcut_args in shortcut_list: (sc_name, sc_type, sc_path, description, icon_path, extra_args) = ( shortcut_args.get('name'), shortcut_args.get('type'), self.format_configuration_path( shortcut_args.get('path')), shortcut_args.get('description'), self.format_configuration_path( shortcut_args.get('icon_path')), self.format_configuration_path( shortcut_args.get('extra_args'))) try: self.create_desktop_shortcut( sc_name, sc_type, sc_path, description, icon_path, extra_args) except Exception as sc_exception: logger.warning( 'Fail to create shortcut {0} ! ({1})'. format(sc_name, sc_exception)) else: error_msg = "Cannot download artifact {0} to {1}".format( source, destination) logger.error(error_msg) self.error('SetupManagerException', error_msg)
def error_handler(this, callback, args): code = callback(args) if code != 0: msg = 'Error while executing PIP {0}'.format(' '.join(args)) logger.error(msg) this.error(this.SetupManagerException, msg)
def run(self): """ Runs the manager """ if self.data: super(BinaryManager, self).run() win32 = self.session.os == OsSupport.windows is32 = self.session.system.is32bit for b in self.data: binary_dirname = b.split('@')[0].strip() binary_desc = path.abspath( path.join(self.CONFIG_DIR, b.strip() + '.json')) binary = Loader.load(binary_desc) is_binary_installed, binary_installed_path = self.is_executable_present( binary) artifact_location = binary.get('artifacts_location') bin_data = binary.get('windows') if win32 else binary.get( 'linux') if not bin_data: logger.warning( 'Missing configuration file (.json) for binary ' '{0} on {1}'.format(binary_dirname, self.session.os)) continue # Execute pre installation scripts pre_processing_instructions = bin_data.get("pre_{0}".format( self.execute)) post_processing_instructions = bin_data.get("post_{0}".format( self.execute)) # handling Ubuntu direct command form system packages manager. (Apt-get) formatted_cmd = bin_data.get(self.execute) if formatted_cmd is None: logger.info( 'No {0} command provided for binary {1}'.format( self.execute, binary_dirname)) continue if not win32 and formatted_cmd and 'apt-get' in formatted_cmd: if is_binary_installed: info_msg = 'Binary {0} is already {1}ed !'.format( binary_dirname, self.execute) logger.info(info_msg) print(info_msg) continue self.pre_processing(pre_processing_instructions) output_cmd = self.run_command(formatted_cmd) if output_cmd.return_code != 0: error_msg = 'Error while executing {0}'.format( formatted_cmd) self.error('SetupManagerException', error_msg) else: self.post_processing(post_processing_instructions) continue artifact_location = bin_data.get('artifacts_location', artifact_location) if not artifact_location: artifact_location = self.DEFAULT_REPO_BASE # Handling OS arch if is32: bin_arch = bin_data.get('32') else: bin_arch = bin_data.get('64') if not bin_arch: logger.info( 'No 64bits version found for binary {0}'.format( binary_dirname)) logger.info('Looking for 32bits ...') bin_arch = bin_data.get('32') if not bin_arch and win32: logger.warning( 'None artifact found for binary {0} at all!! ' 'Check your Configuration!'.format(binary_dirname)) continue # Handling Python binaries, str.format(), will NOT replace anything if not expected # see Configuration file (.json) bin_arch = bin_arch.format( pyversion=self.session.python.version) binary_name = bin_arch.split('/')[-1:][0] binary['name'] = binary_dirname print("{0}ing binary {1}".format(self.execute.capitalize(), binary_dirname)) if ((self.execute == "install" and is_binary_installed) or (self.execute == "uninstall" and not is_binary_installed)): info_msg = 'Binary {0} is already {1}ed !'.format( binary_dirname, self.execute) logger.info(info_msg) print(info_msg) continue if (self.execute == "uninstall" and is_binary_installed and path.exists(ur'{0}'.format(binary_installed_path))): local_artifact = binary_installed_path else: local_artifact = os.path.join(self.cache, binary_dirname, binary_name) if not os.path.exists(local_artifact): artifact_uri = urljoin(artifact_location, bin_arch) opts = {} # Internal Artifacts location (ACS Artifactory repository) else external (PFT, ...) if not artifact_location.strip() == self.DEFAULT_REPO_BASE: opts['local_location'] = '{0}/{1}'.format( binary_dirname, artifact_uri[len(artifact_location):]) local_artifact = self.download_file(artifact_uri, **opts) if local_artifact: if local_artifact.endswith('.zip'): rootdir, name = path.split(local_artifact) self.unzip(local_artifact, rootdir) zip_info = bin_data.get('zip') if zip_info: local_artifact = path.join(rootdir, zip_info.get('bin')) else: local_artifact = local_artifact.replace( '.zip', '.exe') # Executing pre-processing actions self.pre_processing(pre_processing_instructions) execute_cmd = '' if win32: execute_cmd += 'start /w ' execute_cmd += formatted_cmd.format(bin=r'%s' % local_artifact) logger.info('Executing {1} {0} ...'.format( execute_cmd, self.execute.upper())) output_cmd = self.run_command(execute_cmd) if output_cmd.return_code != 0: error_msg = 'Error while executing {0}'.format( execute_cmd) self.error('SetupManagerException', error_msg) else: # Executing post-processing actions self.post_processing(post_processing_instructions) else: error_msg = "Cannot install binary {0} !".format( binary_name) logger.error(error_msg) self.error('SetupManagerException', error_msg)