def version(): """Returns the current version of the tool Checks all installed packages and finds the msr package, then prints its version. Shamelessly adapted from click.version_option() """ module = 'msr' ver = None try: import pkg_resources except ImportError: pass else: for dist in pkg_resources.working_set: scripts = dist.get_entry_map().get("console_scripts") or {} for _, entry_point in iteritems(scripts): if entry_point.module_name.startswith(module): ver = dist.version break if ver is None: raise RuntimeError("Could not determine version") print(ver)
def callback(ctx, param, value): if not value or ctx.resilient_parsing: return prog = prog_name if prog is None: prog = ctx.find_root().info_name ver = version if ver is None: try: import pkg_resources except ImportError: pass else: for dist in pkg_resources.working_set: scripts = dist.get_entry_map().get('console_scripts') or {} for script_name, entry_point in iteritems(scripts): if entry_point.module_name == module: ver = dist.version break if ver is None: raise RuntimeError('Could not determine version') msg_parts = [] for s in re.split(r'(%\(version\)s|%\(prog\)s)', message): if s == '%(prog)s': msg_parts.append(_colorize(prog_name, prog_name_color)) elif s == '%(version)s': msg_parts.append(_colorize(version, version_color)) else: msg_parts.append(_colorize(s, message_color)) echo(''.join(msg_parts)) ctx.exit()
def make_context(self, info_name, args, parent=None, **extra): for key, value in iteritems(self.context_settings): if key not in extra: extra[key] = value ctx = GkeepContext(self, info_name=info_name, parent=parent, **extra) with ctx.scope(cleanup=False): self.parse_args(ctx, args) return ctx
def get_client_version() -> Optional[str]: try: import pkg_resources ver: Optional[str] = None module = sys._getframe(1).f_globals.get("__name__") for dist in pkg_resources.working_set: scripts = dist.get_entry_map().get("console_scripts") or {} for _, entry_point in iteritems(scripts): if entry_point.module_name == module: ver = dist.version return ver except Exception as e: banner("Could not find client version", "fatal") return None
def isolation(self, input=None, env=None, color=False): """A context manager that sets up the isolation for invoking of a command line tool. This sets up stdin with the given input data and `os.environ` with the overrides from the given dictionary. This also rebinds some internals in Click to be mocked (like the prompt functionality). This is automatically done in the :meth:`invoke` method. .. versionadded:: 4.0 The ``color`` parameter was added. :param input: the input stream to put into sys.stdin. :param env: the environment overrides as dictionary. :param color: whether the output should contain color codes. The application can still override this explicitly. """ input = make_input_stream(input, self.charset) old_stdin = sys.stdin old_stdout = sys.stdout old_stderr = sys.stderr old_forced_width = clickpkg.formatting.FORCED_WIDTH clickpkg.formatting.FORCED_WIDTH = 80 env = self.make_env(env) if PY2: bytes_output = StringIO() if self.echo_stdin: input = EchoingStdin(input, bytes_output) sys.stdout = bytes_output if not self.mix_stderr: bytes_error = StringIO() sys.stderr = bytes_error else: bytes_output = io.BytesIO() if self.echo_stdin: input = EchoingStdin(input, bytes_output) input = io.TextIOWrapper(input, encoding=self.charset) sys.stdout = io.TextIOWrapper(bytes_output, encoding=self.charset) if not self.mix_stderr: bytes_error = io.BytesIO() sys.stderr = io.TextIOWrapper(bytes_error, encoding=self.charset) if self.mix_stderr: sys.stderr = sys.stdout sys.stdin = input def visible_input(prompt=None): sys.stdout.write(prompt or "") val = input.readline().rstrip("\r\n") sys.stdout.write(val + "\n") sys.stdout.flush() return val def hidden_input(prompt=None): sys.stdout.write((prompt or "") + "\n") sys.stdout.flush() return input.readline().rstrip("\r\n") def _getchar(echo): char = sys.stdin.read(1) if echo: sys.stdout.write(char) sys.stdout.flush() return char default_color = color def should_strip_ansi(stream=None, color=None): if color is None: return not default_color return not color old_visible_prompt_func = clickpkg.termui.visible_prompt_func old_hidden_prompt_func = clickpkg.termui.hidden_prompt_func old__getchar_func = clickpkg.termui._getchar old_should_strip_ansi = clickpkg.utils.should_strip_ansi clickpkg.termui.visible_prompt_func = visible_input clickpkg.termui.hidden_prompt_func = hidden_input clickpkg.termui._getchar = _getchar clickpkg.utils.should_strip_ansi = should_strip_ansi old_env = {} try: for key, value in iteritems(env): old_env[key] = os.environ.get(key) if value is None: try: del os.environ[key] except Exception: # nosec pass else: os.environ[key] = value yield (bytes_output, not self.mix_stderr and bytes_error) finally: for key, value in iteritems(old_env): if value is None: try: del os.environ[key] except Exception: # nosec pass else: os.environ[key] = value sys.stdout = old_stdout sys.stderr = old_stderr sys.stdin = old_stdin clickpkg.termui.visible_prompt_func = old_visible_prompt_func clickpkg.termui.hidden_prompt_func = old_hidden_prompt_func clickpkg.termui._getchar = old__getchar_func clickpkg.utils.should_strip_ansi = old_should_strip_ansi clickpkg.formatting.FORCED_WIDTH = old_forced_width