datasnakes / renv

Creating virtual environments for R.
MIT License
17 stars 0 forks source link

Make alternate install_scripts() method. #6

Closed grabear closed 6 years ago

grabear commented 6 years ago

This method may not need changing. This method uses the scripts in a folder. The default scripts create the activate/deactivate functionality of the python virtual environment. The default scripts should generally stay the same. Below is the python version of this method:

def install_scripts(self, context, path):
        """
        Install scripts into the created environment from a directory.
        :param context: The information for the environment creation request
                        being processed.
        :param path:    Absolute pathname of a directory containing script.
                        Scripts in the 'common' subdirectory of this directory,
                        and those in the directory named for the platform
                        being run on, are installed in the created environment.
                        Placeholder variables are replaced with environment-
                        specific values.
        """
        binpath = context.bin_path
        plen = len(path)
        for root, dirs, files in os.walk(path):
            if root == path: # at top-level, remove irrelevant dirs
                for d in dirs[:]:
                    if d not in ('common', os.name):
                        dirs.remove(d)
                continue # ignore files in top level
            for f in files:
                srcfile = os.path.join(root, f)
                suffix = root[plen:].split(os.sep)[2:]
                if not suffix:
                    dstdir = binpath
                else:
                    dstdir = os.path.join(binpath, *suffix)
                if not os.path.exists(dstdir):
                    os.makedirs(dstdir)
                dstfile = os.path.join(dstdir, f)
                with open(srcfile, 'rb') as f:
                    data = f.read()
                if not srcfile.endswith('.exe'):
                    try:
                        data = data.decode('utf-8')
                        data = self.replace_variables(data, context)
                        data = data.encode('utf-8')
                    except UnicodeError as e:
                        data = None
                        logger.warning('unable to copy script %r, '
                                       'may be binary: %s', srcfile, e)
                if data is not None:
                    with open(dstfile, 'wb') as f:
                        f.write(data)
                    shutil.copymode(srcfile, dstfile)
grabear commented 6 years ago

This method was originally going to be left as the default for the EnvBuilder class, but it had to be overridden in 7544f43a6e9cda89541fc9dda1e704d463f1e944