-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbagman.py
165 lines (133 loc) · 5.46 KB
/
bagman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import argparse
import os
import click
from utils import bagman_utils, db_utils
def arg_parser():
parser = argparse.ArgumentParser(description="bagman CLI")
subparsers = parser.add_subparsers(dest="command")
# upload command
upload_parser = subparsers.add_parser("upload", help="upload a recording to storage (optional: add to database)")
upload_parser.add_argument("recording_path_local", help="path to the local recording to upload")
upload_parser.add_argument(
"-m", "--move", action="store_true", help="move instead of copy the recording"
)
upload_parser.add_argument(
"-a", "--add", action="store_true", help="add recording to database"
)
# add command
add_parser = subparsers.add_parser(
"add", help="add a recording to database"
)
add_parser.add_argument("recording_name", help="name of the recording")
# delete command
delete_parser = subparsers.add_parser(
"delete", help="delete a recording from storage (optional: remove from database)"
)
delete_parser.add_argument("recording_name", help="name of the recording")
delete_parser.add_argument(
"-r", "--remove", action="store_true", help="remove recording from database"
)
# remove command
remove_parser = subparsers.add_parser(
"remove", help="remove a recording from database"
)
remove_parser.add_argument("recording_name", help="name of the recording")
# exist command
exist_parser = subparsers.add_parser(
"exist", help="check if recording exists in storage and database"
)
exist_parser.add_argument("recording_name", help="name of the recording")
return parser
def add_recording(db, recording_path):
if not os.path.exists(recording_path):
print("Recording does not exist in recordings storage. First upload recording before adding to database.")
exit(0)
exists_recording = db.contains_record("name", args.recording_name)
if exists_recording:
if not click.confirm(
"Recording already exists in database. Do you want to override it?",
default=True,
):
print("Operation cancelled.")
return
bagman_utils.db_add_recording(
recording_path,
db,
override=True,
store_metadata_file=True,
)
def remove_recording(db, recording_name):
exists_recording = db.contains_record("name", recording_name)
if not exists_recording:
print("Recording does not exist in database.")
# TODO check if available in storage
return
if not click.confirm(
f"Are you sure you want to delete {recording_name} from the database?",
default=False,
):
print("Operation cancelled.")
exit(0)
db.remove_record("name", recording_name)
if __name__ == "__main__":
config = bagman_utils.load_config()
args = arg_parser().parse_args()
if not args.command:
arg_parser().print_help()
exit(0)
db = db_utils.BagmanDB(config["database_path"])
if args.command == "upload":
recording_name = os.path.basename(os.path.normpath(args.recording_path_local))
if os.path.exists(os.path.join(config["recordings_storage"], recording_name)):
if not click.confirm(
"Recording already exists in storage. Do you want to override it?",
default=True,
):
print("Operation cancelled.")
exit(0)
try:
bagman_utils.upload_recording(
args.recording_path_local,
config["recordings_storage"],
move=args.move,
verbose=True,
)
except Exception as e:
print(f"upload failed: {str(e)}")
exit(0)
if args.add:
recording_path = os.path.join(
config["recordings_storage"], recording_name
)
add_recording(db, recording_path)
elif args.command == "add":
recording_path = os.path.join(config["recordings_storage"], args.recording_name)
add_recording(db, recording_path)
elif args.command == "delete":
recording_path = os.path.join(config["recordings_storage"], args.recording_name)
if not os.path.exists(recording_path):
print("Recording does not exist in storage")
else:
if click.confirm(
f"Are you sure you want to delete {args.recording_name} from storage?",
default=False,
):
os.remove(recording_path)
if os.path.exists(recording_path):
print(f"Failed to delete {args.recording_name} from storage.")
else:
print(f"{args.recording_name} has been successfully deleted from storage.")
else:
print("Operation cancelled.")
if args.remove:
remove_recording(db, args.recording_name)
elif args.command == "remove":
remove_recording(db, args.recording_name)
elif args.command == "exist":
recording_path = os.path.join(config["recordings_storage"], args.recording_name)
exists_recording_storage = os.path.exists(recording_path)
exists_recording = db.contains_record("name", args.recording_name)
print(
f"recording exists in storage: {'yes' if exists_recording_storage else 'no'}"
)
print(f"recording exists in database: {'yes' if exists_recording else 'no'}")