|
| 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 | +``` |
0 commit comments