-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11195 from pradyunsg/distutils-replace-fancygetop…
…t-with-getopt Replace `distutils.fancy_getopt` with `getopt`
- Loading branch information
Showing
2 changed files
with
27 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
from distutils.errors import DistutilsArgError | ||
from distutils.fancy_getopt import FancyGetopt | ||
from getopt import GetoptError, getopt | ||
from typing import Dict, List | ||
|
||
_options = [ | ||
("exec-prefix=", None, ""), | ||
("home=", None, ""), | ||
("install-base=", None, ""), | ||
("install-data=", None, ""), | ||
("install-headers=", None, ""), | ||
("install-lib=", None, ""), | ||
("install-platlib=", None, ""), | ||
("install-purelib=", None, ""), | ||
("install-scripts=", None, ""), | ||
("prefix=", None, ""), | ||
("root=", None, ""), | ||
("user", None, ""), | ||
"exec-prefix=", | ||
"home=", | ||
"install-base=", | ||
"install-data=", | ||
"install-headers=", | ||
"install-lib=", | ||
"install-platlib=", | ||
"install-purelib=", | ||
"install-scripts=", | ||
"prefix=", | ||
"root=", | ||
"user", | ||
] | ||
|
||
|
||
# typeshed doesn't permit Tuple[str, None, str], see python/typeshed#3469. | ||
_distutils_getopt = FancyGetopt(_options) # type: ignore | ||
|
||
|
||
def parse_distutils_args(args: List[str]) -> Dict[str, str]: | ||
"""Parse provided arguments, returning an object that has the | ||
matched arguments. | ||
"""Parse provided arguments, returning an object that has the matched arguments. | ||
Any unknown arguments are ignored. | ||
""" | ||
result = {} | ||
for arg in args: | ||
try: | ||
_, match = _distutils_getopt.getopt(args=[arg]) | ||
except DistutilsArgError: | ||
parsed_opt, _ = getopt(args=[arg], shortopts="", longopts=_options) | ||
except GetoptError: | ||
# We don't care about any other options, which here may be | ||
# considered unrecognized since our option list is not | ||
# exhaustive. | ||
pass | ||
else: | ||
result.update(match.__dict__) | ||
continue | ||
|
||
if not parsed_opt: | ||
continue | ||
|
||
option = parsed_opt[0] | ||
name_from_parsed = option[0][2:].replace("-", "_") | ||
value_from_parsed = option[1] or "true" | ||
result[name_from_parsed] = value_from_parsed | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters