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 @@
"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";
......
......@@ -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 -
```
{
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 @@
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:
......
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