Compare commits

..

No commits in common. "cfbc53afc6c0ae25e56585f0c1bcaf9cbcf47fc4" and "a0658af0b061f4d26f35f54216b38e5c2f348ed4" have entirely different histories.

13 changed files with 27 additions and 306 deletions

View File

@ -1,38 +0,0 @@
name: CI Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9, "3.10", 3.11, 3.12, 3.13]
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e .[dev]
- name: Lint code with flake8
run: flake8
- name: Type checking with mypy
run: mypy
- name: Run tests with pytest
run: pytest

View File

@ -1,37 +0,0 @@
name: Build and Deploy Docs
on:
push:
branches:
- main
permissions:
contents: write
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.x"
- name: Install dependencies
run: pip install sphinx sphinx_rtd_theme sphinx-autodoc-typehints myst-parser
- name: Generate Class List
run: |
pip install .
python ./docs/source/generate_class_list.py
- name: Build Docs
run: |
cd docs
sphinx-apidoc -o ./source/ ../src/ -M --no-toc
rm ./source/*.rst
make html
touch ./build/html/.nojekyll
- name: Deploy to GitHub Pages
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages
folder: docs/build/html

View File

@ -1,35 +0,0 @@
name: Publish to PyPI
on:
push:
tags:
- "v*"
jobs:
publish:
name: Build and publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Ensure this is main branch
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
run: echo "Proceeding with publish"
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install build tools
run: python -m pip install --upgrade build twine
- name: Build package
run: python -m build
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: python -m twine upload dist/*

4
.gitignore vendored
View File

@ -45,7 +45,3 @@ dmypy.json
# pytype static type analyzer # pytype static type analyzer
.pytype/ .pytype/
# docs
docs/build/
docs/source/modules.md

View File

@ -1,20 +0,0 @@
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

View File

@ -1,35 +0,0 @@
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)
if "%1" == "" goto help
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd

View File

@ -1,32 +0,0 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import os
import sys
sys.path.insert(0, os.path.abspath("../src/"))
project = 'pelfy'
copyright = '2025, Nicolas Kruse'
author = 'Nicolas Kruse'
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon", "sphinx_autodoc_typehints", "myst_parser"]
templates_path = ['_templates']
exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
# html_theme = 'alabaster'
html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
autodoc_inherit_docstrings = True

View File

@ -1,60 +0,0 @@
import importlib
import inspect
import fnmatch
from io import TextIOWrapper
def write_classes(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None:
module = importlib.import_module(module_name)
classes = [
name for name, obj in inspect.getmembers(module, inspect.isclass)
if (obj.__module__ == module_name and
any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude) and
obj.__doc__ and '(Automatic generated stub)' not in obj.__doc__)
]
"""Write the classes to the file."""
f.write(f'## {title}\n\n')
if description:
f.write(f'{description}\n\n')
for cls in classes:
f.write('```{eval-rst}\n')
f.write(f'.. autoclass:: {module_name}.{cls}\n')
f.write(' :members:\n')
f.write(' :undoc-members:\n')
f.write(' :show-inheritance:\n')
f.write(' :inherited-members:\n')
if title != 'Base classes':
f.write(' :exclude-members: select\n')
f.write('```\n\n')
def write_functions(f: TextIOWrapper, patterns: list[str], module_name: str, title: str, description: str = '', exclude: list[str] = []) -> None:
module = importlib.import_module(module_name)
classes = [
name for name, obj in inspect.getmembers(module, inspect.isfunction)
if (obj.__module__ == module_name and
any(fnmatch.fnmatch(name, pat) for pat in patterns if pat not in exclude))
]
"""Write the classes to the file."""
f.write(f'## {title}\n\n')
if description:
f.write(f'{description}\n\n')
for func in classes:
f.write('```{eval-rst}\n')
f.write(f'.. autofunction:: {module_name}.{func}\n')
f.write('```\n\n')
with open('docs/source/modules.md', 'w') as f:
f.write('# Functions and classes\n\n')
write_functions(f, ['*'], 'pelfy', title='Functions')
write_classes(f, ['elf_*'], 'pelfy', title='ELF classes')
write_classes(f, ['*_list'], 'pelfy', title='Lists of ELF classes')

View File

@ -1,10 +0,0 @@
```{toctree}
:maxdepth: 2
:caption: Contents:
readme
modules
```
```{include} ../../README.md
```

View File

@ -1,2 +0,0 @@
```{include} ../../README.md
```

View File

@ -1,21 +1,21 @@
[project] [project]
name = "pelfy" name = "pelfy"
version = "1.0.2" version = "1.0.1"
authors = [ authors = [
{ name="Nicolas Kruse", email="nicolas.kruse@nonan.net" }, { name="Nicolas Kruse", email="nicolas.kruse@nonan.net" },
] ]
description = "Parser for ELF files written in python" description = "Parser for ELF files written in python"
readme = "README.md" readme = "README.md"
requires-python = ">=3.9" requires-python = ">=3.9"
license = "MIT"
classifiers = [ classifiers = [
"Programming Language :: Python :: 3", "Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent", "Operating System :: OS Independent",
] ]
[project.urls] [project.urls]
Homepage = "https://github.com/Nonannet/pelfy" Homepage = "https://github.com/Nonannet/pelfy"
Issues = "https://github.com/Nonannet/pelfy/issues" Issues = "https://github.com/Nonannet/pelfy/issues"
documentation = "https://nonannet.github.io/pelfy/"
[build-system] [build-system]
requires = ["setuptools>=61.0"] requires = ["setuptools>=61.0"]
@ -24,11 +24,6 @@ build-backend = "setuptools.build_meta"
[tool.setuptools.packages.find] [tool.setuptools.packages.find]
where = ["src"] where = ["src"]
[project.optional-dependencies]
dev = [
"pytest", "flake8", "mypy"
]
[tool.mypy] [tool.mypy]
files = ["src", "tests"] files = ["src", "tests"]
strict = true strict = true

View File

@ -8,7 +8,7 @@ Typical usage example:
from . import fields_data as fdat from . import fields_data as fdat
from . import output_formatter from . import output_formatter
from typing import TypeVar, Literal, Iterable, Generic, Iterator, Generator, Optional, Union from typing import TypeVar, Literal, Iterable, Generic, Iterator, Generator
_T = TypeVar('_T') _T = TypeVar('_T')
@ -58,7 +58,7 @@ class elf_symbol():
else: else:
self.name = '' self.name = ''
self.section: Optional[elf_section] = self.file.sections[self['st_shndx']] if self['st_shndx'] < len(self.file.sections) else None self.section: elf_section | None = self.file.sections[self['st_shndx']] if self['st_shndx'] < len(self.file.sections) else None
self.index = index self.index = index
@ -100,7 +100,7 @@ class elf_symbol():
ret.append(reloc) ret.append(reloc)
return relocation_list(ret) return relocation_list(ret)
def __getitem__(self, key: Union[str, int]) -> int: def __getitem__(self, key: str | int) -> int:
if isinstance(key, str): if isinstance(key, str):
assert key in self.fields, f'Unknown field name: {key}' assert key in self.fields, f'Unknown field name: {key}'
return self.fields[key] return self.fields[key]
@ -172,7 +172,7 @@ class elf_section():
""" """
return ' '.join(f'{d:02X}' for d in self.data) return ' '.join(f'{d:02X}' for d in self.data)
def __getitem__(self, key: Union[str, int]) -> int: def __getitem__(self, key: str | int) -> int:
if isinstance(key, str): if isinstance(key, str):
assert key in self.fields, f'Unknown field name: {key}' assert key in self.fields, f'Unknown field name: {key}'
return self.fields[key] return self.fields[key]
@ -226,7 +226,7 @@ class elf_relocation():
self.calculation = '' self.calculation = ''
self.target_section: elf_section = file.sections[sh_info] self.target_section: elf_section = file.sections[sh_info]
def __getitem__(self, key: Union[str, int]) -> int: def __getitem__(self, key: str | int) -> int:
if isinstance(key, str): if isinstance(key, str):
assert key in self.fields, f'Unknown field name: {key}' assert key in self.fields, f'Unknown field name: {key}'
return self.fields[key] return self.fields[key]
@ -251,7 +251,7 @@ class elf_list(Generic[_T]):
def __init__(self, data: Iterable[_T]): def __init__(self, data: Iterable[_T]):
self._data = list(data) self._data = list(data)
def __getitem__(self, key: Union[str, int]) -> _T: def __getitem__(self, key: int | str) -> _T:
if isinstance(key, str): if isinstance(key, str):
elements = [el for el in self._data if getattr(el, 'name', '') == key] elements = [el for el in self._data if getattr(el, 'name', '') == key]
assert elements, f'Unknown name: {key}' assert elements, f'Unknown name: {key}'
@ -265,20 +265,20 @@ class elf_list(Generic[_T]):
def __iter__(self) -> Iterator[_T]: def __iter__(self) -> Iterator[_T]:
return iter(self._data) return iter(self._data)
def _compact_table(self) -> tuple[list[str], list[list[Union[str, int]]], list[str]]: def _compact_table(self) -> tuple[list[str], list[list[str | int]], list[str]]:
return [], [[]], [] return [], [[]], []
def _repr_table(self, format: output_formatter.table_format, raw_data: bool = False) -> str: def _repr_table(self, format: output_formatter.table_format, raw_data: bool = False) -> str:
if raw_data and len(self): if raw_data and len(self):
table_dict: list[dict[str, int]] = [el.__dict__.get('fields', {' ': 0}) for el in self] table_dict: list[dict[str, int]] = [el.__dict__.get('fields', {' ': 0}) for el in self]
columns = list(table_dict[0].keys()) columns = list(table_dict[0].keys())
data: list[list[Union[str, int]]] = [list(el.values()) for el in table_dict] data: list[list[str | int]] = [list(el.values()) for el in table_dict]
radj = columns radj = columns
else: else:
columns, data, radj = self._compact_table() columns, data, radj = self._compact_table()
return output_formatter.generate_table(data, columns, right_adj_col=radj, format=format) return output_formatter.generate_table(data, columns, right_adj_col=radj, format=format)
def to_dict_list(self) -> list[dict[str, Union[str, int]]]: def to_dict_list(self) -> list[dict[str, str | int]]:
"""Exporting the ELF item data table to a list of dicts. It can be used with pandas: """Exporting the ELF item data table to a list of dicts. It can be used with pandas:
df = pandas.DataFrame(elements.to_dict_list()) df = pandas.DataFrame(elements.to_dict_list())
@ -317,30 +317,30 @@ class elf_list(Generic[_T]):
class section_list(elf_list[elf_section]): class section_list(elf_list[elf_section]):
"""A class for representing a list of ELF section """A class for representing a list of ELF section
""" """
def _compact_table(self) -> tuple[list[str], list[list[Union[str, int]]], list[str]]: def _compact_table(self) -> tuple[list[str], list[list[int | str]], list[str]]:
columns = ['index', 'name', 'type', 'description'] columns = ['index', 'name', 'type', 'description']
data: list[list[Union[str, int]]] = [[item.index, item.name, item.type, data: list[list[str | int]] = [[item.index, item.name, item.type,
item.description] for item in self] item.description] for item in self]
return columns, data, ['index'] return columns, data, ['index']
class symbol_list(elf_list[elf_symbol]): class symbol_list(elf_list[elf_symbol]):
"""A class for representing a list of ELF symbols """A class for representing a list of ELF symbols
""" """
def _compact_table(self) -> tuple[list[str], list[list[Union[str, int]]], list[str]]: def _compact_table(self) -> tuple[list[str], list[list[int | str]], list[str]]:
columns = ['index', 'name', 'info', 'size', 'stb', 'section', 'description'] columns = ['index', 'name', 'info', 'size', 'stb', 'section', 'description']
data: list[list[Union[str, int]]] = [[item.index, item.name, item.info, item.fields['st_size'], data: list[list[str | int]] = [[item.index, item.name, item.info, item.fields['st_size'],
item.stb, item.section.name if item.section else '', item.description] for item in self] item.stb, item.section.name if item.section else '', item.description] for item in self]
return columns, data, ['index', 'size'] return columns, data, ['index', 'size']
class relocation_list(elf_list[elf_relocation]): class relocation_list(elf_list[elf_relocation]):
"""A class for representing a list of ELF relocations """A class for representing a list of ELF relocations
""" """
def _compact_table(self) -> tuple[list[str], list[list[Union[str, int]]], list[str]]: def _compact_table(self) -> tuple[list[str], list[list[int | str]], list[str]]:
columns = ['index', 'symbol name', 'type', 'calculation', 'bits'] columns = ['index', 'symbol name', 'type', 'calculation', 'bits']
data: list[list[Union[str, int]]] = [[item.index, item.symbol.name, item.type, data: list[list[str | int]] = [[item.index, item.symbol.name, item.type,
item.calculation, item.bits] for item in self] item.calculation, item.bits] for item in self]
return columns, data, ['index', 'bits'] return columns, data, ['index', 'bits']
@ -364,7 +364,7 @@ class elf_file:
string_table_section: The string table section (first section with string_table_section: The string table section (first section with
the name .strtab) the name .strtab)
""" """
def __init__(self, data: Union[bytes, bytearray]): def __init__(self, data: bytes | bytearray):
"""Initializes an ELF file instance """Initializes an ELF file instance
Args: Args:
@ -415,7 +415,7 @@ class elf_file:
offs = self.fields['e_shoff'] + i * self.fields['e_shentsize'] offs = self.fields['e_shoff'] + i * self.fields['e_shentsize']
yield {fn: self._read_from_sh_field(offs, fn) for fn in fdat.section_header.keys()} yield {fn: self._read_from_sh_field(offs, fn) for fn in fdat.section_header.keys()}
def _list_symbols(self, section_index: Optional[int] = None) -> Generator[elf_symbol, None, None]: def _list_symbols(self, section_index: int | None = None) -> Generator[elf_symbol, None, None]:
if self.symbol_table_section: if self.symbol_table_section:
offs = self.symbol_table_section['sh_offset'] offs = self.symbol_table_section['sh_offset']
@ -438,7 +438,7 @@ class elf_file:
if section_index is None or section_index == ret['st_shndx']: if section_index is None or section_index == ret['st_shndx']:
yield elf_symbol(self, ret, j) yield elf_symbol(self, ret, j)
def get_relocations(self, reloc_section: Optional[Union[elf_section, str, list[str]]] = None) -> relocation_list: def get_relocations(self, reloc_section: elf_section | str | list[str] | None = None) -> relocation_list:
"""List relocations. """List relocations.
Args: Args:

View File

@ -1,7 +1,6 @@
import pelfy import pelfy
def test_readme_example():
def test_readme_example() -> None:
elf = pelfy.open_elf_file('tests/obj/test-c-riscv64-linux-gnu-gcc-12-O3.o') elf = pelfy.open_elf_file('tests/obj/test-c-riscv64-linux-gnu-gcc-12-O3.o')
assert ' description ' in elf.sections.to_markdown() assert ' description ' in elf.sections.to_markdown()