CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-virtualenv

A tool for creating isolated 'virtual' python environments

Pending
Overview
Eval results
Files

file-operations.mddocs/

File Operations

Comprehensive file and directory manipulation functions for environment setup, including copying, linking, and permission management with cross-platform compatibility.

Capabilities

Directory Operations

Functions for creating and removing directories with proper error handling and cross-platform support.

def mkdir(at_path):
    """
    Create directory if it doesn't exist.
    
    Creates directory and any necessary parent directories. No-op if
    directory already exists. Handles permission errors gracefully.
    
    Parameters:  
    - at_path (str): Directory path to create
    
    Returns:
    None
    
    Raises:
    OSError: If directory creation fails due to permissions or other system error
    """

def rm_tree(folder):
    """
    Remove directory tree recursively.
    
    Removes directory and all contents recursively. Handles read-only files
    and permission issues by attempting to make files writable before deletion.
    Safe to call on non-existent directories.
    
    Parameters:
    - folder (str): Directory path to remove
    
    Returns:
    None
    
    Raises:
    OSError: If directory removal fails after permission adjustments
    """

Usage Examples:

import virtualenv

# Create directory structure
virtualenv.mkdir('/path/to/new/directory')
virtualenv.mkdir('/path/to/nested/directory/structure')

# Remove directory tree
virtualenv.rm_tree('/path/to/unwanted/directory')

File Copying and Linking

Functions for copying files and directories with support for symbolic linking and cross-platform compatibility.

def copy_file_or_folder(src, dest, symlink=True):
    """
    Copy file or folder with symlink support.
    
    Copies files or directories from source to destination. Uses symbolic
    links when possible and supported by platform. Handles both files and
    directories recursively.
    
    Parameters:
    - src (str): Source file or directory path
    - dest (str): Destination file or directory path  
    - symlink (bool): Use symbolic links when possible instead of copying
    
    Returns:
    None
    
    Raises:
    OSError: If source doesn't exist or copy operation fails
    """

def copyfile(src, dest, symlink=True):
    """
    Copy a single file with symlink support.
    
    Copies individual file from source to destination. Creates symbolic
    link when possible and requested. Preserves file permissions and
    timestamps when copying.
    
    Parameters:
    - src (str): Source file path
    - dest (str): Destination file path
    - symlink (bool): Create symbolic link instead of copying if possible
    
    Returns:
    None
    
    Raises:
    OSError: If source file doesn't exist or copy fails
    IOError: If destination cannot be written
    """

Usage Examples:

import virtualenv

# Copy file with automatic symlink detection
virtualenv.copyfile('/usr/bin/python3', '/path/to/env/bin/python')

# Copy directory tree
virtualenv.copy_file_or_folder('/usr/lib/python3.8', '/path/to/env/lib/python3.8')

# Force copying instead of symlinking
virtualenv.copyfile('/etc/hosts', '/path/to/backup/hosts', symlink=False)

File Writing and Permissions

Functions for writing content to files and managing file permissions.

def writefile(dest, content, overwrite=True):
    """
    Write content to a file.
    
    Writes string or bytes content to specified file path. Creates parent
    directories if they don't exist. Handles text encoding appropriately.
    
    Parameters:
    - dest (str): Destination file path
    - content (str/bytes): Content to write to file
    - overwrite (bool): Whether to overwrite existing files
    
    Returns:
    None
    
    Raises:
    OSError: If file cannot be created or written
    IOError: If file already exists and overwrite=False
    """

def make_exe(fn):
    """
    Make a file executable.
    
    Sets executable permissions on file for owner, group, and others
    as appropriate for the platform. On Windows, ensures file has
    appropriate extension or is marked executable.
    
    Parameters:
    - fn (str): File path to make executable
    
    Returns:
    None
    
    Raises:
    OSError: If file doesn't exist or permission change fails
    """

Usage Examples:

import virtualenv

# Write activation script
script_content = "#!/bin/bash\nexport PATH=/path/to/env/bin:$PATH"
virtualenv.writefile('/path/to/env/bin/activate', script_content)

# Make script executable
virtualenv.make_exe('/path/to/env/bin/activate')

# Write without overwriting existing file
virtualenv.writefile('/path/to/config.ini', 'key=value', overwrite=False)

Specialized Copy Operations

Functions for copying specific types of files and directories required for virtual environment setup.

def copy_required_modules(dst_prefix, symlink):
    """
    Copy required Python modules to virtual environment.
    
    Copies essential Python standard library modules that are needed
    for virtual environment functionality. Handles platform-specific
    module requirements and dependencies.
    
    Parameters:
    - dst_prefix (str): Destination prefix path for virtual environment
    - symlink (bool): Use symbolic links when possible
    
    Returns:
    None
    
    Raises:
    OSError: If required modules cannot be found or copied
    """

def copy_required_files(src_dir, lib_dir, symlink):
    """
    Copy required files for virtual environment.
    
    Copies platform-specific files needed for Python environment
    functionality including shared libraries, configuration files,
    and platform-specific resources.
    
    Parameters:
    - src_dir (str): Source directory containing Python installation
    - lib_dir (str): Target library directory in virtual environment
    - symlink (bool): Use symbolic links when possible
    
    Returns:
    None
    
    Raises:
    OSError: If required files cannot be copied
    """

def copy_include_dir(include_src, include_dest, symlink):
    """
    Copy Python include directory for C extension compilation.
    
    Copies Python header files needed for compiling C extensions
    in the virtual environment. Essential for packages that include
    native code components.
    
    Parameters:
    - include_src (str): Source include directory
    - include_dest (str): Destination include directory
    - symlink (bool): Use symbolic links when possible  
    
    Returns:
    None
    
    Raises:
    OSError: If include directory cannot be copied
    """

Usage Examples:

import virtualenv

# Copy required modules during environment setup
virtualenv.copy_required_modules('/path/to/env', symlink=True)

# Copy required files from Python installation
virtualenv.copy_required_files('/usr/lib/python3.8', '/path/to/env/lib', True)

# Copy include directory for C extensions
virtualenv.copy_include_dir('/usr/include/python3.8', '/path/to/env/include', True)

File System Fixes and Adjustments

Functions for adjusting file system layout and fixing common virtual environment issues.

def fix_local_scheme(home_dir, symlink=True):
    """
    Fix local installation scheme for virtual environment.
    
    Adjusts Python installation scheme to work properly in virtual
    environment context. Handles platform-specific path adjustments
    and ensures proper package installation behavior.
    
    Parameters:
    - home_dir (str): Virtual environment home directory
    - symlink (bool): Use symbolic links when adjusting paths
    
    Returns:
    None
    
    Raises:
    OSError: If scheme adjustments cannot be applied
    """

def fix_lib64(lib_dir, symlink=True):
    """
    Fix lib64 directory issues on 64-bit systems.
    
    Creates or adjusts lib64 symbolic link to lib directory on systems
    that expect lib64 for 64-bit libraries. Common requirement on
    Linux systems for proper library discovery.
    
    Parameters:
    - lib_dir (str): Library directory path in virtual environment
    - symlink (bool): Create symbolic link (vs. copy) for lib64
    
    Returns:
    None
    
    Raises:
    OSError: If lib64 link cannot be created
    """

def fixup_pth_and_egg_link(home_dir, sys_path=None):
    """
    Fix .pth and .egg-link files for virtual environment.
    
    Adjusts path files (.pth) and egg-link files to use correct paths
    for virtual environment. Essential for proper package discovery
    and import behavior in isolated environments.
    
    Parameters:
    - home_dir (str): Virtual environment home directory
    - sys_path (list, optional): Python sys.path for path resolution
    
    Returns:
    None
    
    Raises:
    OSError: If path files cannot be fixed
    """

Usage Examples:

import virtualenv
import sys

# Fix installation scheme
virtualenv.fix_local_scheme('/path/to/env')

# Fix lib64 on 64-bit systems  
virtualenv.fix_lib64('/path/to/env/lib')

# Fix path files with custom sys.path
virtualenv.fixup_pth_and_egg_link('/path/to/env', sys_path=sys.path)

Script and Configuration Management

Functions for managing activation scripts and configuration files within virtual environments.

def fixup_scripts(_, bin_dir):
    """
    Fix script shebangs and paths in virtual environment.
    
    Updates shebang lines in executable scripts to point to virtual
    environment Python interpreter. Ensures scripts use isolated
    environment rather than system Python.
    
    Parameters:
    - _ (unused): Legacy parameter, ignored
    - bin_dir (str): Binary directory containing scripts to fix
    
    Returns:
    None
    
    Raises:
    OSError: If scripts cannot be read or modified
    """

def relative_script(lines):
    """
    Convert script to use relative paths.
    
    Modifies script lines to use relative paths instead of absolute
    paths, making scripts relocatable with their virtual environment.
    
    Parameters:
    - lines (list): List of script lines to modify
    
    Returns:
    list: Modified script lines with relative paths
    """

Usage Examples:

import virtualenv

# Fix scripts in virtual environment
virtualenv.fixup_scripts(None, '/path/to/env/bin')

# Make script relocatable
with open('/path/to/script.py', 'r') as f:
    lines = f.readlines()
    
relative_lines = virtualenv.relative_script(lines)

with open('/path/to/script.py', 'w') as f:
    f.writelines(relative_lines)

Install with Tessl CLI

npx tessl i tessl/pypi-virtualenv

docs

environment-creation.md

file-operations.md

index.md

utility-functions.md

tile.json