Skip to content

Commit 4d42b14

Browse files
authored
Merge pull request #513 from minijackson/using-python-scripts-doc
docs/ioc: rewrite Python script article to use writePython3
2 parents b796247 + b3b8646 commit 4d42b14

5 files changed

Lines changed: 152 additions & 196 deletions

File tree

docs/ioc/user-guides/testing/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
unit-testing
1111
integration-tests
12-
packaging-python-scripts
12+
python-scripts
1313
```
1414

1515
[how-to guides]: https://diataxis.fr/how-to-guides/

docs/ioc/user-guides/testing/packaging-python-scripts.md

Lines changed: 0 additions & 195 deletions
This file was deleted.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Using Python scripts
2+
3+
If you want to use Python packages in integration tests,
4+
you can use Nix's `writePython3` functions:
5+
6+
```{code-block} nix
7+
:caption: Define a Python script with dependencies
8+
9+
let
10+
myScript =
11+
pkgs.writers.writePython3 "myScript"
12+
{
13+
libraries = with pkgs.python3Packages; [
14+
p4p
15+
# Other packages...
16+
];
17+
}
18+
''
19+
# The actual Python script
20+
from p4p.nt import NTScalar
21+
from p4p.server import Server
22+
from p4p.server.thread import SharedPV
23+
24+
pv = SharedPV(nt=NTScalar("d"), initial=0.0)
25+
26+
...
27+
28+
Server.forever(providers=[{"demo:pv:name": pv}])
29+
'';
30+
in ...
31+
```
32+
33+
Two variants of this function exists:
34+
35+
`writePython3`
36+
: Creates a Python script as a single file in {samp}`${out}`.
37+
38+
`writePython3Bin`
39+
: Creates a Python script under {samp}`${out}/bin/${name}`.
40+
41+
## As an installed package
42+
43+
Since `writePython3Bin` creates a Python script under the `bin` folder,
44+
it is more suited for installing packages
45+
in the `environment.systemPackages` NixOS option:
46+
47+
```{code-block} nix
48+
:caption: Installing a Python script as a package
49+
50+
{ lib, pkgs, ... }:
51+
{
52+
nodes.machine = {
53+
environment.systemPackages = [
54+
(pkgs.writers.writePython3 "p4p-client"
55+
{
56+
libraries = [ pkgs.python3Packages.p4p ];
57+
}
58+
''
59+
from p4p.client.thread import Context
60+
61+
ctxt = Context('pva')
62+
v = ctxt.get('demo:pv:name')
63+
''
64+
)
65+
];
66+
};
67+
68+
testScript = ''
69+
start_all()
70+
...
71+
machine.succeed("p4p-client")
72+
'';
73+
}
74+
```
75+
76+
## Calling it in directly in test script
77+
78+
For calling the script directly,
79+
`writePython3` is more direct to call than its `Bin` counterpart:
80+
81+
```{code-block} nix
82+
:caption: Using a Python script directly in the test script
83+
84+
{ lib, pkgs, ... }:
85+
{
86+
nodes.machine = {
87+
# ...
88+
};
89+
90+
testScript =
91+
let
92+
p4p-client =
93+
pkgs.writers.writePython3 "p4p-client"
94+
{
95+
libraries = [ pkgs.python3Packages.p4p ];
96+
}
97+
''
98+
from p4p.client.thread import Context
99+
100+
ctxt = Context('pva')
101+
v = ctxt.get('demo:pv:name')
102+
'';
103+
in
104+
''
105+
start_all()
106+
...
107+
machine.succeed("${p4p-client}")
108+
'';
109+
}
110+
```
111+
112+
## As a systemd service
113+
114+
`writePython3` is again more direct to call than its `Bin` counterpart
115+
and can be used directly as an `ExecStart=` argument:
116+
117+
```{code-block} nix
118+
:caption: Using a Python script as a systemd service
119+
120+
{ lib, pkgs, ... }:
121+
{
122+
nodes.machine = {
123+
systemd.services.p4p-server = {
124+
wantedBy = [ "multi-user.target" ];
125+
wants = [ "network-online.target" ];
126+
after = [ "network-online.target" ];
127+
serviceConfig.ExecStart =
128+
pkgs.writers.writePython3 "p4p-server" { libraries = [ pkgs.python3Packages.p4p ]; }
129+
''
130+
from p4p.nt import NTScalar
131+
from p4p.server import Server
132+
from p4p.server.thread import SharedPV
133+
134+
pv = SharedPV(nt=NTScalar("d"), initial=0.0)
135+
136+
...
137+
138+
Server.forever(providers=[{"demo:pv:name": pv}])
139+
'';
140+
};
141+
};
142+
143+
testScript = ''
144+
...
145+
'';
146+
}
147+
```

docs/redirects.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
"pre-requisites.md" "prerequisites.md"
66
"nixos-services/user-guides/pre-requisites.md" "nixos-services/user-guides/prerequisites.md"
77
"ioc/references/packages" "pkgs/packages.md"
8+
"ioc/user-guides/testing/packaging-python-scripts.md" "ioc/user-guides/testing/python-scripts.md"

docs/release-notes/2605.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ No breaking change were introduced in this EPNix release.
1010
## New features and highlights
1111

1212
## Documentation
13+
14+
- The {doc}`../ioc/user-guides/testing/python-scripts` article was rewritten
15+
and is now much simpler.

0 commit comments

Comments
 (0)