File: update-command-not-found

package info (click to toggle)
command-not-found 0.2.38-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,268 kB
  • sloc: python: 829; makefile: 42; sh: 18
file content (105 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python -Su
#
# Copyright (C) 2008 Julian Andres Klode <jak@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
'''Create the databases (or cache) for command-not-found

This program creates the databases in /var/cache/command-not-found/. It may
take some time, because the files it reads are big.

Patches are accepted, aswell as rewrites in Perl.'''
import subprocess
import os.path
import gzip
import sys
import gdbm

databases = {}

def write_db_core(text):
	'''Create the database from the Contents-*.gz file
	
	Write the information found in the fileobject 'text' to the database at
	the path 'tgt'.
	'''
	
	for i in text:
		if not (i.startswith('usr/sbin') or i.startswith('usr/bin') or
		   i.startswith('bin') or i.startswith('sbin')):
		   continue
		try:
			fname, packages = i.split(None, 1)
		except ValueError:
			continue

		fname = os.path.basename(fname)
		
		for package in packages.split(','):
			try:
				section, package = package.strip().rsplit('/', 1)
			except ValueError:
				package = package.strip()
				section = "unknown"
			if len(section.split('/')) == 2:
				component, section = section.split('/')
			else:
				component = 'main'
			if not component in databases:
				databases[component] = gdbm.open(
				"/var/cache/command-not-found/%s.db" % component, "n", 0644)
				databases[component][':component:'] = component
			try:
				databases[component][fname] += '|' + package.strip()
			except KeyError:
				databases[component][fname] = package.strip()
			break

def write_db_apt_file():
	import glob
	for fname in glob.glob('/var/lib/apt/lists/*Contents*.*'):
		if "Contents-source" in fname:
			continue
		if fname.endswith(".diff_Index"):
			continue
		print 'I: Writing data for %s ...' % os.path.basename(fname),
		proc = subprocess.Popen(["/usr/lib/apt/apt-helper", "cat-file", fname], stdout=subprocess.PIPE)
		try:
			write_db_core(proc.stdout)
		finally:
			proc.stdout.close()
			proc.wait()
		print '. done'


if __name__ == '__main__':
	from optparse import OptionParser

	parser = OptionParser()
	parser.add_option("-u", "--no-umask", action="store_true",
                          help="Do not change the umask to 0022.")
    
	(options, args) = parser.parse_args()

	
	try:
		if not options.no_umask:
			umask = os.umask(0022)
		write_db_apt_file()
	finally:
		for component, fobj in databases.iteritems():
			fobj.close()
		if not options.no_umask:
			os.umask(umask)