diff --git a/flake.nix b/flake.nix index 08504f7280122522e1075d96ae7df2e64e46a796..7a302218ca1ef378c638965c41cc98caeda98527 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 a6d6f5f7257970fd6d2793b2a12536f4457053e1..402cf820f239a08e00164e9db9a65c2e7c4d666c 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 0000000000000000000000000000000000000000..950b74a948a91c3214d862cb6b2e8cfa304d0815 --- /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 62c9db1bbb281ad18b01025a63ded45736f36a66..fde6db28c8985ca06c0372444904e1cf5497ec86 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: