Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template files for header #85

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Calendar Versioning](https://calver.org).

## [Unreleased]
### Added
- Template for header [85](https://github.com/greenbone/pontos/pull/85)
### Changed
### Deprecated
### Removed
Expand Down
17 changes: 17 additions & 0 deletions pontos/updateheader/templates/AGPL-3.0-or-later/template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (C) 2020 Company
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
17 changes: 17 additions & 0 deletions pontos/updateheader/templates/GPL-2.0-only/template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (C) 2020 Company
*
* SPDX-License-Identifier: GPL-2.0-only
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
18 changes: 18 additions & 0 deletions pontos/updateheader/templates/GPL-2.0-or-later/template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (C) 2020 Company
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
17 changes: 17 additions & 0 deletions pontos/updateheader/templates/GPL-3.0-or-later/template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (C) 2020 Company
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
65 changes: 33 additions & 32 deletions pontos/updateheader/updateheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,30 @@
from pathlib import Path

SUPPORTED_FILE_TYPES = [
'.bash',
'.c',
'.cmake',
'.js',
'.nasl',
'.po',
'.py',
'.sh',
'.txt',
'.xml',
".bash",
".c",
".h",
".cmake",
".js",
".nasl",
".po",
".py",
".sh",
".txt",
".xml",
]
SUPPORTED_LICENCES = [
'AGPL-3.0-or-later',
'GPL-2.0-only',
'GPL-2.0-or-later',
'GPL-3.0-or-later',
"AGPL-3.0-or-later",
"GPL-2.0-only",
"GPL-2.0-or-later",
"GPL-3.0-or-later",
]


def _get_modified_year(f: Path) -> str:
"""In case of the changed arg, update year to last modified year"""
try:
cmd = ['git', 'log', '-1', '--format=%ad', '--date=format:%Y', str(f)]
cmd = ["git", "log", "-1", "--format=%ad", "--date=format:%Y", str(f)]
proc = run(
cmd,
text=True,
Expand All @@ -78,9 +79,9 @@ def _find_copyright(
return (
True,
{
'creation_year': copyright_match.group(1),
'modification_year': copyright_match.group(2),
'company': copyright_match.group(3),
"creation_year": copyright_match.group(1),
"modification_year": copyright_match.group(2),
"company": copyright_match.group(3),
},
)
return False, None
Expand All @@ -94,9 +95,9 @@ def _add_header(suffix: str, licence: str, company: str) -> Union[str, None]:
"""
if suffix in SUPPORTED_FILE_TYPES:
root = Path(__file__).parent
licence_file = root / 'templates' / licence / f'template{suffix}'
licence_file = root / "templates" / licence / f"template{suffix}"
try:
return licence_file.read_text().replace('Company', company)
return licence_file.read_text().replace("Company", company)
except FileNotFoundError as e:
raise e
else:
Expand Down Expand Up @@ -129,7 +130,7 @@ def _update_file(
i = 10 # assume that copyright is in the first 10 lines
while not found and i > 0:
line = fp.readline()
if line == '':
if line == "":
i = 0
continue
found, copyright_match = _find_copyright(line=line, regex=regex)
Expand Down Expand Up @@ -160,10 +161,10 @@ def _update_file(
return 1
# replace header and write it to file
if (
not copyright_match['modification_year']
and copyright_match['creation_year'] < args.year
or copyright_match['modification_year']
and copyright_match['modification_year'] < args.year
not copyright_match["modification_year"]
and copyright_match["creation_year"] < args.year
or copyright_match["modification_year"]
and copyright_match["modification_year"] < args.year
):
copyright_term = (
f'Copyright (C) {copyright_match["creation_year"]}'
Expand All @@ -176,20 +177,20 @@ def _update_file(
fp.write(new_line)
fp.write(rest_of_file)
print(
f'{file}: Changed Licence Header Copyright Year '
f"{file}: Changed Licence Header Copyright Year "
f'{copyright_match["modification_year"]} -> '
f'{args.year}'
f"{args.year}"
)

return 0
else:
print(f'{file}: Licence Header is ok.')
print(f"{file}: Licence Header is ok.")
return 0
except FileNotFoundError as e:
print(f'{file}: File is not existing.')
print(f"{file}: File is not existing.")
raise e
except UnicodeDecodeError as e:
print(f'{file}: Ignoring binary file.')
print(f"{file}: Ignoring binary file.")
raise e


Expand Down Expand Up @@ -226,13 +227,13 @@ def _parse_args(args=None):
"-l",
"--licence",
choices=SUPPORTED_LICENCES,
default='GPL-3.0-or-later',
default="GPL-3.0-or-later",
help="Add header f files",
)

parser.add_argument(
"--company",
default='Greenbone Networks GmbH',
default="Greenbone Networks GmbH",
help=(
"If a header will be added to file, \n"
"it will be licenced by company."
Expand Down