]> git.saurik.com Git - apt.git/blame - cmdline/apt-mark
* debian/apt.cron.daily:
[apt.git] / cmdline / apt-mark
CommitLineData
b1a8717a
MV
1#!/usr/bin/python
2
3from optparse import OptionParser
4
3f35be50
MV
5import sys
6import os.path
7
b1a8717a
MV
8try:
9 import apt_pkg
10except ImportError:
d63a1458 11 print >> sys.stderr, "Error importing apt_pkg, is python-apt installed?"
3f35be50 12 sys.exit(1)
b1a8717a
MV
13
14actions = { "markauto" : 1,
15 "unmarkauto": 0
16 }
17
a9b5e24b
MV
18def show_automatic(filename):
19 if not os.path.exists(STATE_FILE):
20 return
21 auto = set()
946b0e06
JAK
22 tagfile = apt_pkg.TagFile(open(STATE_FILE))
23 for section in tagfile:
24 pkgname = section.get("Package")
25 autoInst = section.get("Auto-Installed")
a9b5e24b
MV
26 if int(autoInst):
27 auto.add(pkgname)
28 print "\n".join(sorted(auto))
29
b1a8717a 30
a9b5e24b
MV
31def mark_unmark_automatic(filename, action, pkgs):
32 " mark or unmark automatic flag"
b1a8717a
MV
33 # open the statefile
34 if os.path.exists(STATE_FILE):
8b8fdb11 35 try:
946b0e06 36 tagfile = apt_pkg.TagFile(open(STATE_FILE))
8b8fdb11
MV
37 outfile = open(STATE_FILE+".tmp","w")
38 except IOError, msg:
39 print "%s, are you root?" % (msg)
40 sys.exit(1)
946b0e06
JAK
41 for section in tagfile:
42 pkgname = section.get("Package")
43 autoInst = section.get("Auto-Installed")
b1a8717a
MV
44 if pkgname in pkgs:
45 if options.verbose:
46 print "changing %s to %s" % (pkgname,action)
946b0e06 47 newsec = apt_pkg.rewrite_section(section,
b1a8717a 48 [],
503a68c6
MV
49 [ ("Auto-Installed",str(action)) ])
50 pkgs.remove(pkgname)
b1a8717a
MV
51 outfile.write(newsec+"\n")
52 else:
946b0e06 53 outfile.write(str(section)+"\n")
503a68c6
MV
54 if action == 1:
55 for pkgname in pkgs:
56 if options.verbose:
57 print "changing %s to %s" % (pkgname,action)
58 outfile.write("Package: %s\nAuto-Installed: %d\n\n" % (pkgname, action))
b1a8717a 59 # all done, rename the tmpfile
54eda6ae 60 os.chmod(outfile.name, 0644)
b1a8717a 61 os.rename(outfile.name, STATE_FILE)
526d4369 62 os.chmod(STATE_FILE, 0644)
a9b5e24b
MV
63
64
65if __name__ == "__main__":
66 apt_pkg.init()
67
68 # option parsing
69 parser = OptionParser()
70 parser.usage = "%prog [options] {markauto|unmarkauto} packages..."
d63a1458 71 parser.epilog = "apt-mark is deprecated, use apt-get markauto/unmarkauto."
a9b5e24b
MV
72 parser.add_option("-f", "--file", action="store", type="string",
73 dest="filename",
74 help="read/write a different file")
75 parser.add_option("-v", "--verbose",
76 action="store_true", dest="verbose", default=False,
77 help="print verbose status messages to stdout")
78 (options, args) = parser.parse_args()
79
a94263ba
DK
80 if not args:
81 parser.print_help()
82 sys.exit(1)
83
a9b5e24b
MV
84 # get the state-file
85 if not options.filename:
946b0e06 86 STATE_FILE = apt_pkg.config.find_dir("Dir::State") + "extended_states"
a9b5e24b
MV
87 else:
88 STATE_FILE=options.filename
89
eefacd4c
MV
90 if len(args) == 0:
91 parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
92
a9b5e24b
MV
93 if args[0] == "showauto":
94 show_automatic(STATE_FILE)
95 else:
96 # get pkgs to change
97 if args[0] not in actions.keys():
98 parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
99 pkgs = args[1:]
100 action = actions[args[0]]
101 mark_unmark_automatic(STATE_FILE, action, pkgs)