OPSI.Util package

Module contents

General utility functions.

This module holds various utility functions for the work with opsi. This includes functions for (de)serialisation, converting classes from or to JSON, working with librsync and more.

copyright:uib GmbH <info@uib.de>
author:Jan Schneider <j.schneider@uib.de>
author:Niko Wenselowski <n.wenselowski@uib.de>
license:GNU Affero General Public License version 3
exception OPSI.Util.BlowfishError

Bases: OPSI.Util.CryptoError

exception OPSI.Util.CryptoError

Bases: exceptions.ValueError

class OPSI.Util.PickleString

Bases: str

OPSI.Util.blowfishDecrypt(key, crypt)

Takes hex-encoded, blowfish-encrypted string, returns cleartext string.

Raises:BlowfishError – In case things go wrong.
Return type:unicode
OPSI.Util.blowfishEncrypt(key, cleartext)

Takes cleartext string, returns hex-encoded, blowfish-encrypted string.

Raises:BlowfishError – In case things go wrong.
Return type:unicode
OPSI.Util.chunk(iterable, size)

Returns chunks (parts) of a specified size from iterable. It will not pad (fill) the chunks.

This works lazy and therefore can be used with any iterable without much overhead.

Original recipe from http://stackoverflow.com/a/22045226

OPSI.Util.compareVersions(v1, condition, v2)
OPSI.Util.decryptWithPrivateKeyFromPEMFile(data, filename)
OPSI.Util.deserialize(obj, preventObjectCreation=False)
OPSI.Util.encryptWithPublicKeyFromX509CertificatePEMFile(data, filename)
OPSI.Util.findFiles(directory, prefix=u'', excludeDir=None, excludeFile=None, includeDir=None, includeFile=None, returnDirs=True, returnLinks=True, followLinks=False, repository=None)
OPSI.Util.flattenSequence(sequence)

Flattens nested sequences so that only a flat list will be returned.

Returntype:list
OPSI.Util.formatFileSize(sizeInBytes)
OPSI.Util.fromJson(obj, objectType=None, preventObjectCreation=False)
OPSI.Util.generateOpsiHostKey(forcePython=False)

Generates an random opsi host key.

This will try to make use of an existing random device. As a fallback the generation is done in plain Python.

Parameters:forcePython – Force the usage of Python for host key generation.
OPSI.Util.getGlobalConfig(name, configFile=u'/etc/opsi/global.conf')

Reads the value of name from the global configuration.

Parameters:configFile (str) – The path of the config file.
OPSI.Util.getfqdn(name='', conf=None)

Get the fqdn.

If name is not given it will try various ways to get a valid fqdn from the current host. If conf but no name is given it will try to read the FQDN from the specified configuration file.

OPSI.Util.ipAddressInNetwork(ipAddress, networkAddress)

Checks if the given IP address is in the given network range. Returns True if the given address is part of the network. Returns False if the given address is not part of the network.

Parameters:
  • ipAddress (str) – The IP which we check.
  • networkAddress (str) – The network address written with slash notation.
OPSI.Util.isRegularExpressionPattern(object)
OPSI.Util.librsyncDeltaFile(filename, signature, deltafile)
OPSI.Util.librsyncPatchFile(oldfile, deltafile, newfile)
OPSI.Util.librsyncSignature(filename, base64Encoded=True)
OPSI.Util.md5sum(filename)

Returns the md5sum of the given file.

OPSI.Util.objectToBash(obj, bashVars=None, level=0)
OPSI.Util.objectToBeautifiedText(obj, level=0)
OPSI.Util.objectToHtml(obj, level=0)
OPSI.Util.randomString(length, characters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')

Generates a random string for a given length.

Parameters:characters – The characters to choose from. This defaults to 0-9a-Z.
OPSI.Util.removeDirectory(directory)

Removing an directory.

If this fails with shutil it will try to use system calls.

New in version 4.0.5.1.

Parameters:directory – Path to the directory
Tye directory:str
OPSI.Util.removeUnit(x)

Take a string representing a byte-based size and return the value in bytes.

Parameters:x – str
Returns:x in bytes.
Return type:int or float
OPSI.Util.replaceSpecialHTMLCharacters(text)
OPSI.Util.serialize(obj)
OPSI.Util.timestamp(secs=0, dateOnly=False)

Returns a timestamp of the current system time format: YYYY-mm-dd[ HH:MM:SS]

OPSI.Util.toJson(obj, ensureAscii=False)