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

Replace optparse with argparse #362

Merged
merged 2 commits into from
Sep 11, 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
47 changes: 26 additions & 21 deletions bin/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import ssm.agents
from ssm import __version__, LOG_BREAK

from argparse import ArgumentParser

Check warning on line 24 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L24

Added line #L24 was not covered by tests
import logging
import os
import sys
from optparse import OptionParser

import configparser

Expand All @@ -34,35 +34,40 @@
ver = "SSM %s.%s.%s" % __version__
default_conf_location = '/etc/apel/receiver.cfg'
default_dns_location = '/etc/apel/dns'
op = OptionParser(description=__doc__, version=ver)
op.add_option('-c', '--config',
help=('location of config file, '
'default path: ' + default_conf_location),
default=default_conf_location)
op.add_option('-l', '--log_config',
help='DEPRECATED - location of logging config file (optional)',
default=None)
op.add_option('-d', '--dn_file',
help=('location of the file containing valid DNs, '
'default path: ' + default_dns_location),
default=default_dns_location)

options, unused_args = op.parse_args()
arg_parser = ArgumentParser(description=__doc__)

Check warning on line 37 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L37

Added line #L37 was not covered by tests

arg_parser.add_argument('-c', '--config',

Check warning on line 39 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L39

Added line #L39 was not covered by tests
help='location of config file, default path: '
'%s' % default_conf_location,
default=default_conf_location)
arg_parser.add_argument('-l', '--log_config',

Check warning on line 43 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L43

Added line #L43 was not covered by tests
help='DEPRECATED - location of logging config file',
default=None)
arg_parser.add_argument('-d', '--dn_file',

Check warning on line 46 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L46

Added line #L46 was not covered by tests
help='location of the file containing valid DNs, '
'default path: %s' % default_dns_location,
default=default_dns_location)
arg_parser.add_argument('-v', '--version',

Check warning on line 50 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L50

Added line #L50 was not covered by tests
action='version',
version=ver)

# Using the vars function to output a dict-like view rather than Namespace object.
options = vars(arg_parser.parse_args())

Check warning on line 55 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L55

Added line #L55 was not covered by tests

# Deprecating functionality.
old_log_config_default_path = '/etc/apel/logging.cfg'
if (os.path.exists(old_log_config_default_path) or options.log_config is not None):
if (os.path.exists(old_log_config_default_path) or options['log_config'] is not None):
logging.warning('Separate logging config file option has been deprecated.')

# Absolute file path required when refreshing dn_file, relative path resulted in an error.
options.dn_file = os.path.abspath(options.dn_file)
options['dn_file'] = os.path.abspath(options['dn_file'])

Check warning on line 63 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L63

Added line #L63 was not covered by tests

# Check if config file exists using os.path.isfile function.
if os.path.isfile(options.config):
if os.path.isfile(options['config']):
cp = configparser.ConfigParser({'use_ssl': 'true'})
cp.read(options.config)
cp.read(options['config'])

Check warning on line 68 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L68

Added line #L68 was not covered by tests
else:
print("Config file not found at", options.config)
print("Config file not found at", options['config'])

Check warning on line 70 in bin/receiver.py

View check run for this annotation

Codecov / codecov/patch

bin/receiver.py#L70

Added line #L70 was not covered by tests
sys.exit(1)

# Check for pidfile
Expand All @@ -85,7 +90,7 @@
brokers, project, token = ssm.agents.get_ssm_args(protocol, cp, log)

ssm.agents.run_receiver(protocol, brokers, project, token,
cp, log, options.dn_file)
cp, log, options['dn_file'])


if __name__ == '__main__':
Expand Down
35 changes: 20 additions & 15 deletions bin/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import ssm.agents
from ssm import __version__, LOG_BREAK

from argparse import ArgumentParser

Check warning on line 24 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L24

Added line #L24 was not covered by tests
import logging
from optparse import OptionParser
import os
import sys

Expand All @@ -33,28 +33,33 @@
"""Set up connection, send all messages and quit."""
ver = "SSM %s.%s.%s" % __version__
default_conf_location = '/etc/apel/sender.cfg'
op = OptionParser(description=__doc__, version=ver)
op.add_option('-c', '--config',
help=('location of config file, '
'default path: ' + default_conf_location),
default=default_conf_location)
op.add_option('-l', '--log_config',
help='DEPRECATED - location of logging config file (optional)',
default=None)

options, unused_args = op.parse_args()
arg_parser = ArgumentParser(description=__doc__)

Check warning on line 36 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L36

Added line #L36 was not covered by tests

arg_parser.add_argument('-c', '--config',

Check warning on line 38 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L38

Added line #L38 was not covered by tests
help='location of config file, default path: '
'%s' % default_conf_location,
default=default_conf_location)
arg_parser.add_argument('-l', '--log_config',

Check warning on line 42 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L42

Added line #L42 was not covered by tests
help='DEPRECATED - location of logging config file',
default=None)
arg_parser.add_argument('-v', '--version',

Check warning on line 45 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L45

Added line #L45 was not covered by tests
action='version',
version=ver)

# Using the vars function to output a dict-like view rather than Namespace object.
options = vars(arg_parser.parse_args())

Check warning on line 50 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L50

Added line #L50 was not covered by tests

# Deprecating functionality.
old_log_config_default_path = '/etc/apel/logging.cfg'
if (os.path.exists(old_log_config_default_path) or options.log_config is not None):
if (os.path.exists(old_log_config_default_path) or options['log_config'] is not None):
logging.warning('Separate logging config file option has been deprecated.')

# Check if config file exists using os.path.isfile function.
if os.path.isfile(options.config):
if os.path.isfile(options['config']):
cp = configparser.ConfigParser({'use_ssl': 'true'})
cp.read(options.config)
cp.read(options['config'])

Check warning on line 60 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L60

Added line #L60 was not covered by tests
else:
print("Config file not found at", options.config)
print("Config file not found at", options['config'])

Check warning on line 62 in bin/sender.py

View check run for this annotation

Codecov / codecov/patch

bin/sender.py#L62

Added line #L62 was not covered by tests
sys.exit(1)

ssm.agents.logging_helper(cp)
Expand Down