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
+70-65Lines changed: 70 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,49 +11,51 @@
11
11
Scripting with Spack
12
12
====================
13
13
14
-
This tutorial introduces advanced Spack features related to scripting.
15
-
Specifically, we will show you how to write scripts using ``spack find`` and ``spack python``.
16
-
Earlier sections of the tutorial demonstrated using ``spack find`` to list and search installed packages.
17
-
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.
14
+
This tutorial introduces advanced Spack features related to scripting.
15
+
Specifically, we'll show how to write scripts using ``spack find`` and ``spack python``.
18
16
19
-
Since Spack has an extensive API, we'll only scratch the surface here.
20
-
We'll give you enough information to start writing your own scripts and to find what you need, with a little digging.
17
+
Earlier sections demonstrated using ``spack find`` to list and search installed packages.
18
+
The ``spack python`` command provides access to all of Spack's `internal APIs <https://spack.readthedocs.io/en/latest/spack.html>`_, allowing you to write more complex queries.
19
+
20
+
Since Spack has an extensive API, we'll only scratch the surface here.
21
+
Our goal is to give you enough information to start writing your own scripts and to help you discover what you need—with a little digging.
21
22
22
23
-----------------------------
23
24
Scripting with ``spack find``
24
25
-----------------------------
25
26
26
-
So far, the output we've seen from ``spack find`` has been for human consumption.
27
-
But you can take advantage of some advanced options of the command to generate machine-readable output suitable for piping to a script.
27
+
So far, the output we've seen from ``spack find`` has been intended for human consumption.
28
+
However, the command also provides options for generating machine-readable output that can be piped into scripts.
28
29
29
30
^^^^^^^^^^^^^^^^^^^^^^^
30
31
``spack find --format``
31
32
^^^^^^^^^^^^^^^^^^^^^^^
32
33
33
-
The main job of ``spack find`` is to show the user a bunch of concrete specs that correspond to installed packages.
34
-
By default, we display them with some default attributes, like the ``@version`` suffix you're used to seeing in the output.
34
+
The main purpose of ``spack find`` is to display information about concrete specs corresponding to installed packages.
35
+
By default, it shows these specs with a set of standard attributes, such as the familiar ``@version`` suffix.
36
+
37
+
The ``--format`` argument allows you to customize the output string for each found package.
38
+
Format strings let you specify which *parts* of each spec you want to display.
35
39
36
-
The ``--format`` argument allows you to display the specs however you choose, using custom format strings.
37
-
Format strings let you specify the names of particular *parts* of the specs you want displayed.
38
-
Let's see the first option in action.
40
+
Let's see this option in action.
39
41
40
-
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.
42
+
Suppose you only want to display the *name*, *version*, and the first ten (10) characters of the *hash* for every package installed in your Spack instance.
41
43
You can generate that output 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.
49
+
Note that ``name``, ``version``, and ``hash`` are attributes of Spack's internal ``Spec`` object, and enclosing them in braces ensures they are rendered according to your format string.
48
50
49
-
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``.
51
+
Using ``spack find --format`` allows you to retrieve only the information you need—making it easy to pipe the output into standard UNIX command-line tools like ``sort`` or ``uniq``.
50
52
51
53
^^^^^^^^^^^^^^^^^^^^^
52
54
``spack find --json``
53
55
^^^^^^^^^^^^^^^^^^^^^
54
56
55
-
Alternatively, you can get a serialized version of Spec objects in the `JSON` format using the ``--json`` option.
56
-
For example, you can get attributes for all installations of ``zlib-ng`` by entering:
57
+
Alternatively, you can get a serialized version of ``Spec`` objects in `JSON` format using the ``--json`` option.
58
+
For example, to retrieve attributes for all installations of ``zlib-ng``, you can run:
Since we are in a python interpreter, use ``exit()`` to end the session and return to the terminal.
90
+
Since we are in a Python interpreter, use ``exit()`` to end the session and return to the terminal.
89
91
90
92
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91
93
Accessing the ``Spec`` object
92
94
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
93
95
94
-
Now let's take a look at the internal representation of the Spack ``Spec``.
95
-
As you already know, specs can be either *abstract* or *concrete*.
96
-
The specs you've seen in ``package.py`` files (e.g., in the ``install()`` method) have been *concrete*, or fully specified.
97
-
The specs you've typed on the command line have been *abstract*.
98
-
Understanding the differences between the two types is key to using Spack's internal API.
96
+
Now let's take a look at the internal representation of the Spack ``Spec``.
97
+
As you may already know, specs can be either *abstract* or *concrete*.
98
+
The specs you've seen in ``package.py`` files (e.g., in the ``install()`` method) have been *concrete*—fully specified with resolved dependencies, compilers, and variants.
99
+
In contrast, the specs you've typed on the command line have been *abstract*.
99
100
100
-
Let's open another python interpreter with ``spack python``, instantiate the ``zlib`` spec, and check a few properties of an abstract spec:
101
+
Understanding the difference between these two forms is key to effectively using Spack's internal API.
102
+
103
+
Let's open another Python interpreter using ``spack python``, instantiate the ``zlib`` spec, and inspect a few properties of an abstract spec:
We will primarily make use of the ``query_spec`` argument.
145
148
146
-
Recall that queries using the ``spack find`` command are limited to queries of attributes with matching values, not values they do *not* have.
147
-
In other words, we cannot use the ``spack find`` command for all packages that *do not* satisfy a certain criterion.
149
+
Recall that queries using the ``spack find`` command are primarily intended to *match* packages based on specified criteria.
150
+
However, it's more difficult to use ``spack find`` for queries that involve *excluding* packages based on complex logic (e.g., “does *not* depend on X”).
151
+
152
+
Using the Python interface, we *can* write these more advanced queries.
153
+
For example, let's find all packages that were compiled with ``gcc`` but do **not** depend on ``mpich``.
154
+
155
+
We'll use ``spack.cmd.display_specs`` to print the results, replicating the display behavior of the ``spack find`` command:
148
156
149
-
We *can* use the python interface to write these types of queries.
150
-
For example, let's find all packages that were compiled with ``gcc`` but do not depend on ``mpich``.
151
-
We can do this by using custom python code and Spack database queries.
152
-
We will use the ``spack.cmd.display_specs`` for output to achieve the same printing functionality as the ``spack find`` command:
Notice we added importing and using the system package (``sys``) to access the first and second commandline arguments.
184
+
Notice that we've imported the ``sys`` module and used it to access the first and second command-line arguments.
181
185
182
186
Now we can run our new script by entering the following:
183
187
@@ -191,12 +195,12 @@ This is *great* for us, as long as we remember to use Spack's ``python`` command
191
195
Using the ``spack-python`` executable
192
196
-------------------------------------
193
197
194
-
What if we want to make our script available for others to usewithout the hassle of having to remember to use ``spack python``?
198
+
What if we want to make our script available for others to use—without requiring them to remember to run it with ``spack python``?
195
199
196
-
We can take advantage of the shebang line typically added as the first line of python executable files.
197
-
But there is a catch, as we will soon see.
200
+
We can take advantage of a shebang line, typically included as the first line in executable Python scripts.
201
+
However, there's a catch, as we'll see shortly.
198
202
199
-
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``:
203
+
Open the ``find_exclude.py`` script you created earlier in your preferred editor, and add a shebang line that invokes ``spack python`` using ``env``:
@@ -208,25 +212,26 @@ Then exit our editor and add execute permissions to the script before running it
208
212
:language: console
209
213
:emphasize-lines: 1-2
210
214
211
-
If you are lucky, it worked on your system, but there is no guarantee.
212
-
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.
215
+
If you're lucky, the script worked on your system—but there's no guarantee.
216
+
Some systems only support a single argument on the shebang line (see `here <https://www.in-ulm.de/~mascheck/various/shebang/>`_).
217
+
``spack-python``, a wrapper script for ``spack python``, solves this limitation.
213
218
214
-
Bring up the file in your editor again and change the ``env`` argument to ``spack-python`` as follows:
219
+
Open the script in your editor again and change the ``env`` argument to ``spack-python`` as shown below:
0 commit comments