|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from ._version import __commit_id__, __version__ |
| 6 | + |
| 7 | + |
| 8 | +def get_git_revision_hash(): |
| 9 | + """ |
| 10 | + Get the hash for the git commit currently being used. |
| 11 | +
|
| 12 | + @return The most recent commit hash or a suitable message |
| 13 | +
|
| 14 | + """ |
| 15 | + |
| 16 | + return __commit_id__ |
| 17 | + |
| 18 | + |
| 19 | +def get_build_type(): |
| 20 | + if "dev" in __version__: |
| 21 | + return "Develop" |
| 22 | + else: |
| 23 | + return "Release" |
| 24 | + |
| 25 | + |
| 26 | +def setup_output_directory(dirname: str = None, subdir_name: str = None): |
| 27 | + """ |
| 28 | + Create an output directory if one doesn't already exist. Place an info |
| 29 | + file in this directory which lists the date/time created, the version of |
| 30 | + pcpostprocess, the command-line arguments provided, and the most recent git |
| 31 | + commit. The two parameters allow for a user specified top-level directory and |
| 32 | + a script-defined name for a subdirectory. |
| 33 | +
|
| 34 | + @param Optional directory name |
| 35 | + @param Optional subdirectory name |
| 36 | +
|
| 37 | + @return The path to the created file directory (String) |
| 38 | + """ |
| 39 | + |
| 40 | + if dirname is None: |
| 41 | + if subdir_name: |
| 42 | + dirname = os.path.join("output", f"{subdir_name}") |
| 43 | + else: |
| 44 | + dirname = os.path.join("output", "output") |
| 45 | + |
| 46 | + if subdir_name is not None: |
| 47 | + dirname = os.path.join(dirname, subdir_name) |
| 48 | + if not os.path.exists(dirname): |
| 49 | + os.makedirs(dirname) |
| 50 | + |
| 51 | + with open(os.path.join(dirname, "pcpostprocess_info.txt"), "w") as description_fout: |
| 52 | + git_hash = get_git_revision_hash() |
| 53 | + datetimestr = str(datetime.datetime.now()) |
| 54 | + description_fout.write("pcpostprocess output " |
| 55 | + "https://github.com/CardiacModelling/pcpostprocess\n") |
| 56 | + description_fout.write(f"Date: {datetimestr}\n") |
| 57 | + description_fout.write(f"Version: {__version__}\n") |
| 58 | + description_fout.write(f"Build type: {get_build_type()}\n") |
| 59 | + description_fout.write(f"Commit: {git_hash}\n") |
| 60 | + command = " ".join(sys.argv) |
| 61 | + description_fout.write(f"Command: {command}\n") |
| 62 | + return dirname |
| 63 | + |
0 commit comments