-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.old-pyide
executable file
·124 lines (104 loc) · 3.47 KB
/
.old-pyide
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# This script is used to generate the list of all the files needed for a new complete python project
# It has to be run as follows:
# pypr <project_name> -m <module1> <module2> ... <moduleN>
# The first parameter is the name of the project (if not provided, exit)
# The -m flag is used to specify the modules to install
# The modules are installed in the virtual environment created in the current directory
# The script also creates a pyproject.toml file with the configuration for the pyright linter
# If -o is provided, the installed packages will be saved to installed_packages.txt
# The project is opened in the Zed code editor by default, unless --no-editor is provided
#
# If -h is provided, the script will print the help message
# Store the initial working directory
initial_working_dir=$(pwd)
# Function to handle cleanup on SIGINT (Ctrl+C)
cleanup() {
echo "==> Interrupt received. Cleaning up..."
deactivate 2>/dev/null
# Ensure we're back to the initial working directory before deleting
cd "$initial_working_dir" || { echo "==> Failed to return to initial directory. Aborting cleanup."; exit 1; }
if [ -d "$project_name" ]; then
rm -rf "$project_name"
echo "==> Project directory $project_name removed."
fi
exit 1
}
# Trap SIGINT signal (Ctrl+C)
trap cleanup SIGINT
# Check if the first argument is -h or --help
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo -e "==> Usage: pypr <project_name>\n\t -m <module1> <module2> ... <moduleN> (optional)\n\t --no-pyright (optional)\n\t -o (optional, save installed packages to installed_packages.txt)\n\t -h or --help to print this message"
exit 0
fi
# Check if at least one argument (project_name) is provided
if [ $# -lt 1 ]; then
echo "==> Usage: pypr <project_name>"
exit 1
fi
project_name=$1
shift
echo "==> Creating project $project_name"
mkdir "$project_name"
cd "$project_name" || { echo "==> Failed to enter project directory"; exit 1; }
echo "==> Creating virtual environment"
python3 -m venv .venv
source .venv/bin/activate
echo "==> Installing modules"
pip install --upgrade pip
# Flags for optional operations
install_modules=false
output_installed_packages=false
no_pyright=false
open_in_zed=true
# Iterate over the remaining arguments
while [ "$#" -gt 0 ]; do
case "$1" in
-m)
install_modules=true
shift
;;
--no-pyright)
no_pyright=true
shift
;;
-o)
output_installed_packages=true
shift
;;
--no-editor)
open_in_zed=false
shift
;;
*)
if [ "$install_modules" = true ]; then
pip install "$1"
fi
shift
;;
esac
done
# Save installed packages to installed_packages.txt if -o was provided
if [ "$output_installed_packages" = true ]; then
echo "==> Saving installed packages to installed_packages.txt"
pip freeze > installed_packages.txt
fi
deactivate
# Create pyproject.toml unless --no-pyright was provided
if [ "$no_pyright" = false ]; then
echo "==> Creating pyproject.toml file (for pyright in IDEs)"
cat <<EOF > pyproject.toml
[tool.pyright]
venvPath = "."
venv = ".venv"
EOF
fi
# Create a main.py file
touch main.py
if [ "$open_in_zed" = true ]; then
echo "==> Opening project in Zed code editor"
zeditor .
fi
# Cleanup the trap before exiting normally
trap - SIGINT
echo "==> Project setup complete."