-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-131524: Update platform CLI to use argparse #131542
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
base: main
Are you sure you want to change the base?
Changes from all commits
5eed43f
119de84
994f46f
a0bb407
a21a6be
b4e0ed2
67cebfd
9ca5a96
2c9a01c
fb133e1
5fd4601
6381470
1f230f2
a5dcbf2
48e3559
cd389f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1464,9 +1464,38 @@ def invalidate_caches(): | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
### Command line interface | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if __name__ == '__main__': | ||||||||||||||||||||||||||||||
# Default is to print the aliased verbose platform string | ||||||||||||||||||||||||||||||
terse = ('terse' in sys.argv or '--terse' in sys.argv) | ||||||||||||||||||||||||||||||
aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv) | ||||||||||||||||||||||||||||||
def _parse_args(args: list[str] | None): | ||||||||||||||||||||||||||||||
import argparse | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
parser = argparse.ArgumentParser() | ||||||||||||||||||||||||||||||
parser.add_argument("args", nargs="*", choices=["nonaliased", "terse"]) | ||||||||||||||||||||||||||||||
parser.add_argument( | ||||||||||||||||||||||||||||||
"--terse", | ||||||||||||||||||||||||||||||
action="store_true", | ||||||||||||||||||||||||||||||
help=("return only the absolute minimum information needed to identify the " | ||||||||||||||||||||||||||||||
"platform"), | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
parser.add_argument( | ||||||||||||||||||||||||||||||
"--nonaliased", | ||||||||||||||||||||||||||||||
dest="aliased", | ||||||||||||||||||||||||||||||
action="store_false", | ||||||||||||||||||||||||||||||
help=("disable system/OS name aliasing. If aliasing is enabled, " | ||||||||||||||||||||||||||||||
"some platforms will report system names which differ from " | ||||||||||||||||||||||||||||||
"their common names, e.g. SunOS will be reported as Solaris"), | ||||||||||||||||||||||||||||||
Comment on lines
+1482
to
+1484
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slightly shorter, with align:
Suggested change
Or if using Black style above:
Suggested change
|
||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return parser.parse_args(args) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def _main(args: list[str] | None = None): | ||||||||||||||||||||||||||||||
args = _parse_args(args) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
terse = args.terse or ("terse" in args.args) | ||||||||||||||||||||||||||||||
aliased = args.aliased and ('nonaliased' not in args.args) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
print(platform(aliased, terse)) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||||||
_main() | ||||||||||||||||||||||||||||||
sys.exit(0) | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realise this was in the original, but looks redundant, especially with a single call under
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import contextlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import io | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import itertools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import pickle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import platform | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import subprocess | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -741,5 +744,66 @@ def test_parse_os_release(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertEqual(len(info["SPECIALS"]), 5) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class CommandLineTest(unittest.TestCase): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def setUp(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
platform.invalidate_caches() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.addCleanup(platform.invalidate_caches) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def invoke_platform(self, *flags): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
output = io.StringIO() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with contextlib.redirect_stdout(output): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
platform._main(args=flags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return output.getvalue() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_unknown_flag(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with self.assertRaises(SystemExit): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
output = io.StringIO() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# suppress argparse error message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with contextlib.redirect_stderr(output): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_ = self.invoke_platform('--unknown') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertStartsWith(output, "usage: ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_invocation(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
flags = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"--terse", "--nonaliased", "terse", "nonaliased" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Harry-Lees marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for r in range(len(flags) + 1): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for combination in itertools.combinations(flags, r): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.invoke_platform(*combination) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_arg_parsing(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Due to backwards compatibility, the `aliased` and `terse` parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# are computed based on a combination of positional arguments and flags. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+776
to
+777
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to try and limit lines to 79 chars: https://peps.python.org/pep-0008/#maximum-line-length
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# This test tests that the arguments are correctly passed to the underlying | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# `platform.platform()` call. The parameters are two booleans for `aliased` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# and `terse`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["--nonaliased"], (False, False)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["nonaliased"], (False, False)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["--terse"], (True, True)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["terse"], (True, True)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["nonaliased", "terse"], (False, True)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["--nonaliased", "terse"], (False, True)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(["--terse", "nonaliased"], (False, True)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for flags, args in options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with self.subTest(flags=flags, args=args): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with mock.patch.object(platform, 'platform') as obj: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.invoke_platform(*flags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
obj.assert_called_once_with(*args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+779
to
+796
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps like this, to document the parameters with variable names?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_help(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
output = io.StringIO() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with self.assertRaises(SystemExit): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with contextlib.redirect_stdout(output): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
platform._main(args=["--help"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.assertStartsWith(output.getvalue(), "usage:") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add help message to :mod:`platform` command-line interface. Contributed by | ||
Harry Lees. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either align to opening delimiter:
Or I'd just go for Black style: