<commit_before>#Copyright 2012 Calxeda, Inc.  All Rights Reserved. 

""" Image objects used by the cx_manage_util controller """

import os
import tempfile

from cxmanage.simg import create_simg, has_simg

class Image:
    """ An image consists of an image type, a filename, and any info needed
    to build an SIMG out of it. """

    def __init__(self, image_type, filename, version, daddr,
            force_simg, skip_simg, skip_crc32):
        self.type = image_type
        self.filename = filename
        self.version = version
        self.daddr = daddr
        self.force_simg = force_simg
        self.skip_simg = skip_simg
        self.skip_crc32 = skip_crc32

    def upload(self, work_dir, tftp, slot, new_version):
        """ Create and upload an SIMG file """
        filename = self.filename

        # Create new image if necessary
        contents = open(filename).read()
        if self.type == "SPIF":
            start = int(slot.offset, 16)
            end = start + int(slot.size, 16)
            filename = tempfile.mkstemp(".simg", work_dir + "/")[1]
            open(filename, "w").write(contents[start:end])
        elif self.force_simg or not (self.skip_simg or has_simg(contents)):
            # Figure out version and daddr
            version = self.version
            daddr = self.daddr
            if version == None:
                version = new_version
            if daddr == None:
                daddr = int(slot.daddr, 16)

            # Create simg
            simg = create_simg(contents, version=version,
                    daddr=daddr, skip_crc32=self.skip_crc32)
            filename = tempfile.mkstemp(".simg", work_dir + "/")[1]
            open(filename, "w").write(simg)

        # Upload to tftp
        basename = os.path.basename(filename)
        tftp.put_file(filename, basename)
        return basename<commit_msg>image: Validate image type with "file" tool, and image size<commit_after>#Copyright 2012 Calxeda, Inc.  All Rights Reserved. 

""" Image objects used by the cx_manage_util controller """

import os
import subprocess
import tempfile

from cxmanage.simg import create_simg, has_simg

class Image:
    """ An image consists of an image type, a filename, and any info needed
    to build an SIMG out of it. """

    def __init__(self, image_type, filename, version, daddr,
            force_simg, skip_simg, skip_crc32):
        self.type = image_type
        self.filename = filename
        self.version = version
        self.daddr = daddr
        self.skip_crc32 = skip_crc32

        if force_simg:
            self.has_simg = False
        elif skip_simg:
            self.has_simg = True
        else:
            contents = open(filename).read()
            self.has_simg = has_simg(contents)

        if not self.valid_type():
            raise ValueError("%s is not a valid %s image" % 
                    (os.path.basename(filename), image_type))

    def upload(self, work_dir, tftp, slot, new_version):
        """ Create and upload an SIMG file """
        filename = self.filename

        # Create new image if necessary
        contents = open(filename).read()
        if self.type == "SPIF":
            start = int(slot.offset, 16)
            end = start + int(slot.size, 16)
            filename = tempfile.mkstemp(".simg", work_dir + "/")[1]
            open(filename, "w").write(contents[start:end])
        elif not self.has_simg:
            # Figure out version and daddr
            version = self.version
            daddr = self.daddr
            if version == None:
                version = new_version
            if daddr == None:
                daddr = int(slot.daddr, 16)

            # Create simg
            simg = create_simg(contents, version=version,
                    daddr=daddr, skip_crc32=self.skip_crc32)
            filename = tempfile.mkstemp(".simg", work_dir + "/")[1]
            open(filename, "w").write(simg)

        # Verify image size
        if os.path.getsize(filename) > int(slot.size, 16):
            raise ValueError("%s is too large for slot %i" %
                    (os.path.basename(self.filename), int(slot.slot, 16)))

        # Upload to tftp
        basename = os.path.basename(filename)
        tftp.put_file(filename, basename)
        return basename

    def valid_type(self):
        """ Make sure the file type (reported by the "file" tool) is valid
        for this image type.
        
        Returns true/false """

        file_type = subprocess.check_output(["file", self.filename]).split()[1]
        print file_type

        if self.type == "SOC_ELF" and file_type != "ELF":
            return False
        elif file_type != "data":
            return False

        return True
{'fraction_non_alphanumeric': 0.09816339455351489, 'fraction_numerical': 0.010344099641123074, 'mean_word_length': 2.002534854245881, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 4, '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': '6768075', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1369, 'n_words': 394}