[READ-ONLY] Mirror of https://github.com/mrgnw/scripts.
0

Configure Feed

Select the types of activity you want to include in your feed.

scripts / pippy
1.1 kB 39 lines
1#!/usr/bin/env zsh 2 3# Function to install packages required by a single Python file 4function install_requirements_for_file() { 5 local python_file=$1 6 shift # Remove the first argument 7 8 # Use Python to extract required packages and install them 9 python3 -c " 10import ast, sys 11 12def is_standard_lib(module_name): 13 return module_name in getattr(sys, 'stdlib_module_names', []) 14 15def get_top_level_module(name): 16 return name.split('.')[0] 17 18with open('$python_file', 'r') as file: 19 root = ast.parse(file.read(), filename='$python_file') 20 21 imports = { 22 get_top_level_module(alias.name) 23 for node in ast.walk(root) if isinstance(node, ast.Import) 24 for alias in node.names if not is_standard_lib(get_top_level_module(alias.name)) 25 } 26 27 from_imports = { 28 get_top_level_module(node.module) 29 for node in ast.walk(root) if isinstance(node, ast.ImportFrom) 30 if not is_standard_lib(get_top_level_module(node.module)) 31 } 32 33for package in imports.union(from_imports): 34 print(package) 35" | xargs pip install "$@" 36} 37 38# Main script execution 39install_requirements_for_file "$@"