def real_absolute_path_for_path(self, path: GcsfsFilePath) -> str:
        if path.abs_path() in self.uploaded_test_path_to_actual:
            return self.uploaded_test_path_to_actual[path.abs_path()]

        directory_path, _ = os.path.split(path.abs_path())

        parts = filename_parts_from_path(path)
        suffix = f'_{parts.filename_suffix}' if parts.filename_suffix else ''
        fixture_filename = f'{parts.file_tag}{suffix}.{parts.extension}'

        actual_fixture_file_path = \
            fixtures.file_path_from_relative_path(
                os.path.join(directory_path, fixture_filename))

        tempfile_path = self.generate_random_temp_path()
        return shutil.copyfile(actual_fixture_file_path, tempfile_path)
Exemple #2
0
def _get_fixture_for_direct_ingest_path(path: GcsfsFilePath) -> str:
    """Gets the path to the fixture file based on the input path.

    If `path` is normalized, strips it to just `file_tag`, `filename_suffix`, and `extension`. Takes `path` and uses
    it as a relative path from `recidiviz/tests/ingest` to get the associated fixture.
    """
    relative_path = path.abs_path()

    try:
        directory_path, _ = os.path.split(path.abs_path())
        parts = filename_parts_from_path(path)
        suffix = f'_{parts.filename_suffix}' if parts.filename_suffix else ''
        relative_path = os.path.join(
            directory_path, f'{parts.file_tag}{suffix}.{parts.extension}')
    except DirectIngestError as e:
        if e.error_type != DirectIngestErrorType.INPUT_ERROR:
            raise
        # Otherwise, assume it failed because it is not a normalized path so we can just use the `fixture_filename`
        # directly.

    return fixtures.file_path_from_relative_path(relative_path)
    def download_to_temp_file(self, path: GcsfsFilePath) -> Optional[GcsfsFileContentsHandle]:
        """Downloads file contents into local temporary_file, returning path to
        temp file, or None if the path no-longer exists in the GCS file system.
        """
        if not self.exists(path):
            return None

        if path.abs_path() in self.uploaded_test_path_to_actual:
            return GcsfsFileContentsHandle(self.uploaded_test_path_to_actual[path.abs_path()])

        directory_path, _ = os.path.split(path.abs_path())

        parts = filename_parts_from_path(path)
        suffix = f'_{parts.filename_suffix}' if parts.filename_suffix else ''
        fixture_filename = f'{parts.file_tag}{suffix}.{parts.extension}'

        actual_fixture_file_path = \
            fixtures.file_path_from_relative_path(
                os.path.join(directory_path, fixture_filename))

        tempfile_path = self.generate_random_temp_path()

        return GcsfsFileContentsHandle(shutil.copyfile(actual_fixture_file_path, tempfile_path))