summaryrefslogtreecommitdiff
path: root/src/dh_xul-ext
blob: e9c4276321ba95b9726ec6c7f48e6db6dac6795e (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/python

# Copyright (c) 2009 Benjamin Drung <bdrung@ubuntu.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import getopt
import glob
import os
import subprocess
import sys

from rdflib import Namespace
from rdflib.Graph import Graph

# error codes
COMMAND_LINE_SYNTAX_ERROR = 1
MULTIPLE_INSTALL_RDFs = 2

def get_xul_apps():
	packages = list()
	for filename in glob.glob("/var/lib/apt/lists/*_Packages"):
		with open(filename) as f:
			package_name = None
			version = None
			xul_appid = None
			xul_eol = "*"
			for line in f:
				if line == "\n":
					if xul_appid is not None:
						# find duplicates and keep older version
						found_duclicate = False
						for i in xrange(len(packages) - 1, -1, -1):
							if packages[i][0] == package_name:
								command = ['dpkg', '--compare-versions', version, 'lt', packages[i][1]]
								if subprocess.call(command) == 0:
									del packages[i]
								else:
									found_duclicate = True
									break
						if not found_duclicate:
							packages.append([package_name, version, xul_appid, xul_eol])
					package_name = None
					version = None
					xul_appid = None
					xul_eol = "*"
				elif line.startswith("Package:"):
					package_name = line[line.find(":")+1:].strip()
				elif line.startswith("Version:"):
					version = line[line.find(":")+1:].strip()
				elif line.startswith("Xul-AppId"):
					xul_appid = line[line.find(":")+1:].strip()
				elif line.startswith("Xul-Eol"):
					xul_eol = line[line.find(":")+1:].strip()
	return sorted(packages)

def get_supported_apps(xul_apps, install_rdf, package):
	# create array of id_max_min triples
	id_max_min = []
	rdf_graph = Graph()
	rdf_graph.parse(install_rdf)
	results = rdf_graph.query(
	  """
		SELECT ?id ?max ?min
		WHERE {
			?x1 em:targetApplication ?x2 .
			?x2 em:id ?id .
			OPTIONAL {
				?x2 em:maxVersion ?max .
				?x2 em:minVersion ?min .
			} .
		}
	  """, initNs=dict(em=Namespace("http://www.mozilla.org/2004/em-rdf#")))

	# append to id_max_min tripe to array
	for target in results:
		id_max_min.append (( str(target[0]), str(target[1]), str (target[2]) ))

# TODO - figure out apps etc.
## call parameters_
##   1- target app id
##   2- package name
#CHECK_VERSION = $(shell \
#	moz-version -cs "$($(2)_eol)" ge $(call TARGET_VERSION,$(1),minVersion) && \
#	moz-version -cs "$($(2)_sol)" le $(call TARGET_VERSION,$(1),maxVersion) && \
#	echo $(2))

#XPI_RECOMMENDS = $(strip $(foreach id,$(XPI_TARGET_EMIDs), \
#	$(foreach package,$(packages_$(id)),$(call CHECK_VERSION,$(id),$(package)))))
	return ["firefox-3.5", "abrowser-3.5", "firefox-3.0"]

def get_all_packages():
	lines = open("debian/control").readlines()
	package_lines = filter(lambda x: x.find("Package:") >= 0, lines)
	packages = map(lambda x: x[x.find(":")+1:].strip(), package_lines)
	return packages

def get_source_package_name():
	source = None
	with open("debian/control") as f:
		for line in f:
			if line.startswith("Source:"):
				source = line[line.find(":")+1:].strip()
				break
	return source


def get_provided_package_names(package, supported_apps):
	# check if MOZ_XPI_EXT_NAME is defined in debian/rules
	lines = open("debian/rules").readlines()
	lines = filter(lambda x: x.find("MOZ_XPI_EXT_NAME") != -1 or x.find("MOZ_EXT_NAME") != -1, lines)
	if len(lines) > 0:
		ext_name = lines[-1][line.find("=")+1:].strip()
	else:
		ext_name = package
		for prefix in ("firefox-", "iceweasel-", "mozilla-", "xul-ext-"):
			if ext_name.startswith(prefix):
				ext_name = ext_name[len(prefix):]

	provides = set()
	provides.add("xul-ext-" + ext_name)
	if ext_name == get_source_package_name():
		provides.add(ext_name)

	for app in supported_apps:
		for i in xrange(len(app) - 1, -1, -1):
			if app[i] == '-':
				app = app[:i]
			elif not app[i].isdigit() and not app[i] == '.':
				break
		provides.add(app + "-" + ext_name)

	# remove package name from provide list
	provides.discard(package)

	return list(provides)

def find_install_rdfs(package):
	install_rdfs = list()
	for root, dirs, files in os.walk("debian/" + package):
		if "install.rdf" in files:
			install_rdfs.append(os.path.join(root, "install.rdf"))
		dirs = filter(lambda d: not os.path.islink(os.path.join(root, d)), dirs)
	return install_rdfs

def generate_substvars(xul_apps, package):
	install_rdfs = find_install_rdfs(package)
	if len(install_rdfs) == 0:
		# this package does not contain a xul extension
		return
	elif len(install_rdfs) > 1:
		print >> sys.stderr, "%s: %s contains multiple install.rdf files. That's not supported." % (sys.argv[0], package)
		sys.exit(MULTIPLE_INSTALL_RDFs)
	install_rdf = install_rdfs.pop()

	filename = "debian/" + package + ".substvars"
	if os.path.exists(filename):
		f = open(filename)
		lines = f.readlines()
		f.close()
	else:
		lines = list()

	# remove existing varibles
	lines = filter(lambda s: not s.startswith("xpi:"), lines)

	packages = get_supported_apps(xul_apps, install_rdf, package)
	lines.append("xpi:Recommends=" + " | ".join(packages) + "\n")
	lines.append("xpi:Enhances=" + ", ".join(sorted(packages)) + "\n")
	packages = get_provided_package_names(package, packages)
	lines.append("xpi:Provides=" + ", ".join(sorted(packages)) + "\n")

	# write new variables
	f = open(filename, "w")
	f.writelines(lines)
	f.close()

if __name__ == "__main__":
	try:
		long_opts = ["help", "package", "verbose"]
		opts, args = getopt.gnu_getopt(sys.argv[1:], "hp:v", long_opts)
	except getopt.GetoptError, e:
		# print help information and exit:
		print >> sys.stderr, str(e) # will print something like "option -a not recognized"
		usage(sys.stderr)
		sys.exit(COMMAND_LINE_SYNTAX_ERROR)

	packages = list()
	verbose = False

	for o, a in opts:
		if o in ("-h", "--help"):
			usage(sys.stdout)
			sys.exit()
		elif o in ("-p", "--package"):
			packages.append(a)
		elif o in ("-v", "--verbose"):
			verbose = True
		else:
			assert False, "unhandled option"

	if len(packages) == 0:
		packages = get_all_packages()

	if verbose:
		print sys.argv[0] + ": packages:", ", ".join(packages)

	xul_apps = get_xul_apps()
	if verbose and len(xul_apps) > 0:
		print sys.argv[0] + ": found %i Xul applications:" % (len(xul_apps))
		for xul_app in xul_apps:
			print xul_app[0] + " " + xul_app[1] + " (" + xul_app[2] + ")"

	for package in packages:
		generate_substvars(xul_apps, package)