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

Remove optparse, add argparse #360

Closed
Closed
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
46 changes: 25 additions & 21 deletions bin/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import os
import sys
from optparse import OptionParser
from argparse import ArgumentParser

try:
import ConfigParser
Expand All @@ -37,35 +37,39 @@ def main():
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()
op = ArgumentParser(description=__doc__)
op.add_argument('-c', '--config',
help=('location of config file, '
'default path: %s' % default_conf_location),
default=default_conf_location)
op.add_argument('-l', '--log_config',
help='DEPRECATED - location of logging config file (optional)',
default=None)
op.add_argument('-d', '--dn_file',
help=('location of the file containing valid DNs, '
'default path: %s' % default_dns_location),
default=default_dns_location)
op.add_argument('-v', '--version',
help=('current version number, default: %s' % ver),
default=ver)

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

# 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 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'])
else:
print("Config file not found at", options.config)
print("Config file not found at", options['config'])
sys.exit(1)

# Check for pidfile
Expand All @@ -88,7 +92,7 @@ def main():
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
25 changes: 15 additions & 10 deletions bin/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from ssm import __version__, LOG_BREAK

import logging
from optparse import OptionParser
import os
import sys
from argparse import ArgumentParser

try:
import ConfigParser
Expand All @@ -36,28 +36,33 @@ def main():
"""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',

op = ArgumentParser(description=__doc__)
op.add_argument('-c', '--config',
help=('location of config file, '
'default path: ' + default_conf_location),
'default path: %s' % default_conf_location),
default=default_conf_location)
op.add_option('-l', '--log_config',
op.add_argument('-l', '--log_config',
help='DEPRECATED - location of logging config file (optional)',
default=None)
op.add_argument('-v', '--version',
help='current version number, default: %s' % ver,
default=ver)

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

# 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'])
else:
print("Config file not found at", options.config)
print("Config file not found at", options['config'])
sys.exit(1)

ssm.agents.logging_helper(cp)
Expand Down
Loading