<commit_before>import re
import traceback
from inaugurator import sh


class PartitionTable:
    def __init__(self, device):
        self._device = device
        self._cachedDiskSize = None
        self._created = False

    def created(self):
        return self._created

    def clear(self):
        sh.run("/usr/sbin/busybox dd if=/dev/zero of=%s bs=1M count=512" % self._device)

    def _create(self):
        self.clear()
        table = self._expected()
        script = "echo -ne '%s' | sfdisk --unit M %s --in-order" % (self._sfdiskScript(table), self._device)
        print "creating new partition table:", script
        sh.run(script)
        sh.run("/usr/sbin/busybox mdev -s")
        for partition in table:
            if partition['id'] == 82:
                print "creating swapspace on %s" % partition['device']
                sh.run("/usr/sbin/mkswap %s" % partition['device'])
            elif partition['id'] == 83:
                print "creating ext4 filesystem on %s" % partition['device']
                sh.run("/usr/sbin/mkfs.ext4 %s" % partition['device'])
            else:
                assert False, "Unrecognized partition id: %d" % partition['id']
        self._created = True

    def parse(self):
        LINE = re.compile(r"(/\S+) : start=\s*\d+, size=\s*(\d+), Id=\s*(\d+)")
        lines = LINE.findall(sh.run("/usr/sbin/sfdisk --dump %s" % self._device))
        return [
            dict(device=device, sizeMB=int(size) * 512 / 1024 / 1024, id=int(id))
            for device, size, id in lines if int(size) > 0]

    def _diskSizeMB(self):
        if self._cachedDiskSize is None:
            self._cachedDiskSize = int(sh.run("/usr/sbin/sfdisk -s %s" % self._device).strip()) / 1024
        return self._cachedDiskSize

    def _expected(self):
        swapSizeMB = 1024 if self._diskSizeMB() <= 32 * 1024 else 8 * 1024
        return [
            dict(device="%s1" % self._device, sizeMB=256, id=83),
            dict(device="%s2" % self._device, sizeMB=swapSizeMB, id=82),
            dict(device="%s3" % self._device, sizeMB='fill', id=83)]

    def _sfdiskScript(self, table):
        lines = []
        offsetMB = '2'
        for partition in table:
            sizeMB = "" if partition['sizeMB'] == 'fill' else partition['sizeMB']
            line = r'%(offsetMB)s,%(sizeMB)s,%(id)d\n' % dict(
                partition, sizeMB=sizeMB, offsetMB=offsetMB)
            lines.append(line)
            offsetMB = ''<commit_msg>Fixed partition table to match rotating disks<commit_after>import re
import traceback
from inaugurator import sh


class PartitionTable:
    def __init__(self, device):
        self._device = device
        self._cachedDiskSize = None
        self._created = False

    def created(self):
        return self._created

    def clear(self):
        sh.run("/usr/sbin/busybox dd if=/dev/zero of=%s bs=1M count=512" % self._device)

    def _create(self):
        self.clear()
        table = self._expected()
        script = "echo -ne '%s' | sfdisk --unit M %s --in-order" % (self._sfdiskScript(table), self._device)
        print "creating new partition table:", script
        sh.run(script)
        sh.run("/usr/sbin/busybox mdev -s")
        for partition in table:
            if partition['id'] == 82:
                print "creating swapspace on %s" % partition['device']
                sh.run("/usr/sbin/mkswap %s" % partition['device'])
            elif partition['id'] == 83:
                print "creating ext4 filesystem on %s" % partition['device']
                sh.run("/usr/sbin/mkfs.ext4 %s" % partition['device'])
            else:
                assert False, "Unrecognized partition id: %d" % partition['id']
        self._created = True

    def parse(self):
        LINE = re.compile(r"(/\S+) : start=\s*\d+, size=\s*(\d+), Id=\s*(\d+)")
        lines = LINE.findall(sh.run("/usr/sbin/sfdisk --dump %s" % self._device))
        return [
            dict(device=device, sizeMB=int(size) * 512 / 1024 / 1024, id=int(id))
            for device, size, id in lines if int(size) > 0]

    def _diskSizeMB(self):
        if self._cachedDiskSize is None:
            self._cachedDiskSize = int(sh.run("/usr/sbin/sfdisk -s %s" % self._device).strip()) / 1024
        return self._cachedDiskSize

    def _expected(self):
        swapSizeMB = 1024 if self._diskSizeMB() <= 32 * 1024 else 8 * 1024
        return [
            dict(device="%s1" % self._device, sizeMB=256, id=83),
            dict(device="%s2" % self._device, sizeMB=swapSizeMB, id=82),
            dict(device="%s3" % self._device, sizeMB='fill', id=83)]

    def _sfdiskScript(self, table):
        lines = []
        offsetMB = '8'
        for partition in table:
            sizeMB = "" if partition['sizeMB'] == 'fill' else partition['sizeMB']
            line = r'%(offsetMB)s,%(sizeMB)s,%(id)d\n' % dict(
                partition, sizeMB=sizeMB, offsetMB=offsetMB)
            lines.append(line)
            offsetMB = ''
{'fraction_non_alphanumeric': 0.1466933867735471, 'fraction_numerical': 0.021643286573146292, 'mean_word_length': 2.1769573520050924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6619633', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1530, 'n_words': 380}