Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getopt deprecation warnings (part 1) #4908

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 63 additions & 66 deletions base/server/python/pki/server/cli/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
# All rights reserved.
#

from __future__ import absolute_import
from __future__ import print_function

import getopt
import argparse
import logging
import io
import sys
Expand All @@ -45,7 +42,25 @@ class BannerShowCLI(pki.cli.CLI):
def __init__(self):
super().__init__('show', 'Show banner')

def usage(self):
self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default='pki-tomcat')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')

def print_help(self):
print('Usage: pki-server banner-show [OPTIONS]')
print()
print(' -i, --instance <instance ID> Instance ID (default: pki-tomcat).')
Expand All @@ -56,36 +71,19 @@ def usage(self):

def execute(self, argv):

try:
opts, _ = getopt.gnu_getopt(argv, 'i:v', [
'instance=',
'verbose', 'debug', 'help'])

except getopt.GetoptError as e:
logger.error(e)
self.usage()
sys.exit(1)

instance_name = 'pki-tomcat'

for o, a in opts:
if o in ('-i', '--instance'):
instance_name = a
args = self.parser.parse_args(args=argv)

elif o == '--debug':
logging.getLogger().setLevel(logging.DEBUG)
if args.help:
self.print_help()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method print_help() is not defined in the BannerShowCLI.

The argparse has its way to print the help message, we could use it making easier to keep option and description aligned but not sure how to configure with these modular commands. Can be investigated in separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I've renamed usage() to print_help().

For now I just want to fix the deprecation warnings, but later we can try to use sub-parsers to define the options & help messages for the sub-commands.

return

elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.INFO)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

elif o == '--help':
self.usage()
sys.exit()
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

else:
logger.error('Unknown option: %s', o)
self.usage()
sys.exit(1)
instance_name = args.instance

instance = pki.server.PKIServerFactory.create(instance_name)

Expand All @@ -107,7 +105,29 @@ class BannerValidateCLI(pki.cli.CLI):
def __init__(self):
super().__init__('validate', 'Validate banner')

def usage(self):
self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'-i',
'--instance',
default='pki-tomcat')
self.parser.add_argument('--file')
self.parser.add_argument(
'--silent',
action='store_true')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')

def print_help(self):
print('Usage: pki-server banner-validate [OPTIONS]')
print()
print(' -i, --instance <instance ID> Instance ID (default: pki-tomcat).')
Expand All @@ -120,44 +140,21 @@ def usage(self):

def execute(self, argv):

try:
opts, _ = getopt.gnu_getopt(argv, 'i:v', [
'instance=', 'file=', 'silent',
'verbose', 'debug', 'help'])
args = self.parser.parse_args(args=argv)

except getopt.GetoptError as e:
logger.error(e)
self.usage()
sys.exit(1)

instance_name = 'pki-tomcat'
banner_file = None
silent = False

for o, a in opts:
if o in ('-i', '--instance'):
instance_name = a

elif o == '--file':
banner_file = a
if args.help:
self.print_help()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

return

elif o == '--debug':
logging.getLogger().setLevel(logging.DEBUG)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

elif o in ('-v', '--verbose'):
logging.getLogger().setLevel(logging.INFO)
elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

elif o == '--silent':
silent = True

elif o == '--help':
self.usage()
sys.exit()

else:
logger.error('Unknown option: %s', o)
self.usage()
sys.exit(1)
instance_name = args.instance
banner_file = args.file
silent = args.silent

try:
if banner_file:
Expand Down
Loading
Loading