Beispiel #1
0
    def open_in_editor(self, options: Values, args: List[str]) -> None:
        editor = self._determine_editor(options)

        fname = self.configuration.get_file_to_edit()
        if fname is None:
            raise PipError("Could not determine appropriate file.")

        try:
            subprocess.check_call([editor, fname])
        except subprocess.CalledProcessError as e:
            raise PipError("Editor Subprocess exited with exit code {}".format(
                e.returncode))
Beispiel #2
0
    def _determine_file(self, options: Values,
                        need_value: bool) -> Optional[Kind]:
        file_options = [
            key for key, value in (
                (kinds.USER, options.user_file),
                (kinds.GLOBAL, options.global_file),
                (kinds.SITE, options.site_file),
            ) if value
        ]

        if not file_options:
            if not need_value:
                return None
            # Default to user, unless there's a site file.
            elif any(
                    os.path.exists(site_config_file) for site_config_file in
                    get_configuration_files()[kinds.SITE]):
                return kinds.SITE
            else:
                return kinds.USER
        elif len(file_options) == 1:
            return file_options[0]

        raise PipError("Need exactly one file to operate upon "
                       "(--user, --site, --global) to perform.")
Beispiel #3
0
 def _determine_editor(self, options: Values) -> str:
     if options.editor is not None:
         return options.editor
     elif "VISUAL" in os.environ:
         return os.environ["VISUAL"]
     elif "EDITOR" in os.environ:
         return os.environ["EDITOR"]
     else:
         raise PipError("Could not determine editor to use.")
Beispiel #4
0
 def _save_configuration(self) -> None:
     # We successfully ran a modifying command. Need to save the
     # configuration.
     try:
         self.configuration.save()
     except Exception:
         logger.exception(
             "Unable to save configuration. Please report this as a bug.")
         raise PipError("Internal Error.")
Beispiel #5
0
    def _get_n_args(self, args: List[str], example: str, n: int) -> Any:
        """Helper to make sure the command got the right number of arguments"""
        if len(args) != n:
            msg = ("Got unexpected number of arguments, expected {}. "
                   '(example: "{} config {}")').format(n, get_prog(), example)
            raise PipError(msg)

        if n == 1:
            return args[0]
        else:
            return args
Beispiel #6
0
    def _determine_file(self, options, need_value):
        file_options = {
            kinds.USER: options.user_file,
            kinds.GLOBAL: options.global_file,
            kinds.VENV: options.venv_file
        }

        if sum(file_options.values()) == 0:
            if not need_value:
                return None
            # Default to user, unless there's a virtualenv file.
            elif os.path.exists(venv_config_file):
                return kinds.VENV
            else:
                return kinds.USER
        elif sum(file_options.values()) == 1:
            # There's probably a better expression for this.
            return [key for key in file_options if file_options[key]][0]

        raise PipError(
            "Need exactly one file to operate upon "
            "(--user, --venv, --global) to perform."
        )