{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [], "user_expressions": [] }, "source": [ "# Lab Session 8: Automation and Make\n", "\n", "* **Statistics 159/259, Spring 2022**\n", "* Prof. F. Pérez and GSI F. Sapienza, Department of Statistics, UC Berkeley.\n", "* 04/07/2023\n", "\n", "**Acknowledgment:** The contents we are following for this course is based on the amazing tutorial about Automation and Make created by [The Carpentries](https://carpentries.org)" ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "Make is called a **build tool**: it builds files, plots, papers, etc. Today we are going to do everything from the the shell. If you haven't done the setup for the tutorial during last lecture, please follow the [setup page](https://swcarpentry.github.io/make-novice/setup) in order to start working with the contents of the tutorial. " ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "## 0. Setup\n", "\n", "Download the files form the [setup page](https://swcarpentry.github.io/make-novice/setup.html) in the Carpentries tutorial.\n", "We will start from the following version of our `Makefile` created inside the folder `make-lesson` provided in the tutorial.) you will find the data and scripts for this exercise. Add the corresponding files to your JupyterHub session. " ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "We will start working from the following template `Makefile`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
# Count words.\n",
       "\n",
       "isles.dat : books/isles.txt\n",
       "\tpython countwords.py books/isles.txt isles.dat\n",
       "\n",
       "abyss.dat : books/abyss.txt\n",
       "\tpython countwords.py books/abyss.txt abyss.dat\n",
       "\n",
       ".PHONY : clean\n",
       "clean :\n",
       "\trm -f *.dat\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{c}{\\PYZsh{} Count words.}\n", "\n", "\\PY{n+nf}{isles.dat }\\PY{o}{:} \\PY{n}{books}/\\PY{n}{isles}.\\PY{n}{txt}\n", "\tpython countwords.py books/isles.txt isles.dat\n", "\n", "\\PY{n+nf}{abyss.dat }\\PY{o}{:} \\PY{n}{books}/\\PY{n}{abyss}.\\PY{n}{txt}\n", "\tpython countwords.py books/abyss.txt abyss.dat\n", "\n", "\\PY{n+nf}{.PHONY }\\PY{o}{:} \\PY{n}{clean}\n", "\\PY{n+nf}{clean }\\PY{o}{:}\n", "\trm \\PYZhy{}f *.dat\n", "\\end{Verbatim}\n" ], "text/plain": [ "# Count words.\n", "\n", "isles.dat : books/isles.txt\n", "\tpython countwords.py books/isles.txt isles.dat\n", "\n", "abyss.dat : books/abyss.txt\n", "\tpython countwords.py books/abyss.txt abyss.dat\n", "\n", ".PHONY : clean\n", "clean :\n", "\trm -f *.dat" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython import display\n", "display.Code('make-lesson/Makefile')" ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "## 1. Recap" ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "Before continuing, be sure to know the answer to the following questions.\n", "\n", "1. What is a phony target? When and how to use them?\n", "1. What does `$^`, `$<` and `$@` designate?\n", "1. What happen is we modify the Python script we used to generate data?\n", "1. What does it means to do a dry run of your make file?\n", "1. How do you execute a make file? Does the name of the file needs to be `Makefile`?\n", "1. Try to make a schematic plot of the tree of dependencies of this makefiles." ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "## 2. Growing our Makefile\n", "\n", "1. Add a new `.PHONY` variable at the top of `Makefile` called `outputs` that generates both `isles.dat` and `abyss.dat`. \n", "2. Add more commands for generating `last.dat` and `sierra.dat`.\n", "3. Replace the name of target and dependencies files using the special characters `$<`, `$@`.\n", "4. Replace the repeated commands by using a [pattern rule](https://swcarpentry.github.io/make-novice/05-patterns/index.html). \n", " 1. What does `%` designates?\n", " 1. What does `$*` do and how to use it?\n", " 1. When can and cannot use or the other?\n", "5. Add a `clean` command that removes all the `.dat` files. " ] }, { "cell_type": "markdown", "metadata": { "user_expressions": [] }, "source": [ "## [Extra] Variables and functions\n", "\n", "All this exercises can be found in the [Variables](https://swcarpentry.github.io/make-novice/06-variables/index.html)\n", "and [Functions](https://swcarpentry.github.io/make-novice/07-functions/index.html) chapters. \n", "\n", "1. Update `Makefile` so that the `%.dat` rule references the variables `COUNT_SRC` and `COUNT_EXE`. Then do the same for the `testzipf.py` script and the `results.txt` rule, using `ZIPF_SRC` and `ZIPF_EXE` as variable names.\n", "1. Move the varaible declaration to a new file `config.mk` that you import into `Makefiles`. What happens when you touch `config.mk` and then `make` again? Why? Try changing `LANGUAGE=python` to `LANGUAGE=python3` to see if there is any difference. \n", "1. Follow the functions tutorial to simplify all the unnecessary explicit syntax in `Makefile`. This includes the use of both function `wildcard` and `patsubst`. If you feel adventurous, you can add more books to `books` and test Zipf's Law. \n", "1. Explore how to add documentation to the makefiles. " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 4 }