Metadata-Version: 2.4
Name: jprotect
Version: 0.1.0
Summary: Tool bao ve source code module Odoo theo huong an toan.
Author-email: Duong Tien Hieu <tienhieu1212@gmail.com>
License: PolyForm Noncommercial License 1.0.0
        
        https://polyformproject.org/licenses/noncommercial/1.0.0
        
        Required Notice: Copyright (c) 2024 Duong Tien Hieu (tienhieu1212@gmail.com)
        
        Acceptance
        
        In order to get any license under these terms, you must agree to them as
        both strict obligations and conditions to all your licenses.
        
        Copyright License
        
        The licensor grants you a copyright license for the software to do
        everything you might do with the software that would otherwise infringe
        the licensor's copyright in it for any permitted purpose. However, you
        may only distribute the software according to Distribution License and
        make changes or new works based on the software according to Changes and
        New Works License.
        
        Distribution License
        
        The licensor grants you an additional copyright license to distribute
        copies of the software. Your license to distribute covers distributing
        the software with changes and new works permitted by Changes and New
        Works License.
        
        Notices
        
        You must ensure that anyone who gets a copy of any part of the software
        from you also gets a copy of these terms or the URL for them above, as
        well as copies of any plain-text lines beginning with Required Notice:
        that the licensor provided with the software.
        
        Changes and New Works License
        
        The licensor grants you an additional copyright license to make changes
        and new works based on the software for any permitted purpose.
        
        Patent License
        
        The licensor grants you a patent license for the software that covers
        patent claims the licensor can license, or becomes able to license, that
        you would infringe by using the software.
        
        Noncommercial Purposes
        
        Any noncommercial purpose is a permitted purpose.
        
        Personal Uses
        
        Personal use for research, experiment, and testing for the benefit of
        public knowledge, personal study, private entertainment, hobby projects,
        amateur pursuits, or religious observance, without any anticipated
        commercial application, is use for a permitted purpose.
        
        Noncommercial Organizations
        
        Use by any charitable organization, educational institution, public
        research organization, public safety or health organization,
        environmental protection organization, or government institution is use
        for a permitted purpose regardless of the source of funding or
        obligations resulting from the funding.
        
        Fair Use
        
        You may have "fair use" rights for the software under the law. These
        terms do not limit them.
        
        No Other Rights
        
        These terms do not allow you to sublicense or transfer any of your
        licenses to anyone else, or prevent the licensor from granting licenses
        to anyone else. These terms do not imply any other licenses.
        
        Patent Defense
        
        If you make any written claim that the software infringes or contributes
        to infringement of any patent, your patent license for the software
        granted under these terms ends immediately. If your company makes such a
        claim, your patent license ends immediately for work on behalf of your
        company.
        
        Violations
        
        The first time you are notified in writing that you have violated any of
        these terms, or done anything with the software not covered by your
        licenses, your licenses can nonetheless continue if you come into full
        compliance with these terms, and take practical steps to correct past
        violations, within 32 days of receiving notice. Otherwise, all your
        licenses end immediately.
        
        No Liability
        
        As far as the law allows, the software comes as is, without any warranty
        or condition, and the licensor will not be liable to you for any damages
        arising out of these terms or the use or nature of the software, under
        any kind of legal claim.
        
        Definitions
        
        The licensor is the individual or entity offering these terms, and the
        software is the software the licensor makes available under these terms.
        
        You refers to the individual or entity agreeing to these terms.
        
        Your company is any legal entity, sole proprietorship, or other kind of
        organization that you work for, plus all organizations that have control
        over, are under the control of, or are under common control with that
        organization. Control means ownership of substantially all the assets of
        an entity, or the power to direct its management and policies by vote,
        contract, or otherwise. Control can be direct or indirect.
        
        Your licenses are all the licenses granted to you for the software under
        these terms.
        
        Use means anything you do with the software requiring one of your
        licenses.
        
Keywords: odoo,protect,cython,obfuscate
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Build Tools
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Cython<3.2,>=3.0
Requires-Dist: setuptools>=61
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Dynamic: license-file

# jProtect

**Protect your Odoo module's business logic** by extracting method bodies into compiled binaries (`.so`/`.pyd`) — the module still installs and runs normally on Odoo.

[![PyPI version](https://img.shields.io/pypi/v/jprotect)](https://pypi.org/project/jprotect/)
[![Python](https://img.shields.io/pypi/pyversions/jprotect)](https://pypi.org/project/jprotect/)
[![License: PolyForm NC](https://img.shields.io/badge/License-PolyForm%20NC-blue.svg)](LICENSE)

---

## 🔒 What It Does

jProtect takes an Odoo module and:

1. **Keeps the public file intact** — class definitions, fields, decorators, and `_name`/`_inherit` stay in place so Odoo registers the model normally.
2. **Extracts method bodies** into a hidden `models/_protected/` package as standalone functions.
3. **Replaces each method body** with a thin delegation call to `_protected`.
4. **Compiles `_protected/`** into `.so`/`.pyd` binaries (optional `--compile` flag).

The result: a module that looks and behaves exactly like the original — but the business logic is gone from the source files.

---

## ✨ Features

- 🔍 **Dry-run mode** — preview what will be protected before writing anything
- ⚙️ **Selective protection** — include/exclude files with glob patterns
- 🚀 **Cython compilation** — compile protected code to native binaries
- 🔄 **`super()` rewriting** — zero-arg `super()` is automatically rewritten to `super(cls, self)` so inheritance chains keep working
- 🛡️ **Safe by default** — skips dunders, `@property`, generators, `__init__.py`, migrations, and any pattern that could break Odoo

---

## 📦 Installation

Requires Python 3.10+. Cython is installed automatically.

```bash
pip install jprotect
```

> When using `--compile`, a C compiler is also required:
> - **Linux**: `gcc` + `python3-dev`
> - **Windows**: MSVC Build Tools

---

## 🚀 Quick Start

Given a module at `./addons/sale_ext`:

```bash
# Step 1 — Preview (no files written)
jprotect protect-module \
  --input ./addons/sale_ext \
  --output ./dist/dist/sale_ext \
  --dry-run

# Step 2 — Protect (extract logic, no compile)
jprotect protect-module \
  --input ./addons/sale_ext \
  --output ./dist/dist/sale_ext

# Step 3 — Protect + compile to binary
jprotect protect-module \
  --input ./addons/sale_ext \
  --output ./dist/dist/sale_ext \
  --compile
```

After running, jProtect generates a `protection-manifest.json` in the output directory listing every processed file.

---

## 🔍 How It Works

```python
# ── Original file ────────────────────────────────────────────────
class SaleOrder(models.Model):
    _inherit = 'sale.order'

    def action_confirm(self):
        # your business logic here...
        return super().action_confirm()


# ── Public file after jProtect ───────────────────────────────────
from . import _protected

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    def action_confirm(self):
        return _protected.sale_order.SaleOrder__action_confirm(self, SaleOrder)


# ── _protected/sale_order.py (or .so after --compile) ───────────
def SaleOrder__action_confirm(self, cls):
    # your business logic here...
    return super(cls, self).action_confirm()
```

Odoo sees the same class structure and registers the model exactly as before. The logic only lives inside the compiled binary.

---

## ⚙️ CLI Reference

```
jprotect protect-module --input <dir> --output <dir> [options]
```

| Option | Description | Example |
|---|---|---|
| `--input` | Odoo module directory (must contain `__manifest__.py`) | `--input ./addons/sale_ext` |
| `--output` | Output directory | `--output ./dist/dist/sale_ext` |
| `--mode` | `dist` creates a copy (default); `inplace` modifies in place | `--mode inplace` |
| `--dry-run` | Scan and report only, no files written | `--dry-run` |
| `--compile` | Compile `_protected/` to `.so`/`.pyd` | `--compile` |
| `--include` | Glob pattern for files to protect (repeatable, default: `models/**/*.py`) | `--include "models/**/*.py" --include "utils/*.py"` |
| `--exclude` | Glob pattern for files to exclude (repeatable) | `--exclude "models/res_*.py"` |

---

## 🐳 Docker

Build once, run anywhere — useful for compiling binaries that match your Odoo server environment:

```bash
docker build -t jprotect .

docker run --rm \
  -v "$PWD/addons/sale_ext:/work/input:ro" \
  -v "$PWD/dist:/work/output" \
  jprotect protect-module \
    --input /work/input \
    --output /work/output/dist/sale_ext \
    --compile
```

---

## ⚠️ Important Notes

**Binaries are environment-specific.** A `.so`/`.pyd` file only runs on the exact Python version + OS + CPU architecture it was compiled on. You must build on an environment that matches your Odoo server.

| Odoo Version | Python |
|---|---|
| 16, 17, 18 | 3.10 |
| 14, 15 | 3.8 |

**Always test on staging first.** Install and upgrade the protected module on a staging Odoo database and run through the main flows before going to production.

---

## 🛡️ What Gets Protected

jProtect protects regular instance methods only. The following are intentionally left untouched:

| Skipped | Reason |
|---|---|
| `@staticmethod`, `@classmethod`, `@property` | Different call semantics, cannot be safely delegated |
| Property setters/deleters (`@x.setter`, `@x.deleter`) | Setter return values are ignored by Python; wrapping changes semantics |
| Dunder methods (`__init__`, `__str__`, ...) | Called directly by Odoo/Python internals |
| Generator methods (contain `yield`) | Extracting a generator body breaks the iterator protocol |
| Methods with `super()` inside nested functions | `super()` without args relies on the declaring scope's `__class__` cell |
| `__init__.py`, `migrations/`, `controllers/` | Breaking these would prevent Odoo from loading the module |

---

## 🛠️ Development

### Setup

```bash
git clone https://github.com/your-org/jprotect.git
cd jprotect

python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

pip install -e ".[test]"
```

### Run tests

```bash
pytest -q                          # all tests
pytest tests/unit/ -q              # unit tests only
pytest tests/integration/ -q       # integration tests only
pytest tests/unit/test_ast_transformer.py -v  # specific file
```

### Project structure

```
src/jprotect/
├── cli/            # CLI entry point (argparse)
├── config/         # Config schema and loader
├── core/
│   ├── compile/    # Cython backend
│   ├── transform/  # Core AST transformation logic
│   │   ├── ast_transformer.py   # parse, extract, build delegation
│   │   └── transformer.py       # orchestrate transform over directory tree
│   ├── pipeline.py
│   └── ...
└── errors.py

tests/
├── unit/           # per-function tests with real source strings
└── integration/    # full pipeline tests on a fixture Odoo module
```

See [`docs/CODE_CONVENTIONS.md`](docs/CODE_CONVENTIONS.md) for coding conventions.

---

## 📄 License

PolyForm Noncommercial 1.0.0 — free for personal and noncommercial use. Commercial use requires a separate license from the author. See [LICENSE](LICENSE).
