]> git.saurik.com Git - apt.git/blame - cmdline/apt-mark
merge from debian-sid apt 0.7.24
[apt.git] / cmdline / apt-mark
CommitLineData
b1a8717a
MV
1#!/usr/bin/python
2
3from optparse import OptionParser
4
5try:
6 import apt_pkg
7except ImportError:
8 print "Error importing apt_pkg, is python-apt installed?"
9
10import sys
11import os.path
12
13actions = { "markauto" : 1,
14 "unmarkauto": 0
15 }
16
a9b5e24b
MV
17def show_automatic(filename):
18 if not os.path.exists(STATE_FILE):
19 return
20 auto = set()
21 tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
22 while tagfile.Step():
23 pkgname = tagfile.Section.get("Package")
24 autoInst = tagfile.Section.get("Auto-Installed")
25 if int(autoInst):
26 auto.add(pkgname)
27 print "\n".join(sorted(auto))
28
b1a8717a 29
a9b5e24b
MV
30def mark_unmark_automatic(filename, action, pkgs):
31 " mark or unmark automatic flag"
b1a8717a
MV
32 # open the statefile
33 if os.path.exists(STATE_FILE):
34 tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
35 outfile = open(STATE_FILE+".tmp","w")
36 while tagfile.Step():
37 pkgname = tagfile.Section.get("Package")
38 autoInst = tagfile.Section.get("Auto-Installed")
39 if pkgname in pkgs:
40 if options.verbose:
41 print "changing %s to %s" % (pkgname,action)
42 newsec = apt_pkg.RewriteSection(tagfile.Section,
43 [],
44 [ ("Auto-Installed",str(action)) ]
45 )
46 outfile.write(newsec+"\n")
47 else:
48 outfile.write(str(tagfile.Section)+"\n")
49 # all done, rename the tmpfile
54eda6ae 50 os.chmod(outfile.name, 0644)
b1a8717a 51 os.rename(outfile.name, STATE_FILE)
526d4369 52 os.chmod(STATE_FILE, 0644)
a9b5e24b
MV
53
54
55if __name__ == "__main__":
56 apt_pkg.init()
57
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
69 # get the state-file
70 if not options.filename:
71 STATE_FILE = apt_pkg.Config.FindDir("Dir::State") + "extended_states"
72 else:
73 STATE_FILE=options.filename
74
75 if args[0] == "showauto":
76 show_automatic(STATE_FILE)
77 else:
78 # get pkgs to change
79 if args[0] not in actions.keys():
80 parser.error("first argument must be 'markauto', 'unmarkauto' or 'showauto'")
81 pkgs = args[1:]
82 action = actions[args[0]]
83 mark_unmark_automatic(STATE_FILE, action, pkgs)