-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrototiller.py
885 lines (752 loc) · 31.7 KB
/
rototiller.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
import os
import shutil
import psutil
from pathlib import Path
import subprocess
import random
import string
import asyncio
from datetime import datetime
import time
import yaml
from sshkeyboard import listen_keyboard, stop_listening
import click
plots = []
#plot_size = 70000000000 # byte
plot_size = 42000000 # byte
is_running = False
max_concurrent = 1
bwlimit = 100000
configPath = 'config.yaml'
# global to keep track of all the plots moved, so i can check if i placed already in the queue.
# we can eliminate by using some os call when new files arrive
plots_archive = []
# unit
space_unit_size = {'Mb': 1024**2, 'Gb': 1024**3, 'Tb': 1024**4}
sel_unit = 'Gb'
unit = {'size' : sel_unit, 'factor' : space_unit_size[sel_unit]}
info_move_active = []
info_delete_active = []
# Load logging file
loggingPath = 'debugRoto.log'
# i should check that dest suffix is not in obsolete dir to avoid problems
#plots_dir.append("/mnt/intelRock/")
#plots_dir.append("/home/boon/plotting_folder01/")
#plots_dir.append("/home/boon/plotting_folder02")
#destination_dir.append("/mnt/dinothrfarm/na62_hgst01")
#destination_dir.append("/mnt/dinothrfarm/na62_hgst02")
#destination_dir.append("/mnt/sm487_01")
#destination_dir.append("/mnt/sm487_02/")
#destination_dir.append("/mnt/sm487_03")
#
#
#@click.command()
#@click.option('--sources',
# prompt='plotting directories',
# help='the list of plotting directories or a path to a file.')
#@click.option('--destinations',
# prompt='destination directories',
# help='the list of the final directories for plots or a path to a file.')
#
#def printCmd(sources, destinations):
# click.echo(f"I am running with plotting source: {sources} and destination: {destinations}")
class ConfigParam:
def __init__(self):
self.is_running = False
self.plot_size = 0
self.plots_dir = []
self.destination_dir = []
self.dest_suffix = ''
self.obsolete_dir = []
self.max_concurrent = 1
self.bwlimit = 0
def loadYamlConfig(configPath, config):
"""Load YAML data. """
with open(configPath, 'r') as file:
data = yaml.safe_load(file)
config.is_running = data['running']
config.plot_size = data['plot_size']
config.plots_dir = data['plot_directories']
config.destination_dir =data['destination_directories']
config.dest_suffix = data['destination_suffix']
config.obsolete_dir = data['obsolete_folder']
config.max_concurrent = data['max_concurrent']
config.bwlimit = data['rsync_bw_limit']
config.debugging = data['debugging']
log_lock = asyncio.Lock()
async def logging(*args):
timestamp = datetime.now().isoformat(timespec='seconds')
str_message = timestamp + ' '
for arg in args:
str_message += str(arg)
str_message += '\n'
async with log_lock:
with open(loggingPath, 'a') as logFile:
logFile.write(str_message)
def findPlots(sources):
for source in sources:
for plot in Path(source).glob("*.plot"):
plots.append(plot)
def movePlot(plot, destination):
# add delete option
cmd = ['rsync', '-av', '--bwlimit=1000', '--remove-source-files', plot, destination]
subprocess.run(cmd)
def freeSpace(path):
return shutil.disk_usage(path).free
def totalSpace(path):
return shutil.disk_usage(path).total
# find folder space
async def get_dir_size(path):
total = 0
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += await get_dir_size(entry.path)
return total
async def get_dir_size_noSub(path):
total = 0
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
return total
async def findPlotsA(sources, plots_queue):
for source in sources:
for plot in Path(source).glob("*.plot"):
if plot in plots_archive:
await logging(plot)
continue
await plots_queue.put(plot)
plots_archive.append(plot)
class MountingPoint():
def __init__(self):
self.path = ''
self.name = ''
self.size = 0
self.freeSpace = 0 # byte i think
self.occupiedSpace = 0
self.directories = []
self.nPlots = 0
self.nFiles = 0
# self.ndirs = 0 get the dim of directories
class MountingPointDir():
def __init__(self):
self.path = ''
self.name = 'none'
self.size = 0
self.nPlots = 0
self.nFiles = 0
# TODO move on yaml
ignoreFolders = ['lost+found','.Trash-1000']
async def get_dir_name_size_nFiles(path, root, mountingPointDir_list):
"""get size and number of plots per directories. Path: name of the root dir;
root: if it is the root; mountingPointDir_list: ref of a list of the subdirectories"""
path = Path(path)
print(path)
mpd = MountingPointDir()
mpd.path = path
mpd.name = 'root' if root else path.name
total = 0
nFiles = 0
nPlots = 0
for entry in path.glob('*'):
print(entry, " entry")
if entry.is_file():
print("entry is a file")
total += entry.stat().st_size
nFiles += 1
if entry.suffix == '.plot':
nPlots += 1
elif entry.is_dir() and not entry.name in ignoreFolders:
await get_dir_name_size_nFiles(entry, False, mountingPointDir_list)
mpd.size = total
mpd.nFiles = nFiles
mpd.nPlots = nPlots
mountingPointDir_list.append(mpd)
def sumPlotsAndFiles(mountingPointDir_list):
nPlots = 0
nFiles = 0
for mpd in mountingPointDir_list:
nPlots += mpd.nPlots
nFiles += mpd.nFiles
return nPlots, nFiles
async def analyzeMountingPoint(paths):
mountingPoints = []
for path in paths:
mp = MountingPoint()
mp.path = Path(path) # consider to do this when importing the yaml
mp.name = mp.path.name
mp.size = totalSpace(mp.path)
mp.freeSpace = freeSpace(mp.path)
# add subs
mountingPointDir_list = []
await get_dir_name_size_nFiles(path, True, mountingPointDir_list)
mp.directories = mountingPointDir_list
mp.nPlots, mp.nFiles = sumPlotsAndFiles(mp.directories)
mountingPoints.append(mp)
return mountingPoints
async def evaluateDestinations(destinations, plot_size, dest_queue, obsolete_queue, obsolete_folders=None):
# shuld i add the ability to rescan ?
for path in destinations:
path = Path(path)
if freeSpace(path) < plot_size:
if obsolete_folders:
obsolete_folders_number = len(obsolete_folders)
for folder in obsolete_folders:
path_folder = path.joinpath(folder)
await logging(path_folder)
if path_folder.exists():
size = await get_dir_size_noSub(path_folder)
if size != 0:
await obsolete_queue.put(path)
await logging(f"The disk: {path} is full, but it can be emptied of {size / unit['factor']} {unit['size']}")
break
else:
obsolete_folders_number -= 1
await logging(f"The disk: {path} has no subfolder {folder}")
if obsolete_folders_number == 0:
await logging(f"The disk: {path} is full.")
else:
await logging(f"The disk: {path} is full.")
else:
await dest_queue.put(path)
async def replantPlots(dest_queue, dest_suffix, obsolete_queue, plots_queue, bwlimit):
# IF there is free space, lets fill first
await logging("replant starting...")
error = "EE_"
try:
if not dest_queue.empty():
dest_root = await dest_queue.get()
#if not dest_root.is_mount():
# await logging(f"The {dest_root} destination is not mounted, the destination will be dropped")
# return 0
if False:
await logging("not possible I am printed")
else:
plot = await plots_queue.get()
plot_size = plot.stat().st_size
await logging(f'plot size {plot_size}')
dest = dest_root.joinpath(dest_suffix)
error = error + str(dest) + str(plot)
if not dest.exists():
dest.mkdir(exist_ok=False)
if freeSpace(dest) > plot_size:
# move
await logging(f"replant: moving to this destination: {dest}")
await movePlotA(plot, dest, bwlimit)
await logging(f"plot moved to {dest}")
await dest_queue.put(dest_root)
else:
# here something should happen? like it exit from the queue, and then
# it will be pick up again if it is eligible to deleto old plots?
await plots_queue.put(plot)
await obsolete_queue.put(dest_root)
await logging(f"The {dest_root} destination is full, the destination will be dropped,\
if there are old plots that can be canceled it will be pick up again.")
except Exception as e:
await logging(f"something wrong with the replant: {e}")
await plots_queue.put(plot)
await dest_queue.put(dest_root)
with open('error_log', 'a') as f:
f.write(datetime.now().isoformat(timespec='seconds'))
f.write(error)
f.write("error")
f.write(e)
f.write("END")
f.close()
except KeyboardInterrupt:
await logging("who stoppped my replant pressing a key?")
await plots_queue.put(plot)
await dest_queue.put(dest_root)
pass
# If the dest queue has less then the limit moves, lets empty the
# number of drives that we move plus 2 drives, put the drives in the dest_queue
#
return 0
async def movePlotA(plot, destination, bwlimit):
"""Move a file from source to destination directory."""
cmd = f"ionice -c 3 rsync -v --preallocate --whole-file --bwlimit={bwlimit} --remove-source-files {plot} {destination}"
#cmd = f"mv -v {plot} {destination}"
await logging(f"MOVING {plot}")
await logging(f"to {destination}")
global info_move_active
try:
info_move_active.append(cmd)
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
start = datetime.now()
stdout, stderr = await proc.communicate()
finish = datetime.now()
info_move_active.remove(cmd)
if proc.returncode != 0:
await logging(f"ERROR CODE {proc.returncode}")
with open(cmd, "w") as f:
f.write(cmd)
f.write("error")
f.write(f"ERROR CODE {proc.returncode}")
f.write("END")
f.close()
if proc.returncode == 0:
await logging(f"Finished the move to {destination} in {finish - start} seconds.")
except Exception as e:
await logging(f'ERROR {e} in movePlotA')
with open(cmd, "w") as f:
f.write(cmd)
f.write("error")
f.write(e)
f.write("END")
f.close()
async def deleteObsoletePlots(obsolete_queue, obsolete_folders, dest_queue, max_concurrent):
await logging("inside the obsolete oblivion")
while dest_queue.qsize() < max_concurrent or obsolete_queue.empty() != False:
await logging("inside the while delete")
path = Path(await obsolete_queue.get())
for folder in obsolete_folders:
path_folder = path.joinpath(folder)
if path_folder.exists():
await logging(f'the plot folder is {path_folder}')
with os.scandir(path_folder) as files:
count = 0
for entry in files:
if entry.is_file() and Path(entry.name).suffix == '.plot':
await logging("inside")
await logging(entry.name)
try:
await logging("plot to delete is:")
await logging(entry)
file_path = entry.path
await logging(file_path)
await logging("DELETING")
info_delete_active.append(file_path)
await asyncio.sleep(0) # Allow other tasks to run
os.remove(file_path)
await logging("file deleted")
except Exception as e:
await logging(f'an error happend deleting {file_path}')
await logging(f'the exception is: {e}')
finally:
await asyncio.sleep(5)
await logging("obsolete oblivion awaited")
info_delete_active.remove(file_path)
count += 1
if freeSpace(path) > (2 * plot_size):
await logging("breaked the oblivion")
break
if count != 0:
await logging('added a new disk into the destination queue')
await dest_queue.put(path)
async def updateConfig(configPath, config):
loadYamlConfig(configPath, config)
class UiState:
def __init__(self):
self.faceController = 'none'
# it is an hack to make it considering other input other then the key
#def on_press(key, uiState):
async def on_press(key):
print(uiState.faceController, " from the inside")
print(key)
print("printed key char")
global ui_var
try:
if key == 'm':
uiState.faceController = 'moving'
print("set ui to moving")
if key == 'a':
uiState.faceController = 'analytics'
print("set ui to analytics")
if key == 'v':
uiState.faceController = 'variables'
print("set ui to variables")
if uiState.faceController == "analytics":
if key == 'j':
ui_var += 8
if key == 'k':
ui_var -= 8
#if uiState.faceController == "variables":
# if key == 'q':
# stop_listening()
# print("change the q")
# newq = input("Entere new concurent plots transfer valure: ")
# print("new input is:", newq)
# listen_keyboardd(on_press=on_press)
# if key == 'w':
# newq = input("Entere new banwith limit for plot transfer: ")
# print("new input is:", newq)
# print("change the bandwith")
except:
print("no valid Key")
def sizeToTb(size):
return round(size / (1024**4), 2)
async def main(config):
# get the loop
loop = asyncio.get_running_loop()
# global (it is not necessary)
global info_move_active
# init the queue
dest_queue = asyncio.Queue()
obsolete_queue = asyncio.Queue()
plots_queue = asyncio.Queue()
await evaluateDestinations(config.destination_dir, config.plot_size, dest_queue, obsolete_queue, config.obsolete_dir)
await findPlotsA(config.plots_dir, plots_queue)
await logging(f'Dest queue {dest_queue}')
await logging("")
await logging(f'obsolete queue {obsolete_queue}')
await logging("")
await logging("plots archive")
await logging(plots_archive)
mountingPointsStats = await analyzeMountingPoint(config.destination_dir)
statsCount = 0
terminal_size = os.get_terminal_size()
start_time = time.time()
tasks = []
d_tasks = []
while config.is_running:
await logging("checking update config")
await updateConfig(configPath, config)
# update stats
if statsCount == 1000:
statsCount = 0
mountingPointsStats = await analyzeMountingPoint(config.destination_dir)
else:
statsCount += 1
# DEBUGGING: clear the terminal
# create a config variable for debugging the interface? so there is only
# one place to change
if not config.debugging:
os.system('cls' if os.name == 'nt' else 'clear')
# interface
print("the face controller is on: ", uiState.faceController, end="")
print("Screens: 'a' analytics, 'm' plot transfers, 'v' variables", end="")
print(" Thats all for now!")
if uiState.faceController == "moving":
print()
print(f'Dest queue: {dest_queue.qsize()}')
print(f'Obsolete queue: {obsolete_queue.qsize()}')
print(f'Plots queue: {plots_queue.qsize()}')
print()
print(f'Number of full disks with old plots: {obsolete_queue.qsize()}')
print()
print(f"Terminal width: {terminal_size.columns} characters")
print(f"Terminal height: {terminal_size.lines} lines")
print()
print(f'Time elapsed: {time.time() - start_time}')
print("Active moving")
for i in info_move_active:
print(i)
print("Active deleting")
for i in info_delete_active:
print(i)
start_time = time.time()
print()
print(f'is running: {is_running}')
elif uiState.faceController == "analytics":
print("Analytics, use j and k to navigate through the mounting point")
print()
ndir = len(mountingPointsStats)
print(ui_var, "uivar")
startRange = ui_var % ndir
# 8 is the number of disks dispalyed
endRange = ((startRange + 8) % ndir) if 8 < ndir else ndir
for hdd in mountingPointsStats[startRange:endRange]:
print("mount point: ", end='')
print(hdd.path, end='; ')
print("hdd size: ", round(hdd.size / (1024**4), 2), 'TB ', end='')
print("free space: ", round(hdd.freeSpace / (1024**4), 2), 'TB ', end='')
print("n dirs: ", len(hdd.directories), "; ", "number of plots; ", hdd.nPlots, "; ", end='')
print("number of files: ", hdd.nFiles)
for entry in hdd.directories:
print(" ", entry.name, end="\t")
print("size: ", sizeToTb(entry.size), " TB; ", end=" ")
print("n. pltos: ", entry.nPlots, "; ", end=" ")
print("n. files: ", entry.nFiles)
print()
# total sum for folder
directories = {}
for hdd in mountingPointsStats:
for entry in hdd.directories:
if entry.name in directories:
directories[entry.name] += entry.size
else:
directories[entry.name] = entry.size
print("the total")
for key in directories:
print(key, " ", sizeToTb(directories[key]))
elif uiState.faceController == "variables":
print("variables")
print(f"concurrent plots: {config.max_concurrent}. To change it press q.")
print(f"band limit: {config.bwlimit} bytes. To change it press w.")
else:
print("press m to watch moving plot, a for disks analitics")
print("_________real time logs_________")
# delete plots
if obsolete_queue.qsize() != 0 and dest_queue.qsize() <= config.max_concurrent:
try:
# should i check here also the number of free disk?
task = asyncio.create_task(deleteObsoletePlots(obsolete_queue, config.obsolete_dir,
dest_queue, config.max_concurrent))
d_tasks.append(task)
# Remove completed tasks
for task in d_tasks:
if task.done():
d_tasks.remove(task)
except Exception as e:
await logging(f"something WRONG by creating free space: {e}")
except KeyboardInterrupt:
await logging("who enlighed my oblivion?")
finally:
await logging("new free space here... maybe")
# check for new plots
if plots_queue.qsize() < config.max_concurrent:
await logging("checking for new plots")
await logging("logging in the main")
await findPlotsA(config.plots_dir, plots_queue)
else:
await logging("plots queue is greater then max_concurrent")
await logging(plots_queue.qsize(), " queue size")
await logging(config.max_concurrent, "max concurrent")
# check if we capped
if len(tasks) == config.max_concurrent or dest_queue.empty() or plots_queue.empty():
await logging("lest rest hoping something will happen")
await logging(f'plots queue: size {plots_queue.qsize()} and emptyness {plots_queue.empty()}')
# Remove completed tasks
for task in tasks:
if task.done():
tasks.remove(task)
await asyncio.sleep(10)
continue
try:
await logging(f"Tasks running: {len(tasks)} of {config.max_concurrent}")
task = asyncio.create_task(replantPlots(dest_queue, config.dest_suffix, obsolete_queue, plots_queue, config.bwlimit))
tasks.append(task)
# Remove completed tasks
for task in tasks:
if task.done():
tasks.remove(task)
# Sleep for 1 second before checking for new files to move
await asyncio.sleep(1)
except Exception as e:
await logging(f"something wrong in the main loop: {e}")
except KeyboardInterrupt:
await logging("who stoppped my replant?")
finally:
await logging("main loop done, take some time")
await asyncio.sleep(1)
await asyncio.sleep(1)
await logging("ASPETTO FINE TASK")
# stop sshkeyboard
stop_listening()
# Wait for the tasks to complete
await asyncio.gather(*tasks)
await asyncio.gather(*d_tasks)
await logging("esco dal giro")
# Close the loop waiting for all the tasks
loop.stop()
await logging("uscito dal giro")
def createFakePlots():
plotting_dir = [Path('/home/boon/plotting_folder01'), Path('/home/boon/plotting_folder02/')]
src_path = Path('/home/boon/plotting_folder01/THEPLOT')
prefix = 'plot_k32_'
postfix = '.plot'
# create plots
for dir_p in plotting_dir:
for i in range(100):
random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=40))
filename = ''.join([prefix,random_string,postfix])
dest_path = dir_p.joinpath(filename)
print(filename)
print(dest_path)
print(src_path)
print()
shutil.copy(src_path, dest_path)
print(prefix, random_string, postfix)
def checkSourceAndDestination():
# check that or the paths or the files with tha path are given at startup
return 0
if __name__ == '__main__':
#createFakePlots()
#exit()
# thread for UI
uiState = UiState()
# import
import threading
#ui variables
ui_var = 0
# listen to the keyboard
ui_thread = threading.Thread(target=listen_keyboard, args=(on_press,), daemon=True)
ui_thread.start()
# initialize the conf
config = ConfigParam()
loadYamlConfig(configPath, config)
print(config.is_running)
print(config.plots_dir)
print(config.plot_size)
print(config.destination_dir)
print(config.dest_suffix)
sources = config.plots_dir
destinations = config.destination_dir
obsolete_folders = config.obsolete_dir
is_running = config.is_running
#asyncio.run(main(sources, destinations, dest_suffix, max_concurrent, obsolete_folders, is_running))
#asyncio.run(main(plots_dir, destination_dir, dest_suffix, max_concurrent, obsolete_dir, is_running))
asyncio.run(main(config))
print("totonno")
print("totonno")
print("totonno")
print("totonno")
print("totonno")
exit()
findPlots(sources)
## create plots
print(plots)
#for plot in plots:
# movePlot(plot, destination_dir[0])
printCmd()
printCmd(sources, destinations)
printCmd()
exit()
###################################### async code ########################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
import asyncio
import os
import shutil
from pathlib import Path
async def move_file(source_file: Path, destination_dir: Path) -> None:
"""Move a file from source to destination directory."""
await asyncio.sleep(1) # Simulate some work
shutil.move(str(source_file), str(destination_dir))
async def move_files(source_dir: Path, destination_dirs: list[Path]) -> None:
"""Move files from source directory to destination directories."""
# Create a queue to hold the paths of the files to move
file_queue = asyncio.Queue()
# Start a task to monitor the source directory for new files to move
async def monitor_dir():
while True:
# Get a list of files in the source directory
files = list(source_dir.glob('*'))
# Add any new files to the queue
for file in files:
if file not in file_queue._queue:
await file_queue.put(file)
# Sleep for 1 second before checking for new files again
await asyncio.sleep(1)
asyncio.create_task(monitor_dir())
# Start tasks to move files from the queue to the destination directories
tasks = []
while True:
# Limit the number of files being moved simultaneously to 5
while len(tasks) < 5 and not file_queue.empty():
source_file = await file_queue.get()
dest_dir = destination_dirs[file_queue.qsize() % len(destination_dirs)]
task = asyncio.create_task(move_file(source_file, dest_dir))
tasks.append(task)
# Remove completed tasks
for task in tasks:
if task.done():
tasks.remove(task)
# Sleep for 1 second before checking for new files to move
await asyncio.sleep(1)
if __name__ == '__main__':
source_dir = Path('/path/to/source/dir')
dest_dirs = [Path('/path/to/dest/dir1'), Path('/path/to/dest/dir2')]
asyncio.run(move_files(source_dir, dest_dirs))
########################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
import asyncio
import os
import shutil
async def move_file(src, dest):
print(f"Moving {src} to {dest}...")
await asyncio.sleep(1) # simulate time it takes to move file
shutil.move(src, dest)
async def move_files(source_dir, dest_dir, limit):
file_queue = asyncio.Queue()
# Populate the initial file queue with existing files in the source directory
for filename in os.listdir(source_dir):
src = os.path.join(source_dir, filename)
dest = os.path.join(dest_dir, filename)
file_queue.put_nowait((src, dest))
while True:
# Check if the limit has been reached
while len(asyncio.all_tasks()) - 1 >= limit:
await asyncio.sleep(1)
# Scan the source directory for new files and add them to the queue
for filename in os.listdir(source_dir):
src = os.path.join(source_dir, filename)
dest = os.path.join(dest_dir, filename)
if not file_queue._queue.count((src, dest)):
file_queue.put_nowait((src, dest))
# Move the next file from the queue
if not file_queue.empty():
src, dest = await file_queue.get()
await move_file(src, dest)
else:
await asyncio.sleep(1) # wait a bit before checking the queue again
async def main():
source_dir = "/path/to/source/dir"
dest_dir = "/path/to/dest/dir"
limit = 5
await move_files(source_dir, dest_dir, limit)
if __name__ == "__main__":
asyncio.run(main())
########################################################################333
print('the size of / is')
size_root = get_dir_size('/home/')
print(f'root: {size_root / space_unit_size[unit]}GB')
#help(shutil)
#help(psutil)
diskUsage = psutil.disk_usage('/')
exit()
print(diskUsage.percent)
print(f'La percentuale è: {diskUsage.percent}%')
print(f'Total: {diskUsage.total / (1024 ** 3):.2f} GB')
print(f'Used: {diskUsage.used / (1024 ** 3):.2f} GB')
print(f'Free: {diskUsage.free / (1024 ** 3):.2f} GB')
# Get all mounted partitions
partitions = psutil.disk_partitions(all=True)
# Get disk I/O counters for all disks
disk_io_counters = psutil.disk_io_counters(perdisk=True)
# Loop through each partition and get the device information
for partition in partitions:
if partition.fstype:
usage = psutil.disk_usage(partition.mountpoint)
device = partition.device
try:
for disk, io_counters in disk_io_counters.items():
print(disk)
print(device)
print()
device = device.split('/')
if disk == device[-1]:
help(io_counters)
model = io_counters.serial_number
print(f"Model of {device}: {model}")
break
except AttributeError:
print(f"Model of {device}: Unknown")