-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpywith
executable file
·82 lines (66 loc) · 2.28 KB
/
pywith
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env nix
#! nix shell nixpkgs#uv -c fish
# Spawn a python environment (and run it) with the given list of packages.
set _dir (dirname (status filename))
source $_dir/_lib.fish
function usage
echo "pywith - Quickly spawn Python environments
"$bold"Usage:"$resf"
pywith [options...] <package>...
pywith [options...] (-r | --requirements) <file>
"$bold"Options:"$resf"
-h, --help Print this help message
-c, --command=<cmd> Run <cmd> in the built shell (default: ptpython)
-s, --shell Same as '-c $SHELL'
-f, --file=<file> Interpret <file> with built Python env
-r, --requirements Read packages from a text file
-R Same as '-r ./requirements.txt'
"$bold"Examples:"$resf"
pywith numpy scipy # Open a python REPL with the numpy and scipy packages available
pywith -R # Open a python REPL with packages from ./requirements.txt
pywith -s requests # Open a shell with a python that has the requests library
pywith -f script.py numpy # Interpret script.py using a python with numpy available
"
end
argparse h/help 'c/command=' s/shell 'f/file=' 'r/requirements=' R -- $argv; or exit 127
if set -q _flag_help
usage
exit 0
end
# Hardcode Python 3.13 for now
set -a uv_args '--python' '3.13'
# Give a default command if none was given
if set -q _flag_command; and begin
set -q _flag_file; or set -q _flag_shell
end
log_error "Options --command, --file, and --shell are mutually exclusive."
exit 127
else if set -q _flag_command
set cmd $_flag_command
else if set -q _flag_shell
set cmd $SHELL
else if set -q _flag_file
if not stat $_flag_file >/dev/null
log_error "Couldn't read file: "$_flag_file
exit 2
end
set cmd python $_flag_file
else
set -a uv_args '--with' 'ptpython'
set cmd ptpython
end
# If -r or -R was passed, read from the given requirements file
if set -q _flag_requirements; or set -q _flag_R
set -a uv_args '--with-requirements'
if set -q _flag_R
set -a uv_args './requirements.txt'
else
set -a uv_args $_flag_requirements
end
end
for pkg in $argv
set -a uv_args '--with' $pkg
end
# Run uv run
uv run $uv_args $cmd
# vim: set ft=fish sw=4: