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..bf6a0c29 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=PIPE) + out, _ = p.communicate() + assert out.decode('utf-8', 'replace').strip() == version