Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python script to gather examples from nixpkgs #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/.envrc
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data.json
node_modules
17 changes: 17 additions & 0 deletions examples/flake-module.nix
Original file line number Diff line number Diff line change
@@ -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 changes: 72 additions & 0 deletions examples/noogle_get_examples.py
Original file line number Diff line number Diff line change
@@ -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))






3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.
Expand Down
Loading