Add help info.

This commit is contained in:
xiao-77 2025-01-23 13:56:44 +08:00
parent 1e7d5e1754
commit 66452f6983
1 changed files with 32 additions and 26 deletions

View File

@ -3,38 +3,44 @@ import sys
import shutil import shutil
import time import time
import os import os
import argparse
if len(sys.argv) < 2: def main():
print("Usage: python3 delete_fid.py <fid> [file_path]") parser = argparse.ArgumentParser(description='Repair TSDB data by removing specified fid.')
sys.exit(1) parser.add_argument('fid', type=int, help='The fid to be removed')
parser.add_argument('file_path', nargs='?', default='current.json', help='The path to the JSON file (default: current.json)')
args = parser.parse_args()
target_fid = int(sys.argv[1]) target_fid = args.fid
file_path = sys.argv[2] if len(sys.argv) > 2 else "current.json" file_path = args.file_path
# Read file content # Read file content
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
data = json.load(file) data = json.load(file)
# Check if the fid exists # Check if the fid exists
fid_exists = any(item.get('fid') == target_fid for item in data['fset']) fid_exists = any(item.get('fid') == target_fid for item in data['fset'])
if not fid_exists: if not fid_exists:
print(f"Error: fid {target_fid} does not exist in the file.") print(f"Error: fid {target_fid} does not exist in the file.")
sys.exit(1) sys.exit(1)
# Generate backup file name # Generate backup file name
timestamp = time.strftime("%Y%m%d%H%M%S") timestamp = time.strftime("%Y%m%d%H%M%S")
parent_directory = os.path.dirname(os.path.dirname(file_path)) parent_directory = os.path.dirname(os.path.dirname(file_path))
backup_file_path = os.path.join(parent_directory, f"current.json.{timestamp}") backup_file_path = os.path.join(parent_directory, f"current.json.{timestamp}")
# Backup file # Backup file
shutil.copy(file_path, backup_file_path) shutil.copy(file_path, backup_file_path)
print(f"Backup created: {backup_file_path}") print(f"Backup created: {backup_file_path}")
# Remove objects with the specified fid from the fset list # Remove objects with the specified fid from the fset list
data['fset'] = [item for item in data['fset'] if item.get('fid') != target_fid] data['fset'] = [item for item in data['fset'] if item.get('fid') != target_fid]
# Write the updated content back to the file, preserving the original format # Write the updated content back to the file, preserving the original format
with open(file_path, 'w') as file: with open(file_path, 'w') as file:
json.dump(data, file, separators=(',', ':'), ensure_ascii=False) json.dump(data, file, separators=(',', ':'), ensure_ascii=False)
print(f"Removed content with fid {target_fid}.") print(f"Removed content with fid {target_fid}.")
if __name__ == '__main__':
main()