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