Ejemplo n.º 1
0
def install_userobjects(source):
    """Installs Grasshopper user objects.

    Parameters
    ----------
    source : str
        Folder where the ghuser files to install are located.

    Returns
    -------
    list
        List of tuples (name, success) indicating whether each of the user objects was successfully installed.
    """
    version = get_version_from_args()

    dstdir = get_grasshopper_userobjects_path(version)
    userobjects = glob.glob(os.path.join(source, '*.ghuser'))

    symlinks_to_remove = []
    symlinks_to_add = []
    for src in userobjects:
        dst = os.path.join(dstdir, os.path.basename(src))
        symlinks_to_remove.append(dst)
        symlinks_to_add.append((src, dst))

    # Remove existing links first
    remove_symlinks(symlinks_to_remove)

    # And the create new ones
    created = create_symlinks(symlinks_to_add)

    return list(zip(symlinks_to_add, created))
Ejemplo n.º 2
0
def uninstall():
    """Uninstalls the Grasshopper components library."""
    results = []

    try:
        version = get_version_from_args()
        grasshopper_library_path = get_grasshopper_library_path(version)

        for f in _remove_old_ghpy_components(grasshopper_library_path):
            results.append(('compas_fab',
                            'Uninstalled old component: {}'.format(f), True))

        dstdir = get_grasshopper_userobjects_path(version)
        srcdir = os.path.dirname(__file__)
        userobjects = glob.glob(os.path.join(srcdir, '*.ghuser'))

        symlinks = []
        for src in userobjects:
            dst = os.path.join(dstdir, os.path.basename(src))
            symlinks.append(dst)

        remove_symlinks(symlinks)

        results.append(
            ('compas_fab',
             'Uninstalled {} GH User Objects'.format(len(userobjects)), True))
    except PermissionError:
        raise Exception(
            'Please close first all instances of Rhino and then rerun the command'
        )

    return results
Ejemplo n.º 3
0
def uninstall_userobjects(userobjects):
    """
    Uninstalls Grasshopper user objects.

    Parameters
    ----------
    userobjects : list of str
        List of user object names to uninstall, eg. ``['Compas_Info.ghuser']``

    Returns
    -------
    list
        List of tuples (name, success) indicating whether each of the user objects was successfully removed.
    """
    version = get_version_from_args()
    dstdir = get_grasshopper_userobjects_path(version)

    symlinks = []
    for src in userobjects:
        dst = os.path.join(dstdir, os.path.basename(src))
        symlinks.append(dst)

    removed = remove_symlinks(symlinks)

    return list(zip(symlinks, removed))
Ejemplo n.º 4
0
def uninstall_userobjects(userobjects):
    """
    Uninstalls Grasshopper user objects.

    Parameters
    ----------
    userobjects : list of str
        List of user object names to uninstall, eg. ``['Compas_Info.ghuser']``

    Returns
    -------
    list
        List of tuples (name, success) indicating whether each of the user objects was successfully removed.
    """
    version = get_version_from_args()
    dstdir = get_grasshopper_userobjects_path(version)

    userobjects = []
    for name in os.listdir(dstdir):
        if name.lower().startswith('compas'):
            userobjects.append(name)

    symlinks = []
    for obj in userobjects:
        path = os.path.join(dstdir, os.path.basename(obj))
        symlinks.append(path)

    removed = remove_symlinks(symlinks)

    return list(zip(symlinks, removed))
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
def install_plugin(plugin, version=None):
    """Install a Rhino Python Command Plugin.

    Parameters
    ----------
    plugin : str
        The path to the plugin folder.
    version : {'5.0', '6.0', '7.0', '8.0'}, optional
        The version of Rhino for which the plugin should be installed.
        Default is ``'6.0'``.

    Notes
    -----
    The version info is only relevant for Rhino on Windows.

    The function creates an *editable install*, which means that the plugin
    folder is *symlinked* into the correct location so Rhino can find and load it.
    The contents of the source folder can still be edited and next time you start
    Rhino those canges will be reflected in the loaded plugin.

    Examples
    --------
    Assuming the plugin folder is at the following location on your computer

    .. code-block:: none

        ~/Code/compas_xxx/ui/Rhino/XXX

    and contains at least a "dev" folder with a ``__plugin__.py`` file with

    * the GUID of the tool: ``id={...}``, and
    * the name of the tool: ``title="..."``,

    it can be installed with the following command,

    .. code-block:: bash

        cd ~/Code/compas_xxx
        python -m compas_rhino.install_plugin ui/Rhino/XXX

    or the following, if the plugin should be installed for Rhino 7.

    .. code-block:: bash

        cd ~/Code/compas_xxx
        python -m compas_rhino.install_plugin -v 7.0 ui/Rhino/XXX

    """
    if not os.path.isdir(plugin):
        raise Exception('Cannot find the plugin: {}'.format(plugin))

    version = compas_rhino._check_rhino_version(version)

    plugin_dir = os.path.abspath(plugin)

    # Clean up the plugin directory
    # by removing all broken symlinks
    # because ... they're broken!

    symlinks_to_remove = []

    for name in os.listdir(plugin_dir):
        path = os.path.join(plugin_dir, name)
        if os.path.islink(path):
            if not os.path.exists(path):
                symlinks_to_remove.append(dict(name=name, link=path))

    symlinks_removed = []
    symlinks_unremoved = []

    if symlinks_to_remove:
        symlinks = [link['link'] for link in symlinks_to_remove]
        removal_results = remove_symlinks(symlinks)
        for uninstall_data, success in zip(symlinks_to_remove, removal_results):
            if success:
                symlinks_removed.append(uninstall_data['name'])
            else:
                symlinks_unremoved.append(uninstall_data['name'])

    if symlinks_removed:
        print('\nThe following package symlinks are broken and were removed:\n')
        for name in symlinks_removed:
            print('   {}'.format(name.ljust(20)))

    if symlinks_unremoved:
        print('\nThe following package symlinks are broken but could not be removed:\n')
        for name in symlinks_unremoved:
            print('   {}'.format(name.ljust(20)))

    # -----------------------------
    # proceed with the installation
    # -----------------------------

    plugin_path, plugin_name = os.path.split(plugin_dir)
    plugin_path = plugin_path or os.getcwd()

    plugin_dev = os.path.join(plugin_dir, 'dev')

    if not os.path.isdir(plugin_dev):
        raise Exception('The plugin does not contain a dev folder.')

    plugin_info = os.path.join(plugin_dev, '__plugin__.py')

    if not os.path.isfile(plugin_info):
        raise Exception('The plugin does not contain plugin info.')

    __plugin__ = imp.load_source('__plugin__', plugin_info)

    if not __plugin__.id:
        raise Exception('Plugin id is not set.')

    if not __plugin__.title:
        raise Exception('Plugin title is not set.')

    plugin_fullname = "{}{}".format(__plugin__.title, __plugin__.id)

    python_plugins_path = compas_rhino._get_rhino_pythonplugins_path(version)

    if not os.path.exists(python_plugins_path):
        os.mkdir(python_plugins_path)

    source = plugin_dir
    destination = os.path.join(python_plugins_path, plugin_fullname)

    print('\nInstalling PlugIn {} to Rhino PythonPlugIns.'.format(plugin_name))

    remove_symlinks([destination])
    create_symlinks([(source, destination)])

    print('\nPlugIn {} Installed.'.format(plugin_name))
    print('\nRestart Rhino and open the Python editor at least once to make it available.')
Ejemplo n.º 7
0
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)
    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)
Ejemplo n.º 8
0
def install_plugin(plugin, version=None):
    """Install a Rhino Python Command Plugin.

    Parameters
    ----------
    plugin : str
        The path to the plugin folder.
    version : {'5.0', '6.0', '7.0'}, optional
        The version of Rhino for which the plugin should be installed.
        Default is ``'6.0'``.

    Notes
    -----
    The version info is only relevant for Rhino on Windows.

    The function creates an *editable install*, which means that the plugin
    folder is *symlinked* into the correct location so Rhino can find and load it.
    The contents of the source folder can still be edited and next time you start
    Rhino those canges will be reflected in the loaded plugin.

    Examples
    --------
    Assuming the plugin folder is at the following location on your computer

    .. code-block:: none

        ~/Code/compas_xxx/ui/Rhino/XXX

    and contains at least a "dev" folder with a ``__plugin__.py`` file with

    * the GUID of the tool: ``id={...}``, and
    * the name of the tool: ``title="..."``,

    it can be installed with the following command,

    .. code-block:: bash

        cd ~/Code/compas_xxx
        python -m compas_rhino.install_plugin ui/Rhino/XXX

    or the following, if the plugin should be installed for Rhino 7.

    .. code-block:: bash

        cd ~/Code/compas_xxx
        python -m compas_rhino.install_plugin -v 7.0 ui/Rhino/XXX

    """
    if version not in ('5.0', '6.0', '7.0'):
        version = '6.0'

    if not os.path.isdir(plugin):
        raise Exception('Cannot find the plugin: {}'.format(plugin))

    plugin_dir = os.path.abspath(plugin)

    plugin_path, plugin_name = os.path.split(plugin_dir)
    if not plugin_path:
        plugin_path = os.getcwd()

    plugin_dev = os.path.join(plugin_dir, 'dev')

    if not os.path.isdir(plugin_dev):
        raise Exception('The plugin does not contain a dev folder.')

    plugin_info = os.path.join(plugin_dev, '__plugin__.py')

    if not os.path.isfile(plugin_info):
        raise Exception('The plugin does not contain plugin info.')

    __plugin__ = imp.load_source('__plugin__', plugin_info)

    if not __plugin__.id:
        raise Exception('Plugin id is not set.')

    if not __plugin__.title:
        raise Exception('Plugin title is not set.')

    plugin_fullname = "{}{}".format(__plugin__.title, __plugin__.id)

    python_plugins_path = compas_rhino._get_python_plugins_path(version)

    if not os.path.exists(python_plugins_path):
        os.mkdir(python_plugins_path)

    source = plugin_dir
    destination = os.path.join(python_plugins_path, plugin_fullname)

    print('Installing PlugIn {} to Rhino PythonPlugIns.'.format(plugin_name))

    remove_symlinks([destination])
    create_symlinks([(source, destination)])

    print()
    print('PlugIn {} Installed.'.format(plugin_name))
    print()
    print(
        'Restart Rhino and open the Python editor at least once to make it available.'
    )