The OS module provides common operating system functions that are platform independent.
Note that for most of the functions/methods on this page, arguments are not listed. You should look at the source code to determine the arguments needed.
- Finding a suitable version of python installed in the OS for compatibility reasons
- Retrieving general operating system information
- Interacting with operating system services - start, stop, restart, set boot state, etc.
- Interacting with the OS package manager (apt, yum, macports) - installing, removing, upgrading, etc.
- A simple GPG encryption/decryption library
The following is accessible by using the PyVersion module:
import PyVersion
PyVersion.PY_EXE
PyVersion.checkSuitablePyExe()
The following is accessible by using the OS module:
import OS
OS.flavor
OS.version
Always returns a list or tuple of 3 ints, ex. [2, 6, 45]
OS.getKernelVersion()
OS.isUnix()
OS.hasRootPermissions()
There are lots of arguments here. Refer to the code!
OS.runCMD()
The following is accessible by using the services module:
import OS.svc
These features are self-explanatory with a quick glance at the code.
The following is accessible by using the packages module:
import OS.pkg
OS.pkg is more complex than some modules. Most of this is due to differences between the underlying package managers supported by OS.pkg, including:
yum
for CentOS and Red Hat is supported natively through python, since yum itself is written in pythonapt
for Debian, Ubuntu, etc. is supported by using thepython-apt
module which will be installed along with thepy.OS
package if your OS is apt-basedmacports
for Mac OSX is supported (sadly) only by parsing command line output using theport
command
The good news is that one similar API can be used to work with packages for all 3 package managers across different platforms.
The bad news is that sometimes a package will have a different name across different package managers, even though they effectively install the same thing. To work around this, you can do for example:
if OS.pkg.isYumOS():
OS.pkg.install("yum package name here")
elif OS.pkg.isAptOS():
OS.pkg.install("apt-get package name here")
elif OS.pkg.isPortOS():
OS.pkg.install("macports package name here")
Also, when using install, update, isAvailable, and similar functions, there is no need to manually update the list of available packages from the internet. This is done automatically when OS.pkg is imported.
OS.pkg.isSupported()
OS.pkg.isInstalled()
OS.pkg.isAvailable()
See above. You can also install an iterable of packages like:
OS.pkg.install(("nano", "vim", "emacs"))
OS.pkg.update()
OS.pkg.remove()
- On apt, an additional argument
purgeConfigFiles
can be specified which, whenTrue
, is identical toapt-get purge
OS.pkg.updateKernel()
OS.pkg.installRepo_RPM()
OS.pkg.installRepo_allKeys()
This is a convenience method for installing EPEL. The latest EPEL RPM will be fetched.
OS.pkg.installRepo_EPEL()
The following is accessible by using the GPG module:
from OS import GPG
GPG.importPublicKey(filePath)
GPG.importPrivateKey(filePath)
id = GPG.getKeyID(filename)
GPG.signKey(id)
Note that this is an interactive only function and requires the user's input, due to a limitation in the gnupg command line.
GPG.markKeyTrusted(id)
GPG.isKeyImported(id)
outputFilePath = GPG.encrypt(inputFilePath, keyID)
Note that password
is only required if the private key requires a password.
outputFilePath = GPG.decrypt(inputFilePath, password)