8448438
import glob
8448438
import os
8448438
import re
8448438
import rpm
8448438
import sys
8448438
8448438
ts = rpm.TransactionSet()
8448438
8448438
packages = []
8448438
for arg in sys.argv[1:]:
8448438
	packages += glob.glob(arg)
8448438
8448438
erlang_provides = {}
8448438
erlang_requires = {}
8448438
8448438
package_names = []
8448438
package_headers = {}
8448438
8448438
##
8448438
## Custom tweaks begins here
8448438
##
8448438
8448438
# These packages should be marked as noarch
b2acbda
package_noarch = [
b2acbda
		"emacs-erlang",
b2acbda
		"emacs-erlang-el",
b2acbda
		"erlang-doc",
b2acbda
		"xemacs-erlang",
b2acbda
		"xemacs-erlang-el"]
8448438
8448438
# These are additional Requires which cannot be picked up automatically (yet).
8448438
# TODO these should be added automatically
8448438
package_additional_requires = {
8448438
		"emacs-erlang": ["emacs-common-erlang = %{version}-%{release}", "emacs(bin) >= %{_emacs_version}" ],
8448438
		"emacs-erlang-el": ["emacs-erlang = %{version}-%{release}"],
8448438
		"erlang-dialyzer": ["graphviz"],
8448438
		"erlang-erl_interface": ["%{name}-erts%{?_isa} = %{version}-%{release}"],
b2acbda
		# This library (lksctp-tools) is dlopened so it can't be picked
b2acbda
		# up automatically by the RPM dependency checker
8448438
		"erlang-erts": ["lksctp-tools"],
8448438
		"erlang-gs": ["tk"],
b2acbda
		# Stores files/links in /usr/share/java so has to depend on jpackage-utils
b2acbda
		"erlang-ic": ["jpackage-utils"],
b2acbda
		# Stores files/links in /usr/share/java so has to depend on jpackage-utils
b2acbda
		"erlang-jinterface": ["%{name}-erts%{?_isa} = %{version}-%{release}", "jpackage-utils"],
8448438
		"erlang-wx": ["mesa-libGL", "mesa-libGLU"],
8448438
		"xemacs-erlang": ["emacs-common-erlang = %{version}-%{release}", "xemacs(bin) >= %{_xemacs_version}"],
8448438
		"xemacs-erlang-el": ["xemacs-erlang = %{version}-%{release}"]
8448438
		}
8448438
8448438
package_additional_buildrequires = {
b2acbda
		"emacs-erlang": ["emacs", "emacs-el"],
b2acbda
		"erlang-crypto": ["openssl-devel"],
8448438
		"erlang-diameter": ["ed"],
b2acbda
b2acbda
		# BEWARE. No fop for EPEL5, and only for x86/x86_64 in EPEL6,
b2acbda
		# so we cannot regenerate docs here. (Un)Fortunately we dropped
b2acbda
		# support for EPEL6 and older versions.
b2acbda
		# FIXME add bootstrap condition first.
b2acbda
		"erlang-doc": ["fop", "libxslt"],
b2acbda
b2acbda
		"erlang-erts": ["lksctp-tools-devel", "m4", "ncurses-devel", "zlib-devel"],
8448438
		"erlang-gs": ["tcl-devel", "tk-devel"],
b2acbda
8448438
		# in EPEL6 on arches different from %{ix86} x86_64 we have to
8448438
		# use java-devel-gcj, so technically this requirement makes it
8448438
		# impossible to build Java support there. (Un)Fortunately we
b2acbda
		# already dropped full support for EPEL6 and older versions.
8448438
		"erlang-ic": ["java-devel"],
8448438
		"erlang-jinterface": ["java-devel"],
b2acbda
8448438
		"erlang-odbc": ["unixODBC-devel"],
8448438
		"erlang-wx": ["wxGTK-devel"],
b2acbda
		"xemacs-erlang": ["xemacs", "xemacs-packages-extra-el"],
8448438
		}
8448438
8448438
package_additional_obsoletes = {
b2acbda
		"erlang-erts": [
b2acbda
			"erlang-appmon",
b2acbda
			"erlang-docbuilder",
b2acbda
			"erlang-inviso",
b2acbda
			"erlang-pman",
b2acbda
			"erlang-toolbar",
b2acbda
			"erlang-tv"],
8448438
		}
8448438
8448438
##
8448438
## Custom tweaks ends here
8448438
##
8448438
8448438
# To match 'erlang(asn1ct_eval_ext:transform_to_EXTERNAL1994/1)'
8448438
prog = re.compile("^erlang(.*:.*/\d+)")
8448438
rpmmask = re.compile(".*\.rpm")
8448438
8448438
# iterate over all rpms
8448438
for package in sorted([p for p in packages if rpmmask.match(p)]):
b2acbda
b2acbda
	# A tricky part. We are processing packages, rebuilt with
b2acbda
	# %{__erlang_provides_requires}. Otherwise we won't get information
b2acbda
	# about imports/exports (until we learn how to parse Erlang BEAM file
b2acbda
	# headers with Python.
8448438
	fd = os.open(package, os.O_RDONLY)
8448438
	h = ts.hdrFromFdno(fd)
8448438
	os.close(fd)
8448438
8448438
	rpm_name = h[rpm.RPMTAG_NAME]
8448438
8448438
	# Let's calculate provides
8448438
	# We'll create dictionary to speedup future use
8448438
	# FIXME duplicate provides
8448438
	local_provides = [p for p in h[rpm.RPMTAG_PROVIDENAME] if prog.match(p)]
8448438
	erlang_provides.update({k: rpm_name for k in local_provides})
8448438
8448438
	# Let's calculate requires
8448438
	erlang_requires.update({rpm_name: [r for r in h[rpm.RPMTAG_REQUIRENAME] if prog.match(r) and not r in local_provides]})
8448438
8448438
	package_headers.update({rpm_name: h})
8448438
8448438
	package_names.append(rpm_name)
8448438
8448438
namemask = re.compile("^erlang-[a-zA-Z0-9_]*$")
8448438
8448438
print "### BEGIN OF AUTOGENERATED LIST ###"
8448438
print ""
8448438
8448438
for name in package_names:
8448438
	h = package_headers[name]
8448438
8448438
	if name == "erlang-doc":
8448438
		# Additional ifdef just for this sub-package (see below)
8448438
		print "%if %{with doc}"
8448438
8448438
	if namemask.match(name):
8448438
		print "%%package %s" % name.replace("erlang-", "")
8448438
	else:
8448438
		print "%%package -n %s" % name
8448438
8448438
	print "Summary: %s" % h[rpm.RPMTAG_SUMMARY]
8448438
	print "Group: %s" % h[rpm.RPMTAG_GROUP]
8448438
8448438
	rawdeps = [erlang_provides.get(r, "Error: missing requires: %s" % r) for r in erlang_requires[name]]
8448438
8448438
	deps = set()
8448438
	dependency_add = deps.add
8448438
	[x for x in rawdeps if not (x in deps or dependency_add(x))]
8448438
8448438
	# Additional BuildRequires (if any):
8448438
	for br in package_additional_buildrequires.get(name, []):
8448438
		print "BuildRequires: %s" % br
8448438
8448438
	# Add basic autogenerated Requires:
8448438
	for r in sorted(deps):
8448438
		#Requires: %{name}-stdlib%{?_isa} = %{version}-%{release}
8448438
		print "Requires: %s%%{?_isa} = %%{version}-%%{release}" % r.replace("erlang-", "%{name}-")
8448438
8448438
	# Add custom additional Requires (if any):
8448438
	for r in package_additional_requires.get(name, []):
8448438
		print "Requires: %s" % r
8448438
8448438
	# Add custom additional Obsoletes (if any):
8448438
	for os in package_additional_obsoletes.get(name, []):
8448438
		print "Obsoletes: %s" % os
8448438
8448438
	if name in package_noarch:
8448438
		print "BuildArch: noarch"
8448438
8448438
	print ""
8448438
	if namemask.match(name):
8448438
		print "%%description %s" % name.replace("erlang-", "")
8448438
	else:
8448438
		print "%%description -n %s" % name
8448438
	print "%s" % h[rpm.RPMTAG_DESCRIPTION]
8448438
8448438
	if name == "erlang-doc":
8448438
		# Additional ifdef just for this sub-package (see above)
8448438
		print "%endif"
8448438
8448438
	print ""
8448438
8448438
print "### END OF AUTOGENERATED LIST ###"