Skip to content

Commit

Permalink
Merge pull request lunaflight#25 from lunaflight/add-scripts
Browse files Browse the repository at this point in the history
Add scripts
  • Loading branch information
lunaflight authored Aug 4, 2024
2 parents a945b08 + d635e1b commit 4443000
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 74 deletions.
96 changes: 22 additions & 74 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Read the corresponding `README.md` files in the respective subdirectories for mo
### About

- The project uses Python 3.9 syntax. (See `Branches` for more information.)
- The project assumes no setup of virtual environments. If you know how to
manage your own virtual environment, then remove the activation and
deactivation of the venv of scripts in `scripts/`.

### Installation

Expand All @@ -46,43 +49,20 @@ git clone https://github.com/lunaflight/courserekt.git &&
cd courserekt
```

Setting up the virual environment:
```sh
python -m venv venv &&
echo "export PYTHONPATH=\$(pwd)" >> venv/bin/activate &&
source venv/bin/activate &&
pip install -r local-requirements.txt &&
```
**Important:** Replace the link for `git clone` with the link of your repository, if you have forked it.

Generating all intermediate files and binaries:
Building the project (only run this once):
```sh
python -m src.history.build
source scripts/init.sh
```

**Important:** Replace the link for `git clone` with the link of your repository, if you have forked it.

**Please be patient. This might take some time (around 2 minutes, on my machine).**

**An explanation:**
1. First, we clone the repository from GitHub.

2. Then, we set up a virtual environment with `python -m venv venv`, as a good practice when working with libraries and projects.

3. We set up `PYTHONPATH` upon activating the virtual environment, to fix issues of being unable to find `src`.
- Upon activating `venv` every time in the future, `PYTHONPATH` will be set up.
Note that this utilises [tabula-py](https://pypi.org/project/tabula-py/), a thin Python wrapper around [tabula-java](https://github.com/tabulapdf/tabula-java), a Java library for extracting tables from PDF files. You may find yourself needing to install Java:
- **ArchLinux:** `sudo pacman -S jre-openjdk-headless` should suffice. [Variants found here.](https://wiki.archlinux.org/title/java)
- **Fedora Linux:** Please refer to [your distribution's installation instructions](https://docs.fedoraproject.org/en-US/quick-docs/installing-java/). Choose an appropriate minimal (or more) JRE to install.

4. We install all the required dependencies as outlined in `local-requirements.txt`, to ensure everybody works with the same dependencies.
- You may run into issues during `pip install` if you do not have the latest version of python.
- You may just continue from this command (and not run all commands from `git clone`) if it fails.

5. We run `python -m src.history.build` to generate `database.db`.
- Note that this utilises [tabula-py](https://pypi.org/project/tabula-py/), a thin Python wrapper around [tabula-java](https://github.com/tabulapdf/tabula-java), a Java library for extracting tables from PDF files. You may find yourself needing to install Java:
- **ArchLinux:** `sudo pacman -S jre-openjdk-headless` should suffice. [Variants found here.](https://wiki.archlinux.org/title/java)
- **Fedora Linux:** Please refer to [your distribution's installation instructions](https://docs.fedoraproject.org/en-US/quick-docs/installing-java/). Choose an appropriate minimal (or more) JRE to install.

Note: If at any point, one of these commands fail (such as being unable to install the dependencies), you may have to rectify that command first, before continuing with the rest of the installation script.

### Virtual Environment
### Virtual Environment & Dependencies
A virtual environment ensures that everyone is working with the same set of dependencies.

The dependencies described in `local-requirements.txt` describe all the dependencies used in the development of the project.
Expand All @@ -95,64 +75,32 @@ The dependencies described in `local-requirements.txt` describe all the dependen

`requirements.txt`, on the other hand, is used for deployment on Vercel, which only allows 250MB of libraries to be imported. Hence, we keep the number of libraries to a minimum in `requirements.txt`.

**Activate the virtual environment:**
- Activating:
- **Windows:** `venv\Scripts\activate`
- **Mac/Linux:** `source venv/bin/activate`
- You should see the `(venv)` prefix in your command prompt.

**Deactivate the virtual environment:**
- `deactivate`
- You should see the `(venv)` prefix disappear in your command prompt.

### Branches
- `main` refers to the branch being deployed on Vercel which supports only Python 3.9 syntax.
- `py312-github-pages` refers to the branch that employs Python 3.12 syntax in preparation to move to GitHub Pages.

### Web App

To start the web app, navigate to the **project root** and do the following:

```shell
python -m src.web.main
To start the web app, run the following:
```sh
source scripts/main.sh
```

This will precompute and cache all pages.
Optionally, you may supply the following to `python -m src.web.main`.
- `-p`, `--port PORT`: Port where the app is run. Otherwise, it defaults to `5000`.
- `-s`, `--skip-precompute`: Use the existing files in `static/pages` to load the HTML instead.

After running the command, open a web browser and navigate to `http://localhost:5000/`.

### Unit Testing

This project employs the built-in `unittest` module in Python for automated testing.
This will take some time as it precomputes and caches all pages.

To run the tests, navigate to the **project root** of this project, and run:

```shell
python -m unittest
You may instead run the underlying command directly.
```sh
python -m src.web.main --help
```

### Static Code Analysis

First, ensure Mypy is installed. If it is not, run `pip install mypy` or `python -m pip install mypy`.
### Sanity Checks

To run the analysis, navigate to the **project root** of this project, and run:

```shell
python -m mypy --strict .
Run the following to ensure run unit testing, static code analysis and the style checker.
```sh
source scripts/checks.sh
```

### Style Checker

First, ensure Ruff is installed. If it is not, run `pip install ruff` or `python -m pip install ruff`.

To run the checker, navigate to the **project root** of this project, and run:

```shell
python -m ruff check
```
This uses `unittest`, `mypy` and `ruff`.

### Adding New CourseReg Data
When new data is released, the program can be updated easily. You can follow the following steps to update it.
Expand Down
12 changes: 12 additions & 0 deletions scripts/checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

main ()
{
python -m unittest &&
python -m mypy --strict . &&
python -m ruff check --statistics
}

source venv/bin/activate
main
deactivate
18 changes: 18 additions & 0 deletions scripts/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

main ()
{
pip install -r local-requirements.txt &&
python -m src.history.build
}

# Create virtual environment
python -m venv venv
if [ $? -ne 0 ]; then
echo "Error creating virtual environment"
exit 1
fi

source venv/bin/activate
main
deactivate
10 changes: 10 additions & 0 deletions scripts/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

main ()
{
python -m src.web.main
}

source venv/bin/activate
main
deactivate

0 comments on commit 4443000

Please sign in to comment.