Skip to content
Snippets Groups Projects
Commit 9dfa308d authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

package newcomers in nix

parent c25833d3
No related branches found
No related tags found
No related merge requests found
...@@ -24,24 +24,35 @@ ...@@ -24,24 +24,35 @@
"x86_64-darwin" "x86_64-darwin"
]; ];
perSystem = perSystem =
{ config, pkgs, ... }:
{ {
config,
pkgs,
self',
...
}:
{
apps.newcomers = {
type = "app";
program = self'.packages.newcomers;
};
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
nativeBuildInputs = [ config.treefmt.build.wrapper ]; nativeBuildInputs = [ config.treefmt.build.wrapper ];
packages = with pkgs; [ packages = [ self'.packages.python ];
(python3.withPackages ( };
p: with p; [ packages = {
beautifulsoup4 newcomers = pkgs.python3Packages.callPackage ./newcomers { };
httpx python = pkgs.python3.withPackages (
ldap3 p: with p; [
numpy beautifulsoup4
pandas httpx
requests ldap3
tabulate numpy
wand pandas
] requests
)) tabulate
]; wand
]
);
}; };
treefmt = { treefmt = {
projectRootFile = "flake.nix"; projectRootFile = "flake.nix";
......
...@@ -2,17 +2,15 @@ ...@@ -2,17 +2,15 @@
## Get dependencies ## Get dependencies
- Get Python 3 This project is packaged with a nix flake
- `pip3 install ldap3` (you might need `sudo`, or *better* `--user`, or *best* a virtualenv)
- `mkdir -p ~/.cache`
## Go ! ## Go !
- `./greet_newcomers.py` - `./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`. The template of this mail is in `template.txt`.
## Cron job ## Cron job
...@@ -20,5 +18,5 @@ The template of this mail is in `template.txt`. ...@@ -20,5 +18,5 @@ The template of this mail is in `template.txt`.
To run this script everyday at 5 AM: To run this script everyday at 5 AM:
```bash ```bash
(crontab -l; echo "0 5 * * * $(which python) $(pwd)/greet_newcomers.py") | crontab - (crontab -l; echo "0 5 * * * cd $(pwd); nix run .#newcomers") | crontab -
``` ```
{
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";
};
}
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
import shelve import shelve
from email.mime.text import MIMEText from email.mime.text import MIMEText
from getpass import getuser from getpass import getuser
from smtplib import SMTP from os import environ
from pathlib import Path from pathlib import Path
from smtplib import SMTP
from ldap3 import Connection from ldap3 import Connection
HERE = Path(__file__).resolve().parent HERE = Path(__file__).resolve().parent
SHELF = str(HERE / ".cache") SHELF = Path(environ.get("XDG_CACHE_HOME", Path.home() / ".cache")) / "greet-newcomers"
def get_gepetto(): def get_gepetto():
...@@ -53,7 +54,13 @@ def greet(to, sender): ...@@ -53,7 +54,13 @@ def greet(to, sender):
if "@" not in to: if "@" not in to:
to = "%s@laas.fr" % 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 = MIMEText(f.read())
msg["Subject"] = "Welcome in Gepetto !" msg["Subject"] = "Welcome in Gepetto !"
...@@ -86,9 +93,9 @@ if __name__ == "__main__": ...@@ -86,9 +93,9 @@ if __name__ == "__main__":
# Retrieve the login of the current user, who must already be a member # Retrieve the login of the current user, who must already be a member
me = whoami(gepetto) me = whoami(gepetto)
for guy in gepetto_ldap: for member in gepetto_ldap:
if guy not in gepetto: if member not in gepetto:
greet(guy, me) greet(member, me)
# Save the new list # Save the new list
with shelve.open(SHELF) as shelf: with shelve.open(SHELF) as shelf:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment