Exemplo n.º 1
0
    def from_path(cls, name, platforms_path):
        # TODO: Parse rest of settings.ini, eg memory available
        full_path = os.path.join(platforms_path, name)
        config = configparser.ConfigParser()
        config.read(os.path.join(full_path, "settings.ini"))
        tmpl_path = None
        if config.has_option('platform_settings', 'templatePlatform'):
            tmpl_path = os.path.join(
                platforms_path,
                config.get('platform_settings', 'templatePlatform'))

        clock_hz = config.getint('platform_settings', 'cyclespersecond')
        pagesize = config.getint('platform_settings', 'pagesize')

        compilers = cls.__enum_compilers(full_path, tmpl_path)

        hash = dirchecksum(full_path)
        tmpl_rel_path = None
        if tmpl_path:
            h = hashlib.sha256()
            h.update(hash)
            h.update(dirchecksum(tmpl_path))
            hash = h.sha256()
            if tmpl_path:
                tmpl_rel_path = os.path.relpath(tmpl_path, platforms_path)

        return cls(hash=hash,
                   name=name,
                   path=os.path.relpath(full_path, platforms_path),
                   tmpl_path=tmpl_rel_path,
                   clock_hz=clock_hz,
                   pagesize=pagesize,
                   compilers=compilers)
Exemplo n.º 2
0
def hash_config(config_path):
    config_hash = xbx.util.sha256_file(config_path)

    if config_hash in config_hash_cache:
        return config_hash_cache[config_hash]

    config = configparser.ConfigParser()
    config.read(DEFAULT_CONF)
    config.read(config_path)

    file_paths = (DEFAULT_CONF, config_path, config.get('paths', 'operations'),
                  config.get('paths', 'impl_conf'))
    #        os.path.realpath(__file__)    # We hash this file so that edits here trigger a reparse

    dir_paths = (
        config.get('paths', 'platforms'),
        config.get('paths', 'algopacks'),
        config.get('paths', 'embedded'),
    )

    dir_hashes = list(map(lambda x: dirchecksum(x), dir_paths))
    file_hashes = list(map(lambda x: xbx.util.sha256_file(x), file_paths))

    h = hashlib.sha256()
    for f in (dir_hashes + file_hashes):
        h.update(f.encode())

    h.update(config_hash.encode())

    config_hash_cache[config_hash] = h.hexdigest()

    return config_hash_cache[config_hash]
Exemplo n.º 3
0
def _enum_prim_impls(operation, primitive_names, algopack_path):
    """Enumerations primitives and implementation, and sets lists of
    implementations in config

    Parameters:
        operation       Instance of Operation
        primitive_names List of primitive names
    """

    _logger.debug("Enumerating primitives and implementations")
    primitives = []
    implementations = []

    # Get operation path
    op_path = os.path.join(algopack_path, operation.name)

    if not os.path.isdir(op_path):
        raise ValueError("Operation {} directory does not exist!".format(
            operation.name))

    # Get list of primitive directories
    primdirs = [
        d for d in os.listdir(op_path)
        if os.path.isdir(os.path.join(op_path, d))
    ]

    # Get prim directories intersecting w/ primitive list
    for name in primitive_names:
        if name not in primdirs:
            raise ValueError(
                "Primitive {} directory does not exist!".format(name))

        path = os.path.join(op_path, name)
        try:
            checksumfile = os.path.join(path, "checksumsmall")
            checksumsmall = ""
            with open(checksumfile) as f:
                checksumsmall = f.readline().strip()
        except FileNotFoundError:
            _logger.warn(("Checksumsmall file not found for "
                          "{}/{}").format(operation.name, name))

        try:
            checksumfile = os.path.join(path, "checksumbig")
            checksumbig = ""
            with open(checksumfile) as f:
                checksumbig = f.readline().strip()
        except FileNotFoundError:
            _logger.warn(("Checksumbig file not found for "
                          "{}/{}").format(operation.name, name))

        p = Primitive(operation=operation,
                      name=name,
                      path=os.path.relpath(path, algopack_path),
                      checksumsmall=checksumsmall,
                      checksumbig=checksumbig)

        primitives += p,

    for p in primitives:
        all_impls = {}
        p_fpath = os.path.join(algopack_path, p.path)

        # Find all directories w/ api.h
        walk = os.walk(p_fpath)
        for dirpath, dirnames, filenames in walk:
            if "api.h" in filenames:
                path = os.path.relpath(dirpath, p_fpath)
                name = path.translate(str.maketrans("./-", "___"))
                all_impls[name] = path

        for name in all_impls.keys():
            # Parse api.h and get macro values
            path = os.path.join(p_fpath, all_impls[name])
            checksum = dirchecksum(path)

            api_h = None
            with open(os.path.join(path, "api.h")) as f:
                api_h = f.read()

            macros = {}
            macro_names = operation.macro_names + ['_VERSION']

            for m in macro_names:
                value = None
                match = re.search(m + r'\s+"(.*)"\s*$', api_h, re.MULTILINE)
                if (match):
                    value = match.group(1).strip('"')
                else:
                    match = re.search(m + r'\s+(.*)\s*$', api_h, re.MULTILINE)
                    try:
                        value = int(match.group(1))
                    except ValueError:
                        value = match.group(1)
                    except AttributeError:
                        value = None
                macros[m] = value

            # Don't have to add to p.implementations, as Implementation sets
            # Primitive as parent, which inserts into list
            implementations += Implementation(hash=checksum,
                                              name=name,
                                              path=os.path.relpath(
                                                  path, algopack_path),
                                              primitive=p,
                                              macros=macros),

    return implementations
Exemplo n.º 4
0
 def validate_hash(self, platforms_path):
     """Verifies if hash still valid"""
     hash = dirchecksum(os.path.join(platforms_path, self.path))
     return hash == self.hash
Exemplo n.º 5
0
 def valid_hex_checksum(self):
     """Verifies if checksum still valid"""
     hash = dirchecksum(self.hex_path)
     return hash == self.hex_checksum