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
Provenance
- Commit
a6125e1- Tree
686006f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7ebd3b9
2 files changed+23 added−2 removed
modified:click/_compat.py+2 added
18 unmodified lines19 text_type = unicode20 bytes = str21 raw_input = raw_input22 string_types = (str, unicode)23+ iteritems = lambda x: x.iteritems()24 25 _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')26 27 def isidentifier(x):25 unmodified lines53 text_type = str54 raw_input = input55 string_types = (str,)56 isidentifier = lambda x: x.isidentifier()57+ iteritems = lambda x: iter(x.items())58 59 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 linesmodified:click/decorators.py+21 added-2 removed
1+import sys2 import inspect3 4 from functools import update_wrapper5 6+from ._compat import iteritems7 from .utils import echo8 9 10 def pass_context(f):193 unmodified lines204 return option(*(param_decls or ('--password',)), **attrs)(f)205 return decorator206 207 206-def version_option(version, *param_decls, **attrs):208+def 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.212 211- :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')235+236 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