Skip to content

Local Module

This section documents the local components of the Nextmv Python SDK - Local experience.

local

Local module to hold convenience functions used in the local package.

FUNCTION DESCRIPTION
calculate_files_size

Function to calculate the total size of files in a directory.

ATTRIBUTE DESCRIPTION
OUTPUT_KEY

Output key constant used for identifying output in the run output.

TYPE: str

LOGS_KEY

Logs key constant used for identifying logs in the run output.

TYPE: str

LOGS_FILE

Constant used for identifying the file used for logging.

TYPE: str

DEFAULT_OUTPUT_JSON_FILE

Constant for the default output JSON file name.

TYPE: str

RUNS_KEY

Runs key constant used for identifying the runs directory in the nextmv location.

TYPE: str

NEXTMV_DIR

Constant for the Nextmv directory name.

TYPE: str

DEFAULT_INPUT_JSON_FILE

Constant for the default input JSON file name.

TYPE: str

DEFAULT_INPUT_JSON_FILE module-attribute

DEFAULT_INPUT_JSON_FILE = 'input.json'

Constant for the default input JSON file name.

DEFAULT_INPUT_TEXT_FILE module-attribute

DEFAULT_INPUT_TEXT_FILE = 'input'

Constant for the default input text file name.

DEFAULT_OUTPUT_JSON_FILE module-attribute

DEFAULT_OUTPUT_JSON_FILE = 'solution.json'

Constant for the default output JSON file name.

LOGS_FILE module-attribute

LOGS_FILE = 'logs.log'

Constant used for identifying the file used for logging.

LOGS_KEY module-attribute

LOGS_KEY = 'logs'

Logs key constant used for identifying logs in the run output.

NEXTMV_DIR module-attribute

NEXTMV_DIR = '.nextmv'

Constant for the Nextmv directory name.

OUTPUT_KEY module-attribute

OUTPUT_KEY = 'output'

Output key constant used for identifying output in the run output.

REGISTRY_FILE module-attribute

REGISTRY_FILE = 'registry.yaml'

Constant for the local registry file name. This file stores information about the apps on the local machine. It is located in the Nextmv directory in the user's home directory.

RUNS_KEY module-attribute

RUNS_KEY = 'runs'

Runs key constant used for identifying the runs directory in the nextmv location.

calculate_files_size

calculate_files_size(
    run_dir: str,
    run_id: str,
    dir_path: str,
    metadata_key: str,
) -> None

Calculates the total size of the files in a directory, in bytes.

The calculated size is stored in the run information metadata under the specified key.

PARAMETER DESCRIPTION

run_dir

The path to the run directory.

TYPE: str

run_id

The ID of the run.

TYPE: str

dir_path

The path to the directory whose size is to be calculated.

TYPE: str

metadata_key

The key under which to store the calculated size in the run information metadata.

TYPE: str

Source code in nextmv/nextmv/local/local.py
def calculate_files_size(run_dir: str, run_id: str, dir_path: str, metadata_key: str) -> None:
    """
    Calculates the total size of the files in a directory, in bytes.

    The calculated size is stored in the run information metadata under the
    specified key.

    Parameters
    ----------
    run_dir : str
        The path to the run directory.
    run_id : str
        The ID of the run.
    dir_path : str
        The path to the directory whose size is to be calculated.
    metadata_key : str
        The key under which to store the calculated size in the run information
        metadata.
    """

    total_size = 0
    for dirpath, _, filenames in os.walk(dir_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            # Skip if it is a symbolic link
            if os.path.islink(fp):
                continue
            total_size += os.path.getsize(fp)

    info_file = os.path.join(run_dir, f"{run_id}.json")
    with open(info_file, "r+") as f:
        info = json.load(f)
        info["metadata"][metadata_key] = total_size
        f.seek(0)
        json.dump(info, f, indent=2)
        f.truncate()