Debian Codegraph @

dh_virtualenv/cmdline.py

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
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 _set_builtin_venv(
81 option, opt_str, value, parser, *args, **kwargs
82 ):
83 if parser.values.setuptools:
84 raise OptionValueError(
85 '--setuptools flag is not supported by builtin venv module'
86 )
87 else:
88 parser.values.builtin_venv = True
89
90
91 def _set_setuptools(
92 option, opt_str, value, parser, *args, **kwargs
93 ):
94 if parser.values.builtin_venv:
95 raise OptionValueError(
96 '--setuptools flag is not supported by builtin venv module'
97 )
98 else:
99 parser.values.setuptools = True
100
101
102 def get_default_parser():
103 usage = '%prog [options]'
104 parser = DebhelperOptionParser(usage, version='%prog ' + version)
105 parser.add_option('-p', '--package', action='append', metavar='PACKAGE',
106 help='Act on the package(s) named PACKAGE')
107 parser.add_option('-N', '--no-package', action='append', metavar='PACKAGE',
108 help='Do not act on the specified package(s)')
109 parser.add_option('-v', '--verbose', action='store_true',
110 default=False, help='Turn on verbose mode')
111 parser.add_option('-s', '--setuptools', action='callback',
112 dest='setuptools',
113 callback=_set_setuptools,
114 default=False, help='Use Setuptools instead of Distribute')
115 parser.add_option('--extra-index-url', action='append', metavar='URL',
116 help='Extra index URL(s) to pass to pip.',
117 default=[])
118 parser.add_option('--preinstall', action='append', metavar='PACKAGE',
119 help=('Package(s) to install before processing '
120 'requirements.txt.'),
121 default=[])
122 parser.add_option('--extras', action='append', metavar='NAME',
123 help=('Activate one or more extras of the main package.'),
124 default=[])
125 parser.add_option('--pip-tool', default='pip', metavar='EXECUTABLE',
126 help="Executable that will be used to install "
127 "requirements after the preinstall stage. Usually "
128 "you'll install this program by using the --preinstall "
129 "argument. The replacement is expected to be found in "
130 "the virtualenv's bin/ directory.")
131 parser.add_option('--upgrade-pip', action='store_true', default=False,
132 help='Upgrade pip to the latest available version')
133 parser.add_option('--upgrade-pip-to', default='', metavar='VERSION',
134 help='Upgrade pip to a specific version')
135 parser.add_option('--extra-pip-arg', action='append', metavar='ARG',
136 help='One or more extra args for the pip binary.'
137 'You can use this flag multiple times to pass in'
138 ' parameters to pip.', default=[])
139 parser.add_option('--extra-virtualenv-arg', action='append', metavar='ARG',
140 help='One or more extra args for the virtualenv binary.'
141 'You can use this flag multiple times to pass in'
142 ' parameters to the virtualenv binary.', default=[])
143 parser.add_option('--index-url', metavar='URL',
144 help='Base URL of the PyPI server',
145 action='callback',
146 type='string',
147 dest='index_url',
148 callback=_check_for_deprecated_options)
149 parser.add_option('--python', metavar='EXECUTABLE',
150 help='The Python command to use')
151 parser.add_option('--builtin-venv', action='callback',
152 dest='builtin_venv',
153 callback=_set_builtin_venv,
154 help='Use the built-in venv module. Only works on '
155 'Python 3.4 and later.')
156 parser.add_option('-D', '--sourcedirectory', dest='sourcedirectory',
157 help='The source directory')
158 parser.add_option('-n', '--noscripts', action='store_false', dest='autoscripts',
159 help="Do not modify postinst and similar scripts.",
160 default=True)
161 parser.add_option('-S', '--use-system-packages', action='store_true',
162 dest='use_system_packages',
163 help="Set the --system-site-packages flag in virtualenv "
164 "creation, allowing you to use system packages.",
165 default=False)
166 parser.add_option('--skip-install', action='store_true',
167 default=False,
168 dest='skip_install',
169 help="Skip running pip install within the source directory.")
170 parser.add_option('--install-suffix', metavar='DIRNAME',
171 dest='install_suffix',
172 help="Override installation path suffix")
173 parser.add_option('--requirements', metavar='FILEPATH',
174 dest='requirements_filename',
175 help='Specify the filename for requirements.txt',
176 default='requirements.txt')
177 parser.add_option('--setuptools-test',
178 dest='setuptools_test',
179 default=False,
180 action='callback',
181 help='Run `setup.py test` when building the package',
182 callback=_check_for_deprecated_options)
183
184 # Ignore user-specified option bundles
185 parser.add_option('-O', help=SUPPRESS_HELP)
186 parser.add_option('-a', '--arch', dest="arch",
187 help=("Act on architecture dependent packages that "
188 "should be built for the build architecture. "
189 "This option is ignored"),
190 action="store", type="string")
191
192 parser.add_option('-i', '--indep', dest="indep",
193 help=("Act on all architecture independent packages. "
194 "This option is ignored"),
195 action="store_true")
196
197 # Deprecated options
198 parser.add_option('--pypi-url', metavar='URL',
199 help=('!!DEPRECATED, use --index-url instead!! '
200 'Base URL of the PyPI server'),
201 action='callback',
202 dest='index_url',
203 type='string',
204 callback=_check_for_deprecated_options)
205 parser.add_option('--no-test',
206 help="!!DEPRECATED, this command has no effect. "
207 "See --setuptools-test!! "
208 "Don't run tests for the package. Useful "
209 "for example when you have packaged with distutils.",
210 action='callback',
211 callback=_check_for_deprecated_options)
212 return parser