Skip to content
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

Add sources kwarg/flag #195

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ __pycache__
*.pyc
*.log
*.trace
*.egg-info
77 changes: 58 additions & 19 deletions cpuinfo/cpuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@

CAN_CALL_CPUID_IN_SUBPROCESS = True

SOURCES = ["WMIC",
"REGISTRY",
"PROC_CPUINFO",
"CPUFREQ_INFO",
"LSCPU",
"SYSCTL",
"KSTAT",
"DMESG",
"DMESG_BOOT",
"LSPROP",
"SYSINFO",
"CPUID",
"UNAME"]

g_trace = None


Expand Down Expand Up @@ -2652,11 +2666,13 @@ def _get_cpu_info_from_platform_uname():
g_trace.fail(err)
return {}

def _get_cpu_info_internal():
def _get_cpu_info_internal(sources=None):
'''
Returns the CPU info by using the best sources of information for your OS.
Returns {} if nothing is found.
'''
if not sources:
sources = SOURCES

g_trace.write('!' * 80)

Expand Down Expand Up @@ -2685,50 +2701,63 @@ def _get_cpu_info_internal():
g_trace.write("arch_string_raw: {0}".format(info['arch_string_raw']))

# Try the Windows wmic
_copy_new_fields(info, _get_cpu_info_from_wmic())
if "WMIC" in sources:
_copy_new_fields(info, _get_cpu_info_from_wmic())

# Try the Windows registry
_copy_new_fields(info, _get_cpu_info_from_registry())
if "REGISTRY" in sources:
_copy_new_fields(info, _get_cpu_info_from_registry())

# Try /proc/cpuinfo
_copy_new_fields(info, _get_cpu_info_from_proc_cpuinfo())
if "PROC_CPUINFO" in sources:
_copy_new_fields(info, _get_cpu_info_from_proc_cpuinfo())

# Try cpufreq-info
_copy_new_fields(info, _get_cpu_info_from_cpufreq_info())
if "CPUFREQ_INFO" in sources:
_copy_new_fields(info, _get_cpu_info_from_cpufreq_info())

# Try LSCPU
_copy_new_fields(info, _get_cpu_info_from_lscpu())
if "LSCPU" in sources:
_copy_new_fields(info, _get_cpu_info_from_lscpu())

# Try sysctl
_copy_new_fields(info, _get_cpu_info_from_sysctl())
if "SYSCTL" in sources:
_copy_new_fields(info, _get_cpu_info_from_sysctl())

# Try kstat
_copy_new_fields(info, _get_cpu_info_from_kstat())
if "KSTAT" in sources:
_copy_new_fields(info, _get_cpu_info_from_kstat())

# Try dmesg
_copy_new_fields(info, _get_cpu_info_from_dmesg())
if "DMESG" in sources:
_copy_new_fields(info, _get_cpu_info_from_dmesg())

# Try /var/run/dmesg.boot
_copy_new_fields(info, _get_cpu_info_from_cat_var_run_dmesg_boot())
if "DMESG_BOOT" in sources:
_copy_new_fields(info, _get_cpu_info_from_cat_var_run_dmesg_boot())

# Try lsprop ibm,pa-features
_copy_new_fields(info, _get_cpu_info_from_ibm_pa_features())
if "LSPROP" in sources:
_copy_new_fields(info, _get_cpu_info_from_ibm_pa_features())

# Try sysinfo
_copy_new_fields(info, _get_cpu_info_from_sysinfo())
if "SYSINFO" in sources:
_copy_new_fields(info, _get_cpu_info_from_sysinfo())

# Try querying the CPU cpuid register
# FIXME: This should print stdout and stderr to trace log
_copy_new_fields(info, _get_cpu_info_from_cpuid())
if "CPUID" in sources:
_copy_new_fields(info, _get_cpu_info_from_cpuid())

# Try platform.uname
_copy_new_fields(info, _get_cpu_info_from_platform_uname())
if "UNAME" in sources:
_copy_new_fields(info, _get_cpu_info_from_platform_uname())

g_trace.write('!' * 80)

return info

def get_cpu_info_json():
def get_cpu_info_json(sources=None):
'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a json string
Expand All @@ -2740,7 +2769,7 @@ def get_cpu_info_json():

# If running under pyinstaller, run normally
if getattr(sys, 'frozen', False):
info = _get_cpu_info_internal()
info = _get_cpu_info_internal(sources=sources)
output = json.dumps(info)
output = "{0}".format(output)
# if not running under pyinstaller, run in another process.
Expand All @@ -2750,6 +2779,8 @@ def get_cpu_info_json():
from subprocess import Popen, PIPE

command = [sys.executable, __file__, '--json']
if sources:
command += ["--sources", ",".join(sources)]
p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE)
output = p1.communicate()[0]

Expand All @@ -2760,15 +2791,15 @@ def get_cpu_info_json():

return output

def get_cpu_info():
def get_cpu_info(sources=None):
'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a dict
'''

import json

output = get_cpu_info_json()
output = get_cpu_info_json(sources=sources)

# Convert JSON to Python with non unicode strings
output = json.loads(output, object_hook = _utf_to_str)
Expand All @@ -2784,6 +2815,7 @@ def main():
parser.add_argument('--json', action='store_true', help='Return the info in JSON format')
parser.add_argument('--version', action='store_true', help='Return the version of py-cpuinfo')
parser.add_argument('--trace', action='store_true', help='Traces code paths used to find CPU info to file')
parser.add_argument('--sources', help='Sources to use when gathering data')
args = parser.parse_args()

global g_trace
Expand All @@ -2795,7 +2827,14 @@ def main():
sys.stderr.write(str(err) + "\n")
sys.exit(1)

info = _get_cpu_info_internal()
try:
sources = [s for s in args.sources.split(",") if s]
if not sources:
sources = None
except AttributeError:
sources = None

info = _get_cpu_info_internal(sources=sources)

if not info:
sys.stderr.write("Failed to find cpu info\n")
Expand Down