Advertisement
Guest User

lazzareth

a guest
Feb 2nd, 2009
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import re
  5.  
  6. join = os.path.join
  7. re_cfgfile = re.compile("^\._cfg[0-9]+_[^\s]+$")
  8.  
  9. def slicefn(fn):
  10.     """Strip the leading ._cfg0000_ from the configuration file"""
  11.     # Slice is good too ... until something unexpected happens.
  12.     return fn[10:]
  13.  
  14.  
  15. def scanetc(filepath="/etc"):
  16.     """Recurse down /etc to find configuration files collided during a
  17.     package update"""
  18.  
  19.     cfgfiles = []
  20.     filelist = os.listdir(filepath)
  21.    
  22.     for file in filelist:
  23.         absolute = join(filepath,file)
  24.         if os.path.isdir(absolute):
  25.             cfgfiles += scanetc(absolute)
  26.         elif os.path.isfile(absolute):
  27.             if re_cfgfile.match(file):
  28.                 cfgfiles.append((filepath,file,slicefn(file)))
  29.    
  30.     return cfgfiles
  31.  
  32. def askreplace(filelist):
  33.     """Takes a list of configuration files then prompts the user on what
  34.     action to take for each (Replace all, backup then replace or manual)."""
  35.  
  36.     for file in filelist:
  37.         print "    %s -> %s" % (join(file[0],file[1]),
  38.             join(file[0],file[2]))
  39.    
  40.     while True:
  41.         msg = "(A) Replace All, (B) Replace & Backup, (M) Manual, (C) Cancel: "
  42.         c = raw_input("    " + msg)
  43.         c = c.lower().strip()
  44.         if c in ['a','b','m','c']:
  45.             break
  46.    
  47.     if c == 'c':
  48.         return False
  49.  
  50.     elif c in ['a','b']:
  51.         for file in filelist:
  52.             orig = join(file[0],file[2])
  53.             #print "Original file:",orig
  54.             new = join(file[0],file[1])
  55.             #print "New file:",new
  56.             if c == 'b':
  57.                 #print "Selected backup & replace"
  58.                 backup = join(file[0],"._bak_"+file[2])
  59.                 #print "Backup location:", backup
  60.                 os.rename(orig,backup)
  61.  
  62.             os.rename(new,orig)
  63.  
  64.         return True
  65.    
  66.     elif c == 'm':
  67.         print "\n>>> Manual mode selected:"
  68.         msg="(R) Replace, (B) Replace & Backup, (S) Skip, (C) Cancel: "
  69.         for file in filelist:
  70.             print "    %s -> %s" % (join(file[0],file[1]),
  71.                 join(file[0],file[2]))
  72.            
  73.             while True:
  74.                 c = raw_input("    " + msg)
  75.                 c = c.lower().strip()
  76.                 if c in ['r','b','s','c']:
  77.                     break
  78.  
  79.             if c == 'c':
  80.                 return False
  81.            
  82.             elif c == 's':
  83.                 continue
  84.            
  85.             orig = join(file[0],file[2])
  86.             #print "Original file:",orig
  87.             new = join(file[0],file[1])
  88.             #print "New file:",new
  89.             if c == 'b':
  90.                 #print "Selected Backup & Replace"
  91.                 backup = join(file[0],"._bak_"+file[2])
  92.                 #print "Backup file:",backup
  93.                 os.rename(orig,backup)
  94.  
  95.             os.rename(new,orig)
  96.         return True
  97.  
  98.     return False
  99.  
  100. if __name__ == "__main__":
  101.  
  102.     print ">>> Scanning /etc for configuration file debris...",
  103.     filelist = scanetc()
  104.     print "Done."
  105.     if not len(filelist):
  106.         print ">>> No crashes found."
  107.         import sys
  108.         sys.exit(0)
  109.     print ">>> Found %d crashes." % len(filelist)
  110.    
  111.     niceexit = askreplace(filelist)
  112.     if niceexit:
  113.         print ">>> All done."
  114.     else:
  115.         print "!!! Interupted."
  116.    
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement