<commit_before># Copyright 2018 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Functionality pertaining to device mapper
"""

import os
import subprocess


_DM_BIN = os.getenv('STRATIS_DMSETUP_BIN', "/usr/sbin/dmsetup")


def _get_stratis_devices():
    """
    Return a list of stratis DM devices
    :return: A list of stratis DM devices
    """
    output = subprocess.check_output([_DM_BIN, 'ls'])
    decoded = output.decode("utf-8")
    return [l for l in decoded.split() if l.startswith('stratis-')]


def _remove_device(device):
    """
    Attempt to remove a device mapper device.
    :param device: The device to remove
    :return: True if device was removed, else False
    """
    return_code = subprocess.call([_DM_BIN, 'remove', device],
                                  stderr=subprocess.PIPE)
    return return_code == 0


def remove_stratis_setup():
    """
    Removes all device mapper tables which are related to stratis (best attempt)
    :return: None
    """
    attempts = 0
    devices = _get_stratis_devices()
    max_loops = len(devices) * 5

    # There is some dependency ordering with regards to dm tables, we will
    # simply iterate over the list attempting to remove them until they are
    # all gone or we give up trying.
    while devices != [] and attempts < max_loops:
        attempts += 1

        for dev in devices.copy():
            if _remove_device(dev):
                devices.remove(dev)


if __name__ == '__main__':
    remove_stratis_setup()<commit_msg>_dm.py: Add comment to clarify intent<commit_after># Copyright 2018 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Functionality pertaining to device mapper
"""

import os
import subprocess


_DM_BIN = os.getenv('STRATIS_DMSETUP_BIN', "/usr/sbin/dmsetup")


def _get_stratis_devices():
    """
    Return a list of stratis DM devices
    :return: A list of stratis DM devices
    """
    output = subprocess.check_output([_DM_BIN, 'ls'])
    decoded = output.decode("utf-8")
    return [l for l in decoded.split() if l.startswith('stratis-')]


def _remove_device(device):
    """
    Attempt to remove a device mapper device.
    :param device: The device to remove
    :return: True if device was removed, else False
    """
    return_code = subprocess.call([_DM_BIN, 'remove', device],
                                  stderr=subprocess.PIPE)
    return return_code == 0


def remove_stratis_setup():
    """
    Removes all device mapper tables which are related to stratis (best attempt)
    :return: None
    """
    attempts = 0
    devices = _get_stratis_devices()
    max_loops = len(devices) * 5

    # There is some dependency ordering with regards to dm tables, we will
    # simply iterate over the list attempting to remove them until they are
    # all gone or we give up trying.
    while devices != [] and attempts < max_loops:
        attempts += 1

        # Make a copy of the list as we are traversing a list we want to
        # modify.
        for dev in devices.copy():
            if _remove_device(dev):
                devices.remove(dev)


if __name__ == '__main__':
    remove_stratis_setup()
{'fraction_non_alphanumeric': 0.0870712401055409, 'fraction_numerical': 0.006236507555768769, 'mean_word_length': 3.0485436893203883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4993879', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1181, 'n_words': 476}