summaryrefslogtreecommitdiff
blob: 47e9a3eea9088223bf407e795303c1bc19687a33 (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
# Copyright 2019-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# QA check: ensure that Python modules are compiled after installing
# Maintainer: Python project <python@gentoo.org>

# EAPI guard to prevent errors from trying to import python-utils-r1
# in unsupported EAPIs.  Please keep the list in sync with the eclass!
if [[ ${EAPI} == [6-8] ]]; then
	inherit python-utils-r1

	python_pyc_check() {
		local impl missing=() outdated=()
		for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
			_python_export "${impl}" EPYTHON PYTHON
			[[ -x ${PYTHON} ]] || continue
			local sitedir=$(python_get_sitedir "${impl}")

			if [[ -d ${D}${sitedir} ]]; then
				local suffixes=() subdir=
				case ${EPYTHON} in
					python2*)
						suffixes=( .py{c,o} )
						;;
					pypy)
						suffixes=( .pyc )
						;;
					python3*|pypy3*)
						local tag=$("${PYTHON}" -c 'import sys; print(sys.implementation.cache_tag)')
						suffixes=( ".${tag}"{,.opt-{1,2}}.pyc )
						subdir=__pycache__/
						;;
					*)
						# skip testing unknown impl
						continue
						;;
				esac

				einfo "Verifying compiled files in ${sitedir}"
				local f s
				while read -d $'\0' -r f; do
					local dir=${f%/*}
					local basename=${f##*/}
					basename=${basename%.py}

					for s in "${suffixes[@]}"; do
						local cache=${dir}/${subdir}${basename}${s}
						if [[ ! -f ${cache} ]]; then
							missing+=( "${cache}" )
						elif [[ ${f} -nt ${cache} ]]; then
							outdated+=( "${cache}" )
						fi
					done
				done < <(find "${D}${sitedir}" -name '*.py' -print0)
			fi
		done

		if [[ ${missing[@]} ]]; then
			eqawarn
			eqawarn "QA Notice: This package installs one or more Python modules that are"
			eqawarn "not byte-compiled."
			eqawarn "The following files are missing:"
			eqawarn
			eqatag -v python-pyc.missing "${missing[@]#${D}}"
		fi

		if [[ ${outdated[@]} ]]; then
			eqawarn
			eqawarn "QA Notice: This package installs one or more compiled Python modules that have"
			eqawarn "older timestamps than the corresponding source files:"
			eqawarn
			eqatag -v python-pyc.outdated "${outdated[@]#${D}}"
		fi

		if [[ ${missing[@]} || ${outdated[@]} ]]; then
			eqawarn
			eqawarn "Please either fix the upstream build system to byte-compile Python modules"
			eqawarn "correctly, or call python_optimize after installing them.  For more"
			eqawarn "information, see:"
			eqawarn "https://projects.gentoo.org/python/guide/helper.html#byte-compiling-python-modules"
			eqawarn
		fi
	}

	python_pyc_check
fi

: # guarantee successful exit

# vim:ft=ebuild