]> git.saurik.com Git - apt.git/blob - cmdline/apt-mark
print an error if a new state file can't be created in apt-mark,
[apt.git] / cmdline / apt-mark
1 #!/usr/bin/python
2
3 from optparse import OptionParser
4
5 try:
6 import apt_pkg
7 except ImportError:
8 print "Error importing apt_pkg, is python-apt installed?"
9
10 import sys
11 import os.path
12
13 actions = { "markauto" : 1,
14 "unmarkauto": 0
15 }
16
17 def 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
29
30 def mark_unmark_automatic(filename, action, pkgs):
31 " mark or unmark automatic flag"
32 # open the statefile
33 if os.path.exists(STATE_FILE):
34 try:
35 tagfile = apt_pkg.ParseTagFile(open(STATE_FILE))
36 outfile = open(STATE_FILE+".tmp","w")
37 except IOError, msg:
38 print "%s, are you root?" % (msg)
39 sys.exit(1)
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)
57
58
59 if __name__ == "__main__":
60 apt_pkg.init()
61
62 # option parsing
63 parser = OptionParser()
64 parser.usage = "%prog [options] {markauto|unmarkauto} packages..."
65 parser.add_option("-f", "--file", action="store", type="string",
66 dest="filename",
67 help="read/write a different file")
68 parser.add_option("-v", "--verbose",
69 action="store_true", dest="verbose", default=False,
70 help="print verbose status messages to stdout")
71 (options, args) = parser.parse_args()
72
73 # get the state-file
74 if not options.filename:
75 STATE_FILE = apt_pkg.Config.FindDir("Dir::State") + "extended_states"
76 else:
77 STATE_FILE=options.filename
78
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)