apt-mark 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/python
  2. from optparse import OptionParser
  3. try:
  4. import apt_pkg
  5. except ImportError:
  6. print "Error importing apt_pkg, is python-apt installed?"
  7. import sys
  8. import os.path
  9. actions = { "markauto" : 1,
  10. "unmarkauto": 0
  11. }
  12. if __name__ == "__main__":
  13. apt_pkg.init()
  14. # option parsing
  15. parser = OptionParser()
  16. parser.usage = "%prog [options] {markauto|unmarkauto} packages..."
  17. parser.add_option("-f", "--file", action="store", type="string",
  18. dest="filename",
  19. help="read/write a different file")
  20. parser.add_option("-v", "--verbose",
  21. action="store_true", dest="verbose", default=False,
  22. help="print verbose status messages to stdout")
  23. (options, args) = parser.parse_args()
  24. if len(args) < 2:
  25. parser.error("not enough argument")
  26. # get pkgs to change
  27. if args[0] not in actions.keys():
  28. parser.error("first argument must be 'markauto' or 'unmarkauto'")
  29. pkgs = args[1:]
  30. action = actions[args[0]]
  31. # get the state-file
  32. if not options.filename:
  33. STATE_FILE = apt_pkg.Config.FindDir("Dir::State") + "extended_states"
  34. else:
  35. STATE_FILE=options.filename
  36. # open the statefile
  37. if os.path.exists(STATE_FILE):
  38. tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
  39. outfile = open(STATE_FILE+".tmp","w")
  40. while tagfile.Step():
  41. pkgname = tagfile.Section.get("Package")
  42. autoInst = tagfile.Section.get("Auto-Installed")
  43. if pkgname in pkgs:
  44. if options.verbose:
  45. print "changing %s to %s" % (pkgname,action)
  46. newsec = apt_pkg.RewriteSection(tagfile.Section,
  47. [],
  48. [ ("Auto-Installed",str(action)) ]
  49. )
  50. outfile.write(newsec+"\n")
  51. else:
  52. outfile.write(str(tagfile.Section)+"\n")
  53. # all done, rename the tmpfile
  54. os.chmod(outfile.name, 0644)
  55. os.rename(outfile.name, STATE_FILE)
  56. os.chmod(STATE_FILE, 0644)