Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- import re
- join = os.path.join
- re_cfgfile = re.compile("^\._cfg[0-9]+_[^\s]+$")
- def slicefn(fn):
- """Strip the leading ._cfg0000_ from the configuration file"""
- # Slice is good too ... until something unexpected happens.
- return fn[10:]
- def scanetc(filepath="/etc"):
- """Recurse down /etc to find configuration files collided during a
- package update"""
- cfgfiles = []
- filelist = os.listdir(filepath)
- for file in filelist:
- absolute = join(filepath,file)
- if os.path.isdir(absolute):
- cfgfiles += scanetc(absolute)
- elif os.path.isfile(absolute):
- if re_cfgfile.match(file):
- cfgfiles.append((filepath,file,slicefn(file)))
- return cfgfiles
- def askreplace(filelist):
- """Takes a list of configuration files then prompts the user on what
- action to take for each (Replace all, backup then replace or manual)."""
- for file in filelist:
- print " %s -> %s" % (join(file[0],file[1]),
- join(file[0],file[2]))
- while True:
- msg = "(A) Replace All, (B) Replace & Backup, (M) Manual, (C) Cancel: "
- c = raw_input(" " + msg)
- c = c.lower().strip()
- if c in ['a','b','m','c']:
- break
- if c == 'c':
- return False
- elif c in ['a','b']:
- for file in filelist:
- orig = join(file[0],file[2])
- #print "Original file:",orig
- new = join(file[0],file[1])
- #print "New file:",new
- if c == 'b':
- #print "Selected backup & replace"
- backup = join(file[0],"._bak_"+file[2])
- #print "Backup location:", backup
- os.rename(orig,backup)
- os.rename(new,orig)
- return True
- elif c == 'm':
- print "\n>>> Manual mode selected:"
- msg="(R) Replace, (B) Replace & Backup, (S) Skip, (C) Cancel: "
- for file in filelist:
- print " %s -> %s" % (join(file[0],file[1]),
- join(file[0],file[2]))
- while True:
- c = raw_input(" " + msg)
- c = c.lower().strip()
- if c in ['r','b','s','c']:
- break
- if c == 'c':
- return False
- elif c == 's':
- continue
- orig = join(file[0],file[2])
- #print "Original file:",orig
- new = join(file[0],file[1])
- #print "New file:",new
- if c == 'b':
- #print "Selected Backup & Replace"
- backup = join(file[0],"._bak_"+file[2])
- #print "Backup file:",backup
- os.rename(orig,backup)
- os.rename(new,orig)
- return True
- return False
- if __name__ == "__main__":
- print ">>> Scanning /etc for configuration file debris...",
- filelist = scanetc()
- print "Done."
- if not len(filelist):
- print ">>> No crashes found."
- import sys
- sys.exit(0)
- print ">>> Found %d crashes." % len(filelist)
- niceexit = askreplace(filelist)
- if niceexit:
- print ">>> All done."
- else:
- print "!!! Interupted."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement