Set the directory for global logging. Args: dirname(str): log directory action(str): an action of ["k","d","q"] to be performed when the directory exists. Will ask user by default. "d": delete the directory. Note that the deletion may fail when
(dirname, action=None)
| 93 | |
| 94 | |
| 95 | def set_logger_dir(dirname, action=None): |
| 96 | """ |
| 97 | Set the directory for global logging. |
| 98 | |
| 99 | Args: |
| 100 | dirname(str): log directory |
| 101 | action(str): an action of ["k","d","q"] to be performed |
| 102 | when the directory exists. Will ask user by default. |
| 103 | |
| 104 | "d": delete the directory. Note that the deletion may fail when |
| 105 | the directory is used by tensorboard. |
| 106 | |
| 107 | "k": keep the directory. This is useful when you resume from a |
| 108 | previous training and want the directory to look as if the |
| 109 | training was not interrupted. |
| 110 | Note that this option does not load old models or any other |
| 111 | old states for you. It simply does nothing. |
| 112 | |
| 113 | """ |
| 114 | dirname = os.path.normpath(dirname) |
| 115 | global LOG_DIR, _FILE_HANDLER |
| 116 | if _FILE_HANDLER: |
| 117 | # unload and close the old file handler, so that we may safely delete the logger directory |
| 118 | _logger.removeHandler(_FILE_HANDLER) |
| 119 | del _FILE_HANDLER |
| 120 | |
| 121 | def dir_nonempty(dirname): |
| 122 | # If directory exists and nonempty (ignore hidden files), prompt for action |
| 123 | return os.path.isdir(dirname) and len([x for x in os.listdir(dirname) if x[0] != '.']) |
| 124 | |
| 125 | if dir_nonempty(dirname): |
| 126 | if not action: |
| 127 | _logger.warning("""\ |
| 128 | Log directory {} exists! Use 'd' to delete it. """.format(dirname)) |
| 129 | _logger.warning("""\ |
| 130 | If you're resuming from a previous run, you can choose to keep it. |
| 131 | Press any other key to exit. """) |
| 132 | while not action: |
| 133 | action = input("Select Action: k (keep) / d (delete) / q (quit):").lower().strip() |
| 134 | act = action |
| 135 | if act == 'b': |
| 136 | backup_name = dirname + _get_time_str() |
| 137 | shutil.move(dirname, backup_name) |
| 138 | info("Directory '{}' backuped to '{}'".format(dirname, backup_name)) # noqa: F821 |
| 139 | elif act == 'd': |
| 140 | shutil.rmtree(dirname, ignore_errors=True) |
| 141 | if dir_nonempty(dirname): |
| 142 | shutil.rmtree(dirname, ignore_errors=False) |
| 143 | elif act == 'n': |
| 144 | dirname = dirname + _get_time_str() |
| 145 | info("Use a new log directory {}".format(dirname)) # noqa: F821 |
| 146 | elif act == 'k': |
| 147 | pass |
| 148 | else: |
| 149 | raise OSError("Directory {} exits!".format(dirname)) |
| 150 | LOG_DIR = dirname |
| 151 | from .fs import mkdir_p |
| 152 | mkdir_p(dirname) |
no test coverage detected