<commit_before>import os
import re
import subprocess

class Image:
    '''Helps to deal with images more easily. Can mount, unmount, and get all
    relevant information.
    '''

    def __init__(self, path='', make=False, name=None, volume=None):
        if make:
            if not name or not volume:
                raise ValueError("Must specify 'name' and 'volume' to create image.")
            path = create(image=name, vol=volume)
        if not os.path.isfile(path):
            raise ValueError("Invalid path specified: '" + path + "'")
        self.path = os.path.abspath(str(path))
        self.name = os.path.splitext(os.path.basename(self.path))[0]
        if not os.access(self.path, os.R_OK):
            raise ValueError("Image " + self.name + " is not readable.")
        self.__revert()

    def __repr__(self):
        result = "Image: " + self.name
        if self.mounted:
            result += "\n       Full path:   " + self.path
            result += "\n       Disk:        " + self.disk_id
            result += "\n       Mount point: " + self.mount_point
        else:
            result += "\n       Not mounted."
        return result

    def mount(self):
        self.attach()

    def attach(self):
        if not self.mounted:
            self.disk_id = attach(self.path)
            self.mount_point = find_mount(self.disk_id)
            self.mounted = True

    def unmount(self):
        self.detach()

    def detach(self):
        if self.mounted:
            detach(self.disk_id)
            self.__revert()

    def enable_ownership(self):
        if self.mounted:
            enable_ownership(self.disk_id)

    def clean(self):
        if self.mounted:
            clean(self.mount_point)

    def convert(self, outfile=''):
        if not self.mounted:
            convert(self.path, outfile=outfile)

    def scan(self):
        if not self.mounted:
            scan(self.path)

    def bless(self, label=None):
        if mounted:
            bless(self.mount_point, label)

    def __revert(self):
        self.mount_point = ''
        self.disk_id = ''
        self.mounted = False

def create(image, vol='Mac OS X', size='200g'):
    '''Creates a blank sparse image.

    image - the name of the image
    vol   - the name of the volume once mounted
    size  - the maximum size of the sparse image
    '''

    result = subprocess.call(['hdiutil', 'create', '-size', str(size),
                              '-type', 'SPARSE', '-ov', '-fs', 'HFS+J',
                              '-volname', str(vol), str(image)],
                             stderr=subprocess.STDOUT,
                             stdout=open(os.devnull, 'w'))

    if result != 0:
        raise RuntimeError("The image was not created properly.")

    return result.strip('\n').split(': ')[1]

def convert(image, format='UDRO', outfile=''):
    '''Converts an image to another format. Default is read-only.

    image   - path of the image to be converted
    format  - the desired format<commit_msg>Fixing images<commit_after>import os
import re
import subprocess

class Image:
    '''Helps to deal with images more easily. Can mount, unmount, and get all
    relevant information.
    '''

    def __init__(self, path='', make=False, name=None, volume=None):
        if make:
            if not name or not volume:
                raise ValueError("Must specify 'name' and 'volume' to create image.")
            path = create(image=name, vol=volume)
        if not os.path.isfile(path):
            raise ValueError("Invalid path specified: '" + path + "'")
        self.path = os.path.abspath(str(path))
        self.name = os.path.splitext(os.path.basename(self.path))[0]
        if not os.access(self.path, os.R_OK):
            raise ValueError("Image " + self.name + " is not readable.")
        self.__revert()

    def __repr__(self):
        result = "Image: " + self.name
        if self.mounted:
            result += "\n       Full path:   " + self.path
            result += "\n       Disk:        " + self.disk_id
            result += "\n       Mount point: " + self.mount_point
        else:
            result += "\n       Not mounted."
        return result

    def mount(self):
        self.attach()

    def attach(self):
        if not self.mounted:
            self.disk_id = attach(self.path)
            self.mount_point = find_mount(self.disk_id)
            self.mounted = True

    def unmount(self):
        self.detach()

    def detach(self):
        if self.mounted:
            detach(self.disk_id)
            self.__revert()

    def enable_ownership(self):
        if self.mounted:
            enable_ownership(self.disk_id)

    def clean(self):
        if self.mounted:
            clean(self.mount_point)

    def convert(self, outfile=''):
        if not self.mounted:
            convert(self.path, outfile=outfile)

    def scan(self):
        if not self.mounted:
            scan(self.path)

    def bless(self, label=None):
        if mounted:
            bless(self.mount_point, label)

    def __revert(self):
        self.mount_point = ''
        self.disk_id = ''
        self.mounted = False

def create(image, vol='Mac OS X', size='200g'):
    '''Creates a blank sparse image.

    image - the name of the image
    vol   - the name of the volume once mounted
    size  - the maximum size of the sparse image
    '''

    result = subprocess.check_output(['hdiutil', 'create', '-size', str(size),
                                      '-type', 'SPARSE', '-ov', '-fs', 'HFS+J',
                                      '-volname', str(vol), str(image)])

    if not result.startswith('created: '):
        raise RuntimeError("The image was not created properly.")

    return result.strip('\n').split(': ')[1]

def convert(image, format='UDRO', outfile=''):
    '''Converts an image to another format. Default is read-only.

    image   - path of the image to be converted
    format  - the desired format
{'fraction_non_alphanumeric': 0.12066945606694561, 'fraction_numerical': 0.0018410041841004183, 'mean_word_length': 1.8841698841698842, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '222689', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1714, 'n_words': 491}