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
This commit is contained in:
Tony Breeds 2022-08-19 13:02:43 -05:00
parent 6626d1f62e
commit 2d447598e2
5 changed files with 50 additions and 40 deletions

View File

@ -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()

View File

View File

@ -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)

View File

@ -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

View File

@ -19,7 +19,7 @@ classifier =
[entry_points]
console_scripts =
pymod2pkg = pymod2pkg:main
pymod2pkg = pymod2pkg.cli.pymod2pkg:main
[files]
packages =