Skip to content

Commit

Permalink
Merge a0cd411 into a181df5
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemarmiesse authored Aug 2, 2019
2 parents a181df5 + a0cd411 commit 63d3196
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 92 deletions.
85 changes: 26 additions & 59 deletions sacred/host_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,29 @@

import cpuinfo

from sacred.utils import optional_kwargs_decorator
from sacred.settings import SETTINGS

__all__ = ('host_info_gatherers', 'get_host_info', 'host_info_getter')

host_info_gatherers = {}
"""Global dict of functions that are used to collect the host information."""
__all__ = ('get_host_info',)


class IgnoreHostInfo(Exception):
"""Used by host_info_getters to signal that this cannot be gathered."""


def get_host_info():
"""Collect some information about the machine this experiment runs on.
Returns
-------
dict
A dictionary with information about the CPU, the OS and the
Python version of this machine.
"""
host_info = {}
for k, v in host_info_gatherers.items():
try:
host_info[k] = v()
except IgnoreHostInfo:
pass
return host_info


@optional_kwargs_decorator
def host_info_getter(func, name=None):
"""
The decorated function is added to the process of collecting the host_info.
This just adds the decorated function to the global
``sacred.host_info.host_info_gatherers`` dictionary.
The functions from that dictionary are used when collecting the host info
using :py:func:`~sacred.host_info.get_host_info`.
Parameters
----------
func : callable
A function that can be called without arguments and returns some
json-serializable information.
name : str, optional
The name of the corresponding entry in host_info.
Defaults to the name of the function.
Returns
-------
The function itself.
"""
name = name or func.__name__
host_info_gatherers[name] = func
return func


# #################### Default Host Information ###############################

@host_info_getter(name='hostname')
def _hostname():
return platform.node()


@host_info_getter(name='os')
def _os():
return [platform.system(), platform.platform()]


@host_info_getter(name='python_version')
def _python_version():
return platform.python_version()


@host_info_getter(name='cpu')
def _cpu():
if platform.system() == "Windows":
return _get_cpu_by_pycpuinfo()
Expand All @@ -102,7 +46,6 @@ def _cpu():
return _get_cpu_by_pycpuinfo()


@host_info_getter(name='gpus')
def _gpus():
if not SETTINGS.HOST_INFO.INCLUDE_GPU_INFO:
return
Expand Down Expand Up @@ -130,12 +73,36 @@ def _gpus():
return gpu_info


@host_info_getter(name='ENV')
def _environment():
keys_to_capture = SETTINGS.HOST_INFO.CAPTURED_ENV
return {k: os.environ[k] for k in keys_to_capture if k in os.environ}


def get_host_info():
"""Collect some information about the machine this experiment runs on.
Returns
-------
dict
A dictionary with information about the CPU, the OS and the
Python version of this machine.
"""
host_info_gatherers = [('hostname', _hostname),
('os', _os),
('python_version', _python_version),
('cpu', _cpu),
('gpus', _gpus),
('ENV', _environment)]
host_info = {}
for k, v in host_info_gatherers:
try:
host_info[k] = v()
except IgnoreHostInfo:
pass
return host_info


# ################### Get CPU Information ###############################


Expand Down
34 changes: 1 addition & 33 deletions tests/test_host_info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python
# coding=utf-8

from sacred.host_info import (get_host_info, host_info_getter,
host_info_gatherers)
from sacred.host_info import get_host_info


def test_get_host_info():
Expand All @@ -11,34 +10,3 @@ def test_get_host_info():
assert isinstance(host_info['cpu'], str)
assert isinstance(host_info['os'], (tuple, list))
assert isinstance(host_info['python_version'], str)


def test_host_info_decorator():
try:
assert 'greeting' not in host_info_gatherers

@host_info_getter
def greeting():
return "hello"

assert 'greeting' in host_info_gatherers
assert host_info_gatherers['greeting'] == greeting
assert get_host_info()['greeting'] == 'hello'
finally:
del host_info_gatherers['greeting']


def test_host_info_decorator_with_name():
try:
assert 'foo' not in host_info_gatherers

@host_info_getter(name='foo')
def greeting():
return "hello"

assert 'foo' in host_info_gatherers
assert 'greeting' not in host_info_gatherers
assert host_info_gatherers['foo'] == greeting
assert get_host_info()['foo'] == 'hello'
finally:
del host_info_gatherers['foo']

0 comments on commit 63d3196

Please sign in to comment.