Advertisement
GoodiesHQ

SimpleSecureDelete.py

Jun 27th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import threading
  2. import time
  3. import os
  4. from random import SystemRandom
  5. from argparse import ArgumentParser
  6.  
  7.  
  8. class ProgressSpin(threading.Thread):
  9.     def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=False, sleep=0.5, maximum=100.0):
  10.         super().__init__(group, target, name, args, kwargs, daemon=daemon)
  11.         self.sleep = sleep
  12.         self.value = 0
  13.         self.maximum = maximum
  14.         self.done = False
  15.  
  16.     @property
  17.     def progress(self):
  18.         return 100.0 * self.value / self.maximum
  19.  
  20.     @progress.setter
  21.     def progress(self, percentage):
  22.         if percentage > 100.0 or percentage < 0.0:
  23.             raise ValueError("Progress percentage should be a value between 0.0 and 100.0")
  24.         self.value = int(percentage * self.maximum)
  25.  
  26.     def run(self):
  27.         print()
  28.         i = 1
  29.         while i:
  30.             chars = {0: "/", 1: "-", 2: "\\", 3: "|"}
  31.             print("\r{} {:03.02f}%".format(chars.get(i % 4), self.progress), end="")
  32.             time.sleep(self.sleep)
  33.             i += 1
  34.             if i and (self.done or self.progress == 100.0):
  35.                 i = -1
  36.  
  37.  
  38. class SecureDelete:
  39.     def __init__(self, *, bs=1024):
  40.         self.bs = bs
  41.         self.rdev = SystemRandom()
  42.  
  43.     def erase(self, filename, count, callback=None):
  44.         if not os.path.isfile(filename):
  45.             raise IOError("The file '{}' does not exist.".format(filename))
  46.         size = os.stat(filename).st_size
  47.         if not size:
  48.             return
  49.         chunks, leftover = size // self.bs, size % self.bs
  50.         cnt, total = 0, size * count
  51.         with open(filename, "wb") as fout:
  52.             for i in range(count):
  53.                 fout.seek(0)
  54.                 for n in range(chunks + 1):
  55.                     tmp = leftover if n == chunks else self.bs
  56.                     fout.write(os.urandom(tmp))
  57.                     cnt += tmp
  58.                     if callback:
  59.                         callback(cnt, total)
  60.  
  61. p = ProgressSpin(sleep=0.25)
  62. p.start()
  63.  
  64.  
  65. def cb(count, total):
  66.     if p.maximum != total:
  67.         p.maximum = total
  68.     p.value = count
  69.  
  70. if __name__ == "__main__":
  71.     ap = ArgumentParser()
  72.     ap.add_argument("-f", "--file", help="Location of the file to securely erase.", type=str, required=True)
  73.     ap.add_argument("-c", "--count", help="", type=int, default=7)
  74.     args = ap.parse_args()
  75.     sd = SecureDelete()
  76.     sd.erase(args.file, args.count, callback=cb)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement