mirror of
https://github.com/hsjobeki/noogle.git
synced 2024-09-11 12:59:09 +03:00
Add python script to gather examples from nixpkgs
This commit is contained in:
parent
b9eb3c8157
commit
f134ce27a1
10
examples/.envrc
Normal file
10
examples/.envrc
Normal 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
2
examples/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
data.json
|
||||
node_modules
|
17
examples/flake-module.nix
Normal file
17
examples/flake-module.nix
Normal 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
|
||||
'';
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
72
examples/noogle_get_examples.py
Normal file
72
examples/noogle_get_examples.py
Normal 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))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user