fake_btrfs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import argparse
  2. import json
  3. import os
  4. import shutil
  5. import sys
  6. def parse_arguments(*unparsed_arguments):
  7. global_parser = argparse.ArgumentParser(add_help=False)
  8. action_parsers = global_parser.add_subparsers(dest='action')
  9. subvolume_parser = action_parsers.add_parser('subvolume')
  10. subvolume_subparser = subvolume_parser.add_subparsers(dest='subaction')
  11. list_parser = subvolume_subparser.add_parser('list')
  12. list_parser.add_argument('-s', dest='snapshots_only', action='store_true')
  13. list_parser.add_argument('subvolume_path')
  14. snapshot_parser = subvolume_subparser.add_parser('snapshot')
  15. snapshot_parser.add_argument('-r', dest='read_only', action='store_true')
  16. snapshot_parser.add_argument('subvolume_path')
  17. snapshot_parser.add_argument('snapshot_path')
  18. delete_parser = subvolume_subparser.add_parser('delete')
  19. delete_parser.add_argument('snapshot_path')
  20. property_parser = action_parsers.add_parser('property')
  21. property_subparser = property_parser.add_subparsers(dest='subaction')
  22. get_parser = property_subparser.add_parser('get')
  23. get_parser.add_argument('-t', dest='type')
  24. get_parser.add_argument('subvolume_path')
  25. get_parser.add_argument('property_name')
  26. return (global_parser, global_parser.parse_args(unparsed_arguments))
  27. BUILTIN_SUBVOLUME_LIST_LINES = (
  28. '261 gen 29 top level 5 path sub',
  29. '262 gen 29 top level 5 path other',
  30. )
  31. SUBVOLUME_LIST_LINE_PREFIX = '263 gen 29 top level 5 path '
  32. def load_snapshots():
  33. try:
  34. return json.load(open('/tmp/fake_btrfs.json'))
  35. except FileNotFoundError:
  36. return []
  37. def save_snapshots(snapshot_paths):
  38. json.dump(snapshot_paths, open('/tmp/fake_btrfs.json', 'w'))
  39. def print_subvolume_list(arguments, snapshot_paths):
  40. assert arguments.subvolume_path == '/e2e/mnt/subvolume'
  41. if not arguments.snapshots_only:
  42. for line in BUILTIN_SUBVOLUME_LIST_LINES:
  43. print(line)
  44. for snapshot_path in snapshot_paths:
  45. print(
  46. SUBVOLUME_LIST_LINE_PREFIX
  47. + snapshot_path[snapshot_path.index('.borgmatic-snapshot-') :]
  48. )
  49. def main():
  50. (global_parser, arguments) = parse_arguments(*sys.argv[1:])
  51. snapshot_paths = load_snapshots()
  52. if not hasattr(arguments, 'subaction'):
  53. global_parser.print_help()
  54. sys.exit(1)
  55. if arguments.subaction == 'list':
  56. print_subvolume_list(arguments, snapshot_paths)
  57. elif arguments.subaction == 'snapshot':
  58. snapshot_paths.append(arguments.snapshot_path)
  59. save_snapshots(snapshot_paths)
  60. subdirectory = os.path.join(arguments.snapshot_path, 'subdir')
  61. os.makedirs(subdirectory, mode=0o700, exist_ok=True)
  62. test_file = open(os.path.join(subdirectory, 'file.txt'), 'w')
  63. test_file.write('contents')
  64. test_file.close()
  65. elif arguments.subaction == 'delete':
  66. subdirectory = os.path.join(arguments.snapshot_path, 'subdir')
  67. shutil.rmtree(subdirectory)
  68. snapshot_paths = [
  69. snapshot_path
  70. for snapshot_path in snapshot_paths
  71. if snapshot_path.endswith('/' + arguments.snapshot_path)
  72. ]
  73. save_snapshots(snapshot_paths)
  74. elif arguments.action == 'property' and arguments.subaction == 'get':
  75. print(f'{arguments.property_name}=false')
  76. if __name__ == '__main__':
  77. main()