apt-mark 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/python
  2. from optparse import OptionParser
  3. import sys
  4. import os.path
  5. try:
  6. import apt_pkg
  7. except ImportError:
  8. print "Error importing apt_pkg, is python-apt installed?"
  9. sys.exit(1)
  10. actions = { "markauto" : 1,
  11. "unmarkauto": 0
  12. }
  13. def show_automatic(filename):
  14. if not os.path.exists(STATE_FILE):
  15. return
  16. auto = set()
  17. tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
  18. while tagfile.Step():
  19. pkgname = tagfile.Section.get("Package")
  20. autoInst = tagfile.Section.get("Auto-Installed")
  21. if int(autoInst):
  22. auto.add(pkgname)
  23. print "\n".join(sorted(auto))
  24. def mark_unmark_automatic(filename, action, pkgs):
  25. " mark or unmark automatic flag"
  26. # open the statefile
  27. if os.path.exists(STATE_FILE):
  28. try:
  29. tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
  30. outfile = open(STATE_FILE+".tmp","w")
  31. except IOError, msg:
  32. print "%s, are you root?" % (msg)
  33. sys.exit(1)
  34. while tagfile.Step():
  35. pkgname = tagfile.Section.get("Package")
  36. autoInst = tagfile.Section.get("Auto-Installed")
  37. if pkgname in pkgs:
  38. if options.verbose:
  39. print "changing %s to %s" % (pkgname,action)
  40. newsec = apt_pkg.RewriteSection(tagfile.Section,
  41. [],
  42. [ ("Auto-Installed",str(action)) ])
  43. pkgs.remove(pkgname)
  44. outfile.write(newsec+"\n")
  45. else:
  46. outfile.write(str(tagfile.Section)+"\n")
  47. if action == 1:
  48. for pkgname in pkgs:
  49. if options.verbose:
  50. print "changing %s to %s" % (pkgname,action)
  51. outfile.write("Package: %s\nAuto-Installed: %d\n\n" % (pkgname, action))
  52. # all done, rename the tmpfile
  53. os.chmod(outfile.name, 0644)
  54. os.rename(outfile.name, STATE_FILE)
  55. os.chmod(STATE_FILE, 0644)
  56. if __name__ == "__main__":
  57. apt_pkg.init()
  58. # option parsing
  59. parser = OptionParser()
  60. parser.usage = "%prog [options] {markauto|unmarkauto} packages..."
  61. parser.add_option("-f", "--file", action="store", type="string",
  62. dest="filename",
  63. help="read/write a different file")
  64. parser.add_option("-v", "--verbose",
  65. action="store_true", dest="verbose", default=False,
  66. help="print verbose status messages to stdout")
  67. (options, args) = parser.parse_args()
  68. # get the state-file
  69. if not options.filename:
  70. STATE_FILE = apt_pkg.Config.FindDir("Dir::State") + "extended_states"
  71. else:
  72. STATE_FILE=options.filename
  73. if args[0] == "showauto":
  74. show_automatic(STATE_FILE)
  75. else:
  76. # get pkgs to change
  77. if args[0] not in actions.keys():
  78. parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
  79. pkgs = args[1:]
  80. action = actions[args[0]]
  81. mark_unmark_automatic(STATE_FILE, action, pkgs)