def open_in_editor(self, options, args): 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))
def _determine_editor(self, options): 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.")
def _save_configuration(self): # We successfully ran a modifying command. Need to save the # configuration. try: self.configuration.save() except Exception: logger.error( "Unable to save configuration. Please report this as a bug.", exc_info=1) raise PipError("Internal Error.")
def _get_n_args(self, args, example, n): """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
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.")