CUCC Expedition Handbook

Troggle - software development laptop

python logo

Software development machine

For troggle itself, you need a linux machine. We all use Debian or Debian-derived machines (Debian itself, Ubuntu, Xubuntu etc.) but other forms of linux may work. Running Debian or Ubuntu under WSL on Windows 10 or 11 works fine.

If you want to work on the troggle source code and be able to commit to the server git repo, your account will need to be added to the troggle project members list. Contact wookey at wookware dot org to get this set up.

Before you start

Make sure you are familiar with the debugging tools already built into troggle. You may not need to write any new software for what you need to do. Look at the tools and reports listed on the Troggle Control Panel page (needs 'expoadmin' login password.)

Maybe what you need is simply some fixes or workarounds in the imported data files, not in the python code.

Installing linux

debian logo

If you don't already know how to do this, then you should probably not be attempting to work on the troggle code. But in case you are an experienced linux user who has always had someone else set up the system for them, then Ubuntu is the easiest and more forgiving to install, either directly on the computer or inside WSL on Windows. Ubuntu installs python and various essential python dev tools by default.

The server is running fairly old, stable releases of Debian and Django, but you will probably want Ubuntu-24.04 which is compatible.

Before you do anything else, get yourself set up with a key-pair to access the software on the expo server properly.

Prerequisites

You need to already know really quite a lot about what troggle does, and how it is used in practice. Also you will have installed git and set up the key-pair as documented in your bulk update laptop configuration. In particular read the information there about VS code and git.

Short-cut

If you have never done any bulk updates, and don't understand git, but want to look at the code and maybe run it, then there is a shortcut. You do not actually need to do all the Bulk Update machine configuration first.

You can download the code without having a key-pair like this:

mkdir ~/expo
cd ~/expo
git clone http://expo.survex.com/repositories/troggle/.git trog-temp
This will create ~/expo/trog-temp/ and get you a copy of the configuration scripts but you need to make them executable before they will work:
mv ~/expo/trog-temp/_deploy/wsl/*.sh ~/expo
cd ~/expo
chmod +x *.sh
rm -fr trog-temp
Now go to scripts.
[ Yes it's a pain having to clone the whole repo just to get only 4 scripts. But getting a repo in this way gets it in a config that means you can't work with it (maybe a git nerd can edit this page to tell us how to do that). We have to clone it properly using ssh to do that . Alternatively you can get the first script by cutting and pasting from the webpage at expo.survex.com/repositories/troggle/.git/tree/_deploy/wsl/os-trog24.04.sh ]

Read more here..

This page is a work in progess. Text will be moved here from

expo.survex.com/repositories/troggle/.git/tree/README

Directory (folder) structure

This is up to you of course, but a suggested structure is like this ("~" means your own user home folder, e.g. /home/philip/):

Do familiarise yourself with the directory structure on the expo server, which we will be duplicating (partly) as docmented in The Expo Server. We are not here doing a full install of all the software and scripts on the server, just the minimum to run, test and debug troggle on Django.

Scripts

There are two scripts in the troggle folder which will do semi-automatically what is described below. Have a look at

which you can read without installing by looking in:
troggle/.git/tree/_deploy/wsl.

You will run os-trog24.04.sh just once to install all the basics,and you will run venv-trog.sh just once initially.

We have two configurations for the virtual environment: 'dev' which uses the latest python and Django for speed, and 'server' which mimics the versions currently running on the server. [In December 2024 Django is 5 releases ahead of the version on the server (5.1 versus 3.2). Each has a list of ancilliary packages with the appropriate versions in dev.toml and server.toml.] venv-trog.sh deals with all this python-specific stuff, libraries and Django plug-ins.

os-trog.sh takes a few minutes: it installs the subset of /expofiles/ you need to work with troggle. If you now want to install survex, therion etc. then run os-survey.sh, and go away for an hour, as these drag in a huge number of dependencies and installs all of /expofiles/ except the photos and Martin's mapapp.

Why no Docker container?

Yes, it is true that this would greatly speed up on-boarding new programmers. Or podman.

But there is the significant danger that containers would get copied around and deployed without being properly cleaned up: resulting in configuration drift and a snowflake server situation. File permissions are a big issue.

We should do both: create a Docker or Podman system for getting started, then transition programmers to script-based or recipe-based provisioning so that systems are rebuilt cleanly. CUYC (who also use Django) have a bash script which sets up a new django development system. We should copy that in the first instance. Alas, we haven't got around to doing any of this yet. However uv now makes everything much, much simpler than when were were using pip.

Configuring ubuntu

Set up the key-exchange first. You need to be able to ssh into the server to run this next bit.

Set your indentity

On a new machine you need to configure your git identity. If your name is Anathema Device,then:

git config --global user.email "anathema.device@example.com"
git config --global user.name "Anathema Device"
git config --global pull.rebase true

In your home folder run
~$ bash os-trog24.04.sh
you will hve this in your troggle folder is you have copied it from _deploy (see instructions above) or you can copy it from the .git repo online to get hold of it. Run it in /home/anathema/expo and it will clone all the repositories into it after installing the software you need. It installs some of expofiles but not expofiles/photos/.

key exchange

You absolutely need this so that you can upload your edited code to the git repo on the server. And currently the initial download and setup requires it too.

Follow this link to register a key with the expo server to get git access if you have not already cloned the :troggle: repo.

On WSL you will need to check that the key that you paired is actually loaded:

chmod 700 ~/.ssh/*
eval `ssh-agent`
ssh-add ~/.ssh/anathema@device

Remote EXPOFILES

If you do not have a local copy of the 40GB /expofiles/, don't worry. Later on we can set 'EXPOFILESREMOTE = True' in the localsettings.py file and your test system will use the live expofiles on expo.survex.com (read only).

If you do have 'EXPOFILESREMOTE = True' then the forms which upload scans and photos to the server will not work as you expect. They will upload to your local machine, but read the status of the folders from expo.survex.com. So you will get confusing and apparently inconsistent behaviour: e.g. you will upload a file but then be unable to see it.

For development, you mostly only need a local copy of the wallets and scanned survey notes and sketches in expofiles/surveyscans which is less than 5GB.

You can, if you like, have the expo photo archive collection somewhere else, not inside expofiles, on a troggle development machine. To do this set the PHOTOS_ROOT appropriately in troggle/localsettings.py. By default it is PHOTOS_ROOT = EXPOFILES / 'photos'. This is handy if you want everything else in expofiles in your Linux home drive for speed, but don't care about speed for the 29GB of photos which can sit on an SDdrive.

Installing python, packages and Django

XKCD python install

We do not install Django at the system level using sudo apt install django. We will be installing two variants of Django and python in a separate virtual environment (a 'venv'); not in the main linux system. The reasons why are indicated in the cartoon to the right and explained in "python dependency management is a dumpster fire".

Installing a venv

To prevent compatibility issues when deploying our code, we test using the same libraries that run on expo.survex.com. In December 2024 expo.survex.com uses Python 3.11.2 and Django 3.2. However on our laptops we develop with Python 3.13 and Django 5 to get plenty of warning of future incompatibilities. Additionally, these newer versions are faster.

Do this:

cd ~/expo/troggle
uv sync
source .venv/bin/activate
which resolves the dependences for the packages used with the 'dev' (development) configuration, installs anything missing (including python interpreters), and sets up links to the correct versions of python and django.

To swap to the server mimic environment, do:

deactivate
cd ~/expo/troggle
cp server.toml pyproject.toml
uv sync
source .venv/bin/activate
to swap back to the development environment, do
deactivate
cd ~/expo/troggle
cp dev.toml pyproject.toml
uv sync
source .venv/bin/activate
These scripts are extremely quick because they use uv and everything is cached. The first time you run uv sync it may take a little while as it will install the appropriate version of python.
(troggle) philip@Mohawk:~/expo/troggle$ cp dev.toml pyproject.toml
(troggle) philip@Mohawk:~/expo/troggle$ uv sync
Using CPython 3.13.1
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Resolved 11 packages in 25ms
Installed 9 packages in 545ms
 + asgiref==3.8.1
 + beautifulsoup4==4.12.3
 + coverage==7.6.9
 + django==5.1.4
 + piexif==1.1.3
 + pillow==11.0.0
 + soupsieve==2.6
 + sqlparse==0.5.3
+ unidecode==1.3.8
but it's still extremely quick.

do the basic Django health checks

This all checks that the installation has completed properly.

django-admin

The first line django-admin will complain that it has not got a SETTINGS file, but that's fine. See django-admin initial output for what you should expect to see at this point. If it crashes though, you have not managed to install the software completely. If you get an error when running

django-admin --version
The version number is the version of Django you have installed. Double check that it is the one you meant to install and check with our Django versions page.

do the basic troggle health checks

Now try

python manage.py
You will get an error: No module named 'localsettings'. Fixing this is described below, but for now try:
python manage.py check -v 3 --traceback

The most important is the python manage.py check. If this works, then you have installed the software correctly.

The next task is to edit the SETTINGS files to match your machine and folder structure. So find the appropriate copy of the localsettings in /_deploy/ and copy it into the main troggle folder:

cp _deploy/wsl/localsettingsWSL.py localsettings.py
The git repo copies have got munged passwords. localsettings.py is not stored in git, but the copy on the server does have the correct passwords. So use sFTP to download localsettings.py from expo.survex.com to get these.

We have at one time made localsettings in /_deploy/ appropriate for

WARNING: only the WSL (12/2024) and debian (12/2023) variants are in any way current . All the others are so old that they will need serious work to be useable. Copy what you need from WSL and debian variants of localsettings.py

Now edit localsettings.py and insert useful values for EXPOUSERPASS [e.g. cavey:beery], EXPOADMINUSERPASS [e.g. beery:cavey], SECRET_KEY. SECRET_KEY can be anything, it just has to be unique to each installation and invisible to anyone not a developer.

Set EMAIL_HOST and EMAIL_HOST_PASSWORD to an email account you control that can send email. Then troggle can email you when some things go wrong. This may mean having to set EMAIL_PORT and MAIL_USE_TLS too (this is not used in troggle currently). Set EXPOUSER_EMAIL and EXPOADMINUSER_EMAIL to your own email address while you are doing software development. All these will be different when troggle is deployed on the public server.

Now you need to edit the following settings in your localsettings.py file to match your development machine, e.g. if you have /expofiles/ mounted on another disc:

FILES = Path('/mnt/f/expofiles/')
EXPOFILES = Path('/mnt/f/expofiles/')

All the other settings (drawings, expoweb etc.) will work fine if they are parallel directories to the directory you installed troggle into. The troggle code can find out itself where it is living.

If you do not have a local copy of /expofiles/ (40 GB), you can use the expo server copy if you set:

EXPOFILESREMOTE = TRUE
and then the FILES and EXPOFILES setings will be ignored. (Except for the upload forms which will 'upload' files to your local disc. )

Now try this again:

python manage.py
and in addition to the [django] command list, you will now gets command lists for [auth], [contenttypes], [core], and [sessions]. These are the modules (plugins) loaaded into django. [core] is the core of troggle (but not all of it: the input file parsers are not in [core]).

Now:

python -Wall manage.py check 
This loads the settings files and checks that all the Django packages and python libraries imports all work. It gives warnings of deprecated Django which should be fixed. You should not get any warnings on a fresh install on a new machine but you will see warning and error messages when you are trying to upgrade troggle to use later versions of Django.
python manage.py check -v 3 --traceback
Ideally this will list settings imports and then say
System check identified no issues (0 silenced).

If you get an error your python sys.path is probably not set correctly yet. Do

python -m site
when you are in your troggle directory to see the list of paths python looks for when it is searching for packages (both django and troggle).
Ensure that the path to the troggle/ directory is in the list. It should be at the top, which is where the current working directory is.

Now:

python  manage.py check -v 3 --deploy
which will give security warnings for deployment. You will get various middleware settings because we have not got https:// properly configured everywhere (we have unresolved issues with the Django admin control panel and https:// in October 2021) and a warning not to use DEBUG=true in deployment. We always have DEBUG=True set for troggle as otherwise the users get useless error messages. This is completely contrary to Django official security advice.
Click triangle to see security warnings in detail
?: (security.W001) You do not have 'django.middleware.security.SecurityMiddleware' 
in your MIDDLEWARE so the SECURE_HSTS_SECONDS, SECURE_CONTENT_TYPE_NOSNIFF, 
SECURE_BROWSER_XSS_FILTER, and SECURE_SSL_REDIRECT settings will have no effect.                         

?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only 
session cookie makes it more difficult for network traffic sniffers to hijack user sessions.                                                                                           
?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your 
MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True. Using a secure-only 
CSRF cookie makes it more difficult for network traffic sniffers to steal the CSRF token.              

?: (security.W018) You should not have DEBUG set to True in deployment. 

If you got an error traceback with

python manage.py
then the settings registration of troggle with django is incomplete. Delete all your cached .pyc files and try again. You probably have a mistake in your settings.py or localsettings.py files.
python manage.py diffsettings
This last one shows everything set in global settings, settings and localsettings. Anything different from global settings (django built-in) has '###' appended.
python manage.py help migrate
python manage.py migrate
Tests the uptodateness of your sqlite database. The help option explains what this does and gives extra command line options.

check full functionality

Now run the test suite:

python manage.py test -v 3 --traceback

This will run the entire troggle test suite of ~90 tests (it takes only a few seconds).

If you get an error, and you probably will, have a look in the source code of the test, e.g. for this error:

FAIL: test_page_folk (troggle.core.TESTS.tests.PageTests) 
look in the file troggle/code/TESTS/tests.py in the class PageTests. It will also say:
  File "/mnt/c/EXPO/troggle/core/TESTS/tests.py", line 266, in test_page_folk
which means that the asssert failure in on line 266 of troggle/code/TESTS/tests.py and that the failure function is test_page_folk() . If you look at this you will see that line 264 is:
 response = self.client.get('/folk/index.htm')
so this file is missing. Duh. Of course it is. We downloaded troggle from git but we didn't run the standalone script to generate the folk list. The quickest thing to do when installing troggle is to simply download a copy from the server:
 wget -O ../expoweb/folk/index.htm expo.survex.com/folk/index.htm
and run the tests again:
python  manage.py test -v 2
Click on the triangle to see how to run the folk script. You don't need to do this now. The folk generation script is top of the list in http://expo.survex.com/handbook/troggle/scriptscurrent.html#folk

So do this:

cd ../expoweb/folk
python ../scripts/make-folklist.py <folk.csv >index.htm
cd ../../troggle

The test suite now tidies up after itself, so there should not be any temporary files left behind or local git commits that you will need to clean up.

The test suite has ~90 tests but does not cover all of what troggle does and does not use any real data. You need to manually test these too, after you have done a full data import:
- http://localhost:8000/pathsreport
- http://localhost:8000/stats
- http://localhost:8000/people (takes a minute or so)
- http://localhost:8000/wallets/year/2019l
- http://localhost:8000/survexfile/caves/
- http://localhost:8000/expofiles/training-info/Idiots guide to accessing expo git.pptx
- http://localhost:8000/1623/291/291
- http://localhost:8000/caves
- ttp://localhost:8000/admin/doc/models/core.expedition/ (admin login required)
- http://localhost:8000/survexfile/204

Check parsing and importing

Nearly half the code deals with importing and parsing data, so you need to test that a full data import works. Run the full data import troggle$ python databaseReset.py reset R000. It should take about 5 minutes to import everything. See Troggle Full Import for what you should expect to see.

Use git to commit your edits

You need to know git. Sorry, but there it is. See our git repositories and our git cheat sheet.

Helpful database tools and scripts

The public server uses a MariaDB SQL database and development is usually done using a single-user sqlite database which is a standard Django option.

You will find it very, very useful to see what is going on if you look directly at the data in the database (just a single file in the sqlite case) and browse the data in the tables. This is vital when doing Django migrations between Django versions. A light-weight, simple db browser is DB Browser for SQLite. Connecting directly the the MariaDB database with a control panel or workbench gives even more tools and documentation capabilities. See the troggle server documentation for how to install MariaDB.

When Ubuntu was running on WSL1, it did not use systemctl. Now Ubuntu24.04 does ue systemctl so we should be able to use the same configuration as the server.. [untried as yet].

Previously..

So under WSL1 you needed specific instructions for installing MariaDB under WSL, do what it says in these instructions first (sudo apt install ...etc.):

Create a new dedicated administrative MariaDB user 'expo' who can access all databases. Log in to the MariaDB command with

sudo mysql
and execute these commands:
GRANT ALL PRIVILEGES on *.* TO 'expo'@'%' IDENTIFIED BY 'my-secret-password-schwatzmooskogel' WITH GRANT OPTION;
SET PASSWORD FOR expo=PASSWORD('my-secret-password-schwatzmooskogel');
FLUSH PRIVILEGES;
QUIT;
and you will need to set this user and password in your localsettings.py:
DATABASE = {
    'default': { 
        'ENGINE': 'django.db.backends.mysql', # 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME' : 'troggle',                   
        'USER' : 'expo',                      
        'PASSWORD' : 'my-secret-password-schwatzmooskogel',       
        'HOST' : '',                          # Set to empty string for localhost. 
        'PORT' : '',                          # Set to empty string for default. 
    }
}

But it still did not work

That is because we need to install the python tools that talk to mariadb. And while it 'will just work' for python3.9, the standard installed on the distro, with python3.10 it is a bit more work. but note that there is a problem with using python 3.10 in that some bits of pip are not correct and you will get a ImportError: cannot import name 'html5lib' error. TEMPORARILY use this hack from bootstrap:

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10
pip install mysql-connector-python
installs the correct pip for python3.10 on Ubuntu. Which works, then
sudo apt-get install python3.10-dev
sudo apt install libmariadbclient-dev
pip install mariadb
wwhich now has installed mariadb python stuff, but seems to have trashed my django installattion. Hmph. And pip.

Getting a copy of live javascript libraries

To get the same javascript libraries as the expo server:
scp -r expo@expo.survex.com:/usr/share/javascript/ ~/expo/troggle/javascript/
but you don't need these for most troggle development: copies of what is needed are in the troggle git repo. One example that is not in the troggle git repo is "CaveView".
Go on to: Troggle architecture
Return to: Troggle programmers' guide
Troggle index: Index of all troggle documents
Go to: ※ Basic laptop
Go to: ⁂ Survey laptop
Go to: ☆ Bulk Update laptopWindows Bulk Update laptop
Go to: ⚒ Troggle development laptopTroggle development WSL laptop