Add python script to gather examples from nixpkgs

This commit is contained in:
Scriptkiddi 2024-02-17 05:10:58 +01:00
parent b9eb3c8157
commit f134ce27a1
No known key found for this signature in database
GPG Key ID: F7FBBF6334AF4872
5 changed files with 103 additions and 1 deletions

10
examples/.envrc Normal file
View File

@ -0,0 +1,10 @@
source_up
files=(../../flake.nix flake-module.nix package.json)
if type nix_direnv_watch_file &>/dev/null; then
nix_direnv_watch_file "${files[@]}"
else
watch_file "${files[@]}"
fi
use flake .#builtins-data --builders ''

2
examples/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
data.json
node_modules

17
examples/flake-module.nix Normal file
View File

@ -0,0 +1,17 @@
{ inputs, ... }: {
perSystem = { self', inputs', pkgs, lib, ... }:
{
packages = {
nixpkgs-examples = pkgs.stdenv.mkDerivation {
name = "nixpkgs-examples";
src = ./.;
nativeBuildInputs = [ pkgs.python3 ];
buildPhase = ''
cp -f ${self'.packages.data-json} ./data.json
python noogle_get_examples.py data.json ${inputs.nixpkgs-master}
mv output.json $out
'';
};
};
};
}

View File

@ -0,0 +1,72 @@
#!/usr/bin/env nix-shell
#!nix-shell -p python3
import json
import os
import sys
# Get file and folder path from command line arguments
json_file_path = sys.argv[1]
folder_path = sys.argv[2]
# Load JSON file
with open(json_file_path, 'r') as f:
data = json.load(f)
search_strings = set()
for entry in data:
# Walk through folder structure and search for matches
search_strings.add(entry['meta']['path'][-1])
count = 0
search_strings = ["callPackage", "or", "toInt"]
search_strings = dict.fromkeys(search_strings, {"count": 0, "matches": []})
finished_search_strings = dict()
for root, dirs, files in os.walk(folder_path):
for file in files:
path = os.path.join(root, file)
if file.endswith(".nix") and not os.path.islink(path) and os.path.isfile(path):
with open(path, 'r') as f:
lines = f.readlines()
for line in lines:
for string in search_strings:
if f" {string} " in line:
matches = search_strings[string]["matches"].copy()
linenumber = lines.index(line) + 1
match = {"line": linenumber, "column": None, "example": lines[linenumber-3:linenumber+3], "file": os.path.join(root, file)}
matches.append(match)
string_dict = search_strings[string].copy()
string_dict.update(
{"count": search_strings[string]["count"]+1,
"matches": matches})
search_strings.update({string: string_dict})
left_search_strings = {}
for string in search_strings:
if search_strings[string]["count"] < 3:
left_search_strings.update({string: search_strings[string]})
else:
finished_search_strings.update({string: search_strings[string]})
search_strings = left_search_strings
print(f"{len(search_strings)} left")
finished_search_strings.update(search_strings)
output = {}
for entry in data:
# Walk through folder structure and search for matches
if entry['meta']['path'][-1] in finished_search_strings:
output.update({"meta": {"path": entry["meta"]["path"], "examples": finished_search_strings[entry['meta']['path'][-1]]}})
with open("output.json", "w") as outfile:
outfile.write(json.dumps(output, indent=4))

View File

@ -5,7 +5,7 @@
nixpkgs-master.url = "nixpkgs/master";
nix-master.url = "github:NixOS/nix/?ref=master";
# A custom nix version, to introspect lambda values.
# A custom nix version, to introspect lambda values.
nix.url = "github:hsjobeki/nix/?ref=feat/positions";
pre-commit-hooks = {
@ -33,6 +33,7 @@
./preCommit.nix
./website/flake-module.nix
./salt/flake-module.nix
./examples/flake-module.nix
./pasta/flake-module.nix
./pesto/flake-module.nix
# Deprecated. Will be removed.