Skip to content

project.py

ofrak.project.project

OfrakProject

An OFRAK 'project'

init_from_path(path) staticmethod

Assume path points to a directory with the following structure: (top-level directory) |-metadata.json |-README.md |--binaries | |-binary1.bin | | ... |--scripts |-script1.py | ...

Parameters:

Name Type Description Default
path str required

Returns:

Type Description
OfrakProject
Source code in ofrak/project/project.py
@staticmethod
def init_from_path(path: str) -> "OfrakProject":
    """

    Assume path points to a directory with the following structure:
    (top-level directory)
    |-metadata.json
    |-README.md
    |--binaries
    |   |-binary1.bin
    |   | ...
    |--scripts
        |-script1.py
        | ...

    :param path:
    :return:
    """
    if not os.path.exists(path):
        raise ValueError(f"{path} does not exist")
    if not os.path.isdir(path):
        raise ValueError(f"{path} is not a directory")

    metadata_path = os.path.join(path, "metadata.json")
    binaries_path = os.path.join(path, "binaries")
    scripts_path = os.path.join(path, "scripts")

    if not all(
        [
            os.path.exists(metadata_path),
            os.path.exists(binaries_path),
            os.path.isdir(binaries_path),
            os.path.exists(scripts_path),
            os.path.isdir(scripts_path),
        ]
    ):
        raise ValueError(f"{path} has invalid structure to be a Project")

    with open(metadata_path) as f:
        raw_metadata = json.load(f)

    scripts = [script["name"] for script in raw_metadata["scripts"]]

    binaries = {}

    for binary_name, info in raw_metadata["binaries"].items():
        binaries[binary_name] = _OfrakProjectBinary(
            info["associated_scripts"], info.get("init_script")
        )
    name = raw_metadata["name"]
    project_id = binascii.unhexlify(raw_metadata["project_id"])
    project = OfrakProject(
        path,
        name,
        project_id,
        binaries,
        scripts,
    )

    project._restore_trashed_files()
    return project

_OfrakProjectBinary dataclass private

_OfrakProjectBinary(associated_scripts: List[str], init_script: Union[str, NoneType])