Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/pyrobird/README.md is written in an unsupported language. File is not indexed.

0001 # pyrobird
0002 
0003 [![PyPI - Version](https://img.shields.io/pypi/v/pyrobird.svg)](https://pypi.org/project/pyrobird)
0004 [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrobird.svg)](https://pypi.org/project/pyrobird)
0005 
0006 -----
0007 
0008 ## Installation
0009 
0010 ```bash
0011 pip install pyrobird
0012 ```
0013 
0014 Optional dependencies:
0015 
0016 - `batch` - install playwright, that allows to make screenshots in batch mode
0017 - `xrootd` - install libraries to read xrootd located files and URLs starting with `root://`
0018 - `dev` - install pytest and other development requirements, mainly to run tests
0019 
0020 If using `batch` for screenshots, after installing playwright, you need to install browser binaries:
0021 
0022 ```bash
0023 python -m playwright install chromium
0024 ```
0025 
0026 If installed via pip, `xrootd` library requires compilation, so the system should have cmake,
0027 compiler and some xrootd dependencies installed.
0028 
0029 For debian/ubuntu the packages to install to use xrootd: 
0030 
0031 ```bash
0032 sudo apt install build-essential libxrootd-client-dev cmake zlib1g-dev uuid-dev libssl-dev python3-dev
0033 
0034 ```
0035 
0036 Development installation 
0037 
0038 It is recommended to use [uv](https://docs.astral.sh/uv/getting-started/installation/)
0039 
0040 ```bash
0041 cd pyrobird                 # directory inside main firebird repo. NOT pyrobird/pyrobird - this is lib
0042 uv venv                     # create virtual environment
0043 source .venv/bin/activate   # activate it
0044 uv sync                     # install all requirements
0045 ```
0046 
0047 Still possible without uv
0048 
0049 ```bash
0050 python -m pip install --editable .[dev,batch]
0051 
0052 # with xrootd
0053 python -m pip install --editable .[dev,batch,xrootd]
0054 ```
0055 
0056 Running with Gunicorn (development mode)
0057 
0058 ```bash
0059 gunicorn --bind 0.0.0.0:5454 pyrobird.server:flask_app --log-level debug --capture-output
0060 ```
0061 
0062 
0063 ## Contributing
0064 
0065 - [PEP8](https://peps.python.org/pep-0008/) is required
0066 - [Use Numpy style dockstring comments](https://numpydoc.readthedocs.io/en/latest/format.html)
0067 - [pytest](https://docs.pytest.org/en/latest/) is used for unit tests. Aim for comprehensive coverage of the new code.
0068 - Utilize [type hints](https://docs.python.org/3/library/typing.html) wherever is possible to enhance readability and reduce errors.
0069 - Use of specific exceptions for error handling is better. As described in the [Python documentation](https://docs.python.org/3/tutorial/errors.html) rather than general exceptions.
0070 - Contributions are subject to code review. Please submit pull requests (PRs) against the `main` branch for any contributions.
0071 - Manage dependencies appropriately. Add new dependencies to `pyproject.toml`. Provide a justification for new dependencies
0072 
0073 ## Testing
0074 
0075 To install dependencies with testing libraries included 
0076 
0077 ```bash
0078 pip install .[test]
0079 ```
0080 
0081 Navigate to the pyrobird/tests and execute:
0082 
0083 ```bash
0084 pytest
0085 
0086 # To stop immediately on error and enter debugging mode
0087 pytest -x --pdb 
0088 ```
0089 
0090 
0091 ## Development install
0092 
0093 ```
0094 python -m pip install --upgrade --editable  .[dev]
0095 ```
0096 
0097 # Pyrobird Server
0098 
0099 This server allows Firebird to work with local files and the local file system as
0100 well as to complement frontend features such as opening XRootD files, etc.
0101 
0102 Serve Pirobird locally and have access to files in current directory: 
0103 
0104 ```bash
0105 pyrobird serve
0106 ```
0107 
0108 **pyrobird** (backend) allows **Firebird** (frontend) to access certain files on your system. 
0109 For this reason pyrobird server has multiple endpoints such as `/api/v1/download` 
0110 which allows to download files. There are library files served and by default Firebird
0111 has access to files in your current directory or a directory provided via `--work-path` flag
0112 
0113 
0114 #### **Available Options**
0115 
0116 | Option                     | Short | Type    | Default | Description                                                                                                                                                                                                                                   |
0117 |----------------------------|-------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
0118 | `--allow-any-file`         |       | Flag    | `False` | Allow unrestricted access to download files in the system. When enabled, the server allows downloads of all files which the running user has access to. **Use with caution**: It is considered dangerous in production environments.            |
0119 | `--allow-cors`             |       | Flag    | `False` | Enable CORS for downloaded files. This option should be used if you need to support web applications from different domains accessing the files, such as serving your server from a central Firebird server.                                       |
0120 | `--disable-files`          |       | Flag    | `False` | Disable all file downloads from the server. This option will prevent any file from being downloaded, enhancing security by restricting file access.                                                                                              |
0121 | `--work-path TEXT`         |       | String  | `CWD`   | Set the base directory path for file downloads. Defaults to the current working directory. Use this option to specify where the server should look for files when handling download requests.                                                       |
0122 
0123 
0124 > `--allow-any-file` - allows unrestricted access to download files in a system.
0125 > When enabled, the server allows downloads of all files that user has access to.
0126 > When disabled - only files in `--work-path` and its subdirectories are allowed to be downloaded.
0127 > This option could be considered safe on personal machines with a single user, 
0128 > who runs localhost server in a terminal
0129 > 
0130 > (!) It is considered dangerous in all other cases: farms, interactive nodes, production environments, servers, etc.
0131 > Just think `/etc/passwd` will be accessible through  `localhost:port/api/v1/download?f=/etc/passwd`
0132 > 
0133 > So security wise, it is better to use `--work-path` than `--allow-any-file`
0134 
0135 
0136 - Start server with default settings, Firebird works with files in current directory:
0137    ```bash
0138   pyrobird serve
0139    ```
0140 
0141 - Set where Firebird will take files from:
0142    
0143    ```bash
0144    pyrobird serve --work-path=/home/username/datafiles
0145    ```
0146 
0147    Now if you set file `local://filename.root` in Firebird UI,
0148    the file `/home/username/datafiles/filename.root` will be opened
0149 
0150 
0151 ## API Documentation
0152 
0153 This is technical explanation of what is under the hood of the server part
0154 
0155 ## Features
0156 
0157 - **Secure File Downloading**: Download files with access control to prevent unauthorized access.
0158 - **EDM4eic Event Processing**: Extract and convert specific events from EDM4eic files to JSON.
0159 - **Static File Serving**: Serve frontend assets seamlessly alongside API endpoints.
0160 - **Dynamic Configuration**: Serve configuration files with real-time server information.
0161 - **CORS Support**: Enable Cross-Origin Resource Sharing for specified routes.
0162 
0163 ### Configuration Options
0164 - **DOWNLOAD_PATH**: `str[getcwd()]`, Specifies the directory from which files can be downloaded when using relative paths.
0165 - **PYROBIRD_DOWNLOAD_IS_DISABLED**: `bool[False]` If set to `True`, all download functionalities are disabled.
0166 - **PYROBIRD_DOWNLOAD_IS_UNRESTRICTED**: `bool[False]`, allows unrestricted access to download any file, including sensitive ones.
0167 - **CORS_IS_ALLOWED**: `bool[False]`, If set to `True`, enables Cross-Origin Resource Sharing (CORS) for download routes.
0168 
0169 
0170 
0171 
0172 The API provides endpoints for downloading files, processing EDM4eic events, and serving configuration files. It also includes static file serving for frontend assets.
0173 
0174 ---
0175 
0176 ### Download File
0177 
0178 #### **Endpoint**
0179 
0180 ```
0181 GET /api/v1/download
0182 GET /api/v1/download/<path:filename>
0183 ```
0184 
0185 #### **Description**
0186 
0187 Allows users to download specified files. The download can be restricted based on configuration settings to prevent unauthorized access to sensitive files.
0188 
0189 #### **Parameters**
0190 
0191 - **Query Parameters**:
0192     - `filename` (optional): The name or path of the file to download.
0193     - `f` (optional): An alternative parameter for the filename.
0194 
0195 - **Path Parameters**:
0196     - `filename` (optional): The path of the file to download.
0197 
0198 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0199 
0200 #### **Usage**
0201 
0202 1. **Download via Query Parameter**
0203 
0204    ```bash
0205    curl -O "http://localhost:5454/api/v1/download?filename=example.txt"
0206    ```
0207 
0208 2. **Download via URL Path**
0209 
0210    ```bash
0211    curl -O "http://localhost:5454/api/v1/download/example.txt"
0212    ```
0213 
0214 #### **Security Considerations**
0215 
0216 - **Access Control**: Ensure that `DOWNLOAD_ALLOW_UNRESTRICTED` is set appropriately to prevent unauthorized access.
0217 - **Path Traversal**: The server sanitizes file paths to prevent directory traversal attacks.
0218 
0219 ---
0220 
0221 ### Open EDM4eic Event
0222 
0223 #### **Endpoint**
0224 
0225 ```
0226 GET /api/v1/convert/edm4eic/<int:event_number>
0227 GET /api/v1/convert/edm4eic/<int:event_number>/<path:filename>
0228 ```
0229 
0230 #### **Description**
0231 
0232 Processes an EDM4eic file to extract a specific event and returns the event data in JSON format. Supports both local and remote files.
0233 
0234 #### **Parameters**
0235 
0236 - **Path Parameters**:
0237     - `event_number` (required): The number of the event to extract.
0238     - `filename` (optional): The path or URL of the EDM4eic file.
0239 
0240 - **Query Parameters**:
0241     - `filename` (optional): The name or path of the file to process.
0242     - `f` (optional): An alternative parameter for the filename.
0243 
0244 **Note**: You can provide the filename either as a query parameter or as part of the URL path.
0245 
0246 #### **Usage**
0247 
0248 1. **Process Local File via Query Parameter**
0249 
0250    ```bash
0251    curl "http://localhost:5454/api/v1/convert/edm4eic/5?filename=path/to/file.edm4eic.root"
0252    ```
0253 
0254 2. **Process Remote File via URL Path**
0255 
0256    ```bash
0257    curl "http://localhost:5454/api/v1/convert/edm4eic/5/http://example.com/data/file.edm4eic.root"
0258    ```
0259 
0260 ### Asset Configuration
0261 
0262 #### **Endpoint**
0263 
0264 ```
0265 GET /assets/config.jsonc
0266 ```
0267 
0268 #### **Description**
0269 
0270 Serves the asset configuration file (`config.jsonc`) with additional server information injected dynamically.
0271 
0272 #### **Usage**
0273 
0274 ```bash
0275 curl "http://localhost:5454/assets/config.jsonc"
0276 ```
0277 
0278 ### Publishing
0279 
0280 ```bash
0281 pip install --upgrade build twine
0282 python -m build && python -m twine upload dist/* 
0283 
0284 # You will have to setup your pip authentication key
0285 ```