Skip to content

Instantly share code, notes, and snippets.

@yucefsourani
Created May 15, 2018 12:35
Show Gist options
  • Save yucefsourani/fcd0b966b9707a4873eda2b390f40328 to your computer and use it in GitHub Desktop.
Save yucefsourani/fcd0b966b9707a4873eda2b390f40328 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Require python3-psutil
#
import os
import psutil
import time
import sys
all_command = ["cp"]
def get_pids(commands):
result = []
for command in commands:
pids = [p.info for p in psutil.process_iter(attrs=['pid', 'name']) if command==p.info['name']]
for pid in pids:
result.append(pid)
return result
def all_print_progress(pid,progresssize=100,end="\n",flush=False,timeout=0,clear=False):
progresssize = int(progresssize)
try:
process = psutil.Process(pid)
open_files = process.open_files()
except:
return False
read_file = {}
for of in open_files:
if of.mode=="r":
read_file.setdefault(os.path.basename(of.path),[os.stat(of.path).st_size,of.path])
if read_file:
try:
process = psutil.Process(pid)
open_files = process.open_files()
except:
return False
for of in open_files:
if of.mode=="w":
filepath = of.path
if not os.path.basename(filepath) in read_file.keys():
continue
nowsize = os.stat(filepath).st_size
filesize = read_file[os.path.basename(filepath)][0]
origfilepath = read_file[os.path.basename(filepath)][1]
position = of.position
count = int((position*progresssize)//filesize)+1
n = "#" * count
if end=="\n":
time.sleep(timeout)
if clear:
os.system("clear")
print("\n")
try:
pid = process.pid
name = process.name()
print("{} {} {} ----> {}".format(pid,name,origfilepath,filepath))
except:
print("{} ----> {} Done.".format(origfilepath,filepath))
print ("["+n+"-"*(progresssize-count)+"]"+" "+str(filesize)+"b"+" "+str(int((position*100)//filesize)+1)+"%\n",end=end,flush=flush)
else:
print ("["+n+"-"*(progresssize-count)+"]"+" "+str(filesize)+"b"+" "+str(int((position*100)//filesize)+1)+"%",end=end,flush=flush)
return True
else:
return False
return True
def main(commands):
all_pid = get_pids(commands)
while all_pid:
for pid in all_pid:
if not all_print_progress(pid["pid"],timeout=0.2):
all_pid.remove(pid)
if __name__ == "__main__":
main(["cp"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment