Skip to content

Commit a64eece

Browse files
committed
- proofread style
- remove yous
1 parent 520f003 commit a64eece

1 file changed

Lines changed: 58 additions & 62 deletions

File tree

tutorial_scripting.rst

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,54 @@
1010
Scripting with Spack
1111
====================
1212

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>`_.
1716

1817
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.
2018

2119
-----------------------------
2220
Scripting with ``spack find``
2321
-----------------------------
2422

2523
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.
2725

2826
^^^^^^^^^^^^^^^^^^^^^^^
2927
``spack find --format``
3028
^^^^^^^^^^^^^^^^^^^^^^^
3129

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.
3432

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.
3834

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:
4137

4238
.. literalinclude:: outputs/scripting/find-format.out
4339
:language: console
4440
:emphasize-lines: 1
4541

46-
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.
4743

48-
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``.
44+
``spack find --format`` can be combined with typical command line tools like ``sort`` or ``uniq`` to retrieve information relevant to specific workflows.
4945

5046
^^^^^^^^^^^^^^^^^^^^^
5147
``spack find --json``
5248
^^^^^^^^^^^^^^^^^^^^^
5349

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:
50+
Alternatively, we can get a serialized version of ``Spec`` objects in the `JSON` format using the ``--json`` option.
51+
52+
For example, to get attributes for all installations of ``zlib-ng``:
5653

5754
.. literalinclude:: outputs/scripting/find-json.out
5855
:language: console
5956

60-
The ``spack find --json`` command gives you everything we know about the specs in a structured format.
61-
You can pipe its output to JSON filtering tools like ``jq`` to extract just the parts you want.
57+
This command provides complete information about any spec of interest in a structured format.
58+
The output of ``spack find --json`` can be piped to JSON filtering tools like ``jq`` to extract specific information.
6259

63-
Check out the `basic usage docs <https://spack.readthedocs.io/en/latest/basic_usage.html#machine-readable-output>`_ for more examples.
60+
Visit `basic usage docs <https://spack.readthedocs.io/en/latest/basic_usage.html#machine-readable-output>`_ for more examples.
6461

6562

6663
----------------------------------------
@@ -69,15 +66,15 @@ Introducing the ``spack python`` command
6966

7067
What if we need to perform more advanced queries?
7168

72-
Spack provides the ``spack python`` command to launch a python interpreter with Spack's python modules available to import.
73-
It uses the underlying python for the rest of its commands.
74-
You can write scripts to:
69+
Spack provides the ``spack python`` command to launch an interpreter with Spack's Python modules available to import.
70+
The underlying Python instance is used for all other commands.
71+
We can write scripts to:
7572

76-
- run Spack commands;
77-
- explore abstract and concretized specs; and
78-
- directly access other internal components of Spack.
73+
- run Spack commands
74+
- explore abstract and concretized specs
75+
- directly access other internal components of Spack
7976

80-
Let's launch a Spack-aware python interpreter by entering:
77+
Let's launch a Spack-aware Python interpreter by entering:
8178

8279
.. literalinclude:: outputs/scripting/spack-python-1.out
8380
:language: console
@@ -90,23 +87,22 @@ Accessing the ``Spec`` object
9087
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9188

9289
Let's take a look at the internal representation of the Spack ``Spec``.
93-
As you already know, specs can be either *abstract* or *concrete*.
94-
The specs you've seen in ``package.py`` files (e.g., in the ``install()`` method) have been *concrete*, or fully specified.
95-
The specs you've typed on the command line have been *abstract*.
90+
As previously mentioned, specs can be either *abstract* or *concrete*.
91+
The specs we've seen in ``package.py`` files (e.g., in the ``install()`` method) have been *concrete*, or fully specified.
92+
Specs typed on the command line have been *abstract*.
9693
Understanding the differences between the two types is key to using Spack's internal API.
9794

98-
Let's open another python interpreter with ``spack python``, instantiate the ``zlib`` spec, and check a few properties of an abstract spec:
95+
Let's open another Python interpreter with ``spack python``, instantiate the ``zlib`` spec, and check a few properties of an abstract spec:
9996

10097
.. literalinclude:: outputs/scripting/spack-python-abstract.out
10198
:language: console
10299
:emphasize-lines: 1-3,5,11,13
103100

104-
Notice that there are ``Spec`` properties and methods that are not accessible to abstract specs; specifically:
101+
Notice that there are ``Spec`` properties and methods not accessible to abstract specs; specifically:
105102

106-
- an exception -- ``SpecError`` -- is raised if we try to access its
107-
``version``;
108-
- there are no associated ``versions``; and
109-
- the spec's operating system is ``None``.
103+
- an exception -- ``SpecError`` -- is raised if we try to access its ``version``
104+
- there are no associated ``versions``
105+
- the spec's operating system is ``None``
110106

111107
Without exiting the interpreter, let's concretize the spec and try again:
112108

@@ -116,11 +112,11 @@ Without exiting the interpreter, let's concretize the spec and try again:
116112

117113
Notice that the concretized spec now:
118114

119-
- has a ``version``;
120-
- has a single entry in its ``versions`` list; and
121-
- the operating system is now ``ubuntu22.04``.
115+
- has a ``version``
116+
- has a single entry in its ``versions`` list
117+
- the operating system is now ``ubuntu22.04``
122118

123-
It is not necessary to store the intermediate abstract spec -- you can use the ``.concretized()`` method as shorthand:
119+
It's not necessary to store the intermediate abstract spec, we can use the ``.concretize_one()`` method as shorthand:
124120

125121
.. literalinclude:: outputs/scripting/spack-python-sans-intermediate.out
126122
:language: console
@@ -130,23 +126,23 @@ It is not necessary to store the intermediate abstract spec -- you can use the `
130126
Querying the Spack database
131127
^^^^^^^^^^^^^^^^^^^^^^^^^^^
132128

133-
Even more powerful queries are available when we look at the information stored in the Spack database.
129+
More powerful queries are available when we look at the information stored in the Spack database.
134130
The ``Database`` object in Spack is in the ``spack.store.STORE.db`` variable.
135131
We'll interact with it mainly through the ``query()`` method.
136-
Let's see the documentation available for ``query()`` using python's built-in ``help()`` function:
132+
Let's see the documentation available for ``query()`` using Python's built-in ``help()`` function:
137133

138134
.. literalinclude:: outputs/scripting/spack-python-db-query-help.out
139135
:language: console
140-
:emphasize-lines: 1-2,9-13
136+
:emphasize-lines: 1-2,9-10
141137

142-
We will primarily make use of the ``query_spec`` argument.
138+
We'll primarily make use of the ``query_spec`` argument.
143139

144-
Recall that queries using the ``spack find`` command are limited to queries of attributes with matching values, not values they do *not* have.
145-
In other words, we cannot use the ``spack find`` command for all packages that *do not* satisfy a certain criterion.
140+
Recall that ``spack find`` is limited to queries of attributes with matching values.
141+
It cannot be used to find packages that *do not* meet a specific condition.
146142

147-
We *can* use the python interface to write these types of queries.
143+
We *can* use the Python interface to write these types of queries.
148144
For example, let's find all packages that were compiled with ``gcc`` but do not depend on ``mpich``.
149-
We can do this by using custom python code and Spack database queries.
145+
We can do this by using custom Python code and Spack database queries.
150146
We will use the ``spack.cmd.display_specs`` for output to achieve the same printing functionality as the ``spack find`` command:
151147

152148
.. literalinclude:: outputs/scripting/spack-python-db-query-exclude.out
@@ -167,10 +163,10 @@ before generalizing the functionality for reuse.
167163
Using scripts
168164
^^^^^^^^^^^^^
169165

170-
Now let's parameterize our script to accept arguments on the command line.
171-
With a few generalizations to use the include and exclude specs as arguments, we can create a powerful, general-purpose query script.
166+
Next, the script can be updated to accept arguments from the command line.
167+
By generalizing the script to take include and exclude specs as arguments, it becomes a flexible, general-purpose query tool.
172168

173-
Open a file called ``find_exclude.py`` in your preferred editor and add the following code:
169+
Open a file called ``find_exclude.py`` in a text editor and add the following code:
174170

175171
.. literalinclude:: outputs/scripting/0.find_exclude.py.example
176172
:language: python
@@ -183,48 +179,48 @@ Now we can run our new script by entering the following:
183179
:language: console
184180
:emphasize-lines: 1
185181

186-
This is beneficial for us, as long as we remember to use Spack's ``python`` command to run it.
182+
This works well, as long as we remember to use Spack's ``python`` command to run it.
187183

188184
-------------------------------------
189185
Using the ``spack-python`` executable
190186
-------------------------------------
191187

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``?
188+
What if the script needs to be shared with others, without requiring them to remember to use ``spack python``?
193189

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.
190+
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.
191+
However, there is an important limitation to be aware of, as shown in the next example.
196192

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``:
193+
Open the ``find_exclude.py`` script we created above and add the shebang line with ``spack python`` as the arguments to ``env``:
198194

199195
.. literalinclude:: outputs/scripting/1.find_exclude.py.example
200196
:language: python
201197
:emphasize-lines: 1
202198

203-
Exit our editor and add execute permissions to the script before running it as follows:
199+
Exit the editor and add execute permissions to the script before running it as follows:
204200

205201
.. literalinclude:: outputs/scripting/find-exclude-2.out
206202
:language: console
207203
:emphasize-lines: 1-2
208204

209-
If you are lucky, it worked on your system, but there is no guarantee.
205+
If we're lucky, it ran successfully, but there's no guarantee this will work for every system.
210206
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.
211207

212-
Bring up the file in your editor again and change the ``env`` argument to ``spack-python`` as follows:
208+
Bring up the file in the editor again and change the ``env`` argument to ``spack-python`` as follows:
213209

214210
.. literalinclude:: outputs/scripting/2.find_exclude.py.example
215211
:language: python
216212
:emphasize-lines: 1
217213

218-
Exit your editor and run the script again:
214+
Exit the editor and run the script again:
219215

220216
.. literalinclude:: outputs/scripting/find-exclude-3.out
221217
:language: console
222218
:emphasize-lines: 1
223219

224220
It will now work on any system with Spack installed.
225221

226-
You now have the basic tools to create your own custom Spack queries and prototype ideas.
227-
We encourage you to contribute them back to Spack in the future.
222+
With these tools, we can create custom Spack queries and prototype new ideas.
223+
Contributions that improve or extend common Spack workflows are always welcome in the community.
228224

229225
.. LocalWords: LLC Spack's APIs hdf zlib literalinclude json uniq jq
230226
.. LocalWords: docs concretized REPL API SpecError spec's py ubuntu

0 commit comments

Comments
 (0)