rm-blacklist

Python wrapper for POSIX rm that checks every argument against a blacklist.
git clone git://git.stephanospavlou.info/rm-blacklist.git
Log | Files | Refs | README | LICENSE

rm-blacklist.py (995B)


      1 #!/usr/bin/env python
      2 import os
      3 import sys
      4 
      5 blacklistPath = '.blklst' # SET ME!
      6 
      7 options = []
      8 filesToldToDelete = []
      9 for arg in sys.argv[1:]:
     10     if arg[0] == '-':
     11         options.append(arg)
     12     else:
     13         filesToldToDelete.append(arg)
     14 
     15 filesSafeToDelete = filesToldToDelete
     16 with open(blacklistPath, 'r') as blacklist:
     17     for line in blacklist.readlines():
     18         if line == '' or line[0] == '#':
     19             continue
     20         else:
     21             for file in filesToldToDelete:
     22                 fileAbsPath = os.path.abspath(file)
     23                 if fileAbsPath in line:
     24                     if os.path.isdir(fileAbsPath):
     25                         print('rm-blacklist: either' + file + ' or its contents'
     26                             + ' are blacklisted')
     27                     else:
     28                         print('rm-blacklist: ' + file + ' is blacklisted')
     29 
     30 
     31 if len(filesSafeToDelete) == 0:
     32     sys.exit()
     33 
     34 safeRm = 'rm ' + ' '.join(options) + ' ' + ' '.join(filesSafeToDelete)
     35 os.system(safeRm)