aboutsummaryrefslogtreecommitdiffstats
path: root/tools/queryforest
blob: 8dc979ce7149c2b3536aa94ad16f098b787123a2 (plain) (blame)
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
#!/usr/bin/env python3
import sys, os

args = sys.argv[1:]

if len(args) == 0:
	print('usage: %s (icon | file | directory) ... < icon-forest' % sys.argv[0], file = sys.stderr)
	exit(1)

forest = {}
selected = {}

try:
	while True:
		line = input()
		if not line.strip():
			continue
		if not line.startswith('\t'):
			tree = []
		icon = line.strip()
		tree.append(line)
		if icon in forest:
			print('duplicate entry of %s found' % icon, file = sys.stderr)
		forest[icon] = (tree, len(tree) - 1)
except EOFError:
	pass

basenames = {}
for icon in forest:
	iconbasename = icon.split('/')[-1]
	icons = basenames.get(iconbasename, [])
	icons.append(icon)
	basenames[iconbasename] = icons

def select(icon):
	if icon in forest:
		(tree, line) = forest[icon]
		lines = selected.get(tree[0], {})
		lines[line] = None
		selected[tree[0]] = lines
	elif icon.split('/')[-1] in basenames:
		icondir = '/'.join(icon.split('/')[:-1])
		for alticon in basenames[icon.split('/')[-1]]:
			(tree, line) = forest[alticon]
			lines = selected.get(tree[0], {})
			if line not in lines:
				lines[line] = [icondir]
			elif lines[line] is None:
				continue
			else:
				lines[line].append(icondir)
			selected[tree[0]] = lines

def visit(d):
	for f in os.listdir(d):
		f = d + '/' + f
		if os.path.isfile(f):
			icon = '/'.join('.'.join(f.split('.')[:-1]).split('/')[-2:])
			select(icon)
		elif os.path.isdir(f):
			visit(f)

for icon in args:
	if os.path.isfile(icon) and '/' in icon and icon.endswith('.svg'):
		icon = '/'.join(icon[:-4].split('/')[-2:])
	elif os.path.isdir(icon):
		visit(icon)
		continue
	select(icon)

for tree in sorted(selected):
	lines = selected[tree]
	(tree, _) = forest[tree]
	for i, line in enumerate(tree):
		if i not in lines:
			print(line)
		elif lines[i] is None:
			print('\033[1;31m%s\033[m' % line)
		else:
			print('\033[1;33m%s\033[;33m (%s)\033[m' % (line, ', '.join(sorted(lines[i]))))