This commit is contained in:
dolphinau 2025-11-23 21:35:33 +01:00
parent 42274e7d2c
commit c529e3f6e9
No known key found for this signature in database
6 changed files with 143 additions and 22 deletions

22
.gitignore vendored
View file

@ -1,21 +1 @@
# Generated by Cargo
# will have compiled files and executables
debug
target
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Generated by cargo mutants
# Contains mutation testing data
**/mutants.out*/
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
result

View file

@ -1 +1,26 @@
# secret-santa
# secret-santa
## Add command to nixos
```nix
{ pkgs ? import <nixpkgs> { } }:
let
secret-santa =
let
defaultNix = builtins.fetchurl {
url = "https://raw.githubusercontent.com/dolphinau/secret-santa/refs/heads/main/default.nix";
sha256 = "1sihdgsg84kprycsg102rj9qnwd97zwx17x2wn42q4rmn918wvvx";
};
in pkgs.callPackage defaultNix {
src = pkgs.fetchFromGitHub {
owner = "dolphinau";
repo = "secret-santa";
rev = "a9b1f880d2fa50dbdff03a51b582deacb0972d90";
sha256 = "sha256-v/fvJAlnMxMjlP6Z1cgHyKfno79yRSpKwY7pF0irISA=";
};
};
in [
secret-santa
]
```

49
default.nix Normal file
View file

@ -0,0 +1,49 @@
{ pkgs ? import <nixpkgs> { }, src ? ./., subdir ? "" }:
let
theSource = src;
pythonPackage = pkgs.python313Packages.buildPythonPackage {
pname = "secret-santa";
version = "1.0.0";
src = "${theSource}/${subdir}";
buildInputs = [
pkgs.python313Packages.setuptools
pkgs.python313Packages.wheel
];
propagatedBuildInputs = [ pkgs.python313Packages.flask ];
pyproject = true;
build-system = [
pkgs.python313Packages.setuptools
pkgs.python313Packages.wheel
];
meta = {
description = "Secret santa flask package";
license = pkgs.lib.licenses.mit;
};
};
pythonEnv = pkgs.python313.buildEnv.override {
extraLibs = [ pkgs.python313Packages.flask pythonPackage ];
ignoreCollisions = true;
};
in
pkgs.stdenv.mkDerivation rec {
name = "secret-santa";
propagatedBuildInputs = [ pythonEnv ];
src = "${theSource}/${subdir}/src";
installPhase = ''
mkdir -p $out/bin
cat > $out/bin/${name} <<EOF
#!/bin/sh
exec ${pythonEnv}/bin/python3 ${src}/secret_santa/main.py "\$@"
EOF
chmod +x $out/bin/${name}
'';
meta = {
description = "Secret santa server application";
license = pkgs.lib.licenses.mit;
};
}

8
setup.py Normal file
View file

@ -0,0 +1,8 @@
from setuptools import find_packages, setup
setup(
name="secret-santa",
version="1.0.0",
package_dir={"": "src"},
packages=find_packages(where="src"),
)

View file

59
src/secret_santa/main.py Normal file
View file

@ -0,0 +1,59 @@
import json
import random
import sys
from flask import Flask
app = Flask(__name__)
def check(players):
for player in players:
if (
"target" not in player
or player["target"] in player["invalid"]
or player["target"] == player["name"]
):
return False
return True
def init(input, output):
conf = open(input).read()
players = json.loads(conf)
while not check(players):
remaining = [x["name"] for x in players]
for player in players:
player["target"] = random.choice(remaining)
remaining.remove(player["target"])
open(output, "w").write(json.dumps({p["name"]: p["target"] for p in players}))
if __name__ == "__main__":
if len(sys.argv) == 3:
conf_path = sys.argv[1]
out_path = sys.argv[2]
else:
print("Error: need two args <input conf> <output file>")
exit(1)
try:
players = json.loads(open(out_path).read())
except FileNotFoundError:
players = init(out_path)
@app.route("/<name>")
def get(name):
if name == "raphael":
return f"<p>Hello {name}!<br/>Your secret santa target is {players[name]}<br/>Eva's secret santa target is {players['eva']}</p>"
if name in players:
return (
f"<p>Hello {name}!<br/>Your secret santa target is {players[name]}</p>"
)
else:
return f"<p>Error: Player {name} not found"
app.run(debug=True)