apt-mark 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 >> sys.stderr, "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.TagFile(open(STATE_FILE))
  18. for section in tagfile:
  19. pkgname = section.get("Package")
  20. autoInst = 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.TagFile(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. for section in tagfile:
  35. pkgname = section.get("Package")
  36. autoInst = 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.rewrite_section(section,
  41. [],
  42. [ ("Auto-Installed",str(action)) ])
  43. pkgs.remove(pkgname)
  44. outfile.write(newsec+"\n")
  45. else:
  46. outfile.write(str(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.epilog = "apt-mark is deprecated, use apt-get markauto/unmarkauto."
  62. parser.add_option("-f", "--file", action="store", type="string",
  63. dest="filename",
  64. help="read/write a different file")
  65. parser.add_option("-v", "--verbose",
  66. action="store_true", dest="verbose", default=False,
  67. help="print verbose status messages to stdout")
  68. (options, args) = parser.parse_args()
  69. if not args:
  70. parser.print_help()
  71. sys.exit(1)
  72. # get the state-file
  73. if not options.filename:
  74. STATE_FILE = apt_pkg.config.find_dir("Dir::State") + "extended_states"
  75. else:
  76. STATE_FILE=options.filename
  77. if len(args) == 0:
  78. parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
  79. if args[0] == "showauto":
  80. show_automatic(STATE_FILE)
  81. else:
  82. # get pkgs to change
  83. if args[0] not in actions.keys():
  84. parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
  85. pkgs = args[1:]
  86. action = actions[args[0]]
  87. mark_unmark_automatic(STATE_FILE, action, pkgs)