From 9dfa308d8c520c339ff3a4084ee74f754c2bed34 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel <guilhem.saurel@laas.fr> Date: Wed, 26 Jun 2024 12:16:44 +0200 Subject: [PATCH] package newcomers in nix --- flake.nix | 41 +++++++++++++++++++++++------------- newcomers/README.md | 10 ++++----- newcomers/default.nix | 32 ++++++++++++++++++++++++++++ newcomers/greet_newcomers.py | 19 +++++++++++------ 4 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 newcomers/default.nix diff --git a/flake.nix b/flake.nix index 08504f7..7a30221 100644 --- a/flake.nix +++ b/flake.nix @@ -24,24 +24,35 @@ "x86_64-darwin" ]; perSystem = - { config, pkgs, ... }: { + config, + pkgs, + self', + ... + }: + { + apps.newcomers = { + type = "app"; + program = self'.packages.newcomers; + }; devShells.default = pkgs.mkShell { nativeBuildInputs = [ config.treefmt.build.wrapper ]; - packages = with pkgs; [ - (python3.withPackages ( - p: with p; [ - beautifulsoup4 - httpx - ldap3 - numpy - pandas - requests - tabulate - wand - ] - )) - ]; + packages = [ self'.packages.python ]; + }; + packages = { + newcomers = pkgs.python3Packages.callPackage ./newcomers { }; + python = pkgs.python3.withPackages ( + p: with p; [ + beautifulsoup4 + httpx + ldap3 + numpy + pandas + requests + tabulate + wand + ] + ); }; treefmt = { projectRootFile = "flake.nix"; diff --git a/newcomers/README.md b/newcomers/README.md index a6d6f5f..402cf82 100644 --- a/newcomers/README.md +++ b/newcomers/README.md @@ -2,17 +2,15 @@ ## Get dependencies -- Get Python 3 -- `pip3 install ldap3` (you might need `sudo`, or *better* `--user`, or *best* a virtualenv) -- `mkdir -p ~/.cache` +This project is packaged with a nix flake ## Go ! - `./greet_newcomers.py` -On the first time, it will construct a database of the guys already here. +On the first time, it will construct a database of the members already here. -After that, on each launch, it will find the new guys, and send them a greeting mail. +After that, on each launch, it will find the newcomers, and send them a greeting mail. The template of this mail is in `template.txt`. ## Cron job @@ -20,5 +18,5 @@ The template of this mail is in `template.txt`. To run this script everyday at 5 AM: ```bash -(crontab -l; echo "0 5 * * * $(which python) $(pwd)/greet_newcomers.py") | crontab - +(crontab -l; echo "0 5 * * * cd $(pwd); nix run .#newcomers") | crontab - ``` diff --git a/newcomers/default.nix b/newcomers/default.nix new file mode 100644 index 0000000..950b74a --- /dev/null +++ b/newcomers/default.nix @@ -0,0 +1,32 @@ +{ + lib, + ldap3, + buildPythonApplication +}: +buildPythonApplication { + pname = "greet-newcomers"; + version = "1.0.0"; + pyproject = false; + dependencies = [ ldap3 ]; + + src = lib.fileset.toSource { + root = ./.; + fileset = lib.fileset.unions [ + ./greet_newcomers.py + ./template.txt + ]; + }; + + installPhase = '' + install -D -m 755 greet_newcomers.py $out/bin/greet-newcomers + install -D -m 644 template.txt $out/share/greet-newcomers/template.txt + ''; + + meta = { + description = "Greet gepetto newcomers"; + homepage = "https://github.com/gepetto/gepetto-utils/tree/master/newcomers"; + license = lib.licenses.bsd2; + maintainers = [lib.maintainers.nim65s ]; + mainProgram = "greet-newcomers"; + }; +} diff --git a/newcomers/greet_newcomers.py b/newcomers/greet_newcomers.py index 62c9db1..fde6db2 100755 --- a/newcomers/greet_newcomers.py +++ b/newcomers/greet_newcomers.py @@ -3,13 +3,14 @@ import shelve from email.mime.text import MIMEText from getpass import getuser -from smtplib import SMTP +from os import environ from pathlib import Path +from smtplib import SMTP from ldap3 import Connection HERE = Path(__file__).resolve().parent -SHELF = str(HERE / ".cache") +SHELF = Path(environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "greet-newcomers" def get_gepetto(): @@ -53,7 +54,13 @@ def greet(to, sender): if "@" not in to: to = "%s@laas.fr" % to - with (HERE / "template.txt").open() as f: + template = HERE / "template.txt" + if not template.exists(): + template = HERE.parent / "share" / "greet-newcomers" / "template.txt" + if not template.exists(): + err = f"can't find template.txt around {HERE}" + raise RuntimeError(err) + with template.open() as f: msg = MIMEText(f.read()) msg["Subject"] = "Welcome in Gepetto !" @@ -86,9 +93,9 @@ if __name__ == "__main__": # Retrieve the login of the current user, who must already be a member me = whoami(gepetto) - for guy in gepetto_ldap: - if guy not in gepetto: - greet(guy, me) + for member in gepetto_ldap: + if member not in gepetto: + greet(member, me) # Save the new list with shelve.open(SHELF) as shelf: -- GitLab