Beispiel #1
0
    def create_vex_new_virtual_env(self, vex_options=None):
        """Create a virtual env in which to install packages
        :returns : venv_name - name of virtual environment.
        :rtype : str
        """

        if vex_options is None:
            vex_options = ''
        if self.name is None:
            venv_template = "MagEnv{}"
            # check if name exists and bump repeatedly until new
            i = 0
            while True:
                self.name = venv_template.format(i)
                if not self.vex_check_venv_exists(self.name, vex_options):  # make env
                    break
                i += 1
        else:
            if self.vex_check_venv_exists(self.name, vex_options):
                # vex -r removes virtual env
                run_in_subprocess("vex {} -r {} true".format(
                    vex_options, self.name))

        # vex -m ; makes env
        print("Creating virtual env: {}".format(self.name))
        run_in_subprocess("vex {} -m {} true".format(vex_options, self.name))
Beispiel #2
0
    def query_nodes_edges_in_venv(self):
        """Generate Nodes and Edges of packages in virtual env.
        :rtype list, list
        :return: nodes, edges
        """

        interrogation_file = pkg_res_resource_filename(
            'magellan', 'env_interrogation.py')

        # execute
        self.add_file_to_extant_env_files('nodes.json')
        self.add_file_to_extant_env_files('edges.json')
        try:
            if self.name == "":
                run_in_subprocess("python {}".format(interrogation_file))
            else:
                run_in_subprocess("vex {0} python {1}"
                                  .format(self.name, interrogation_file))
        except Exception as e:
            maglog.exception(e)
            # Cleanup:
            self.remove_extant_env_files_from_disk()
            sys.exit("Error {} when trying to interrogate environment."
                     .format(e))

        # Load in nodes and edges pickles
        self.nodes = json.load(open('nodes.json', 'r'))
        self.edges = json.load(open('edges.json', 'r'))

        self.package_requirements = json.load(
            open('package_requirements.json', 'r'))
        self.add_file_to_extant_env_files('package_requirements.json')
Beispiel #3
0
 def remove_env_file_from_disk(file_to_remove):
     """
     Delete file from disk.
     :param file_to_remove: str, file to delete.
     """
     if os.path.exists(file_to_remove):
         run_in_subprocess('rm {}'.format(file_to_remove))
Beispiel #4
0
    def create_vex_new_virtual_env(self, vex_options=None):
        """Create a virtual env in which to install packages
        :returns : venv_name - name of virtual environment.
        :rtype : str
        """

        if vex_options is None:
            vex_options = ''
        if self.name is None:
            venv_template = "MagEnv{}"
            # check if name exists and bump repeatedly until new
            i = 0
            while True:
                self.name = venv_template.format(i)
                if not self.vex_check_venv_exists(self.name,
                                                  vex_options):  # make env
                    break
                i += 1
        else:
            if self.vex_check_venv_exists(self.name, vex_options):
                # vex -r removes virtual env
                run_in_subprocess("vex {} -r {} true".format(
                    vex_options, self.name))

        # vex -m ; makes env
        print(("Creating virtual env: {}".format(self.name)))
        run_in_subprocess("vex {} -m {} true".format(vex_options, self.name))
Beispiel #5
0
 def vex_remove_virtual_env(venv_name=None, vex_options=None):
     """Removes virtual environment"""
     if vex_options is None:
         vex_options = ''
     if venv_name is not None:
         run_in_subprocess("vex {} -r {} true".format(
             vex_options, venv_name))
Beispiel #6
0
 def remove_env_file_from_disk(file_to_remove):
     """
     Delete file from disk.
     :param file_to_remove: str, file to delete.
     """
     if os.path.exists(file_to_remove):
         run_in_subprocess('rm {}'.format(file_to_remove))
Beispiel #7
0
    def query_nodes_edges_in_venv(self):
        """Generate Nodes and Edges of packages in virtual env.
        :rtype list, list
        :return: nodes, edges
        """

        interrogation_file = pkg_res_resource_filename('magellan',
                                                       'env_interrogation.py')

        # execute
        self.add_file_to_extant_env_files('nodes.json')
        self.add_file_to_extant_env_files('edges.json')
        try:
            if self.name == "":
                run_in_subprocess("python {}".format(interrogation_file))
            else:
                run_in_subprocess("vex {0} python {1}".format(
                    self.name, interrogation_file))
        except Exception as e:
            maglog.exception(e)
            # Cleanup:
            self.remove_extant_env_files_from_disk()
            sys.exit(
                "Error {} when trying to interrogate environment.".format(e))

        # Load in nodes and edges pickles
        self.nodes = json.load(open('nodes.json', 'r'))
        self.edges = json.load(open('edges.json', 'r'))

        self.package_requirements = json.load(
            open('package_requirements.json', 'r'))
        self.add_file_to_extant_env_files('package_requirements.json')
Beispiel #8
0
 def vex_remove_virtual_env(venv_name=None, vex_options=None):
     """Removes virtual environment"""
     if vex_options is None:
         vex_options = ''
     if venv_name is not None:
         run_in_subprocess("vex {} -r {} true".format(
             vex_options, venv_name))
Beispiel #9
0
    def vex_install_requirement(install_location, requirement, pip_options,
                                vex_options=None):
        """Install SINGLE requirement into env_name using vex.

        install_location is either the NAME of a virtual env or will be
        the path, specified as "--path /path/to/env"

        """
        if vex_options is None:
            vex_options = ''

        cmd_to_run = ('vex {} {} pip install {} {}'.format(
            vex_options, install_location, requirement, pip_options))
        run_in_subprocess(cmd_to_run)
Beispiel #10
0
    def vex_install_requirement(install_location,
                                requirement,
                                pip_options,
                                vex_options=None):
        """Install SINGLE requirement into env_name using vex.

        install_location is either the NAME of a virtual env or will be
        the path, specified as "--path /path/to/env"

        """
        if vex_options is None:
            vex_options = ''

        cmd_to_run = ('vex {} {} pip install {} {}'.format(
            vex_options, install_location, requirement, pip_options))
        run_in_subprocess(cmd_to_run)
Beispiel #11
0
    def get_deps_for_package_version(package, version, vex_options=None):
        """Gets dependencies for a specific version of a package.

        Specifically:
        0. Check if this has already been done and cached & return that.
            1. Set up temporary virtualenv
            2. installs package/version into there using pip
            3. Write file to interrogate through virtual env using
            vex/pip/setuptool combo
            4. Run file, which pickles results to temp file
            5. reads that file from current program
            6. deletes file and returns info

        7. Delete tmp env?
        """

        if vex_options is None:
            vex_options = ''

        req_out_file = ("{0}_{1}_req.json".format(package.lower(),
                                                  version.replace(".", "_")))

        # 0. Check if this has already been done and cached & return that.
        cached_file = os.path.join(MagellanConfig.cache_dir, req_out_file)

        if os.path.exists(cached_file):
            maglog.info(
                "Using previously cached result at {0}".format(cached_file))
            return json.load(open(cached_file, 'r'))

        # 1. Set up temporary virtualenv
        tmp_env = Environment(name=MagellanConfig.tmp_env_dir)
        tmp_env.create_vex_new_virtual_env(
            vex_options)  # NB: delete if extant!!

        # todo (aj); by default?
        # 1.5 Upgrade pip
        run_in_subprocess("vex {} {} pip install pip --upgrade".format(
            vex_options, tmp_env.name))

        # 2. installs package/version into there using pip
        # tmp_pip_options = "--cache-dir {}".format(MagellanConfig.cache_dir)
        tmp_pip_options = ("--cache-dir {} --no-deps".format(
            MagellanConfig.cache_dir))
        pip_package_str = '{0}=={1}'.format(package, version)
        tmp_env.vex_install_requirement(tmp_env.name, pip_package_str,
                                        tmp_pip_options, vex_options)

        # 3. File to interrogate through virtual env for package
        interrogation_file = pkg_res_resource_filename(
            'magellan', 'package_interrogation.py')

        # 4. Run file, which pickles results to temp file
        run_in_subprocess("vex {} {} python {} {} {}".format(
            vex_options, tmp_env.name, interrogation_file, package,
            MagellanConfig.cache_dir))

        # 5. reads that file from current program
        try:
            result = json.load(open(cached_file, 'r'))
        except IOError:
            result = {}

        return result
Beispiel #12
0
    def get_deps_for_package_version(package, version, vex_options=None):
        """Gets dependencies for a specific version of a package.

        Specifically:
        0. Check if this has already been done and cached & return that.
            1. Set up temporary virtualenv
            2. installs package/version into there using pip
            3. Write file to interrogate through virtual env using
            vex/pip/setuptool combo
            4. Run file, which pickles results to temp file
            5. reads that file from current program
            6. deletes file and returns info

        7. Delete tmp env?
        """

        if vex_options is None:
            vex_options = ''

        req_out_file = ("{0}_{1}_req.json"
                        .format(package.lower(), version.replace(".", "_")))

        # 0. Check if this has already been done and cached & return that.
        cached_file = os.path.join(MagellanConfig.cache_dir, req_out_file)

        if os.path.exists(cached_file):
            maglog.info("Using previously cached result at {0}"
                        .format(cached_file))
            return json.load(open(cached_file, 'r'))

        # 1. Set up temporary virtualenv
        tmp_env = Environment(name=MagellanConfig.tmp_env_dir)
        tmp_env.create_vex_new_virtual_env(vex_options)  # NB: delete if extant!!

        # todo (aj); by default?
        # 1.5 Upgrade pip
        run_in_subprocess("vex {} {} pip install pip --upgrade"
                          .format(vex_options, tmp_env.name))

        # 2. installs package/version into there using pip
        # tmp_pip_options = "--cache-dir {}".format(MagellanConfig.cache_dir)
        tmp_pip_options = ("--cache-dir {} --no-deps"
                           .format(MagellanConfig.cache_dir))
        pip_package_str = '{0}=={1}'.format(package, version)
        tmp_env.vex_install_requirement(
            tmp_env.name, pip_package_str, tmp_pip_options, vex_options)

        # 3. File to interrogate through virtual env for package
        interrogation_file = pkg_res_resource_filename(
            'magellan', 'package_interrogation.py')

        # 4. Run file, which pickles results to temp file
        run_in_subprocess("vex {} {} python {} {} {}".format(
            vex_options, tmp_env.name, interrogation_file, package,
            MagellanConfig.cache_dir))

        # 5. reads that file from current program
        try:
            result = json.load(open(cached_file, 'r'))
        except IOError:
            result = {}

        return result