Use argparse instead of optparse

Use of optparse module is deprecated [1] in Django 1.8 and 1.9 versions and
its support has been removed in currently developed version 1.10 [2]

[1] https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/#django.core.management.BaseCommand.option_list
[2] 6a70cb5397 (diff-dfc45ab8548a0777543d12d6e77c9173)

Change-Id: I0c7622fce0d5fabce45366d002b5e72e4c170344
This commit is contained in:
Ivan Udovichenko 2016-07-22 15:05:05 +03:00
parent 528358cbbd
commit 374e60307d
2 changed files with 98 additions and 77 deletions

View File

@ -13,7 +13,6 @@
from __future__ import print_function from __future__ import print_function
from optparse import make_option # noqa
import os import os
import re import re
import socket import socket
@ -165,76 +164,100 @@ location you desire, e.g.::
'admin': context['ADMIN'], 'admin': context['ADMIN'],
'hostname': context['VHOSTNAME'], } 'hostname': context['VHOSTNAME'], }
option_list = BaseCommand.option_list + ( def add_arguments(self, parser):
# TODO(ygbo): Add an --nginx option. # TODO(ygbo): Add an --nginx option.
make_option("-a", "--apache", parser.add_argument(
default=False, action="store_true", dest="apache", "-a", "--apache",
help="generate an apache vhost configuration"), default=False, action="store_true", dest="apache",
make_option("--cacert", help="generate an apache vhost configuration"
dest="cacert", )
help=("Use with the --apache and --ssl option to define " parser.add_argument(
"the path to the SSLCACertificateFile" "--cacert",
), dest="cacert",
metavar="CACERT"), help=("Use with the --apache and --ssl option to define the path"
make_option("-f", "--force", " to the SSLCACertificateFile"),
default=False, action="store_true", dest="force", metavar="CACERT"
help="force overwriting of an existing %s file" % )
context['WSGI_FILE']), parser.add_argument(
make_option("-H", "--hostname", "-f", "--force",
dest="hostname", default=False, action="store_true", dest="force",
help=("Use with the --apache option to define the server's" help="force overwriting of an existing %s file" %
" hostname (default : %s)") % context['VHOSTNAME'], context['WSGI_FILE']
metavar="HOSTNAME"), )
make_option("--logdir", parser.add_argument(
dest="logdir", "-H", "--hostname",
help=("Use with the --apache option to define the path to " dest="hostname",
"the apache log directory(default : %s)" help=("Use with the --apache option to define the server's"
% context['LOGDIR']), " hostname (default : %s)") % context['VHOSTNAME'],
metavar="CACERT"), metavar="HOSTNAME"
make_option("-m", "--mail", )
dest="mail", parser.add_argument(
help=("Use with the --apache option to define the web site" "--logdir",
" administrator's email (default : %s)") % dest="logdir",
context['ADMIN'], help=("Use with the --apache option to define the path to "
metavar="MAIL"), "the apache log directory(default : %s)"
make_option("-n", "--namedhost", % context['LOGDIR']),
default=False, action="store_true", dest="namedhost", metavar="CACERT"
help=("Use with the --apache option. The apache vhost " )
"configuration will work only when accessed with " parser.add_argument(
"the proper hostname (see --hostname).")), "-m", "--mail",
make_option("-p", "--project", dest="mail",
dest="project", help=("Use with the --apache option to define the web site"
help=("Use with the --apache option to define the project " " administrator's email (default : %s)") %
"name (default : %s)") % context['PROJECT_NAME'], context['ADMIN'],
metavar="PROJECT"), metavar="MAIL"
make_option("-s", "--ssl", )
default=False, action="store_true", dest="ssl", parser.add_argument(
help=("Use with the --apache option. The apache vhost " "-n", "--namedhost",
"configuration will use an SSL configuration")), default=False, action="store_true", dest="namedhost",
make_option("--sslcert", help=("Use with the --apache option. The apache vhost "
dest="sslcert", "configuration will work only when accessed with "
help=("Use with the --apache and --ssl option to define " "the proper hostname (see --hostname).")
"the path to the SSLCertificateFile (default : %s)" )
) % context['SSLCERT'], parser.add_argument(
metavar="SSLCERT"), "-p", "--project",
make_option("--sslkey", dest="project",
dest="sslkey", help=("Use with the --apache option to define the project "
help=("Use with the --apache and --ssl option to define " "name (default : %s)") % context['PROJECT_NAME'],
"the path to the SSLCertificateKeyFile " metavar="PROJECT"
"(default : %s)") % context['SSLKEY'], )
metavar="SSLKEY"), parser.add_argument(
make_option("--apache-version", "-s", "--ssl",
dest="apache_version", default=False, action="store_true", dest="ssl",
type="float", help=("Use with the --apache option. The apache vhost "
help=("Use with the --apache option to define the apache " "configuration will use an SSL configuration")
"major (as a floating point number) version " )
"(default : %s)." parser.add_argument(
% context['APACHE2_VERSION']), "--sslcert",
metavar="APACHE_VERSION"), dest="sslcert",
make_option("-w", "--wsgi", help=("Use with the --apache and --ssl option to define "
default=False, action="store_true", dest="wsgi", "the path to the SSLCertificateFile (default : %s)"
help="generate the horizon.wsgi file"), ) % context['SSLCERT'],
) metavar="SSLCERT"
)
parser.add_argument(
"--sslkey",
dest="sslkey",
help=("Use with the --apache and --ssl option to define "
"the path to the SSLCertificateKeyFile "
"(default : %s)") % context['SSLKEY'],
metavar="SSLKEY"
)
parser.add_argument(
"--apache-version",
dest="apache_version",
type=float,
help=("Use with the --apache option to define the apache "
"major (as a floating point number) version "
"(default : %s)."
% context['APACHE2_VERSION']),
metavar="APACHE_VERSION"
)
parser.add_argument(
"-w", "--wsgi",
default=False, action="store_true", dest="wsgi",
help="generate the horizon.wsgi file"
)
def handle(self, *args, **options): def handle(self, *args, **options):
force = options.get('force') force = options.get('force')

View File

@ -14,7 +14,6 @@ from __future__ import print_function
import difflib import difflib
import imp import imp
import optparse
import os import os
import shlex import shlex
import subprocess import subprocess
@ -75,24 +74,23 @@ class DirContext(object):
class Command(BaseCommand): class Command(BaseCommand):
option_list = BaseCommand.option_list + ( def add_arguments(self, parser):
optparse.make_option( parser.add_argument(
'--gendiff', '--gendiff',
action='store_true', action='store_true',
dest='gendiff', dest='gendiff',
default=False, default=False,
help=('Generate a diff file between local_settings.py and ' help=('Generate a diff file between local_settings.py and '
'local_settings.py.example'), 'local_settings.py.example'),
), )
optparse.make_option( parser.add_argument(
'-f', '--force', '-f', '--force',
action='store_true', action='store_true',
dest='force', dest='force',
default=False, default=False,
help=('Force destination rewriting without warning if the ' help=('Force destination rewriting without warning if the '
'destination file already exists.'), 'destination file already exists.'),
), )
)
help = ("Creates a local_settings.py file from the " help = ("Creates a local_settings.py file from the "
"local_settings.py.example template.") "local_settings.py.example template.")