Skip to content

Commit

Permalink
Checksum flag (#95)
Browse files Browse the repository at this point in the history
* Use decorator for checksum

* Add decorator for skipping validations

* Add tests for decorator

* Update CHANGELOG

* Document option to skip in README

* Fix linting

* Use functools.wraps around decorated functions
  • Loading branch information
yashpatel6 authored Feb 22, 2024
1 parent 290fa89 commit f9adeae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

---

## [Unreleased]
## [5.0.0] - 2024-02-16
### Added
- Validation skip functionality for checksums

---

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ Input: path/to/input is valid <file-type>
Error: path/to/input <error message>
```

#### Validation Skipping

Certain validations can be skipped through environment variables.
|ENV VAR|Notes|
|:---:|:---:|
|PIPEVAL_SKIP_CHECKSUM|Flag to disable checksum validation. Set to `true` to disable checksum validation within PipeVal.|

### `pipeval generate-checksum`
```
usage: pipeval generate-checksum [-h] [-t {md5,sha512}] [-v] path [path ...]
Expand Down
29 changes: 29 additions & 0 deletions pipeval/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""" Common functions for PipeVal """
import os
from functools import wraps

# pylint: disable=C0103,W0613
def skippedValidation(name):
"""
Conditionally mark a validation to be skipped
If the environment variable f'PIPEVAL_SKIP_{name}' is set to `true`,
the decorated function will skip that validation.
"""
def decorator(func):
return func

def print_skip_message(func):
@wraps(func)
def skip_message(*args, **kwargs):
print(f'PID:{os.getpid()} - Skipping validation {name.upper()}')

return skip_message

value = os.environ.get(f"PIPEVAL_SKIP_{name.upper()}")
should_skip_validate = value is not None and value.lower() == 'true'

if not should_skip_validate:
return decorator

return print_skip_message
3 changes: 3 additions & 0 deletions pipeval/generate_checksum/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from typing import Dict, Union
from pathlib import Path

from pipeval.common import skippedValidation

ChecksumArgs = namedtuple(
'args',
'path, type'
)

@skippedValidation('CHECKSUM')
def _validate_checksums(path:Path):
''' Validate MD5 and/or SHA512 checksums '''
# Checksum validation
Expand Down
32 changes: 32 additions & 0 deletions test/unit/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# pylint: disable=C0116
# pylint: disable=C0114
# pylint: disable=C0103
from pipeval.common import skippedValidation

def test__skippedValidation__properly_skips_function_call(monkeypatch, capsys):
monkeypatch.setenv('PIPEVAL_SKIP_CHECKSUM', 'true')

@skippedValidation('CHECKSUM')
def to_be_decorated():
print('Decorated function called')

to_be_decorated()

out, _ = capsys.readouterr()

assert 'Skipping validation CHECKSUM' in out
assert 'Decorated function called' not in out

def test__skippedValidation__properly_calls_function(monkeypatch, capsys):
monkeypatch.setenv('PIPEVAL_SKIP_CHECKSUM', 'false')

@skippedValidation('CHECKSUM')
def to_be_decorated():
print('Decorated function called')

to_be_decorated()

out, _ = capsys.readouterr()

assert 'Skipping validation CHECKSUM' not in out
assert 'Decorated function called' in out

0 comments on commit f9adeae

Please sign in to comment.