Linux Distribution Detection¶
Autotest has a facility that lets tests determine quite precisely the distribution they’re running on.
This is done through the implementation and registration of probe classes.
Those probe classes can check for given characteristics of the running operating system, such as the existence of a release file, its contents or even the existence of a binary that is exclusive to a distribution (such as package managers).
Quickly detecting the Linux Distribution¶
The autotest.client.shared.distro
module provides many APIs, but
the simplest one to use is the detect()
.
Its usage is quite straighforward:
>>> from autotest.client.shared import distro
>>> detected_distro = distro.detect()
The returned distro can be the result of a probe validating the distribution
detection, or the not so useful UNKNOWN_DISTRO
.
To access the relevant data on a LinuxDistro
, simply use the
attributes:
name
version
release
arch
Example:
>>> detected_distro = distro.detect()
>>> print detected_distro.name
redhat
The unknown Linux Distribution¶
When the detection mechanism can’t precily detect the Linux Distribution, it
will still return a LinuxDistro
instance, but a special one that
contains special values for its name, version, etc.
-
autotest.client.shared.distro.
UNKNOWN_DISTRO
= <LinuxDistro: name=unknown, version=0, release=0, arch=unknown>¶ The distribution that is used when the exact one could not be found
Writing a Linux Distribution Probe¶
The easiest way to write a probe for your target Linux Distribution is to make
use of the features of the Probe
class.
Even if you do plan to use the features documented here, keep in mind that all
probes should inherit from Probe
and provide a basic interface.
Checking the Distrution name only¶
The most trivial probe is one that checks the existence of a file and returns the distribution name:
class RedHatProbe(Probe):
CHECK_FILE = '/etc/redhat-release'
CHECK_FILE_DISTRO_NAME = 'redhat'
To make use of a probe, it’s necessary to register it:
>>> from autotest.client.shared import distro
>>> distro.register_probe(RedHatProbe)
And that’s it. This is a valid example, but will give you nothing but the distro name.
You should usually aim for more information, such as the version numbers.
Checking the Distribution name and version numbers¶
If you want to also detect the distro version numbers (and you should), then
it’s possible to use the Probe.CHECK_VERSION_REGEX
feature
of the Probe
class.
-
Probe.
CHECK_VERSION_REGEX
= None¶ A regular expresion that will be run on the file pointed to by
CHECK_FILE_EXISTS
If your regex has two or more groups, that is, it will look for and save
references to two or more string, it will consider the second group to be
the LinuxDistro.release
number.
Probe Scores¶
To increase the accuracy of the probe results, it’s possible to register a score for a probe. If a probe wants to, it can register a score for itself.
Probes that return a score will be given priority over probes that don’t.
The score should be based on the number of checks that ran during the probe to account for its accuracy.
Probes should not be given a higher score because their checks look more precise than everyone else’s.
Registering your own probes¶
Not only the probes that ship with autotest can be used, but your custom probe classes can be added to the detection system.
To do that simply call the function register_probe()
:
-
autotest.client.shared.distro.
register_probe
(probe_class)[source]¶ Register a probe to be run during autodetection
Now, remember that for things to happen smootlhy your registered probe must be
a subclass of Probe
.
API Reference¶
Probe
¶
-
class
autotest.client.shared.distro.
Probe
[source]¶ Probes the machine and does it best to confirm it’s the right distro
-
CHECK_FILE
= None¶ Points to a file that can determine if this machine is running a given Linux Distribution. This servers a first check that enables the extra checks to carry on.
-
CHECK_FILE_CONTAINS
= None¶ Sets the content that should be checked on the file pointed to by
CHECK_FILE_EXISTS
. Leave it set to None (its default) to check only if the file exists, and not check its contents
-
CHECK_FILE_DISTRO_NAME
= None¶ The name of the Linux Distribution to be returned if the file defined by
CHECK_FILE_EXISTS
exist.
-
CHECK_VERSION_REGEX
= None A regular expresion that will be run on the file pointed to by
CHECK_FILE_EXISTS
-
check_name_for_file
()[source]¶ Checks if this class will look for a file and return a distro
The conditions that must be true include the file that identifies the distro file being set (
CHECK_FILE
) and the name of the distro to be returned (CHECK_FILE_DISTRO_NAME
)
-
check_name_for_file_contains
()[source]¶ Checks if this class will look for text on a file and return a distro
The conditions that must be true include the file that identifies the distro file being set (
CHECK_FILE
), the text to look for inside the distro file (CHECK_FILE_CONTAINS
) and the name of the distro to be returned (CHECK_FILE_DISTRO_NAME
)
-
get_distro
()[source]¶ Returns the
LinuxDistro
this probe detected
-
name_for_file
()[source]¶ Get the distro name if the
CHECK_FILE
is set and exists
-
name_for_file_contains
()[source]¶ Get the distro if the
CHECK_FILE
is set and has content
-
register_probe()
¶
-
autotest.client.shared.distro.
register_probe
(probe_class)[source] Register a probe to be run during autodetection
detect()
¶
-
autotest.client.shared.distro.
detect
()[source]¶ Attempts to detect the Linux Distribution running on this machine
Returns: the detected LinuxDistro
orUNKNOWN_DISTRO
Return type: LinuxDistro