diff options
author | SeongJae Park <sj38.park@gmail.com> | 2024-03-17 10:51:39 -0700 |
---|---|---|
committer | SeongJae Park <sj38.park@gmail.com> | 2024-03-17 10:51:39 -0700 |
commit | e6de2c13398f757b09d422cc297b3bb5e6eab330 (patch) | |
tree | fecd15183234af6ec7568fdb4135795f1f3d6ff1 | |
parent | fa890b00d1dab301a5bcc13e45f7d747b94ecc39 (diff) | |
download | damo-e6de2c13398f757b09d422cc297b3bb5e6eab330.tar.gz |
_damo_fs: Support dryrun
Add dryrun mode from _damo_fs, for debugging and testing purpose.
Signed-off-by: SeongJae Park <sj38.park@gmail.com>
-rw-r--r-- | _damo_fs.py | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/_damo_fs.py b/_damo_fs.py index 075449a6..50289917 100644 --- a/_damo_fs.py +++ b/_damo_fs.py @@ -3,18 +3,35 @@ import os debug_do_print = False +debug_dryrun_logs = None +debug_dryrun_read_outputs = None def debug_print_ops(do_print): global debug_do_print debug_do_print = do_print +def debug_dryrun(read_outputs): + '''Set damo_fs to not do the real io, but just log the ops in a buffer''' + global debug_dryrun_logs + global debug_dryrun_read_outputs + debug_dryrun_logs = [] + debug_dryrun_read_outputs = read_outputs + +def debug_get_dryrun_logs(): + return debug_dryrun_logs + '''Returns content and error''' def read_file(filepath): - try: - with open(filepath, 'r') as f: - content = f.read() - except Exception as e: - return None, 'reading %s failed (%s)' % (filepath, e) + if debug_dryrun_logs is not None: + content = debug_dryrun_read_outputs[filepath] + debug_dryrun_logs.append('read \'%s\': \'%s\'' % + (filepath, content.strip())) + else: + try: + with open(filepath, 'r') as f: + content = f.read() + except Exception as e: + return None, 'reading %s failed (%s)' % (filepath, e) if debug_do_print: print('read \'%s\': \'%s\'' % (filepath, content.strip())) return content, None @@ -37,6 +54,10 @@ Returns None if success error string otherwise def write_file(filepath, content): if debug_do_print: print('write \'%s\' to \'%s\'' % (content.strip(), filepath)) + if debug_dryrun_logs is not None: + debug_dryrun_logs.append( + 'write \'%s\' to \'%s\'' % (content.strip(), filepath)) + return None try: with open(filepath, 'w') as f: f.write(content) |