aboutsummaryrefslogtreecommitdiffstats
path: root/gi-find-deps.sh.in
blob: a66868d1f6a984fc49dc570b3df6f988a8f2f7b5 (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
#!/bin/sh

# Automatically find Provides and Requires for typelib() gobject-introspection bindings.
# can be started with -R (Requires) and -P (Provides)

# Copyright 2011 by Dominique Leuenberger, Amsterdam, Netherlands (dimstar [at] opensuse.org)
# This file is released under the GPLv2 or later.

function split_name_version {
base=$1
tsymbol=${base%-*}
# Sometimes we get a Requires on Gdk.Settings.foo, bebause you can directly use imports.gi.Gdk.Settings.Foo in Javascript.
# We know that the symbol in this case is call Gdk, so we cut everything after the . away.
symbol=$(echo $tsymbol | awk -F. '{print $1}')
version=${base#*-}
# In case there is no '-' in the filename, then the split above 'fails' and version == symbol (thus: no version specified)
if [ "$tsymbol" = "$version" ]; then
	unset version
fi
}

function print_req_prov {
echo -n "typelib($symbol)"
if [ ! -z "$version" ]; then
	echo " = ${version}"
else
	echo ""
fi
}

function find_provides {
while read file; do
	case $file in
		*.typelib)
			split_name_version $(basename $file | sed 's,.typelib$,,')
			print_req_prov
			;;
	esac
done
}

function javascript_requires {
	for module in $(grep -h -P -o "imports\.gi\.([^\s'\";]+)" $1 | grep -v "imports\.gi\.version" | sed -r -e 's,\s+$,,g' -e 's,imports.gi.,,'); do
		split_name_version $module
		print_req_prov
	done
	for module in $(grep -h -P -o "imports\.gi\.versions.([^\s'\";]+)\s*=\s*['\"].+['\"]" $1 | \
		sed -e 's:imports.gi.versions.::' -e "s:['\"]::g" -e 's:=:-:' -e 's: ::g'); do
		split_name_version $module
		print_req_prov
	done
}

function python_requires {
        for module in $(grep -h -P "from gi\.repository import (\w+)" $1 | sed 's:#.*::' | sed -e 's,from gi.repository import,,' -r -e 's:\s+as\s+\w+::g' -e 's:\s*,\s*: :g'); do
		split_name_version $module
		print_req_prov
		# Temporarly disabled... this is not true if the python code is written for python3... And there seems no real 'way' to identify this.
		#echo "python-gi >= 2.90.2"
	done
	for module in $(grep -h -P -o "(gi\.require_version\(['\"][^'\"]+['\"],\s*['\"][^'\"]+['\"]\))" $1 | sed -e 's:gi.require_version::' -e "s:[()\"' ]::g" -e 's:,:-:'); do
		split_name_version $module
		print_req_prov
	done
}

function typelib_requires {
	for module in $(@RPMVENDORDIR@/g-ir-extract-deps $1 | tr '|' ' '); do
		split_name_version $module
		print_req_prov
	done
}

function find_requires {
# Currently, we detect:
# - in python:
#   . from gi.repository import foo [Unversioned requirement of 'foo']
#   . from gi.repository import foo-1.0 [versioned requirement]
#   . gi.require_version('Gtk', '3.0') (To specify a version.. there is still an import needed)
#   . And we do not stumble over:
#     from gi.repository import foo as _bar
#     from gi.repository import foo, bar
# - in JS:
#   . imports.gi.foo; [unversioned requirement of 'foo']
#   . imports.gi.goo-1.0; [versioned requirement]
#   . imports.gi.versions.Gtk = '3.0';
#   . The imports can be listed on one line, and we catch them.

while read file; do
	case $file in
		*.js)
			javascript_requires "$file"
			;;
		*.py)
			python_requires "$file"
			;;
		*.typelib)
			typelib_requires "$file"
			;;
		*)
			case $(file -b $file) in
				Python\ script*)
					python_requires "$file"
					;;
			esac
			;;
	esac
done
}

case $1 in
	-P)
		find_provides
		;;
	-R)
		find_requires
		;;
esac