Skip to content

Commit fc56d0a

Browse files
committed
- proofread style
- remove yous
1 parent 8c2c5b5 commit fc56d0a

1 file changed

Lines changed: 58 additions & 61 deletions

File tree

tutorial_scripting.rst

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,55 @@
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

4844
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.
4946

5047
^^^^^^^^^^^^^^^^^^^^^
5148
``spack find --json``
5249
^^^^^^^^^^^^^^^^^^^^^
5350

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``:
5654

5755
.. literalinclude:: outputs/scripting/find-json.out
5856
:language: console
5957

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.
58+
This command provides complete information about any spec of interest in a structured format.
59+
The output of ``spack find --json`` can be piped to JSON filtering tools like ``jq`` to extract specific information.
6260

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

6563

6664
----------------------------------------
@@ -69,15 +67,15 @@ Introducing the ``spack python`` command
6967

7068
What if we need to perform more advanced queries?
7169

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:
70+
Spack provides the ``spack python`` command to launch an interpreter with Spack's Python modules available to import.
71+
The underlying Python instance is used for all other commands.
72+
We can write scripts to:
7573

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

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

8280
.. literalinclude:: outputs/scripting/spack-python-1.out
8381
:language: console
@@ -90,23 +88,22 @@ Accessing the ``Spec`` object
9088
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9189

9290
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*.
91+
As previously mentioned, specs can be either *abstract* or *concrete*.
92+
The specs we've seen in ``package.py`` files (e.g., in the ``install()`` method) have been *concrete*, or fully specified.
93+
Specs typed on the command line have been *abstract*.
9694
Understanding the differences between the two types is key to using Spack's internal API.
9795

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

10098
.. literalinclude:: outputs/scripting/spack-python-abstract.out
10199
:language: console
102100
:emphasize-lines: 1-3,5,11,13
103101

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

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``.
104+
- an exception -- ``SpecError`` -- is raised if we try to access its ``version``
105+
- there are no associated ``versions``
106+
- the spec's operating system is ``None``
110107

111108
Without exiting the interpreter, let's concretize the spec and try again:
112109

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

117114
Notice that the concretized spec now:
118115

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

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

125122
.. literalinclude:: outputs/scripting/spack-python-sans-intermediate.out
126123
:language: console
@@ -130,23 +127,23 @@ It is not necessary to store the intermediate abstract spec -- you can use the `
130127
Querying the Spack database
131128
^^^^^^^^^^^^^^^^^^^^^^^^^^^
132129

133-
Even more powerful queries are available when we look at the information stored in the Spack database.
130+
More powerful queries are available when we look at the information stored in the Spack database.
134131
The ``Database`` object in Spack is in the ``spack.store.STORE.db`` variable.
135132
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:
133+
Let's see the documentation available for ``query()`` using Python's built-in ``help()`` function:
137134

138135
.. literalinclude:: outputs/scripting/spack-python-db-query-help.out
139136
:language: console
140-
:emphasize-lines: 1-2,9-13
137+
:emphasize-lines: 1-2,9-10
141138

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

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.
141+
Recall that ``spack find`` is limited to queries of attributes with matching values.
142+
It cannot be used to find packages that *do not* meet a specific condition.
146143

147-
We *can* use the python interface to write these types of queries.
144+
We *can* use the Python interface to write these types of queries.
148145
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.
146+
We can do this by using custom Python code and Spack database queries.
150147
We will use the ``spack.cmd.display_specs`` for output to achieve the same printing functionality as the ``spack find`` command:
151148

152149
.. literalinclude:: outputs/scripting/spack-python-db-query-exclude.out
@@ -167,10 +164,10 @@ before generalizing the functionality for reuse.
167164
Using scripts
168165
^^^^^^^^^^^^^
169166

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.
167+
Next, the script can be updated to accept arguments from the command line.
168+
By generalizing the script to take include and exclude specs as arguments, it becomes a flexible, general-purpose query tool.
172169

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

175172
.. literalinclude:: outputs/scripting/0.find_exclude.py.example
176173
:language: python
@@ -183,48 +180,48 @@ Now we can run our new script by entering the following:
183180
:language: console
184181
:emphasize-lines: 1
185182

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

188185
-------------------------------------
189186
Using the ``spack-python`` executable
190187
-------------------------------------
191188

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``?
193190

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

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``:
198195

199196
.. literalinclude:: outputs/scripting/1.find_exclude.py.example
200197
:language: python
201198
:emphasize-lines: 1
202199

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

205202
.. literalinclude:: outputs/scripting/find-exclude-2.out
206203
:language: console
207204
:emphasize-lines: 1-2
208205

209-
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.
210207
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.
211208

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

214211
.. literalinclude:: outputs/scripting/2.find_exclude.py.example
215212
:language: python
216213
:emphasize-lines: 1
217214

218-
Exit your editor and run the script again:
215+
Exit the editor and run the script again:
219216

220217
.. literalinclude:: outputs/scripting/find-exclude-3.out
221218
:language: console
222219
:emphasize-lines: 1
223220

224221
It will now work on any system with Spack installed.
225222

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.
223+
With these tools, we can create custom Spack queries and prototype new ideas.
224+
Contributions that improve or extend common Spack workflows are always welcome in the community.
228225

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

0 commit comments

Comments
 (0)