1#!/usr/bin/env python
2#
3# gnomedepends
4# Analyse pkg/PLIST and give an advice as to which GNOME
5# ports should be listes in {RUN,LIB}_DEPENDS for this port
6#
7# ----------------------------------------------------------------------------
8# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
9# Maxim Sobolev <sobomax@FreeBSD.org> wrote this file.  As long as you retain
10# this notice you can do whatever you want with this stuff. If we meet some
11# day, and you think this stuff is worth it, you can buy me a beer in return.
12#
13# Maxim Sobolev
14# ----------------------------------------------------------------------------
15#
16#
17# MAINTAINER= ports@MidnightBSD.org
18#
19# TODO:
20#  - analyse actual {RUN,LIB}_DEPENDS and give an advice about what should be
21#    added;
22#  - analyse results and remove redundant dependencies (for example if gnomecore
23#    has gnomecontrolcenter listed as dependency, and it is found that the port
24#    requires both gnomecontrolcenter and gnomecore do not list
25#    gnomecontrolcenter then);
26#  - parse ports/INDEX directly.
27#
28
29import os, os.path, sys, string, re
30
31def getcmdout(cmdline):
32	results = []
33	pipe = os.popen(cmdline)
34	buffer = pipe.readlines()
35	for result in buffer:
36		result = string.strip(result)
37		if len(result) > 0:
38			results.append(result)
39	pipe.close()
40	return results
41
42def readfile(filename):
43	file = open(filename)
44	result = file.readlines()
45	file.close()
46	return result
47
48def filter(lines, regobj):
49	results = []
50	for line in lines:
51		match = regobj.match(line)
52		if match != None:
53			result = string.strip(match.group(1))
54			try:
55				tmp = results.index(result)
56			except ValueError:
57				results.append(result)
58	return results
59
60gnomeports = getcmdout('cd /usr/mports && make search key=gnome | grep ^Path:')
61newgnomeports = []
62for i in gnomeports:
63	newgnomeports.append(string.split(i)[1])
64gnomeports = newgnomeports
65newgnomeports = []
66
67regobj = re.compile('^@dirrm (?P<dirname>\S+).*$')
68for portdir in gnomeports:
69	try:
70		lines = readfile(os.path.join(portdir, 'pkg-plist'))
71		lines = list(filter(lines, regobj))
72		if len(lines) > 0:
73			newgnomeports.append([portdir, lines])
74	except IOError:
75		pass
76gnomeports = newgnomeports
77newgnomeports = []
78
79try:
80	currplist = readfile('pkg-plist')
81except IOError as errmsg:
82	print(errmsg)
83	sys.exit(1)
84
85regobj = re.compile('^(?!@)(?P<dirname>\S+)/.*')
86currdirs = list(filter(currplist, regobj))
87regobj = re.compile('^@dirrm (?P<dirname>\S+).*$')
88currdirs.extend(list(filter(currplist, regobj)))
89currportdir = os.getcwd()
90
91newcurrdirs = []
92for dir in currdirs:
93	incremental = ''
94	for component in string.split(dir, '/'):
95		if incremental != '':
96			incremental = incremental + '/'
97		incremental = incremental + component
98		try:
99			tmp = newcurrdirs.index(incremental)
100		except ValueError:
101			newcurrdirs.append(incremental)
102currdirs = newcurrdirs
103
104depends = []
105for gnomeport in gnomeports:
106	if (currportdir == gnomeport[0]):
107		continue
108	matches = []
109	for gnomedir in gnomeport[1]:
110		for dir in currdirs:
111			if (gnomedir == dir):
112				matches.append(dir)
113	if len(matches) > 0:
114		depends.append([gnomeport[0], matches])
115
116if len(depends) == 0:
117	sys.stdout.writelines(['No dependencies found (maybe it is not a GNOME port).\n'])
118	sys.exit(0)
119
120sys.stdout.writelines(['According to the contents of pkg-plist the port depends on the following GNOME\n', 'port(s):\n\n'])
121for depend in depends:
122	sys.stdout.writelines([depend[0], ', for directories:\n'])
123	for dir in depend[1]:
124		sys.stdout.writelines(['\t', dir, '\n'])
125	sys.stdout.writelines(['\n'])
126
127
128