1{ lib
2, stdenv
3, buildPythonPackage
4, fetchFromGitHub
5, fetchpatch
6, fetchurl
7, pythonOlder
8, substituteAll
9
10# build
11, postgresql
12, setuptools
13
14# propagates
15, backports-zoneinfo
16, typing-extensions
17
18# psycopg-c
19, cython_3
20, tomli
21
22# docs
23, furo
24, shapely
25, sphinxHook
26, sphinx-autodoc-typehints
27
28# tests
29, anyio
30, pproxy
31, pytest-randomly
32, pytestCheckHook
33, postgresqlTestHook
34}:
35
36let
37 pname = "psycopg";
38 version = "3.1.9";
39
40 src = fetchFromGitHub {
41 owner = "psycopg";
42 repo = pname;
43 rev = "refs/tags/${version}";
44 hash = "sha256-yRb6yRpX1vDmXpYu4O50MYMpP2j75aSqhXCWMF1xVH0=";
45 };
46
47 patches = [
48 (substituteAll {
49 src = ./ctypes.patch;
50 libpq = "${postgresql.lib}/lib/libpq${stdenv.hostPlatform.extensions.sharedLibrary}";
51 libc = "${stdenv.cc.libc}/lib/libc.so.6";
52 })
53 ];
54
55 baseMeta = {
56 changelog = "https://github.com/psycopg/psycopg/blob/${version}/docs/news.rst#current-release";
57 homepage = "https://github.com/psycopg/psycopg";
58 license = lib.licenses.lgpl3Plus;
59 maintainers = with lib.maintainers; [ hexa ];
60 };
61
62 psycopg-c = buildPythonPackage {
63 pname = "${pname}-c";
64 inherit version src;
65 format = "pyproject";
66
67 # apply patches to base repo
68 inherit patches;
69
70 # move into source root after patching
71 postPatch = ''
72 cd psycopg_c
73 '';
74
75 nativeBuildInputs = [
76 cython_3
77 postgresql
78 setuptools
79 tomli
80 ];
81
82 # tested in psycopg
83 doCheck = false;
84
85 meta = baseMeta // {
86 description = "C optimisation distribution for Psycopg";
87 };
88 };
89
90 psycopg-pool = buildPythonPackage {
91 pname = "${pname}-pool";
92 inherit version src;
93 format = "setuptools";
94
95 # apply patches to base repo
96 inherit patches;
97
98 # move into source root after patching
99 postPatch = ''
100 cd psycopg_pool
101 '';
102
103 propagatedBuildInputs = [
104 typing-extensions
105 ];
106
107 # tested in psycopg
108 doCheck = false;
109
110 meta = baseMeta // {
111 description = "Connection Pool for Psycopg";
112 };
113 };
114
115in
116
117buildPythonPackage rec {
118 inherit pname version src;
119 format = "pyproject";
120
121 disabled = pythonOlder "3.7";
122
123 outputs = [
124 "out"
125 "doc"
126 ];
127
128 sphinxRoot = "../docs";
129
130 # Introduce this file necessary for the docs build via environment var
131 LIBPQ_DOCS_FILE = fetchurl {
132 url = "https://raw.githubusercontent.com/postgres/postgres/REL_14_STABLE/doc/src/sgml/libpq.sgml";
133 hash = "sha256-yn09fR9+7zQni8SvTG7BUmYRD7MK7u2arVAznWz2oAw=";
134 };
135
136 inherit patches;
137
138 # only move to sourceRoot after patching, makes patching easier
139 postPatch = ''
140 cd psycopg
141 '';
142
143 nativeBuildInputs = [
144 furo
145 setuptools
146 shapely
147 sphinx-autodoc-typehints
148 sphinxHook
149 ];
150
151 propagatedBuildInputs = [
152 psycopg-c
153 typing-extensions
154 ] ++ lib.optionals (pythonOlder "3.9") [
155 backports-zoneinfo
156 ];
157
158 pythonImportsCheck = [
159 "psycopg"
160 "psycopg_c"
161 "psycopg_pool"
162 ];
163
164 passthru.optional-dependencies = {
165 c = [ psycopg-c ];
166 pool = [ psycopg-pool ];
167 };
168
169 nativeCheckInputs = [
170 anyio
171 pproxy
172 pytest-randomly
173 pytestCheckHook
174 postgresql
175 ]
176 ++ lib.optional (stdenv.isLinux) postgresqlTestHook
177 ++ passthru.optional-dependencies.c
178 ++ passthru.optional-dependencies.pool;
179
180 env = {
181 postgresqlEnableTCP = 1;
182 PGUSER = "psycopg";
183 };
184
185 preCheck = ''
186 cd ..
187 '' + lib.optionalString (stdenv.isLinux) ''
188 export PSYCOPG_TEST_DSN="host=127.0.0.1 user=$PGUSER"
189 '';
190
191 disabledTests = [
192 # don't depend on mypy for tests
193 "test_version"
194 "test_package_version"
195 ];
196
197 disabledTestPaths = [
198 # Network access
199 "tests/test_dns.py"
200 "tests/test_dns_srv.py"
201 # Mypy typing test
202 "tests/test_typing.py"
203 "tests/crdb/test_typing.py"
204 ];
205
206 pytestFlagsArray = [
207 "-o" "cache_dir=$TMPDIR"
208 "-m" "'not timing'"
209 ];
210
211 postCheck = ''
212 cd ${pname}
213 '';
214
215 passthru = {
216 c = psycopg-c;
217 pool = psycopg-pool;
218 };
219
220 meta = baseMeta // {
221 description = "PostgreSQL database adapter for Python";
222 };
223}