Skip to content

Commit

Permalink
Merge pull request #144 from greenbone/add-header-ignore-file
Browse files Browse the repository at this point in the history
Add pontos-update-header option to ignore files
  • Loading branch information
bjoernricks authored Jul 2, 2021
2 parents dfc4561 + 3b34849 commit 802fcd5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Calendar Versioning](https://calver.org).

## [Unreleased]
### Added
* Add pontos-update-header option to ignore files [#144](https://github.com/greenbone/pontos/pull/144)

### Changed
### Deprecated
### Removed
Expand Down
60 changes: 50 additions & 10 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
"""

import sys
import os
import re

from argparse import ArgumentParser, Namespace
from argparse import ArgumentParser, Namespace, FileType
from datetime import datetime
from glob import glob

from subprocess import CalledProcessError, run
from typing import Dict, Tuple, Union
from typing import Dict, List, Tuple, Union
from pathlib import Path

SUPPORTED_FILE_TYPES = [
Expand Down Expand Up @@ -195,6 +193,31 @@ def _update_file(
raise e


def _get_exclude_list(exclude_file: Path, directory: Path) -> List[Path]:
"""Tries to get the list of excluded files.
If a file is given, it will be used. Otherwise the given directory will be
checked for the default file.
The ignore file should only contain relative paths like *.py,
not absolute as **/*.py
"""

if exclude_file is None:
exclude_file = Path(directory) / ".pontos-header-ignore"
try:
exclude_lines = exclude_file.read_text().split('\n')
except FileNotFoundError:
print("No exclude list file found.")
return []

expanded_globs = [
Path(directory).rglob(line.strip()) for line in exclude_lines if line
]

return [
path.absolute() for glob_paths in expanded_globs for path in glob_paths
]


def _parse_args(args=None):
"""Parsing the args"""

Expand Down Expand Up @@ -250,19 +273,33 @@ def _parse_args(args=None):
"--directory",
help="Directory to find files to update recursively.",
)

parser.add_argument(
"--exclude-file",
help=(
"File containing glob patterns for files to"
" ignore when finding files to update in a directory."
" Will look for '.pontos-header-ignore' in the directory"
" if none is given."
"The ignore file should only contain relative paths like *.py,"
"not absolute as **/*.py"
),
type=FileType('r'),
)

return parser.parse_args(args)


def main() -> None:
args = _parse_args()
exclude_list = []

if args.directory:
directory = Path(args.directory)
# get file paths to exclude
exclude_list = _get_exclude_list(args.exclude_file, args.directory)
# get files to update
files = [
Path(file)
for file in glob(args.directory + "/**/*", recursive=True)
if os.path.isfile(file)
]
files = [Path(file) for file in directory.rglob("*") if file.is_file()]
elif args.files:
files = [Path(name) for name in args.files]

Expand All @@ -278,7 +315,10 @@ def main() -> None:

for file in files:
try:
_update_file(file=file, regex=regex, args=args)
if file.absolute() in exclude_list:
print(f"{file}: Ignoring file from exclusion list.")
else:
_update_file(file=file, regex=regex, args=args)
except (FileNotFoundError, UnicodeDecodeError, ValueError):
continue

Expand Down
17 changes: 16 additions & 1 deletion tests/updateheader/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import struct
import re

Expand All @@ -32,6 +31,7 @@
_find_copyright as find_copyright,
_add_header as add_header,
_update_file as update_file,
_get_exclude_list as get_exclude_list,
_parse_args as parse_args,
main,
)
Expand Down Expand Up @@ -427,6 +427,21 @@ def test_argparser_dir(self):
self.assertEqual(args.year, '2021')
self.assertEqual(args.licence, self.args.licence)

def test_get_exclude_list(self):
# Try to find the current file from two directories up...
test_dirname = Path(__file__) / "../.."
# with a relative glob
test_ignore_file = Path('ignore.file')
test_ignore_file.write_text("*.py\n")

exclude_list = get_exclude_list(
test_ignore_file, test_dirname.resolve()
)

self.assertIn(Path(__file__), exclude_list)

test_ignore_file.unlink()

@patch('pontos.updateheader.updateheader._parse_args')
def test_main(self, argparser_mock):
self.args.year = '2021'
Expand Down

0 comments on commit 802fcd5

Please sign in to comment.