From c1d90d82980901f8da7bd17546e897bdb7a4a345 Mon Sep 17 00:00:00 2001 From: Michel Albert Date: Wed, 1 May 2019 11:50:07 +0200 Subject: [PATCH 1/2] Add "flit info --version" command References #262 --- flit/__init__.py | 13 +++++++++++++ flit/info.py | 20 ++++++++++++++++++++ tests/test_command.py | 9 +++++++++ 3 files changed, 42 insertions(+) create mode 100644 flit/info.py diff --git a/flit/__init__.py b/flit/__init__.py index 64e63e3a..1bf84ee3 100644 --- a/flit/__init__.py +++ b/flit/__init__.py @@ -79,6 +79,14 @@ def main(argv=None): help="Prepare pyproject.toml for a new package" ) + parser_info = subparsers.add_parser('info', + help="Retrieve metadata information from the project", + ) + parser_info.add_argument( + '--version', default=False, action='store_true', dest='show_version', + help="Print the version number of the project to stdout" + ) + args = ap.parse_args(argv) cf = args.ini_file @@ -99,6 +107,11 @@ def main(argv=None): log.debug("Parsed arguments %r", args) + if args.subcmd == 'info' and args.show_version: + from .info import get_version + print(get_version(args.ini_file)) + sys.exit(0) + if args.logo: from .logo import clogo print(clogo.format(version=__version__)) diff --git a/flit/info.py b/flit/info.py new file mode 100644 index 00000000..642dab67 --- /dev/null +++ b/flit/info.py @@ -0,0 +1,20 @@ +""" +This module contains code for the "info" subcommand +""" + + +def get_version(ini_path): + # type: (str) -> str + """ + This will return the package version as a string. + + :param ini_path: The filename of the main config-file + (flit.ini/pyproject.toml) + """ + from . import inifile + from .common import Module, make_metadata + ini_info = inifile.read_pkg_ini(ini_path) + module = Module(ini_info['module'], ini_path.parent) + metadata = make_metadata(module, ini_info) + output = metadata.version + return output diff --git a/tests/test_command.py b/tests/test_command.py index 1cec17da..804154ec 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -11,3 +11,12 @@ def test_flit_usage(): out, _ = p.communicate() assert 'Build wheel' in out.decode('utf-8', 'replace') assert p.poll() == 1 + +def test_flit_version(): + import flit + version = flit.__version__ + + p = Popen([sys.executable, '-m', 'flit', 'info', '--version'], + stdout=PIPE, stderr=STDOUT) + out, _ = p.communicate() + assert out.decode('utf-8', 'replace').strip() == version From eda5758a8d0276219eaae8800abd0bfde8e5b476 Mon Sep 17 00:00:00 2001 From: Michel Albert Date: Wed, 1 May 2019 13:32:18 +0200 Subject: [PATCH 2/2] Fix unit-test for "flit info --version" The "stderr" stream was redirected to stdout which made the test not representative of the intended use-case and also caused issues on travis --- tests/test_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_command.py b/tests/test_command.py index 804154ec..bf6a0c29 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -17,6 +17,6 @@ def test_flit_version(): version = flit.__version__ p = Popen([sys.executable, '-m', 'flit', 'info', '--version'], - stdout=PIPE, stderr=STDOUT) + stdout=PIPE, stderr=PIPE) out, _ = p.communicate() assert out.decode('utf-8', 'replace').strip() == version