diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9a4b538f3..4d4271604 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,7 +18,7 @@ Change Log - Added extract_file_from_zip to zip_utils. - Added http_utils with download function. - Added get_app_specific_directory, get_os_name, and get_cpu_architecture_name to misc_utils. - - Added normalize_file_path and are_files_equal to file_utils. + - Added normalize_file_path, are_files_equal, and create_random_file to file_utils. - Added create_temporary_file_name and remove_temporary_file tmpfile_utils. - Minor fix to misc_utils.create_dict (do not create property only if its value is None). diff --git a/dcicutils/file_utils.py b/dcicutils/file_utils.py index 1cd62da6f..a1bd0def5 100644 --- a/dcicutils/file_utils.py +++ b/dcicutils/file_utils.py @@ -1,7 +1,10 @@ import glob import os import pathlib +import random +import string from typing import List, Optional, Union +from dcicutils.tmpfile_utils import create_temporary_file_name def search_for_file(file: str, @@ -96,3 +99,16 @@ def are_files_equal(filea: str, fileb: str) -> bool: return True except Exception: return False + + +def create_random_file(file: Optional[str] = None, + prefix: Optional[str] = None, suffix: Optional[str] = None, + nbytes: int = 1024, binary: bool = False) -> str: + if not file: + file = create_temporary_file_name(prefix=prefix, suffix=suffix) + with open(file, "wb" if binary is True else "w") as f: + if binary is True: + f.write(os.urandom(nbytes)) + else: + f.write(''.join(random.choices(string.ascii_letters + string.digits, k=nbytes))) + return file diff --git a/pyproject.toml b/pyproject.toml index 70517487e..65a60a7e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.4.1b8" # TODO: To become 8.8.5 +version = "8.8.4.1b9" # TODO: To become 8.8.5 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT"