|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import platform |
| 5 | +import sys |
| 6 | +from optparse import OptionParser |
| 7 | + |
| 8 | +UNAME_ATTRS = frozenset("mnrsv") |
| 9 | + |
| 10 | + |
| 11 | +def uname(opts): |
| 12 | + extras: list[str] = [] |
| 13 | + |
| 14 | + if opts.a: |
| 15 | + for attr in UNAME_ATTRS: |
| 16 | + setattr(opts, attr, True) |
| 17 | + |
| 18 | + if opts.a or opts.p: |
| 19 | + if processor := platform.processor(): |
| 20 | + extras.append(processor) |
| 21 | + elif opts.p: |
| 22 | + extras.append("unknown") |
| 23 | + |
| 24 | + if opts.i: |
| 25 | + extras.append("unknown") |
| 26 | + |
| 27 | + if opts.o: |
| 28 | + extras.append("unknown") |
| 29 | + |
| 30 | + if not extras and not any({getattr(opts, attr) for attr in UNAME_ATTRS}): |
| 31 | + opts.s = True |
| 32 | + |
| 33 | + uname = os.uname() |
| 34 | + |
| 35 | + print( |
| 36 | + " ".join( |
| 37 | + [ |
| 38 | + getattr(uname, attribute) |
| 39 | + for attribute in [ |
| 40 | + "sysname", |
| 41 | + "nodename", |
| 42 | + "release", |
| 43 | + "version", |
| 44 | + "machine", |
| 45 | + ] |
| 46 | + if getattr(opts, attribute[0]) |
| 47 | + ] |
| 48 | + + extras |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + parser = OptionParser( |
| 55 | + usage="Usage: %prog [OPTION]...", |
| 56 | + description="Print system information.", |
| 57 | + add_help_option=False, |
| 58 | + ) |
| 59 | + parser.add_option("--help", action="help", help="show usage information and exit") |
| 60 | + |
| 61 | + parser.add_option("-a", action="store_true", help="print all") |
| 62 | + parser.add_option("-m", action="store_true", help="machine hardware type") |
| 63 | + parser.add_option("-n", action="store_true", help="hostname") |
| 64 | + parser.add_option("-r", action="store_true", help="kernel release") |
| 65 | + parser.add_option("-s", action="store_true", help="kernel name (default)") |
| 66 | + parser.add_option("-v", action="store_true", help="kernel version") |
| 67 | + parser.add_option("-p", action="store_true", help="processor type") |
| 68 | + parser.add_option( |
| 69 | + "-i", action="store_true", help="hardware platform (unimplemented)" |
| 70 | + ) |
| 71 | + parser.add_option( |
| 72 | + "-o", action="store_true", help="operating system (unimplemented)" |
| 73 | + ) |
| 74 | + |
| 75 | + opts, args = parser.parse_args() |
| 76 | + |
| 77 | + if args: |
| 78 | + parser.error(f"extra operand '{args[0]}'") |
| 79 | + sys.exit(1) |
| 80 | + |
| 81 | + uname(opts) |
0 commit comments