diff --git a/.github/smoke_installed.py b/.github/smoke_installed.py new file mode 100644 index 0000000..1013a8f --- /dev/null +++ b/.github/smoke_installed.py @@ -0,0 +1,40 @@ +"""Use the installed package the way a caller does: write a small schedule, +parse it, validate it and run the command line on it. Run from outside the +repository, so the package is the installed copy, not scripts/.""" +import os +import subprocess +import sys +import tempfile + +import cpp_xer_parser as p +import cpp_xer_parser.xer_parser as x + +assert not os.path.isdir(os.path.join(os.getcwd(), "scripts")), "run from outside the repo" +assert x.__file__.startswith(sys.prefix), x.__file__ +assert x._VALIDATION_AVAILABLE +assert x.get_profile.__module__ == "cpp_xer_parser.config_profiles" + +T = "\t" +rows = [ + ["ERMHDR", "24.12", "2026-01-05", "Project", "admin", "Test", "db", "Project Management", "CAD"], + ["%T", "PROJECT"], ["%F", "proj_id", "proj_short_name", "clndr_id", "plan_start_date"], + ["%R", "P1", "SMOKE", "C1", "2026-01-05 08:00"], + ["%T", "CALENDAR"], ["%F", "clndr_id", "clndr_name", "day_hr_cnt", "default_flag", "clndr_data"], + ["%R", "C1", "5-Day", "8", "Y", ""], + ["%T", "TASK"], ["%F", "task_id", "task_code", "task_name", "proj_id", "clndr_id", + "task_type", "status_code", "target_drtn_hr_cnt"], + ["%R", "T1", "A1000", "Start", "P1", "C1", "TT_Task", "TK_NotStart", "40"], + ["%E"], +] +path = os.path.join(tempfile.mkdtemp(), "smoke.xer") +with open(path, "w", encoding="utf-8", newline="") as f: + f.write("\r\n".join(T.join(r) for r in rows) + "\r\n") + +data = p.parse_xer(path) +assert len(p.get_table(data, "TASK")) == 1 +report = p.validate_schedule(data) +assert report is not None +out = subprocess.run([os.path.join(os.path.dirname(sys.executable), "cpp-xer-parser"), + "parse", path], capture_output=True, text=True, check=True).stdout +assert "TASK: 1 records" in out, out +print("cpp_xer_parser", p.__version__, "installed, parsed, validated") diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..b497218 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,79 @@ +name: publish + +# Publishes to PyPI when a GitHub release is published. It uses PyPI trusted +# publishing (OpenID Connect): no API token is stored anywhere. Before the +# first release, the project's owner adds this repository and this workflow +# as a trusted publisher on pypi.org (environment "pypi"). + +on: + release: + types: [published] + +permissions: + contents: read + +jobs: + build: + name: Build the sdist and wheel + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Test the commit being released + run: | + python -m pip install --upgrade pip pytest + pytest tests/ -q + + - name: Build + run: | + python -m pip install --upgrade build twine + python -m build + python -m twine check --strict dist/* + + - name: Install the wheel in a clean environment and parse a schedule + shell: bash + run: | + python -m venv /tmp/pkg + /tmp/pkg/bin/pip install dist/*.whl + cd /tmp && /tmp/pkg/bin/python "$GITHUB_WORKSPACE/.github/smoke_installed.py" + + - name: The release tag names the version being built + shell: bash + run: | + python - <<'EOF' + import os, re, sys + version = re.search(r'(?m)^version = "([^"]+)"$', open('pyproject.toml').read()).group(1) + tag = os.environ['GITHUB_REF_NAME'] + if tag != 'v' + version: + sys.exit('release tag %s is not v%s' % (tag, version)) + # The changelog dates the release (not 'Unreleased'). + heading = r'(?m)^## v%s \u2014 \d{4}-\d{2}-\d{2}$' % re.escape(version) + if not re.search(heading, open('CHANGELOG.md', encoding='utf-8').read()): + sys.exit('CHANGELOG.md has no dated heading for v%s' % version) + EOF + + - uses: actions/upload-artifact@v4 + with: + name: dist + path: dist/ + + publish: + name: Publish to PyPI + needs: build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/project/cpp-xer-parser/ + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + + - uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2bb4f22..ab238fa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python: ['3.10', '3.11', '3.12'] + python: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15'] steps: - uses: actions/checkout@v4 @@ -38,3 +38,35 @@ jobs: python tests/test_calendar_shift_hours_2026_10_10.py python tests/test_p6_rules_2026_10_13.py python tests/test_corrupt_calendar_record_2026_10_28.py + + package: + name: Build, install and use the package (Python ${{ matrix.python }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # The oldest and the newest the classifiers name. + python: ['3.10', '3.15'] + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python }} + + - name: Build and check + run: | + python -m pip install --upgrade pip build twine + python -m build + python -m twine check --strict dist/* + + - name: Install the wheel in a clean environment and use it + shell: bash + run: | + python -m venv /tmp/pkg + /tmp/pkg/bin/pip install dist/*.whl + cd /tmp + /tmp/pkg/bin/python -c "import cpp_xer_parser as p, cpp_xer_parser.xer_parser as x; assert x._VALIDATION_AVAILABLE; assert x.get_profile.__module__ == 'cpp_xer_parser.config_profiles'; print('cpp_xer_parser', p.__version__)" + /tmp/pkg/bin/cpp-xer-parser > usage.txt || test $? -eq 1 + grep -q "parse" usage.txt + /tmp/pkg/bin/python "$GITHUB_WORKSPACE/.github/smoke_installed.py" diff --git a/.gitignore b/.gitignore index 3af07c3..ed3d0d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ # Python __pycache__/ +# Packaging +dist/ +build/ +*.egg-info/ *.pyc *.pyo *.pyd diff --git a/CHANGELOG.md b/CHANGELOG.md index 0891fc4..3c56ad3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ All notable changes to `cpp-xer-parser` are documented here. Versioning follows --- +## v0.6.1 — 2026-11-09 + +The parser installs from PyPI: `pip install cpp-xer-parser`. Nothing it computes changes. + +### Added + +- **Packaging** (`pyproject.toml`). Installed, `scripts/` is the package `cpp_xer_parser`, so `from cpp_xer_parser import parse_xer` works and the generic module names (`validation`, `config_profiles`) do not land at the top level of an environment. The command `cpp-xer-parser` runs the parser's command line. A clone with `scripts/` on `sys.path` works as before: `xer_parser.py` binds its two helpers from the package when installed, else as top-level modules. +- **Release workflow** (`.github/workflows/publish.yml`). Publishing a GitHub release builds the sdist and wheel, checks the release tag names the version in `pyproject.toml`, and uploads to PyPI by trusted publishing, so no API token is stored. CI also builds the wheel, installs it in a clean environment and runs the command on every push. + ## v0.6.0 — 2026-11-08 The DCMA-14 profiles in `config_profiles.py` now carry the thresholds CPP's own tools use, as CPP's written DCMA-14 definition sets them ([criticalpathpartners.ca/dcma-14-definition.html](https://criticalpathpartners.ca/dcma-14-definition.html)). The file is identical to the copy [`cpp-critical-path-validator`](https://github.com/danafitkowski/cpp-critical-path-validator) bundles. The parser itself (`xer_parser.py`) is unchanged. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..fc49293 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include CHANGELOG.md diff --git a/README.md b/README.md index b5669b7..904483a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # cpp-xer-parser -[![license: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE) +[![license: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/danafitkowski/cpp-xer-parser/blob/main/LICENSE) [![python: 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/) [![tests](https://github.com/danafitkowski/cpp-xer-parser/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/danafitkowski/cpp-xer-parser/actions/workflows/test.yml) -[![version: 0.6.0](https://img.shields.io/badge/version-0.6.0-blue.svg)](CHANGELOG.md) +[![version: 0.6.1](https://img.shields.io/badge/version-0.6.1-blue.svg)](https://github.com/danafitkowski/cpp-xer-parser/blob/main/CHANGELOG.md) A standalone parser and generator for Primavera P6 XER files. Pure Python 3.10+, MIT-licensed, no third-party runtime dependencies. @@ -37,10 +37,17 @@ It comes out of forensic schedule analysis, where a silently mangled field becom ## Install +```bash +pip install cpp-xer-parser +``` + +Installed, the modules are the package `cpp_xer_parser`. The command `cpp-xer-parser` runs the same command line as `python scripts/xer_parser.py`. + +Or use a clone with no install: no external dependencies, just put `scripts/` on your `sys.path`. + ```bash git clone https://github.com/danafitkowski/cpp-xer-parser cd cpp-xer-parser -# No external dependencies. Just put scripts/ on your sys.path. ``` The parser is pure Python 3.10+ with no third-party runtime dependencies. @@ -50,10 +57,9 @@ The parser is pure Python 3.10+ with no third-party runtime dependencies. ## Quick start ```python -import sys -sys.path.insert(0, 'scripts') - -from xer_parser import parse_xer, print_summary, get_table, build_wbs_map +from cpp_xer_parser import parse_xer, print_summary, get_table, build_wbs_map +# From a clone instead: sys.path.insert(0, 'scripts'), then +# from xer_parser import parse_xer, print_summary, get_table, build_wbs_map # Parse data = parse_xer('path/to/your.xer') @@ -117,8 +123,9 @@ for t in tasks[:3]: A blank `TASK.clndr_id` means the project calendar, not "no calendar". MPXJ writes it blank for every task of a converted MS Project file that has no task-level calendar. Looked up as it stands, `cal_map.get(task['clndr_id'])` returns `None`, and whatever runs next falls to its last resort: 8 h/day in `duration_hours_to_days`, without a word, and in `cpp-cpm-engine` a continuous seven-day week, with an ALERT, unless the caller hands it the project calendar. Take the TASK rows through the resolver before looking a calendar up: ```python -from xer_parser import (get_table, get_calendar_map, duration_hours_to_days, - resolve_task_calendars, with_resolved_calendars) +from cpp_xer_parser.xer_parser import ( + get_table, get_calendar_map, duration_hours_to_days, + resolve_task_calendars, with_resolved_calendars) resolution = resolve_task_calendars(data) tasks = with_resolved_calendars(get_table(data, 'TASK'), resolution) @@ -147,7 +154,7 @@ The most analytically interesting feature in the repo. The `compute_half_step_xe The forensic value: anything that moves in the half-step moved because work didn't happen as planned. Anything that moves between the half-step and the full update moved because the contractor revised logic or scope. ```python -from xer_parser import compute_half_step_xer +from cpp_xer_parser import compute_half_step_xer result = compute_half_step_xer( base_xer_path='period_start.xer', @@ -225,7 +232,7 @@ Two consequences worth being explicit about: ## License -MIT — see [LICENSE](LICENSE). +MIT — see [LICENSE](https://github.com/danafitkowski/cpp-xer-parser/blob/main/LICENSE). You may use this parser in commercial forensic consulting, in academic research, in your own scheduling product, in court-filed expert reports. Just keep the copyright notice. @@ -233,7 +240,7 @@ You may use this parser in commercial forensic consulting, in academic research, ## Contributing -See [CONTRIBUTING.md](CONTRIBUTING.md). Bug reports and pull requests are welcome. +See [CONTRIBUTING.md](https://github.com/danafitkowski/cpp-xer-parser/blob/main/CONTRIBUTING.md). Bug reports and pull requests are welcome. --- diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..1d6e5f5 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,46 @@ +[build-system] +requires = ["setuptools>=77", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "cpp-xer-parser" +version = "0.6.1" +description = "Parse and write Primavera P6 XER files in pure Python: every table, calendars, cross-reference maps, half-step XERs and DCMA-14 profiles." +readme = "README.md" +license = "MIT" +license-files = ["LICENSE"] +requires-python = ">=3.10" +authors = [{ name = "Dana Fitkowski" }] +keywords = ["primavera", "p6", "xer", "schedule", "cpm", "construction", "forensic", "dcma"] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Intended Audience :: Other Audience", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: 3.15", + "Topic :: Office/Business :: Scheduling", + "Topic :: File Formats", +] +dependencies = [] + +[project.urls] +Homepage = "https://github.com/danafitkowski/cpp-xer-parser" +Changelog = "https://github.com/danafitkowski/cpp-xer-parser/blob/main/CHANGELOG.md" +Issues = "https://github.com/danafitkowski/cpp-xer-parser/issues" + +[project.scripts] +cpp-xer-parser = "cpp_xer_parser.xer_parser:main" + +# The modules live in scripts/, where the tests and a plain clone find them as +# top-level modules. Installed, they are the package cpp_xer_parser, so the +# generic names (validation, config_profiles) do not land at the top level. +[tool.setuptools] +package-dir = { "cpp_xer_parser" = "scripts" } +packages = ["cpp_xer_parser"] diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 0000000..efb070d --- /dev/null +++ b/scripts/__init__.py @@ -0,0 +1,38 @@ +"""cpp-xer-parser, installed: ``pip install cpp-xer-parser``. + +The package is the repository's scripts/ directory. Everything the parser +offers is in ``cpp_xer_parser.xer_parser``; the names below are the ones +most callers need:: + + from cpp_xer_parser import parse_xer, get_table, print_summary + +A plain clone does not import this file: it puts scripts/ on sys.path and +imports ``xer_parser`` directly, as the README's quick start shows. +""" +from .xer_parser import ( # noqa: F401 + build_activity_code_map, + build_predecessor_map, + build_resource_map, + build_udf_map, + build_wbs_map, + compute_half_step_xer, + generate_summary, + generate_xer, + get_calendar_map, + get_fields, + get_table, + get_work_days_between, + parse_xer, + print_summary, + validate_schedule, +) + +__version__ = '0.6.1' + +__all__ = [ + 'parse_xer', 'get_table', 'get_fields', 'get_calendar_map', + 'get_work_days_between', 'build_wbs_map', 'build_resource_map', + 'build_predecessor_map', 'build_activity_code_map', 'build_udf_map', + 'generate_summary', 'print_summary', 'generate_xer', + 'compute_half_step_xer', 'validate_schedule', '__version__', +] diff --git a/scripts/xer_parser.py b/scripts/xer_parser.py index cc3b76d..599a33b 100644 --- a/scripts/xer_parser.py +++ b/scripts/xer_parser.py @@ -49,7 +49,12 @@ # ───────────────────────────────────────────── _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) _CPP_COMMON = os.path.normpath(os.path.join(_SCRIPT_DIR, '..', '..', '_cpp_common', 'scripts')) -if os.path.isdir(_CPP_COMMON) and _CPP_COMMON not in sys.path: +# The package this module was imported from: 'cpp_xer_parser' when installed, +# '' from a clone or run as a script. __spec__.parent, not __package__, which +# Python 3.15 schedules for removal. +_PKG = __spec__.parent if __spec__ is not None else '' +# Only from a clone: an installed copy has no sibling _cpp_common. +if not _PKG and os.path.isdir(_CPP_COMMON) and _CPP_COMMON not in sys.path: sys.path.insert(0, _CPP_COMMON) # Optional imports, guarded so xer_parser stays useful in callers that do not @@ -64,8 +69,14 @@ # raised RuntimeError on a clean clone even though their dependencies were # sitting next to them. Keep these blocks separate. try: - from validation import Finding, ValidationReport, BLOCK, WARN, INFO, PASS - from config_profiles import get_profile + if _PKG: + # Installed with pip, the modules are the package cpp_xer_parser. + from .validation import Finding, ValidationReport, BLOCK, WARN, INFO, PASS + from .config_profiles import get_profile + else: + # A clone with scripts/ on sys.path: they are top-level modules. + from validation import Finding, ValidationReport, BLOCK, WARN, INFO, PASS + from config_profiles import get_profile _VALIDATION_AVAILABLE = True except ImportError: _VALIDATION_AVAILABLE = False diff --git a/tests/test_packaging.py b/tests/test_packaging.py new file mode 100644 index 0000000..f300458 --- /dev/null +++ b/tests/test_packaging.py @@ -0,0 +1,60 @@ +"""The repository installs as the package cpp_xer_parser. + +pyproject.toml maps scripts/ to the package cpp_xer_parser. These checks +load scripts/ as that package, the way an installed copy is imported, so +they run without building anything: the relative imports bind validation +and config_profiles inside the package, and the names __init__ exports +exist. The CI package job builds the wheel, installs it in a clean +environment and runs the console script. +""" +import importlib.util +import os +import re +import sys + +HERE = os.path.dirname(os.path.abspath(__file__)) +ROOT = os.path.dirname(HERE) +SCRIPTS = os.path.join(ROOT, 'scripts') + + +def _load_as_package(): + for name in [n for n in sys.modules if n == 'cpp_xer_parser' + or n.startswith('cpp_xer_parser.')]: + del sys.modules[name] + spec = importlib.util.spec_from_file_location( + 'cpp_xer_parser', os.path.join(SCRIPTS, '__init__.py'), + submodule_search_locations=[SCRIPTS]) + pkg = importlib.util.module_from_spec(spec) + sys.modules['cpp_xer_parser'] = pkg + spec.loader.exec_module(pkg) + return pkg + + +def test_the_package_binds_its_own_helpers(): + pkg = _load_as_package() + xp = sys.modules['cpp_xer_parser.xer_parser'] + assert xp._VALIDATION_AVAILABLE is True + # The helpers are the package's, not top-level modules. + assert xp.get_profile.__module__ == 'cpp_xer_parser.config_profiles' + assert xp.ValidationReport.__module__ == 'cpp_xer_parser.validation' + for name in pkg.__all__: + assert hasattr(pkg, name), name + + +def test_the_version_agrees_everywhere(): + # tomllib is 3.11+; the project supports 3.10, so read the one line. + with open(os.path.join(ROOT, 'pyproject.toml'), encoding='utf-8') as f: + version = re.search(r'(?m)^version = "([^"]+)"$', f.read()).group(1) + pkg = _load_as_package() + assert pkg.__version__ == version + with open(os.path.join(ROOT, 'CHANGELOG.md'), encoding='utf-8') as f: + assert ('## v%s ' % version) in f.read() + + +def test_a_clone_still_imports_the_top_level_modules(): + sys.path.insert(0, SCRIPTS) + try: + import xer_parser + assert xer_parser._VALIDATION_AVAILABLE is True + finally: + sys.path.remove(SCRIPTS)