From 2d447598e2a5cde9351997f529a6417f44045aa4 Mon Sep 17 00:00:00 2001 From: Tony Breeds Date: Fri, 19 Aug 2022 13:02:43 -0500 Subject: [PATCH] Move CLI code into it's own module This makes pypkg2mod more like a conventional library/module by moving the cli code into it's own module. Making room for additional command line interfaces Change-Id: If15b85ac3230adf9cf2bc8a9c1c7a9ac9f31eda9 --- pymod2pkg/__init__.py | 39 ------------------------------- pymod2pkg/cli/__init__.py | 0 pymod2pkg/cli/pymod2pkg.py | 48 ++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + setup.cfg | 2 +- 5 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 pymod2pkg/cli/__init__.py create mode 100644 pymod2pkg/cli/pymod2pkg.py diff --git a/pymod2pkg/__init__.py b/pymod2pkg/__init__.py index 5fad357..4a5d4fe 100644 --- a/pymod2pkg/__init__.py +++ b/pymod2pkg/__init__.py @@ -10,8 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -import argparse -import distro import re @@ -414,40 +412,3 @@ def module2upstream(mod): if pkglist: return pkglist[0] return mod - - -def main(): - """for resolving names from command line""" - parser = argparse.ArgumentParser(description='Python module name to' - 'package name') - group = parser.add_mutually_exclusive_group() - group.add_argument( - '--dist', help='distribution style (default: %(default)s)', - default=distro.LinuxDistribution().id().partition(' ')[0]) - group.add_argument('--upstream', help='map to OpenStack project name', - action='store_true') - parser.add_argument('--pyver', help='Python versions to return. "py" is ' - 'the unversioned name', - action='append', choices=['py', 'py2', 'py3'], - default=[]) - parser.add_argument('modulename', help='python module name') - args = vars(parser.parse_args()) - - pyversions = args['pyver'] if args['pyver'] else ['py'] - - if args['upstream']: - print(module2upstream(args['modulename'])) - else: - pylist = module2package(args['modulename'], args['dist'], - py_vers=pyversions) - # When only 1 version is requested, it will be returned as a string, - # for backwards compatibility. Else, it will be a list. - if type(pylist) is list: - print(' '.join(pylist)) - else: - print(pylist) - - -# for debugging to call the file directly -if __name__ == "__main__": - main() diff --git a/pymod2pkg/cli/__init__.py b/pymod2pkg/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pymod2pkg/cli/pymod2pkg.py b/pymod2pkg/cli/pymod2pkg.py new file mode 100644 index 0000000..ceaf3ef --- /dev/null +++ b/pymod2pkg/cli/pymod2pkg.py @@ -0,0 +1,48 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse +import distro + +import pymod2pkg + + +def main(): + """for resolving names from command line""" + parser = argparse.ArgumentParser(description='Python module name to' + 'package name') + group = parser.add_mutually_exclusive_group() + group.add_argument( + '--dist', help='distribution style (default: %(default)s)', + default=distro.LinuxDistribution().id().partition(' ')[0]) + group.add_argument('--upstream', help='map to OpenStack project name', + action='store_true') + parser.add_argument('--pyver', help='Python versions to return. "py" is ' + 'the unversioned name', + action='append', choices=['py', 'py2', 'py3'], + default=[]) + parser.add_argument('modulename', help='python module name') + args = vars(parser.parse_args()) + + pyversions = args['pyver'] if args['pyver'] else ['py'] + + if args['upstream']: + print(pymod2pkg.module2upstream(args['modulename'])) + else: + pylist = pymod2pkg.module2package(args['modulename'], args['dist'], + py_vers=pyversions) + # When only 1 version is requested, it will be returned as a string, + # for backwards compatibility. Else, it will be a list. + if type(pylist) is list: + print(' '.join(pylist)) + else: + print(pylist) diff --git a/requirements.txt b/requirements.txt index 35cc597..94b08b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ # process, which may cause wedges in the gate later. distro pbr!=2.1.0,>=2.0.0 # Apache-2.0 +packaging diff --git a/setup.cfg b/setup.cfg index 103d634..0ce2be1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,7 +19,7 @@ classifier = [entry_points] console_scripts = - pymod2pkg = pymod2pkg:main + pymod2pkg = pymod2pkg.cli.pymod2pkg:main [files] packages =