You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial_scripting.rst
+58-61Lines changed: 58 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,57 +10,55 @@
10
10
Scripting with Spack
11
11
====================
12
12
13
-
This tutorial introduces advanced Spack features related to scripting.
14
-
Specifically, we will show you how to write scripts using ``spack find`` and ``spack python``.
15
-
Earlier sections of the tutorial demonstrated using ``spack find`` to list and search installed packages.
16
-
The ``spack python`` command gives you access to all of Spack's `internal APIs <https://spack.readthedocs.io/en/latest/spack.html>`_, allowing you to write more complex queries, for example.
13
+
This tutorial introduces advanced scripting features available in Spack, using the ``spack find`` and ``spack python`` commands.
14
+
We've already seen how to list and search installed packages with ``spack find``.
15
+
The ``spack python`` command allows us to write more complex queries, as it gives access to all of Spack's `internal APIs <https://spack.readthedocs.io/en/latest/spack.html>`_.
17
16
18
17
Since Spack has an extensive API, we'll only scratch the surface here.
19
-
We'll give you enough information to start writing your own scripts and to find what you need, with a little digging.
20
18
21
19
-----------------------------
22
20
Scripting with ``spack find``
23
21
-----------------------------
24
22
25
23
The output we've seen from ``spack find`` has been for human consumption.
26
-
But you can take advantage of some advanced options of the command to generate machine-readable output suitable for piping to a script.
24
+
We can take advantage of the command's advanced features to generate machine-readable output suitable for piping to a script.
27
25
28
26
^^^^^^^^^^^^^^^^^^^^^^^
29
27
``spack find --format``
30
28
^^^^^^^^^^^^^^^^^^^^^^^
31
29
32
-
The main job of ``spack find`` is to show the user a bunch of concrete specs that correspond to installed packages.
33
-
By default, we display them with some default attributes, like the ``@version`` suffix you're used to seeing in the output.
30
+
The main function of ``spack find`` is to display concrete specs that correspond to installed packages.
31
+
By default, they are shown with default attributes, like the ``@version`` suffix.
34
32
35
-
The ``--format`` argument allows you to display the specs however you choose, using custom format strings.
36
-
Format strings let you specify the names of particular *parts* of the specs you want displayed.
37
-
Let's examine the first option.
33
+
The ``--format`` argument allows us to display the specs using custom format strings.
38
34
39
-
Suppose you only want to display the *name*, *version*, and first ten (10) characters of the *hash* for every package installed in your Spack instance.
40
-
You can generate that output with the following command:
35
+
Suppose we only want to see the *name*, *version*, and first ten (10) characters of the *hash* for every package installed in the Spack instance.
36
+
This output can be generated with the following command:
Note that ``name``, ``version``, and ``hash`` are attributes of Spack's internal ``Spec`` object and enclosing them in braces ensures they are output according to your format string.
42
+
Note that ``name``, ``version``, and ``hash`` are attributes of Spack's internal ``Spec`` object and enclosing them in braces ensures they are output according to the format string.
47
43
48
44
Using ``spack find --format`` allows you to retrieve just the information you need to do things like pipe the output to typical UNIX command line tools like ``sort`` or ``uniq``.
45
+
``spack find --format`` can be combined with typical command line tools like ``sort`` or ``uniq`` to retrieve information relevant to specific workflows.
49
46
50
47
^^^^^^^^^^^^^^^^^^^^^
51
48
``spack find --json``
52
49
^^^^^^^^^^^^^^^^^^^^^
53
50
54
-
Alternatively, you can get a serialized version of Spec objects in the `JSON` format using the ``--json`` option.
55
-
For example, you can get attributes for all installations of ``zlib-ng`` by entering:
51
+
Alternatively, we can get a serialized version of ``Spec`` objects in the `JSON` format using the ``--json`` option.
52
+
53
+
For example, to get attributes for all installations of ``zlib-ng``:
@@ -183,48 +180,48 @@ Now we can run our new script by entering the following:
183
180
:language: console
184
181
:emphasize-lines: 1
185
182
186
-
This is beneficial for us, as long as we remember to use Spack's ``python`` command to run it.
183
+
This works well, as long as we remember to use Spack's ``python`` command to run it.
187
184
188
185
-------------------------------------
189
186
Using the ``spack-python`` executable
190
187
-------------------------------------
191
188
192
-
What if we want to make our script available for others to use without the hassle of having to remember to use ``spack python``?
189
+
What if the script needs to be shared with others, without requiring them to remember to use ``spack python``?
193
190
194
-
We can take advantage of the shebang line typically added as the first line of python executable files.
195
-
There is a catch, as we will soon see.
191
+
This can be done by adding a shebang line as the first line of the Python script, which allows it to be run as an executable.
192
+
However, there is an important limitation to be aware of, as shown in the next example.
196
193
197
-
Open the ``find_exclude.py`` script we created above in your preferred editor and add the shebang line with ``spack python`` as the arguments to ``env``:
194
+
Open the ``find_exclude.py`` script we created above and add the shebang line with ``spack python`` as the arguments to ``env``:
If you are lucky, it worked on your system, but there is no guarantee.
206
+
If we're lucky, it ran successfully, but there's no guarantee this will work for every system.
210
207
Some systems only support a single argument on the shebang line (see `here <https://www.in-ulm.de/~mascheck/various/shebang/>`_). ``spack-python``, which is a wrapper script for ``spack python``, solves this issue.
211
208
212
-
Bring up the file in your editor again and change the ``env`` argument to ``spack-python`` as follows:
209
+
Bring up the file in the editor again and change the ``env`` argument to ``spack-python`` as follows:
0 commit comments