| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2014 Spotify AB |
| 3 |
|
| 4 |
# This file is part of dh-virtualenv. |
| 5 |
|
| 6 |
# dh-virtualenv is free software: you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU General Public License as |
| 8 |
# published by the Free Software Foundation, either version 2 of the |
| 9 |
# License, or (at your option) any later version. |
| 10 |
|
| 11 |
# dh-virtualenv is distributed in the hope that it will be useful, but |
| 12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# General Public License for more details. |
| 15 |
|
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with dh-virtualenv. If not, see |
| 18 |
# <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
"""Helpers to handle debhelper command line options.""" |
| 21 |
from __future__ import absolute_import |
| 22 |
|
| 23 |
import os |
| 24 |
import warnings |
| 25 |
|
| 26 |
from optparse import OptionParser, SUPPRESS_HELP, OptionValueError |
| 27 |
|
| 28 |
from ._version import version |
| 29 |
|
| 30 |
|
| 31 |
class DebhelperOptionParser(OptionParser): |
| 32 |
"""Special OptionParser for handling Debhelper options. |
| 33 |
|
| 34 |
Basically this means converting -O--option to --option before |
| 35 |
parsing. |
| 36 |
|
| 37 |
""" |
| 38 |
def parse_args(self, args=None, values=None): |
| 39 |
args = [o[2:] if o.startswith('-O-') else o |
| 40 |
for o in self._get_args(args)] |
| 41 |
args.extend(os.environ.get('DH_OPTIONS', '').split()) |
| 42 |
# Unfortunately OptionParser is an old style class :( |
| 43 |
return OptionParser.parse_args(self, args, values) |
| 44 |
|
| 45 |
|
| 46 |
def _check_for_deprecated_options( |
| 47 |
option, opt_str, value, parser, *args, **kwargs): |
| 48 |
# TODO: If more deprectaed options pop up, refactor this method to |
| 49 |
# handle them in more generic way (or actually remove the |
| 50 |
# deprecated options) |
| 51 |
if opt_str in ('--pypi-url', '--index-url'): |
| 52 |
if opt_str == '--pypi-url': |
| 53 |
# Work around 2.7 hiding the DeprecationWarning |
| 54 |
with warnings.catch_warnings(): |
| 55 |
warnings.simplefilter('default') |
| 56 |
warnings.warn('Use of --pypi-url is deprecated. Use ' |
| 57 |
'--index-url instead', |
| 58 |
DeprecationWarning) |
| 59 |
if parser.values.index_url: |
| 60 |
# We've already set the index_url, which means that we have both |
| 61 |
# --index-url and --pypi-url passed in. |
| 62 |
raise OptionValueError('Deprecated --pypi-url and the new ' |
| 63 |
'--index-url are mutually exclusive') |
| 64 |
parser.values.index_url = value |
| 65 |
elif opt_str in ('--no-test', '--setuptools-test'): |
| 66 |
if opt_str == '--no-test': |
| 67 |
with warnings.catch_warnings(): |
| 68 |
warnings.simplefilter('default') |
| 69 |
warnings.warn('Use of --no-test is deprecated and has no ' |
| 70 |
'effect. Use --setuptools-test if you want to ' |
| 71 |
'execute `setup.py test` during package build.', |
| 72 |
DeprecationWarning) |
| 73 |
if getattr(parser.values, '_test_flag_seen', None): |
| 74 |
raise OptionValueError('Deprecated --no-test and the new ' |
| 75 |
'--setuptools-test are mutually exclusive') |
| 76 |
parser.values.setuptools_test = opt_str != '--no-test' |
| 77 |
setattr(parser.values, '_test_flag_seen', True) |
| 78 |
|
| 79 |
|
| 80 |
def get_default_parser(): |
| 81 |
usage = '%prog [options]' |
| 82 |
parser = DebhelperOptionParser(usage, version='%prog ' + version) |
| 83 |
parser.add_option('-p', '--package', action='append', metavar='PACKAGE', |
| 84 |
help='Act on the package(s) named PACKAGE') |
| 85 |
parser.add_option('-N', '--no-package', action='append', metavar='PACKAGE', |
| 86 |
help='Do not act on the specified package(s)') |
| 87 |
parser.add_option('-v', '--verbose', action='store_true', |
| 88 |
default=False, help='Turn on verbose mode') |
| 89 |
parser.add_option('-s', '--setuptools', action='store_true', |
| 90 |
default=False, help='Use Setuptools instead of Distribute') |
| 91 |
parser.add_option('--extra-index-url', action='append', metavar='URL', |
| 92 |
help='Extra index URL(s) to pass to pip.', |
| 93 |
default=[]) |
| 94 |
parser.add_option('--preinstall', action='append', metavar='PACKAGE', |
| 95 |
help=('Package(s) to install before processing ' |
| 96 |
'requirements.txt.'), |
| 97 |
default=[]) |
| 98 |
parser.add_option('--extras', action='append', metavar='NAME', |
| 99 |
help=('Activate one or more extras of the main package.'), |
| 100 |
default=[]) |
| 101 |
parser.add_option('--pip-tool', default='pip', metavar='EXECUTABLE', |
| 102 |
help="Executable that will be used to install " |
| 103 |
"requirements after the preinstall stage. Usually " |
| 104 |
"you'll install this program by using the --preinstall " |
| 105 |
"argument. The replacement is expected to be found in " |
| 106 |
"the virtualenv's bin/ directory.") |
| 107 |
parser.add_option('--upgrade-pip', action='store_true', default=False, |
| 108 |
help='Upgrade pip to the latest available version') |
| 109 |
parser.add_option('--upgrade-pip-to', default='', metavar='VERSION', |
| 110 |
help='Upgrade pip to a specific version') |
| 111 |
parser.add_option('--extra-pip-arg', action='append', metavar='ARG', |
| 112 |
help='One or more extra args for the pip binary.' |
| 113 |
'You can use this flag multiple times to pass in' |
| 114 |
' parameters to pip.', default=[]) |
| 115 |
parser.add_option('--extra-virtualenv-arg', action='append', metavar='ARG', |
| 116 |
help='One or more extra args for the virtualenv binary.' |
| 117 |
'You can use this flag multiple times to pass in' |
| 118 |
' parameters to the virtualenv binary.', default=[]) |
| 119 |
parser.add_option('--index-url', metavar='URL', |
| 120 |
help='Base URL of the PyPI server', |
| 121 |
action='callback', |
| 122 |
type='string', |
| 123 |
dest='index_url', |
| 124 |
callback=_check_for_deprecated_options) |
| 125 |
parser.add_option('--python', metavar='EXECUTABLE', |
| 126 |
help='The Python command to use') |
| 127 |
parser.add_option('--builtin-venv', action='store_true', |
| 128 |
help='Use the built-in venv module. Only works on ' |
| 129 |
'Python 3.4 and later.') |
| 130 |
parser.add_option('-D', '--sourcedirectory', dest='sourcedirectory', |
| 131 |
help='The source directory') |
| 132 |
parser.add_option('-n', '--noscripts', action='store_false', dest='autoscripts', |
| 133 |
help="Do not modify postinst and similar scripts.", |
| 134 |
default=True) |
| 135 |
parser.add_option('-S', '--use-system-packages', action='store_true', |
| 136 |
dest='use_system_packages', |
| 137 |
help="Set the --system-site-packages flag in virtualenv " |
| 138 |
"creation, allowing you to use system packages.", |
| 139 |
default=False) |
| 140 |
parser.add_option('--skip-install', action='store_true', |
| 141 |
default=False, |
| 142 |
dest='skip_install', |
| 143 |
help="Skip running pip install within the source directory.") |
| 144 |
parser.add_option('--install-suffix', metavar='DIRNAME', |
| 145 |
dest='install_suffix', |
| 146 |
help="Override installation path suffix") |
| 147 |
parser.add_option('--requirements', metavar='FILEPATH', |
| 148 |
dest='requirements_filename', |
| 149 |
help='Specify the filename for requirements.txt', |
| 150 |
default='requirements.txt') |
| 151 |
parser.add_option('--setuptools-test', |
| 152 |
dest='setuptools_test', |
| 153 |
default=False, |
| 154 |
action='callback', |
| 155 |
help='Run `setup.py test` when building the package', |
| 156 |
callback=_check_for_deprecated_options) |
| 157 |
|
| 158 |
# Ignore user-specified option bundles |
| 159 |
parser.add_option('-O', help=SUPPRESS_HELP) |
| 160 |
parser.add_option('-a', '--arch', dest="arch", |
| 161 |
help=("Act on architecture dependent packages that " |
| 162 |
"should be built for the build architecture. " |
| 163 |
"This option is ignored"), |
| 164 |
action="store", type="string") |
| 165 |
|
| 166 |
parser.add_option('-i', '--indep', dest="indep", |
| 167 |
help=("Act on all architecture independent packages. " |
| 168 |
"This option is ignored"), |
| 169 |
action="store_true") |
| 170 |
|
| 171 |
# Deprecated options |
| 172 |
parser.add_option('--pypi-url', metavar='URL', |
| 173 |
help=('!!DEPRECATED, use --index-url instead!! ' |
| 174 |
'Base URL of the PyPI server'), |
| 175 |
action='callback', |
| 176 |
dest='index_url', |
| 177 |
type='string', |
| 178 |
callback=_check_for_deprecated_options) |
| 179 |
parser.add_option('--no-test', |
| 180 |
help="!!DEPRECATED, this command has no effect. " |
| 181 |
"See --setuptools-test!! " |
| 182 |
"Don't run tests for the package. Useful " |
| 183 |
"for example when you have packaged with distutils.", |
| 184 |
action='callback', |
| 185 |
callback=_check_for_deprecated_options) |
| 186 |
return parser |