signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def __init__(self):
WSimplePadding.__init__(self)<EOL>
Create new padding object
f9885:c2:m0
@verify_type(data=bytes, total_length=int, padding_symbol=bytes)<EOL><INDENT>@verify_value(total_length=lambda x: x > <NUM_LIT:0>, padding_symbol=lambda x: len(x) == <NUM_LIT:1>)<EOL>def _fill(self, data, total_length, padding_symbol):<DEDENT>
delta = total_length - len(data)<EOL>return ((padding_symbol * random_int(delta)) + data).ljust(total_length, padding_symbol)<EOL>
Overridden :meth:`.WSimplePadding._fill` method. This methods adds padding symbol at the beginning and at the end of the specified data. :param data: data to append to :param total_length: target length :param padding_symbol: symbol to pad :return: bytes
f9885:c3:m0
@verify_type(data=bytes, block_size=int)<EOL><INDENT>@verify_value(block_size=lambda x: x > <NUM_LIT:0>)<EOL>def reverse_pad(self, data, block_size):<DEDENT>
padding_symbol = self.padding_symbol()<EOL>return data.lstrip(padding_symbol).rstrip(padding_symbol)<EOL>
:meth:`.WBlockPadding.reverse_pad` method implementation
f9885:c3:m1
@verify_type(data=bytes, block_size=int)<EOL><INDENT>@verify_value(block_size=lambda x: x > <NUM_LIT:0>)<EOL>def pad(self, data, block_size):<DEDENT>
pad_byte = block_size - (len(data) % block_size)<EOL>return data + bytes([pad_byte] * pad_byte)<EOL>
:meth:`.WBlockPadding.pad` method implementation
f9885:c4:m0
@verify_type(data=bytes, block_size=int)<EOL><INDENT>@verify_value(data=lambda x: len(x) > <NUM_LIT:0>, block_size=lambda x: x > <NUM_LIT:0>)<EOL>def reverse_pad(self, data, block_size):<DEDENT>
pad_byte = data[-<NUM_LIT:1>]<EOL>if pad_byte > block_size:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>padding = bytes([pad_byte] * pad_byte)<EOL>if data[-pad_byte:] != padding:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return data[:-pad_byte]<EOL>
:meth:`.WBlockPadding.reverse_pad` method implementation
f9885:c4:m1
@verify_type(key_size=int, block_cipher_mode=str, padding=(None, WBlockPadding), init_sequence=bytes)<EOL><INDENT>@verify_value(key_size=lambda x: x in WAESMode.__valid_key_sizes__)<EOL>@verify_value(block_cipher_mode=lambda x: x in WAESMode.__modes_descriptor__.keys())<EOL>def __init__(<EOL>self, key_size, block_cipher_mode, init_sequence, padding=None<EOL>):<DEDENT>
self.__key_size = key_size<EOL>self.__mode = block_cipher_mode<EOL>self.__padding = padding<EOL>self.__sequence_chopper = WAESMode.SequenceChopper(key_size, block_cipher_mode, init_sequence)<EOL>if block_cipher_mode == '<STR_LIT>':<EOL><INDENT>mode_code = modes.CBC(self.__sequence_chopper.initialization_vector())<EOL><DEDENT>elif block_cipher_mode == '<STR_LIT>':<EOL><INDENT>mode_code = modes.CTR(self.__sequence_chopper.initialization_counter_value())<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.__cipher_args = (AES(self.__sequence_chopper.secret()), mode_code)<EOL>self.__cipher_kwargs = {'<STR_LIT>': default_backend()}<EOL>
Create new AES-mode. :param key_size: secret length :param block_cipher_mode: name of block cipher mode of operation :param padding: padding object (if required) :param init_sequence: AES secret with initialization vector or counter value
f9885:c5:m0
def key_size(self):
return self.__key_size<EOL>
Return cipher secret key size :return: int
f9885:c5:m1
def mode(self):
return self.__mode<EOL>
Return block cipher mode of operation name :return:
f9885:c5:m2
def padding(self):
return self.__padding<EOL>
Return padding object :return: WBlockPadding or None
f9885:c5:m3
def initialization_vector(self):
return self.__sequence_chopper.initialization_vector()<EOL>
Return currently used initialization vector or None if vector is not used :return: bytes or None
f9885:c5:m4
def initialization_counter_value(self):
return self.__sequence_chopper.initialization_counter_value()<EOL>
Return currently used initialization counter value or None if counter is not used :return: int or None
f9885:c5:m5
def aes_args(self):
return self.__cipher_args<EOL>
Generate and return position-dependent arguments, that are used in :meth:`.AES.new` method :return: tuple
f9885:c5:m6
def aes_kwargs(self):
return self.__cipher_kwargs<EOL>
Generate and return position-independent (named) arguments, that are used in :meth:`.AES.new` method :return: dict
f9885:c5:m7
@classmethod<EOL><INDENT>def init_sequence_length(cls, key_size, block_cipher_mode):<DEDENT>
return WAESMode.SequenceChopper.required_sequence_length(key_size, block_cipher_mode)<EOL>
Return required byte-sequence length :param key_size: secret size :param block_cipher_mode: name of block cipher mode of operation :return: int
f9885:c5:m8
@classmethod<EOL><INDENT>@verify_type(name=str)<EOL>def parse_cipher_name(cls, name):<DEDENT>
r = cls.__mode_re__.match(name.upper())<EOL>if r is None:<EOL><INDENT>raise ValueError('<STR_LIT>' % name)<EOL><DEDENT>key_size = int(int(r.group(<NUM_LIT:2>)) / <NUM_LIT:8>)<EOL>block_mode = '<STR_LIT>' % r.group(<NUM_LIT:4>)<EOL>if key_size not in cls.__valid_key_sizes__:<EOL><INDENT>raise ValueError('<STR_LIT>' % key_size)<EOL><DEDENT>if block_mode not in cls.__modes_descriptor__.keys():<EOL><INDENT>raise ValueError('<STR_LIT>' % block_mode)<EOL><DEDENT>return key_size, block_mode<EOL>
Parse cipher name (name like 'aes_256_cbc' or 'AES-128-CTR'). Also this method validates If the cipher is supported by this class. If no - exception is raised :param name: name to parse :return: tuple where the first element is a key size in bytes (int) and the second element - block cipher mode of operation (str) (for example: (16, 'AES-CTR') or (24, 'AES-CBC'))
f9885:c5:m9
@verify_type(mode=WAESMode)<EOL><INDENT>def __init__(self, mode):<DEDENT>
self.__mode = mode<EOL>
Create new AES cipher with specified mode :param mode: AES mode
f9885:c6:m0
def mode(self):
return self.__mode<EOL>
Return AES mode :return: WAESMode
f9885:c6:m1
def cipher(self):
<EOL>cipher = Cipher(*self.mode().aes_args(), **self.mode().aes_kwargs())<EOL>return WAES.WAESCipher(cipher)<EOL>
Generate AES-cipher :return: Crypto.Cipher.AES.AESCipher
f9885:c6:m2
@verify_type(data=(str, bytes))<EOL><INDENT>def encrypt(self, data):<DEDENT>
padding = self.mode().padding()<EOL>if padding is not None:<EOL><INDENT>data = padding.pad(data, WAESMode.__data_padding_length__)<EOL><DEDENT>return self.cipher().encrypt_block(data)<EOL>
Encrypt the given data with cipher that is got from AES.cipher call. :param data: data to encrypt :return: bytes
f9885:c6:m3
@verify_type(data=bytes, decode=bool)<EOL><INDENT>def decrypt(self, data, decode=False):<DEDENT>
<EOL>result = self.cipher().decrypt_block(data)<EOL>padding = self.mode().padding()<EOL>if padding is not None:<EOL><INDENT>result = padding.reverse_pad(result, WAESMode.__data_padding_length__)<EOL><DEDENT>return result.decode() if decode else result<EOL>
Decrypt the given data with cipher that is got from AES.cipher call. :param data: data to decrypt :param decode: whether to decode bytes to str or not :return: bytes or str (depends on decode flag)
f9885:c6:m4
@verify_type(key=(str, bytes), salt=(bytes, None), derived_key_length=(int, None))<EOL><INDENT>@verify_type(iterations_count=(int, None), hash_fn_name=(str, None))<EOL>@verify_value(key=lambda x: x is None or len(x) >= WPBKDF2.__minimum_key_length__)<EOL>@verify_value(salt=lambda x: x is None or len(x) >= WPBKDF2.__minimum_salt_length__)<EOL>@verify_value(iterations_count=lambda x: x is None or x >= WPBKDF2.__minimum_iterations_count__)<EOL>@verify_value(hash_fn_name=lambda x: x is None or hasattr(hashes, x))<EOL>def __init__(self, key, salt=None, derived_key_length=None, iterations_count=None, hash_fn_name=None):<DEDENT>
self.__salt = salt if salt is not None else self.generate_salt()<EOL>if derived_key_length is None:<EOL><INDENT>derived_key_length = self.__default_derived_key_length__<EOL><DEDENT>if iterations_count is None:<EOL><INDENT>iterations_count = self.__default_iterations_count__<EOL><DEDENT>if hash_fn_name is None:<EOL><INDENT>hash_fn_name = self.__class__.__default_digest_generator_name__<EOL><DEDENT>hash_cls = getattr(hashes, hash_fn_name)<EOL>salt = self.__salt if isinstance(self.__salt, str) is False else self.__salt.encode()<EOL>pbkdf2_obj = PBKDF2HMAC(<EOL>algorithm=hash_cls(), length=derived_key_length, salt=salt, iterations=iterations_count,<EOL>backend=default_backend()<EOL>)<EOL>if isinstance(key, str) is True:<EOL><INDENT>key = key.encode()<EOL><DEDENT>self.__derived_key = pbkdf2_obj.derive(key)<EOL>
Generate new key (derived key) with PBKDF2 algorithm :param key: password :param salt: salt to use (if no salt was specified, then it will be generated automatically) :param derived_key_length: length of byte-sequence to generate :param iterations_count: iteration count :param hash_fn_name: name of hash function to be used with HMAC
f9886:c0:m0
def salt(self):
return self.__salt<EOL>
Return salt value (that was given in constructor or created automatically) :return: bytes
f9886:c0:m1
def derived_key(self):
return self.__derived_key<EOL>
Return derived key :return: bytes
f9886:c0:m2
@classmethod<EOL><INDENT>@verify_type(length=(int, None))<EOL>@verify_value(length=lambda x: x is None or x >= WPBKDF2.__minimum_salt_length__)<EOL>def generate_salt(cls, length=None):<DEDENT>
if length is None:<EOL><INDENT>length = cls.__default_salt_length__<EOL><DEDENT>return random_bytes(length)<EOL>
Generate salt that can be used by this object :param length: target salt length :return: bytes
f9886:c0:m3
@verify_type(hash_fn_name=(str, None))<EOL><INDENT>@verify_value(hash_fn_name=lambda x: x is None or hasattr(hashes, x))<EOL>def __init__(self, hash_fn_name=None):<DEDENT>
self.__hash_fn_name =hash_fn_name if hash_fn_name is not None else self.__class__.__default_hash_fn_name__<EOL>self.__digest_generator = getattr(hashes, self.__hash_fn_name)()<EOL>
Create new "code-authenticator" :param hash_fn_name: a name of hash function
f9887:c0:m0
def hash_function_name(self):
return self.__hash_fn_name<EOL>
Return hash-generator name :return: str
f9887:c0:m1
@verify_type(key=bytes, message=(bytes, None))<EOL><INDENT>def hash(self, key, message=None):<DEDENT>
hmac_obj = hmac.HMAC(key, self.__digest_generator, backend=default_backend())<EOL>if message is not None:<EOL><INDENT>hmac_obj.update(message)<EOL><DEDENT>return hmac_obj.finalize()<EOL>
Return digest of the given message and key :param key: secret HMAC key :param message: code (message) to authenticate :return: bytes
f9887:c0:m2
@classmethod<EOL><INDENT>@verify_type(name=str)<EOL>@verify_value(name=lambda x: WHMAC.__hmac_name_re__.match(x) is not None)<EOL>def hmac(cls, name):<DEDENT>
return WHMAC(cls.__hmac_name_re__.search(name).group(<NUM_LIT:1>))<EOL>
Return new WHMAC object by the given algorithm name like 'HMAC-SHA256' or 'HMAC_SHA1' :param name: name of HMAC algorithm :return: WHMAC
f9887:c0:m3
@abstractmethod<EOL><INDENT>@verify_type(data=bytes)<EOL>def update(self, data):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Update digest by hashing the specified data :param data: data to hash :return: None
f9888:c0:m0
@abstractmethod<EOL><INDENT>def digest(self):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return current digest :return: bytes
f9888:c0:m1
def hexdigest(self):
return '<STR_LIT>'.join(["<STR_LIT>".format(x).upper() for x in self.digest()])<EOL>
Return current digest in hex-alike string :return: str
f9888:c0:m2
@classmethod<EOL><INDENT>@abstractmethod<EOL>def generator_digest_size(cls):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return generator digest size :return: int
f9888:c0:m3
@classmethod<EOL><INDENT>@abstractmethod<EOL>def generator_name(cls):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return hash-function name :return: str
f9888:c0:m4
@classmethod<EOL><INDENT>@abstractmethod<EOL>def generator_family(cls):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return name of hash-function family (like: 'SHA') :return: str or None (if no available)
f9888:c0:m5
@classmethod<EOL><INDENT>@abstractmethod<EOL>@verify_type(data=(bytes, None))<EOL>def new(cls, data=None):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return new generator and hash the specified data (if defined) :param data: data to hash :return: WHashGeneratorProto
f9888:c0:m6
def __init__(self):
WHashGeneratorProto.__init__(self)<EOL>if self.__class__.__py_cryptography_cls__ is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.__pycrypto_obj = hashes.Hash(self.__class__.__py_cryptography_cls__(), backend=default_backend())<EOL>
Create new hash generator
f9888:c1:m0
@verify_type(data=bytes)<EOL><INDENT>def update(self, data):<DEDENT>
self.__pycrypto_obj.update(data)<EOL>
:meth:`.WHashGeneratorProto.update` implementation
f9888:c1:m1
def digest(self):
return self.__pycrypto_obj.copy().finalize()<EOL>
:meth:`.WHashGeneratorProto.digest` implementation
f9888:c1:m2
@classmethod<EOL><INDENT>def generator_digest_size(cls):<DEDENT>
if cls.__py_cryptography_cls__ is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return cls.__py_cryptography_cls__.digest_size<EOL>
:meth:`.WHashGeneratorProto.generator_digest_size` implementation
f9888:c1:m3
@classmethod<EOL><INDENT>def generator_name(cls):<DEDENT>
if cls.__generator_name__ is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if isinstance(cls.__generator_name__, str) is False:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>return cls.__generator_name__.upper()<EOL>
:meth:`.WHashGeneratorProto.generator_name` implementation
f9888:c1:m4
@classmethod<EOL><INDENT>def generator_family(cls):<DEDENT>
if cls.__generator_family__ is not None:<EOL><INDENT>if isinstance(cls.__generator_family__, str) is False:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT><DEDENT>if cls.__generator_family__ is not None:<EOL><INDENT>return cls.__generator_family__.upper()<EOL><DEDENT>
:meth:`.WHashGeneratorProto.generator_family` implementation
f9888:c1:m5
@classmethod<EOL><INDENT>@verify_type(data=(bytes, None))<EOL>def new(cls, data=None):<DEDENT>
obj = cls()<EOL>if data is not None:<EOL><INDENT>obj.update(data)<EOL><DEDENT>return obj<EOL>
:meth:`.WHashGeneratorProto.new` implementation
f9888:c1:m6
@staticmethod<EOL><INDENT>@verify_type(name=str)<EOL>def generator(name):<DEDENT>
name = name.upper()<EOL>if name not in WHash.__hash_map__.keys():<EOL><INDENT>raise ValueError('<STR_LIT>' % name)<EOL><DEDENT>return WHash.__hash_map__[name]<EOL>
Return generator by its name :param name: name of hash-generator :return: WHashGeneratorProto class
f9888:c9:m0
@staticmethod<EOL><INDENT>def generator_by_digest(family, digest_size):<DEDENT>
for generator_name in WHash.available_generators(family=family):<EOL><INDENT>generator = WHash.generator(generator_name)<EOL>if generator.generator_digest_size() == digest_size:<EOL><INDENT>return generator<EOL><DEDENT><DEDENT>raise ValueError('<STR_LIT>')<EOL>
Return generator by hash generator family name and digest size :param family: name of hash-generator family :return: WHashGeneratorProto class
f9888:c9:m1
@staticmethod<EOL><INDENT>@verify_type(family=(str, None), name=(str, None))<EOL>def available_generators(family=None, name=None):<DEDENT>
generators = WHash.__hash_map__.values()<EOL>if family is not None:<EOL><INDENT>family = family.upper()<EOL>generators = filter(lambda x: x.generator_family() == family, generators)<EOL><DEDENT>if name is not None:<EOL><INDENT>name = name.upper()<EOL>generators = filter(lambda x: x.generator_name() == name, generators)<EOL><DEDENT>return tuple([x.generator_name() for x in generators])<EOL>
Return names of available generators :param family: name of hash-generator family to select :param name: name of hash-generator to select (parameter may be used for availability check) :return: tuple of str
f9888:c9:m2
@staticmethod<EOL><INDENT>@verify_type(family=(str, None), name=(str, None))<EOL>def available_digests(family=None, name=None):<DEDENT>
generators = WHash.available_generators(family=family, name=name)<EOL>return set([WHash.generator(x).generator_digest_size() for x in generators])<EOL>
Return names of available generators :param family: name of hash-generator family to select :param name: name of hash-generator to select :return: set of int
f9888:c9:m3
@verify_type(bits_count=int)<EOL>@verify_value(bits_count=lambda x: x > <NUM_LIT:0>)<EOL>def random_bits(bits_count):
bytes_count = int(math.ceil(bits_count / <NUM_LIT:8>))<EOL>random_value = int.from_bytes(os.urandom(bytes_count), byteorder=sys.byteorder)<EOL>result_bits = bytes_count * <NUM_LIT:8><EOL>if result_bits > bits_count:<EOL><INDENT>random_value = (random_value >> (result_bits - bits_count))<EOL><DEDENT>return random_value<EOL>
Random generator (PyCrypto getrandbits wrapper). The result is a non-negative value. :param bits_count: random bits to generate :return: int
f9889:m0
@verify_type(maximum_value=int)<EOL>@verify_value(maximum_value=lambda x: x > <NUM_LIT:0>)<EOL>def random_int(maximum_value):
if maximum_value == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>elif maximum_value == <NUM_LIT:1>:<EOL><INDENT>return random_bits(<NUM_LIT:1>)<EOL><DEDENT>bits = math.floor(math.log2(maximum_value))<EOL>result = random_bits(bits) + random_int(maximum_value - ((<NUM_LIT:2> ** bits) - <NUM_LIT:1>))<EOL>return result<EOL>
Random generator (PyCrypto getrandbits wrapper). The result is a non-negative value. :param maximum_value: maximum integer value :return: int
f9889:m1
@verify_type(bytes_count=int)<EOL>@verify_value(bytes_count=lambda x: x >= <NUM_LIT:0>)<EOL>def random_bytes(bytes_count):
return bytes([random_bits(<NUM_LIT:8>) for x in range(bytes_count)])<EOL>
Generate random bytes sequence. (PyCrypto getrandbits wrapper) :param bytes_count: sequence length :return: bytes
f9889:m2
@abstractmethod<EOL><INDENT>def block_size(self):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Return a size of a block that may be encrypted or decrypted. :return: int (in bytes) or None if cipher is able to encrypt/decrypt block with any length
f9890:c0:m0
@abstractmethod<EOL><INDENT>@verify_type(data=bytes)<EOL>def encrypt_block(self, data):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Encrypt the given data :param data: data to encrypt. The size of the data must be multiple of :meth:`.WCipherProto.block_size` :return: bytes
f9890:c0:m1
@abstractmethod<EOL><INDENT>@verify_type(data=bytes)<EOL>def decrypt_block(self, data):<DEDENT>
raise NotImplementedError('<STR_LIT>')<EOL>
Decrypt the given data :param data: data to decrypt. The size of the data must be multiple of :meth:`.WCipherProto.block_size` :return: bytes
f9890:c0:m2
@verify_type(mount_record=str)<EOL><INDENT>def __init__(self, mount_record):<DEDENT>
parsed_record = mount_record.split()<EOL>if len(parsed_record) != <NUM_LIT:6>:<EOL><INDENT>raise RuntimeError('<STR_LIT>' % mount_record)<EOL><DEDENT>device = parsed_record[<NUM_LIT:0>]<EOL>self.__device = os.path.realpath(device) if os.path.isabs(device) else device<EOL>self.__device_name = os.path.basename(self.__device)<EOL>self.__path = parsed_record[<NUM_LIT:1>]<EOL>self.__fs = parsed_record[<NUM_LIT:2>]<EOL>self.__options = tuple(parsed_record[<NUM_LIT:3>].split('<STR_LIT:U+002C>'))<EOL>def check_flag(flag):<EOL><INDENT>if flag == '<STR_LIT:0>':<EOL><INDENT>return False<EOL><DEDENT>elif flag == '<STR_LIT:1>':<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT><DEDENT>self.__dump_flag = check_flag(parsed_record[<NUM_LIT:4>])<EOL>self.__fsck_order = int(parsed_record[<NUM_LIT:5>])<EOL>
Create new mount point information by parsing mount point record (a single line from /proc/mounts file) :param mount_record: line to parse
f9892:c0:m0
def device(self):
return self.__device<EOL>
Return a mount point device path :return: str
f9892:c0:m1
def device_name(self):
return self.__device_name<EOL>
Return a mount point device name (a base name of a device path) :return: str
f9892:c0:m2
def path(self):
return self.__path<EOL>
Return a mount point path (path where this mount point is mounted to) :return: str
f9892:c0:m3
def fs(self):
return self.__fs<EOL>
Return a mount point filesystem name :return: str
f9892:c0:m4
def options(self):
return self.__options<EOL>
Return a mount point options (options with which this point was mounted) :return: tuple of str
f9892:c0:m5
def dump_flag(self):
return self.__dump_flag<EOL>
Return dump flag for this mount point :return: bool
f9892:c0:m6
def fsck_order(self):
return self.__fsck_order<EOL>
Return fsck (filesystem check) order for this mount point :return: int
f9892:c0:m7
@classmethod<EOL><INDENT>def mounts(cls):<DEDENT>
result = []<EOL>with open(cls.__mounts_file__) as f:<EOL><INDENT>for mount_record in f:<EOL><INDENT>result.append(WMountPoint(mount_record))<EOL><DEDENT><DEDENT>return tuple(result)<EOL>
Return tuple of current mount points :return: tuple of WMountPoint
f9892:c0:m8
@classmethod<EOL><INDENT>@verify_type(file_path=str)<EOL>@verify_value(file_path=lambda x: len(x) > <NUM_LIT:0>)<EOL>def mount_point(cls, file_path):<DEDENT>
mount = None<EOL>for mp in cls.mounts():<EOL><INDENT>mp_path = mp.path()<EOL>if file_path.startswith(mp_path) is True:<EOL><INDENT>if mount is None or len(mount.path()) <= len(mp_path):<EOL><INDENT>mount = mp<EOL><DEDENT><DEDENT><DEDENT>return mount<EOL>
Return mount point that, where the given path is reside on :param file_path: target path to search :return: WMountPoint or None (if file path is outside current mount points)
f9892:c0:m9
@classmethod<EOL><INDENT>@verify_type(device=str, mount_directory=str, fs=(str, None), options=(list, tuple, set, None))<EOL>@verify_type(cmd_timeout=(int, float, None), sudo=bool)<EOL>@verify_value(device=lambda x: len(x) > <NUM_LIT:0>, mount_directory=lambda x: len(x) > <NUM_LIT:0>)<EOL>@verify_value(fs=lambda x: x is None or len(x) > <NUM_LIT:0>, cmd_timeout=lambda x: x is None or x > <NUM_LIT:0>)<EOL>def mount(cls, device, mount_directory, fs=None, options=None, cmd_timeout=None, sudo=False):<DEDENT>
cmd = [] if sudo is False else ['<STR_LIT>']<EOL>cmd.extend(['<STR_LIT>', device, os.path.abspath(mount_directory)])<EOL>if fs is not None:<EOL><INDENT>cmd.extend(['<STR_LIT>', fs])<EOL><DEDENT>if options is not None and len(options) > <NUM_LIT:0>:<EOL><INDENT>cmd.append('<STR_LIT>')<EOL>cmd.extend(options)<EOL><DEDENT>subprocess.check_output(cmd, timeout=cmd_timeout)<EOL>
Mount a device to mount directory :param device: device to mount :param mount_directory: target directory where the given device will be mounted to :param fs: optional, filesystem on the specified device. If specifies - overrides OS filesystem \ detection with this value. :param options: specifies mount options (OS/filesystem dependent) :param cmd_timeout: if specified - timeout with which this mount command should be evaluated (if \ command isn't complete within the given timeout - an exception will be raised) :param sudo: whether to use sudo to run mount command :return: None
f9892:c0:m10
@classmethod<EOL><INDENT>@verify_type(device_or_directory=str, cmd_timeout=(int, float, None), sudo=bool)<EOL>@verify_value(device_or_directory=lambda x: len(x) > <NUM_LIT:0>, cmd_timeout=lambda x: x is None or x > <NUM_LIT:0>)<EOL>def umount(cls, device_or_directory, cmd_timeout=None, sudo=False):<DEDENT>
cmd = [] if sudo is False else ['<STR_LIT>']<EOL>cmd.extend(['<STR_LIT>', device_or_directory])<EOL>subprocess.check_output(cmd, timeout=cmd_timeout)<EOL>
Unmount device (or mount directory) :param device_or_directory: device name or mount directory to unmount :param cmd_timeout: if specified - timeout with which this unmount command should be evaluated (if \ command isn't complete within the given timeout - an exception will be raised) :param sudo: whether to use sudo to run mount command :return: None
f9892:c0:m11
@verify_type(command=str, fields_count=int, cmd_timeout=(int, float, None), sudo=bool)<EOL><INDENT>@verify_value(cmd_timeout=lambda x: x is None or x > <NUM_LIT:0>)<EOL>def __init__(self, command, fields_count, cmd_timeout=None, sudo=False):<DEDENT>
self.__command = command<EOL>self.__fields_count = fields_count<EOL>self.__cmd_timeout = cmd_timeout if cmd_timeout is not None else self.__lvm_cmd_default_timeout__<EOL>self.__sudo = sudo<EOL>
Create new command :param command: program to execute :param fields_count: fields in a program output :param cmd_timeout: timeout for a program (if it is None - then default value is used) :param sudo: flag - whether to run this program with sudo or not
f9893:c0:m0
def command(self):
return self.__command<EOL>
Return target program :return: str
f9893:c0:m1
def fields_count(self):
return self.__fields_count<EOL>
Return number of fields in a program output :return: int
f9893:c0:m2
def cmd_timeout(self):
return self.__cmd_timeout<EOL>
Timeout for a program to complete :return: int, float
f9893:c0:m3
def sudo(self):
return self.__sudo<EOL>
Return 'sudo' flag (whether to run this program with sudo or not) :return: bool
f9893:c0:m4
@verify_type(name=(str, None))<EOL><INDENT>def lvm_info(self, name=None):<DEDENT>
cmd = [] if self.sudo() is False else ['<STR_LIT>']<EOL>cmd.extend([self.command(), '<STR_LIT:-c>'])<EOL>if name is not None:<EOL><INDENT>cmd.append(name)<EOL><DEDENT>output = subprocess.check_output(cmd, timeout=self.cmd_timeout())<EOL>output = output.decode()<EOL>result = []<EOL>fields_count = self.fields_count()<EOL>for line in output.split('<STR_LIT:\n>'):<EOL><INDENT>line = line.strip()<EOL>fields = line.split('<STR_LIT::>')<EOL>if len(fields) == fields_count:<EOL><INDENT>result.append(fields)<EOL><DEDENT><DEDENT>if name is not None and len(result) != <NUM_LIT:1>:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>return tuple(result)<EOL>
Call a program :param name: if specified - program will return information for that lvm-entity only. otherwise - all available entries are returned :return: tuple of str (fields)
f9893:c0:m5
@verify_type('<STR_LIT>', command=str, fields_count=int, sudo=bool)<EOL><INDENT>@verify_type(lvm_entity=(str, tuple, list, set))<EOL>@verify_value(lvm_entity=lambda x: len(x) > <NUM_LIT:0> if isinstance(x, str) else True)<EOL>def __init__(self, command, fields_count, lvm_entity, sudo=False):<DEDENT>
self.__lvm_command = WLVMInfoCommand(<EOL>command, fields_count, cmd_timeout=self.__class__.__lvm_info_cmd_timeout__, sudo=sudo<EOL>)<EOL>if isinstance(lvm_entity, (tuple, list, set)) is True:<EOL><INDENT>if len(lvm_entity) != fields_count:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' %<EOL>(len(lvm_entity), fields_count)<EOL>)<EOL><DEDENT>self.__lvm_entity = tuple(lvm_entity)<EOL><DEDENT>else:<EOL><INDENT>self.__lvm_entity = (self.lvm_command().lvm_info(lvm_entity)[<NUM_LIT:0>])<EOL><DEDENT>
Create new info-object :param command: same as command in :meth:`.WLVMInfoCommand.__init__` :param fields_count: same as fields_count in :meth:`.WLVMInfoCommand.__init__` :param lvm_entity: if this is a list/tuple/set - then it is a collection of fields (collection length \ must be the same as 'fields_count'). If it is a string, then command is executed to get corresponding \ fields :param sudo: same as sudo in :meth:`.WLVMInfoCommand.__init__`
f9893:c1:m0
def lvm_command(self):
return self.__lvm_command<EOL>
Return LVM-command object :return: WLVMInfoCommand
f9893:c1:m1
def lvm_entity(self):
return self.__lvm_entity<EOL>
Return object fields :return: tuple of str (fields)
f9893:c1:m2
@verify_type('<STR_LIT>', physical_volume=(str, tuple, list, set), sudo=bool)<EOL><INDENT>@verify_value('<STR_LIT>', physical_volume=lambda x: len(x) > <NUM_LIT:0> if isinstance(x, str) else True)<EOL>def __init__(self, physical_volume, sudo=False):<DEDENT>
WLVMInfo.__init__(self, '<STR_LIT>', <NUM_LIT:12>, physical_volume, sudo=sudo)<EOL>
Create new physical volume descriptor :param physical_volume: same as 'lvm_entity' in :meth:`.WLVMInfo.__init__` :param sudo: same as 'sudo' in :meth:`.WLVMInfo.__init__`
f9893:c2:m0
def all(self):
return tuple([WPhysicalVolume(x) for x in self.lvm_command().lvm_info()])<EOL>
Return every physical volume in the system :return: tuple of WPhysicalVolume
f9893:c2:m1
def device_name(self):
return self.lvm_entity()[<NUM_LIT:0>]<EOL>
Return physical volume device name :return: str
f9893:c2:m2
def volume_group(self):
return self.lvm_entity()[<NUM_LIT:1>]<EOL>
Return related volume group name (may be empty string if this volume is not allocated to any) :return: str
f9893:c2:m3
def sectors_count(self):
return int(self.lvm_entity()[<NUM_LIT:2>])<EOL>
Return physical volume size in sectors :return: int
f9893:c2:m4
def extent_size(self):
return int(self.lvm_entity()[<NUM_LIT:7>])<EOL>
Return physical extent size in kilobytes (may have 0 value if this volume is not allocated to any) :return: int
f9893:c2:m5
def total_extents(self):
return int(self.lvm_entity()[<NUM_LIT:8>])<EOL>
Return total number of physical extents (may have 0 value if this volume is not allocated to any) :return: int
f9893:c2:m6
def free_extents(self):
return int(self.lvm_entity()[<NUM_LIT:9>])<EOL>
Return free number of physical extents (may have 0 value if this volume is not allocated to any) :return: int
f9893:c2:m7
def allocated_extents(self):
return int(self.lvm_entity()[<NUM_LIT:10>])<EOL>
Return allocated number of physical extents (may have 0 value if this volume is not allocated to \ any) :return: int
f9893:c2:m8
def uuid(self):
return self.lvm_entity()[<NUM_LIT:11>]<EOL>
Return physical volume UUID :return: str
f9893:c2:m9
@verify_type('<STR_LIT>', volume_group=(str, tuple, list, set), sudo=bool)<EOL><INDENT>@verify_value('<STR_LIT>', volume_group=lambda x: len(x) > <NUM_LIT:0> if isinstance(x, str) else True)<EOL>def __init__(self, volume_group, sudo=False):<DEDENT>
WLVMInfo.__init__(self, '<STR_LIT>', <NUM_LIT>, volume_group, sudo=sudo)<EOL>
Create new volume group descriptor :param volume_group: same as 'lvm_entity' in :meth:`.WLVMInfo.__init__` :param sudo: same as 'sudo' in :meth:`.WLVMInfo.__init__`
f9893:c3:m0
def all(self):
return tuple([WVolumeGroup(x) for x in self.lvm_command().lvm_info()])<EOL>
Return every volume group in the system :return: tuple of WVolumeGroup
f9893:c3:m1
def group_name(self):
return self.lvm_entity()[<NUM_LIT:0>]<EOL>
Return volume group name :return: str
f9893:c3:m2
def group_access(self):
return self.lvm_entity()[<NUM_LIT:1>]<EOL>
Return volume group access :return: str
f9893:c3:m3
def maximum_logical_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:4>])<EOL>
Return maximum number of logical volumes (0 - for unlimited) :return: int
f9893:c3:m4
def logical_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:5>])<EOL>
Return current number of logical volumes :return: int
f9893:c3:m5
def opened_logical_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:6>])<EOL>
Return open count of all logical volumes in this volume group :return: int
f9893:c3:m6
def maximum_physical_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:8>])<EOL>
Return maximum number of physical volumes (0 - for unlimited) :return: int
f9893:c3:m7
def physical_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:9>])<EOL>
Return current number of physical volumes :return: int
f9893:c3:m8
def actual_volumes(self):
return int(self.lvm_entity()[<NUM_LIT:10>])<EOL>
Return actual number of physical volumes :return: int
f9893:c3:m9
def size(self):
return int(self.lvm_entity()[<NUM_LIT:11>])<EOL>
Return size of volume group in kilobytes :return: int
f9893:c3:m10
def extent_size(self):
return int(self.lvm_entity()[<NUM_LIT:12>])<EOL>
Return physical extent size in kilobytes :return: int
f9893:c3:m11
def total_extents(self):
return int(self.lvm_entity()[<NUM_LIT>])<EOL>
Return total number of physical extents for this volume group :return: int
f9893:c3:m12
def allocated_extents(self):
return int(self.lvm_entity()[<NUM_LIT>])<EOL>
Return allocated number of physical extents for this volume group :return: int
f9893:c3:m13
def free_extents(self):
return int(self.lvm_entity()[<NUM_LIT:15>])<EOL>
Return free number of physical extents for this volume group :return: int
f9893:c3:m14
def uuid(self):
return self.lvm_entity()[<NUM_LIT:16>]<EOL>
Return UUID of volume group :return: str
f9893:c3:m15
@verify_type('<STR_LIT>', logical_volume=(str, tuple, list, set), sudo=bool)<EOL><INDENT>@verify_value('<STR_LIT>', logical_volume=lambda x: len(x) > <NUM_LIT:0> if isinstance(x, str) else True)<EOL>def __init__(self, logical_volume, sudo=False):<DEDENT>
WLVMInfo.__init__(self, '<STR_LIT>', <NUM_LIT>, logical_volume, sudo=sudo)<EOL>
Create new logical volume descriptor :param logical_volume: same as 'lvm_entity' in :meth:`.WLVMInfo.__init__` :param sudo: same as 'sudo' in :meth:`.WLVMInfo.__init__`
f9893:c4:m0