Skip to content
Myr

Implemented automatic version detection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 7ebd3b9 · Browse files · 2 files+23 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−2 removed

  • other:+23 added−2 removed

2 files+23 added−2 removed

modified:click/_compat.py+2 added
18 unmodified lines
19 text_type = unicode20 bytes = str21 raw_input = raw_input22 string_types = (str, unicode)23 iteritems = lambda x: x.iteritems()2425 _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')2627 def isidentifier(x):
25 unmodified lines
53 text_type = str54 raw_input = input55 string_types = (str,)56 isidentifier = lambda x: x.isidentifier()57 iteritems = lambda x: iter(x.items())5859 def _make_binary_stream(f, stream_name):60 buf = getattr(f, 'buffer', None)61 if buf is not None and isinstance(buf, io.BufferedReader):
48 unmodified lines
modified:click/decorators.py+21 added-2 removed
1import sys2import inspect34from functools import update_wrapper56from ._compat import iteritems7from .utils import echo8910def pass_context(f):
193 unmodified lines
204 return option(*(param_decls or ('--password',)), **attrs)(f)205 return decorator206207206def version_option(version, *param_decls, **attrs):208def version_option(version=None, *param_decls, **attrs):209 """Adds a ``--version`` option which immediately ends the program210 printing out the version number. This is implemented as an eager211 option that prints the version and exits the program in the callback.212211 :param version: the version number to show213 :param version: the version number to show. If not provided click214 attempts an auto discovery via setuptools.215 :param prog_name: the name of the program (defaults to autodetection)216 :param message: custom message to show instead of the default217 (``'%(prog)s, version %(version)s'``)218 :param others: everything else is forwarded to :func:`option`.219 """220 if version is None:221 mod = sys._getframe(1).f_globals.get('__name__')222 try:223 import pkg_resources224 except ImportError:225 pass226 else:227 for dist in pkg_resources.working_set:228 scripts = dist.get_entry_map().get('console_scripts') or {}229 for script_name, entry_point in iteritems(scripts):230 if entry_point.module_name == mod:231 version = dist.version232 break233 if version is None:234 raise RuntimeError('Could not determine version')235236 def decorator(f):237 prog_name = attrs.pop('prog_name', None)238 message = attrs.pop('message', '%(prog)s, version %(version)s')239
47 unmodified lines