]>
Commit | Line | Data |
---|---|---|
cdc9189e JR |
1 | # Python specific macro definitions. |
2 | ||
3 | %__python /usr/bin/python | |
4 | ||
5 | # python main version | |
6 | %py_ver %{expand:%%global py_ver %(%{__python} -c "import sys; print sys.version[:3]" 2>/dev/null || echo ERROR)}%py_ver | |
7 | ||
8 | # directories | |
9 | %py_prefix %{expand:%%global py_prefix %(%{__python} -c "import sys; print sys.prefix" 2>/dev/null || echo ERROR)}%py_prefix | |
10 | %py_libdir %{py_prefix}/%{_lib}/python%{py_ver} | |
11 | %py_scriptdir %{py_prefix}/share/python%{py_ver} | |
12 | %py_incdir /usr/include/python%{py_ver} | |
13 | %py_sitedir %{py_libdir}/site-packages | |
14 | %py_sitescriptdir %{py_scriptdir}/site-packages | |
15 | %py_dyndir %{py_libdir}/lib-dynload | |
16 | ||
17 | # pure python modules compilation | |
18 | %py_comp python -c "import compileall; import sys; compileall.compile_dir(sys.argv[1], ddir=sys.argv[1][len('$RPM_BUILD_ROOT'):])" | |
19 | ||
20 | %py_ocomp python -O -c "import compileall; import sys; compileall.compile_dir(sys.argv[1], ddir=sys.argv[1][len('$RPM_BUILD_ROOT'):])" | |
21 | ||
22 | # Software written in Python language require Python with main version | |
23 | %pyrequires_eq() Requires: %1 | |
24 | ||
25 | # Python 3.x | |
26 | # python main version | |
27 | %__python3 /usr/bin/python3 | |
28 | %py3_ver %{expand:%%global py3_ver %(%{__python3} -c "import sys; print(sys.version[:3])" 2>/dev/null || echo ERROR)}%py3_ver | |
29 | ||
30 | # directories | |
31 | %py3_prefix %{expand:%%global py3_prefix %(%{__python3} -c "import sys; print(sys.prefix)" 2>/dev/null || echo ERROR)}%py3_prefix | |
32 | %py3_incdir %{expand:%%global py3_incdir %(%{__python3} -c "import sysconfig; print(sysconfig.get_path('include'))" 2>/dev/null || echo ERROR)}%py3_incdir | |
33 | %py3_libdir %{py3_prefix}/%{_lib}/python%{py3_ver} | |
34 | %py3_sitedir %{py3_libdir}/site-packages | |
35 | %py3_sitescriptdir %{py3_prefix}/share/python%{py3_ver}/site-packages | |
36 | %py3_dyndir %{py3_libdir}/lib-dynload | |
37 | ||
38 | # pure python modules compilation | |
39 | %py3_comp %{__python3} -c "import compileall; import sys; compileall.compile_dir(sys.argv[1], ddir=sys.argv[1][len('$RPM_BUILD_ROOT'):])" | |
40 | ||
41 | %py3_ocomp %{__python3} -O -c "import compileall; import sys; compileall.compile_dir(sys.argv[1], ddir=sys.argv[1][len('$RPM_BUILD_ROOT'):])" | |
42 | ||
43 | %py_build { \ | |
44 | LDFLAGS="${LDFLAGS:-%rpmldflags}"; export LDFLAGS; \ | |
45 | CFLAGS="${CFLAGS:-%rpmcppflags %rpmcflags}"; export CFLAGS; \ | |
46 | CXXFLAGS="${CXXFLAGS:-%rpmcppflags %rpmcxxflags}"; export CXXFLAGS; \ | |
47 | %{?__cc:CC="%{__cc}"; export CC;} \ | |
48 | %{?__cxx:CXX="%{__cxx}"; export CXX;} \ | |
49 | %{__python} setup.py \ | |
50 | build --build-base=build-2 \ | |
51 | } | |
52 | ||
53 | # "shrink" filter to keep this macro without newlines, | |
54 | # so it could be used as make arguments | |
55 | %py_install_opts() %{shrink: \ | |
56 | --prefix=%{_prefix} \ | |
57 | --install-purelib=%{py_sitescriptdir} \ | |
58 | --install-platlib=%{py_sitedir} \ | |
59 | --optimize=2 \ | |
60 | } | |
61 | ||
62 | %py_install { \ | |
63 | LDFLAGS="${LDFLAGS:-%rpmldflags}"; export LDFLAGS; \ | |
64 | CFLAGS="${CFLAGS:-%rpmcppflags %rpmcflags}"; export CFLAGS; \ | |
65 | CXXFLAGS="${CXXFLAGS:-%rpmcppflags %rpmcxxflags}"; export CXXFLAGS; \ | |
66 | %{?__cc:CC="%{__cc}"; export CC;} \ | |
67 | %{?__cxx:CXX="%{__cxx}"; export CXX;} \ | |
68 | %{__python} setup.py \ | |
69 | build --build-base=build-2 \ | |
70 | install --skip-build \ | |
71 | %{py_install_opts} \ | |
72 | --root=$RPM_BUILD_ROOT \ | |
73 | } | |
74 | ||
75 | %py3_build { \ | |
76 | LDFLAGS="${LDFLAGS:-%rpmldflags}"; export LDFLAGS; \ | |
77 | CFLAGS="${CFLAGS:-%rpmcppflags %rpmcflags}"; export CFLAGS; \ | |
78 | CXXFLAGS="${CXXFLAGS:-%rpmcppflags %rpmcxxflags}"; export CXXFLAGS; \ | |
79 | %{?__cc:CC="%{__cc}"; export CC;} \ | |
80 | %{?__cxx:CXX="%{__cxx}"; export CXX;} \ | |
81 | %{__python3} setup.py build --build-base=build-3 \ | |
82 | } | |
83 | ||
84 | # "shrink" filter to keep this macro without newlines, | |
85 | # so it could be used as make arguments | |
86 | %py3_install_opts() %{shrink: \ | |
87 | --prefix=%{_prefix} \ | |
88 | --install-purelib=%{py3_sitescriptdir} \ | |
89 | --install-platlib=%{py3_sitedir} \ | |
90 | --optimize=2 \ | |
91 | } | |
92 | ||
93 | %py3_install { \ | |
94 | LDFLAGS="${LDFLAGS:-%rpmldflags}"; export LDFLAGS; \ | |
95 | CFLAGS="${CFLAGS:-%rpmcppflags %rpmcflags}"; export CFLAGS; \ | |
96 | CXXFLAGS="${CXXFLAGS:-%rpmcppflags %rpmcxxflags}"; export CXXFLAGS; \ | |
97 | %{?__cc:CC="%{__cc}"; export CC;} \ | |
98 | %{?__cxx:CXX="%{__cxx}"; export CXX;} \ | |
99 | %{__python3} setup.py \ | |
100 | build --build-base=build-3 \ | |
101 | install --skip-build \ | |
102 | %{py3_install_opts} \ | |
103 | --root=$RPM_BUILD_ROOT \ | |
104 | } | |
105 | ||
106 | # Hardlink binary identical .pyc and .pyo files | |
107 | %__spec_install_post_py_hardlink {\ | |
108 | %{!?no_install_post_py_hardlink: __spec_install_post_py_hardlink() { \ | |
109 | %{!?debug:set +x;} \ | |
110 | local a b c=0 t=0 pyc=%{tmpdir}/%{name}-%{version}-%{release}__rpm_pyc; \ | |
111 | if [ -d "$RPM_BUILD_ROOT" ]; then \ | |
112 | find "$RPM_BUILD_ROOT" -name '*.pyc' > $pyc; \ | |
113 | while read a; do \ | |
114 | b="${a%.pyc}.pyo"; \ | |
115 | if cmp -s "$a" "$b"; then \ | |
116 | ln -f "$a" "$b"; \ | |
117 | c=$((c + 1)); \ | |
118 | fi; \ | |
119 | t=$((t + 1)); \ | |
120 | done < $pyc; \ | |
121 | [ $t -gt 0 ] && printf "Hardlink Python files: %d/%d files hardlinked\n" $c $t; \ | |
122 | rm -f $pyc; \ | |
123 | fi; \ | |
124 | }; __spec_install_post_py_hardlink } } | |
125 | ||
126 | # Remove Python 2.x sources, | |
127 | # PLD Linux policy is to package only compiled .py[co] for Python 2.x libraries. | |
128 | # Author: Elan Ruusamäe <glen@pld-linux.org> | |
129 | # | |
130 | # Usage: | |
131 | # %py_postclean /usr/share/extra -x keepme.py,keeptoo.py | |
132 | # Options: | |
133 | # -x - comma separated of file basenames to preserve (rpmbuild(macros) >= 1.658) | |
134 | # ARGS - extra directories to clean for .py sources | |
135 | # | |
136 | # BuildRequires: rpmbuild(macros) >= 1.189 | |
137 | # | |
138 | %py_postclean(x:) \ | |
139 | for d in %{py_sitescriptdir} %{py_sitedir} %*; do \ | |
140 | [ ! -d "$RPM_BUILD_ROOT$d" ] || find "$RPM_BUILD_ROOT$d" -name '*.py' -print0 %{-x:| grep -ZzvE '/(%(echo '%{-x*}' | tr ',' '|'))$'} | xargs -0r -l512 rm;\ | |
141 | done \ | |
142 | %{nil} | |
143 | ||
144 | # Check syntax for Python files | |
145 | # | |
146 | # Author: Elan Ruusamäe <glen@pld-linux.org> | |
147 | # Author: Arkadiusz Miśkiewicz <arekm@pld-linux.org> | |
148 | # | |
149 | # Usage: | |
150 | # %py_lint src | |
151 | # | |
152 | # Requirements: | |
153 | # BuildRequires: python | |
154 | # BuildRequires: rpmbuild(macros) >= 1.602 | |
155 | ||
156 | %py_lint() \ | |
157 | __py_lint() { \ | |
158 | %{!?debug:set +x;} \ | |
159 | find "$@" -type f -name '*.py' | python -c ' \ | |
160 | import sys \ | |
161 | import compiler \ | |
162 | \ | |
163 | found = err = 0\ | |
164 | for f in sys.stdin: \ | |
165 | fd = open(f.rstrip()) \ | |
166 | c = fd.read() \ | |
167 | fd.close() \ | |
168 | found = found + 1 \ | |
169 | try: \ | |
170 | compiler.parse(c) \ | |
171 | except SyntaxError, e: \ | |
172 | print "py_lint: %s: %s" % (f.rstrip(), e) \ | |
173 | err = err + 1\ | |
174 | \ | |
175 | if not found:\ | |
176 | print >> sys.stderr, "\\npy_lint: ERROR: No files processed.\\n" \ | |
177 | sys.exit(1) \ | |
178 | elif err: \ | |
179 | print >> sys.stderr, "\\npy_lint: ERROR: Syntax errors in %d files.\\n" % err \ | |
180 | sys.exit(1) \ | |
181 | else: \ | |
182 | print >> sys.stderr, "py_lint: %d files scanned, found no syntax errors." % found \ | |
183 | ' \ | |
184 | }; __py_lint %* \ | |
185 | %{nil} |