Modified from https://github.com/threestudio-project/threestudio/blob/main/threestudio/utils/callbacks.py#L60
| 108 | |
| 109 | |
| 110 | class CodeSnapshot(Callback): |
| 111 | """ |
| 112 | Modified from https://github.com/threestudio-project/threestudio/blob/main/threestudio/utils/callbacks.py#L60 |
| 113 | """ |
| 114 | def __init__(self, savedir): |
| 115 | self.savedir = savedir |
| 116 | |
| 117 | def get_file_list(self): |
| 118 | return [ |
| 119 | b.decode() |
| 120 | for b in set( |
| 121 | subprocess.check_output( |
| 122 | 'git ls-files -- ":!:configs/*"', shell=True |
| 123 | ).splitlines() |
| 124 | ) |
| 125 | | set( # hard code, TODO: use config to exclude folders or files |
| 126 | subprocess.check_output( |
| 127 | "git ls-files --others --exclude-standard", shell=True |
| 128 | ).splitlines() |
| 129 | ) |
| 130 | ] |
| 131 | |
| 132 | @rank_zero_only |
| 133 | def save_code_snapshot(self): |
| 134 | os.makedirs(self.savedir, exist_ok=True) |
| 135 | for f in self.get_file_list(): |
| 136 | if not os.path.exists(f) or os.path.isdir(f): |
| 137 | continue |
| 138 | os.makedirs(os.path.join(self.savedir, os.path.dirname(f)), exist_ok=True) |
| 139 | shutil.copyfile(f, os.path.join(self.savedir, f)) |
| 140 | |
| 141 | def on_fit_start(self, trainer, pl_module): |
| 142 | try: |
| 143 | self.save_code_snapshot() |
| 144 | except: |
| 145 | rank_zero_warn( |
| 146 | "Code snapshot is not saved. Please make sure you have git installed and are in a git repository." |
| 147 | ) |
| 148 | |
| 149 | |
| 150 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected