This repository was archived by the owner on Jul 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrash
More file actions
executable file
·310 lines (246 loc) · 8.46 KB
/
trash
File metadata and controls
executable file
·310 lines (246 loc) · 8.46 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
####--------------------------------####
#--# Author: by uriid1 #--#
#--# License: GNU GPL #--#
#--# Telegram: @foxdacam #--#
#--# Mail: appdurov@gmail.com #--#
####--------------------------------####
# libs
import sys
import time
import os.path, os
import shutil
from datetime import datetime
from urllib.parse import unquote, quote
# Path to trash
# Constans
HOME = os.path.expanduser("~")
TRASH_PATH = os.path.join(HOME, '.local', 'share', 'Trash')
TRASH_PATH2FILES = os.path.join(TRASH_PATH, 'files')
TRASH_PATH2INFO = os.path.join(TRASH_PATH, 'info')
# Help text :)
help_text = '''Discription:
Managing the Trash from the Command Line.
------------------------------------
Author: by uriid1
License: GNU GPL
Telegram: @main_moderator
Mail: appdurov@gmail.com
------------------------------------
-P, --put
Sending one or more files to the trash.
The '*' operator is supported to send all
files from the specified directory.
Example: trash --put *
-L, --list
show the content of the Trash.
Example: trash --list
-RM, --remove
Deleting one or more files from the trash.
Example: trash -RM foo bar
-E, --empty
Emptying the Trash.
Use the -y option to delete without confirmation.
Example: trash -E -y
-RS, --restore
Restoring one or more files.
Example: trash -RS foo bar
-RSA, --restore-all
Restoring all files.
Example: trash --restore-all
'''
################################
# Auxiliary functions
################################
# Console color print
c_warn = [192, 57, 43]
c_info = [246, 116, 0]
c_folder = [61, 174, 233]
c_file = [28, 220, 154]
def colored(color, text):
return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(color[0], color[1], color[2], text)
# Confirmation for further interaction
def confirm(text):
confirm = input(text)
return confirm == 'y' or confirm == 'Y'
# Get current date and time in format dd-mm-YYTH:M:S
def make_date_and_time():
now = datetime.now()
dt_string = now.strftime("%Y-%m-%dT%H:%M:%S")
return dt_string
# Info file according to the specification
def make_trash_info(target_name, path):
info = "[Trash Info]\nPath={}\nDeletionDate={}".format(quote(path), make_date_and_time())
with open(os.path.join(TRASH_PATH2INFO, target_name+'.trashinfo'), 'a') as out:
out.write(info)
# Checking if a file or directory exists
def is_exists(path):
return os.path.isfile(path) or os.path.isdir(path)
################################
# Main functions
################################
# Put in Trash
def put(target):
# Target check
path_target = os.path.join(os.getcwd(), target)
if not is_exists(path_target):
print(colored(c_warn, target)+' is not a file or directory')
return
# Check file exists in trash
target_name = target
if is_exists(os.path.join(TRASH_PATH2FILES, target)):
old_target_name = target
date = make_date_and_time()
target_prop = os.path.splitext(target)
target_name = target_prop[0]+'_[{}]'.format(date)+target_prop[1]
print('{} is exists in Trash. Rename to {}'.format(colored(c_info, target), colored(c_info, target_name)))
# Move target
shutil.move(target, os.path.join(TRASH_PATH2FILES, target_name))
# Make trash info
make_trash_info(target_name, path_target)
# Inform
print('{} has been moved to {}'.format(colored(c_info, target_name), colored(c_info, 'Trash')))
# List files/dirs from Trash
def list():
files = os.scandir(TRASH_PATH2FILES)
for f in files:
status = os.stat(f)
if f.is_dir():
print(colored(c_info, time.ctime(status.st_ctime)), colored(c_folder, f.name)+'/')
else:
print(colored(c_info, time.ctime(status.st_ctime)), colored(c_file, f.name))
# Remove file or dir from Trash
def rm(path, target):
if not is_exists(path):
print('{} is not exists in Trash'.format(colored(c_warn, target)))
return
# Remove target
if os.path.isfile(path) or os.path.islink(path):
# remove the file
os.remove(path)
elif os.path.isdir(path):
# remove dir and all contains
shutil.rmtree(path)
# Remove info file
info_file = os.path.join(TRASH_PATH2INFO, target+'.trashinfo')
if os.path.isfile(info_file):
os.remove(info_file)
# Inform
print('{} successfully deleted'.format(colored(c_info, target)))
# Restore file
def restore(path, target):
if not is_exists(path):
print('{} is not exists in Trash'.format(colored(c_warn, target)))
return
# Check info file
path_info_file = os.path.join(TRASH_PATH2INFO, target+'.trashinfo')
if not is_exists(path_info_file):
print('{} - info file is not exists in Trash'.format(colored(c_warn, target)))
return
# Parse restore path
with open(path_info_file, "r") as f:
text = f.readlines()
restore_path = unquote(text[1].split('=')[1].replace('\n', ''))
# File exists
if is_exists(restore_path):
if not confirm("[File exists on the restore path. Overwrite? Y/n]: "):
return
# Move file
shutil.move(path, restore_path)
# Delete info file
os.remove(path_info_file)
# Inform
print('Restored '+colored(c_info, restore_path))
################################
# Functions exec by a given key
################################
# Checking the entered arguments for some keys
def args_count_check(argv):
args_len = len(argv)
if args_len == 2:
print(colored(c_warn, "No argument specified. Use: trash -help"))
return False
if args_len > 3:
return True
return False
# Exec a function by key -P, --put
def exec_put(argv):
# Put all files
if args_count_check(argv):
if confirm("[Move all files to the trash? Y/n]: "):
for i in range(2, len(argv)):
put(argv[i])
return
# Put one file
if len(argv) > 2:
put(sys.argv[2])
# Exec a function by key -L, --list
def exec_list():
list()
# Exec a function by key -RM, --remove
def exec_rm(argv):
# Delete multiple files
if args_count_check(argv):
for i in range(2, len(argv)):
rm(os.path.join(TRASH_PATH2FILES, argv[i]), argv[i])
return
# Delete one file
if len(argv) > 2:
rm(os.path.join(TRASH_PATH2FILES, argv[2]), argv[2])
# Exec a function by key -E, --empty
def exec_empty(argv):
# Without confirm
if len(argv) == 3:
if argv[2] == '-y':
files = os.listdir(TRASH_PATH2FILES)
for f in files:
rm(os.path.join(TRASH_PATH2FILES, f), f)
return
# With confirm
if confirm("[Empty trash. Are you sure? Y/n]: "):
files = os.listdir(TRASH_PATH2FILES)
for f in files:
rm(os.path.join(TRASH_PATH2FILES, f), f)
# Exec a function by key -RE, --restore
def exec_restore(argv):
# Restore multiple files
if args_count_check(argv):
for i in range(2, len(sys.argv)):
restore(os.path.join(TRASH_PATH2FILES, argv[i]), argv[i])
return
# Restore target
if len(argv) > 2:
restore(os.path.join(TRASH_PATH2FILES, argv[2]), argv[2])
# Exec a function by key -RSA, --restore-all
def exec_restore_all():
if confirm("[Restore trash. Are you sure? Y/n]: "):
files = os.listdir(TRASH_PATH2FILES)
for f in files:
restore(os.path.join(TRASH_PATH2FILES, f), f)
def main():
# Checking for the existence of arguments
if len(sys.argv) == 1:
print(colored(c_warn, "No argument specified"))
return
# Args switch-case
match sys.argv[1]:
case '-L' | '--list':
exec_list()
case '-P' | '--put':
exec_put(sys.argv)
case '-E' | '--empty':
exec_empty(sys.argv)
case '-RM' | '--remove':
exec_rm(sys.argv)
case '-RS' | '--restore':
exec_restore(sys.argv)
case '-RSA' | '--restore-all':
exec_restore_all()
case '-h' | '-H' | '-help' | '--help':
print(help_text)
case _:
print(colored(c_warn, "Argument not found. Use: trash -help"))
# Standard boilerplate to call the main() function to begin
# the program
if __name__ == '__main__':
main()