def _filter_installed_packages(version, packages): ipylib_path = compas_rhino._get_ironpython_lib_path(version) scripts_path = compas_rhino._get_scripts_path(version) compas_bootstrapper = compas_rhino._get_bootstrapper_path(scripts_path) bootstrapper_data = compas_rhino._get_bootstrapper_data(compas_bootstrapper) # Don't modify the original list if we have one if packages: packages = packages[:] else: packages = bootstrapper_data.get('INSTALLED_PACKAGES', None) # No info, fall back to installable packages list if packages is None: packages = list(itertools.chain.from_iterable(installable_rhino_packages())) # Handle legacy install legacy_bootstrapper = compas_rhino._get_bootstrapper_path(ipylib_path) if os.path.exists(legacy_bootstrapper): bootstrapper_data = compas_rhino._get_bootstrapper_data(legacy_bootstrapper) legacy_packages = bootstrapper_data.get('INSTALLED_PACKAGES', None) if legacy_packages: packages.extend(legacy_packages) return packages
def install(version='5.0', packages=None): """Install COMPAS for Rhino. Parameters ---------- version : {'5.0', '6.0'} The version number of Rhino. packages : list of str List of packages to install or None to use default package list. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.install('5.0') .. code-block:: python $ python -m compas_rhino.install 5.0 """ print('Installing COMPAS packages to Rhino IronPython lib:') ipylib_path = compas_rhino._get_ironpython_lib_path(version) results = [] exit_code = 0 for package in packages: base_path = _get_package_path(importlib.import_module(package)) package_path = os.path.join(base_path, package) symlink_path = os.path.join(ipylib_path, package) if os.path.exists(symlink_path): results.append(( package, 'ERROR: Package "{}" already found in Rhino lib, try uninstalling first' .format(package))) continue try: create_symlink(package_path, symlink_path) results.append((package, 'OK')) except OSError: results.append( (package, 'Cannot create symlink, try to run as administrator.')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status is not 'OK': exit_code = -1 print('\nCompleted.') sys.exit(exit_code)
def RunCommand(is_interactive): # TODO: detect version ipylib_path = compas_rhino._get_ironpython_lib_path("6.0") print(ipylib_path) compas_packages = [ (name, False) for name in os.listdir(ipylib_path) if name.split("_")[0] == "compas" and name != "compas_bootstrapper.py" ] results = rs.CheckListBox(compas_packages, "Select Packages to uninstall") if results: uninstall(results)
def uninstall(version='5.0', packages=None): """Uninstall COMPAS from Rhino. Parameters ---------- version : {'5.0', '6.0'} The version number of Rhino. packages : list of str List of packages to uninstall or None to use default package list. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.uninstall('5.0') .. code-block:: python $ python -m compas_rhino.uninstall 5.0 """ print('Uninstalling COMPAS packages from Rhino IronPython lib:') ipylib_path = compas_rhino._get_ironpython_lib_path(version) results = [] exit_code = 0 for package in packages: symlink_path = os.path.join(ipylib_path, package) if not os.path.exists(symlink_path): continue try: remove_symlink(symlink_path) results.append((package, 'OK')) except OSError: results.append( (package, 'Cannot remove symlink, try to run as administrator.')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status is not 'OK': exit_code = -1 print('\nCompleted.') sys.exit(exit_code)
def install(version=None, packages=None): """Install COMPAS for Rhino. Parameters ---------- version : {'5.0', '6.0', '7.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to install or None to use default package list. Default is ``['compas', 'compas_rhino', 'compas_ghpython']``. Examples -------- .. code-block:: python import compas_rhino compas_rhino.install('6.0') .. code-block:: bash python -m compas_rhino.install -v 6.0 """ if version not in ('5.0', '6.0', '7.0'): version = '6.0' packages = _filter_installable_packages(version, packages) ipylib_path = compas_rhino._get_ironpython_lib_path(version) scripts_path = compas_rhino._get_scripts_path(version) print('Installing COMPAS packages to Rhino {0} scripts folder:'.format( version)) print('Location scripts folder: {}'.format(scripts_path)) print() results = [] symlinks_to_install = [] symlinks_to_uninstall = [] exit_code = 0 for package in packages: package_path = compas_rhino._get_package_path( importlib.import_module(package)) symlink_path = os.path.join(scripts_path, package) symlinks_to_install.append( dict(name=package, source_path=package_path, link=symlink_path)) symlinks_to_uninstall.append(dict(name=package, link=symlink_path)) # Handle legacy install location legacy_path = os.path.join(ipylib_path, package) if os.path.exists(legacy_path): symlinks_to_uninstall.append(dict(name=package, link=legacy_path)) # First uninstall existing copies of packages requested for installation symlinks = [link['link'] for link in symlinks_to_uninstall] uninstall_results = compas._os.remove_symlinks(symlinks) for uninstall_data, success in zip(symlinks_to_uninstall, uninstall_results): if not success: results.append( (uninstall_data['name'], 'ERROR: Cannot remove symlink, try to run as administrator.')) # Handle legacy bootstrapper if not compas_rhino._try_remove_bootstrapper(ipylib_path): results.append(( 'compas_bootstrapper', 'ERROR: Cannot remove legacy compas_bootstrapper, try to run as administrator.' )) # Ready to start installing symlinks = [(link['source_path'], link['link']) for link in symlinks_to_install] install_results = compas._os.create_symlinks(symlinks) for install_data, success in zip(symlinks_to_install, install_results): result = 'OK' if success else 'ERROR: Cannot create symlink, try to run as administrator.' results.append((install_data['name'], result)) if not all(install_results): exit_code = -1 if exit_code == -1: results.append(( 'compas_bootstrapper', 'WARNING: One or more packages failed, will not install bootstrapper, try uninstalling first' )) else: try: _update_bootstrapper(scripts_path, packages) results.append(('compas_bootstrapper', 'OK')) except: # noqa: E722 results.append(( 'compas_bootstrapper', 'ERROR: Could not create compas_bootstrapper to auto-determine Python environment' )) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status != 'OK': exit_code = -1 print('\nCompleted.') if exit_code != 0: sys.exit(exit_code)
def uninstall(version=None, packages=None): """Uninstall COMPAS from Rhino. Parameters ---------- version : {'5.0', '6.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to uninstall. Default is to uninstall all packages installed by the COMPAS installer. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.uninstall('6.0') .. code-block:: python $ python -m compas_rhino.uninstall -v 6.0 """ if version not in ('5.0', '6.0'): version = '6.0' print('Uninstalling COMPAS packages from Rhino {0} IronPython lib:'.format(version)) ipylib_path = compas_rhino._get_ironpython_lib_path(version) compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') bootstrapper_data = compas_rhino.install._get_bootstrapper_data(compas_bootstrapper) if not packages: try: packages = bootstrapper_data.get('INSTALLED_PACKAGES', None) except: # noqa: E722 pass # No info, fall back to installable packages list if packages is None: packages = compas_rhino.install.INSTALLABLE_PACKAGES environment_name = bootstrapper_data.get('ENVIRONMENT_NAME', '') if environment_name: print('The following packages have been detected and will be uninstalled (environment name={})'.format(environment_name)) results = [] symlinks = [] exit_code = 0 for package in packages: symlinks.append(os.path.join(ipylib_path, package)) removal_results = remove_symlinks(symlinks) for package, success in zip(packages, removal_results): result = 'OK' if success else 'ERROR: Cannot remove symlink, try to run as administrator.' results.append((package, result)) if not all(removal_results): exit_code = -1 if exit_code == -1: results.append(('compas_bootstrapper', 'WARNING: One or more packages failed, will not uninstall bootstrapper.')) else: compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') try: if os.path.exists(compas_bootstrapper): os.remove(compas_bootstrapper) results.append(('compas_bootstrapper', 'OK')) except: # noqa: E722 results.append(('compas_bootstrapper', 'ERROR: Could not delete compas_bootstrapper')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status != 'OK': exit_code = -1 print('\nUninstall completed.') sys.exit(exit_code)
def install(version=None, packages=None): """Install COMPAS for Rhino. Parameters ---------- version : {'5.0', '6.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to install or None to use default package list. Default is ``['compas', 'compas_rhino', 'compas_ghpython']``. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.install('6.0') .. code-block:: python $ python -m compas_rhino.install -v 6.0 """ if version not in ('5.0', '6.0'): version = '6.0' if system == 'win32': print('Installing COMPAS packages to Rhino {0} IronPython lib:'.format(version)) elif system == 'darwin': print('Installing COMPAS packages to Rhino IronPython lib.') if not packages: packages = INSTALLABLE_PACKAGES ipylib_path = compas_rhino._get_ironpython_lib_path(version) results = [] exit_code = 0 for package in packages: package_path = _get_package_path(importlib.import_module(package)) symlink_path = os.path.join(ipylib_path, package) if os.path.exists(symlink_path): try: remove_symlink(symlink_path) except OSError: results.append((package, 'ERROR: Cannot remove symlink, try to run as administrator.')) try: create_symlink(package_path, symlink_path) results.append((package, 'OK')) except OSError: results.append((package, 'ERROR: Cannot create symlink, try to run as administrator.')) for _, status in results: if status is not 'OK': exit_code = -1 if exit_code == -1: results.append(('compas_bootstrapper', 'WARNING: One or more packages failed, will not install bootstrapper, try uninstalling first')) else: # Take either the CONDA environment directory or the current Python executable's directory python_directory = os.environ.get('CONDA_PREFIX', None) or os.path.dirname(sys.executable) environment_name = os.environ.get('CONDA_DEFAULT_ENV', '') compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') try: bootstrapper_data = _get_bootstrapper_data(compas_bootstrapper) installed_packages = bootstrapper_data.get('INSTALLED_PACKAGES', []) installed_packages = list(set(installed_packages + list(packages))) with open(compas_bootstrapper, 'w') as f: f.write('ENVIRONMENT_NAME = r"{}"\n'.format(environment_name)) f.write('PYTHON_DIRECTORY = r"{}"\n'.format(python_directory)) f.write('INSTALLED_PACKAGES = {}'.format(repr(installed_packages))) results.append(('compas_bootstrapper', 'OK')) except: results.append(('compas_bootstrapper', 'ERROR: Could not create compas_bootstrapper to auto-determine Python environment')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status is not 'OK': exit_code = -1 print('\nCompleted.') sys.exit(exit_code)
def uninstall(version=None, packages=None): """Uninstall COMPAS from Rhino. Parameters ---------- version : {'5.0', '6.0', '7.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to uninstall. Default is to uninstall all packages installed by the COMPAS installer. Examples -------- .. code-block:: python import compas_rhino compas_rhino.uninstall('6.0') .. code-block:: bash python -m compas_rhino.uninstall -v 6.0 """ if version not in ('5.0', '6.0', '7.0'): version = '6.0' packages = _filter_installed_packages(version, packages) ipylib_path = compas_rhino._get_ironpython_lib_path(version) scripts_path = compas_rhino._get_scripts_path(version) print('Uninstalling COMPAS packages from Rhino {0} scripts folder:'.format(version)) print('Location scripts folder: {}'.format(scripts_path)) print() print('The following packages have been detected and will be uninstalled:') results = [] symlinks_to_uninstall = [] exit_code = 0 for package in packages: symlink_path = os.path.join(scripts_path, package) if os.path.exists(symlink_path): symlinks_to_uninstall.append(dict(name=package, link=symlink_path)) legacy_path = os.path.join(ipylib_path, package) if os.path.exists(legacy_path): symlinks_to_uninstall.append(dict(name=package, link=legacy_path)) symlinks = [link['link'] for link in symlinks_to_uninstall] uninstall_results = compas._os.remove_symlinks(symlinks) for uninstall_data, success in zip(symlinks_to_uninstall, uninstall_results): result = 'OK' if success else 'ERROR: Cannot remove symlink, try to run as administrator.' results.append((uninstall_data['name'], result)) if not all(uninstall_results): exit_code = -1 if exit_code == -1: results.append(('compas_bootstrapper', 'WARNING: One or more packages failed, will not uninstall bootstrapper.')) else: if compas_rhino._try_remove_bootstrapper(scripts_path): results.append(('compas_bootstrapper', 'OK')) else: results.append(('compas_bootstrapper', 'ERROR: Cannot remove compas_bootstrapper, try to run as administrator.')) if not compas_rhino._try_remove_bootstrapper(ipylib_path): results.append(('compas_bootstrapper', 'ERROR: Cannot remove legacy compas_bootstrapper, try to run as administrator.')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status != 'OK': exit_code = -1 print('\nUninstall completed.') if exit_code != 0: sys.exit(exit_code)
def uninstall(version=None, packages=None): """Uninstall COMPAS from Rhino. Parameters ---------- version : {'5.0', '6.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to uninstall. Default is to uninstall all packages installed by the COMPAS installer. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.uninstall('6.0') .. code-block:: python $ python -m compas_rhino.uninstall -v 6.0 """ if version not in ('5.0', '6.0'): version = '6.0' if system == 'win32': print('Uninstalling COMPAS packages from Rhino {0} IronPython lib:'. format(version)) elif system == 'darwin': print('Uninstalling COMPAS packages from Rhino IronPython lib.') ipylib_path = compas_rhino._get_ironpython_lib_path(version) compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') bootstrapper_data = compas_rhino.install._get_bootstrapper_data( compas_bootstrapper) if not packages: try: packages = bootstrapper_data.get('INSTALLED_PACKAGES', None) except: pass # No info, fall back to installable packages list if packages is None: packages = compas_rhino.install.INSTALLABLE_PACKAGES environment_name = bootstrapper_data.get('ENVIRONMENT_NAME', '') if environment_name: print( 'Packages installed from environment: {}'.format(environment_name)) results = [] exit_code = 0 for package in packages: symlink_path = os.path.join(ipylib_path, package) if not (os.path.exists(symlink_path) or os.path.islink(symlink_path)): continue try: remove_symlink(symlink_path) results.append((package, 'OK')) except OSError: results.append( (package, 'ERROR: Cannot remove symlink, try to run as administrator.')) for _, status in results: if status is not 'OK': exit_code = -1 if exit_code == -1: results.append(( 'compas_bootstrapper', 'WARNING: One or more packages failed, will not uninstall bootstrapper.' )) else: compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') try: if os.path.exists(compas_bootstrapper): os.remove(compas_bootstrapper) results.append(('compas_bootstrapper', 'OK')) except: results.append(('compas_bootstrapper', 'ERROR: Could not delete compas_bootstrapper')) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status is not 'OK': exit_code = -1 print('\nCompleted.') sys.exit(exit_code)
def install(version=None, packages=None): """Install COMPAS for Rhino. Parameters ---------- version : {'5.0', '6.0'}, optional The version number of Rhino. Default is ``'6.0'``. packages : list of str, optional List of packages to install or None to use default package list. Default is ``['compas', 'compas_rhino', 'compas_ghpython']``. Examples -------- .. code-block:: python >>> import compas_rhino >>> compas_rhino.install('6.0') .. code-block:: python $ python -m compas_rhino.install -v 6.0 """ if version not in ('5.0', '6.0'): version = '6.0' print('Installing COMPAS packages to Rhino {0} IronPython lib:'.format( version)) ghpython_incompatible = False if system == 'darwin' and version == 5.0: ghpython_incompatible = True if not packages: packages = INSTALLABLE_PACKAGES elif 'compas_ghpython' in packages and ghpython_incompatible: print( 'Skipping installation of compas_ghpython since it\'s not supported for Rhino 5 for Mac' ) if ghpython_incompatible: packages.remove('compas_ghpython') ipylib_path = compas_rhino._get_ironpython_lib_path(version) print('IronPython location: {}'.format(ipylib_path)) print() results = [] symlinks = [] exit_code = 0 for package in packages: package_path = _get_package_path(importlib.import_module(package)) symlink_path = os.path.join(ipylib_path, package) symlinks.append((package_path, symlink_path)) removal_results = remove_symlinks([link[1] for link in symlinks]) for package, success in zip(packages, removal_results): if not success: results.append( (package, 'ERROR: Cannot remove symlink, try to run as administrator.')) create_results = create_symlinks(symlinks) for package, success in zip(packages, create_results): result = 'OK' if success else 'ERROR: Cannot create symlink, try to run as administrator.' results.append((package, result)) if not all(create_results): exit_code = -1 if exit_code == -1: results.append(( 'compas_bootstrapper', 'WARNING: One or more packages failed, will not install bootstrapper, try uninstalling first' )) else: # Take either the CONDA environment directory or the current Python executable's directory python_directory = os.environ.get( 'CONDA_PREFIX', None) or os.path.dirname(sys.executable) environment_name = os.environ.get('CONDA_DEFAULT_ENV', '') compas_bootstrapper = os.path.join(ipylib_path, 'compas_bootstrapper.py') try: bootstrapper_data = _get_bootstrapper_data(compas_bootstrapper) installed_packages = bootstrapper_data.get('INSTALLED_PACKAGES', []) installed_packages = list(set(installed_packages + list(packages))) with open(compas_bootstrapper, 'w') as f: f.write('ENVIRONMENT_NAME = r"{}"\n'.format(environment_name)) f.write('PYTHON_DIRECTORY = r"{}"\n'.format(python_directory)) f.write('INSTALLED_PACKAGES = {}'.format( repr(installed_packages))) results.append(('compas_bootstrapper', 'OK')) except: # noqa: E722 results.append(( 'compas_bootstrapper', 'ERROR: Could not create compas_bootstrapper to auto-determine Python environment' )) for package, status in results: print(' {} {}'.format(package.ljust(20), status)) if status != 'OK': exit_code = -1 print('\nCompleted.') sys.exit(exit_code)