Skip to content

Commit

Permalink
devtools: add update-license (sub-)command
Browse files Browse the repository at this point in the history
Add update-license subcommand to the pr-tool.
It will be called automatically when merging a PR.
  • Loading branch information
joergsteffens authored and BareosBot committed May 17, 2024
1 parent 92a44c7 commit a3eac86
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 52 deletions.
6 changes: 3 additions & 3 deletions LICENSE.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ Comment: BAREOS is a fork of the Bacula source code.


Files: *
Copyright: 2012-${YEAR} Bareos GmbH & Co. KG
Copyright: 2012-{year} Bareos GmbH & Co. KG
License: AGPL-3.0-only


Files: core/* docs/manuals/source/*
Copyright: 2000-2012 Free Software Foundation Europe e.V.
2010-2017 Planets Communications B.V.
2012-${YEAR} Bareos GmbH & Co. KG
2012-{year} Bareos GmbH & Co. KG
License: Bareos
This is basically AGPL-3.0 with Bacula and other exceptions.
This section is the reformatted version of
https://github.com/bareos/bareos/blob/master/core/LICENSE
.
@include("core/LICENSE")
{include("core/LICENSE")}

Files: core/src/lib/bmtio.h
Copyright: 1982,1986,1993 The Regents of the University of California
Expand Down
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ License: Bareos
.
===========================================


Files: core/src/lib/bmtio.h
Copyright: 1982,1986,1993 The Regents of the University of California
License: permissive
Expand Down
20 changes: 20 additions & 0 deletions devtools/pip-tools/license_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 202-2024 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

from .update_license import generate_license_file
106 changes: 106 additions & 0 deletions devtools/pip-tools/license_utils/update_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python3

# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2024-2024 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

from argparse import ArgumentParser
from datetime import date
from os.path import dirname, curdir
import re
from sys import stdout, stderr


def parse_cmdline_args():
parser = ArgumentParser()
parser.add_argument(
"--template",
"-t",
default="LICENSE.template",
help="License file template. Default: %(default)s",
)
parser.add_argument(
"--out",
"-o",
default="-",
help="Target file name. Default: stdout",
)
return parser.parse_args()


def get_include_file_content(path, indent=" "):
emptyline = re.compile(r"^\s*$")
content = ""
with open(path, "r", encoding="utf-8") as file:
for line in file:
if emptyline.fullmatch(line):
line = ".\n"
content += indent + line
return content


def get_file_content(filename):
with open(filename, "r", encoding="utf-8") as file:
content = file.read()
return content


def get_translations(template_filename):
base_dir = dirname(template_filename)
translations = {
"year": date.today().year,
}
translations['include("core/LICENSE")'] = get_include_file_content(
f"{base_dir}/core/LICENSE"
)
return translations


def generate_license_file(template_filename, target_filename):
template = get_file_content(template_filename)
translations = get_translations(template_filename)
if target_filename != "-":
with open(target_filename, "w", encoding="utf-8") as file:
file.write(template.format_map(translations))
else:
stdout.write(template.format_map(translations))


def main():
args = parse_cmdline_args()

if "/" in args.template:
template_filename = args.template
else:
template_filename = f"{curdir}/{args.template}"

if args.out == "-" or "/" in args.out:
target_filename = args.out
else:
target_filename = f"{curdir}/{args.out}"

try:
generate_license_file(template_filename, target_filename)
except FileNotFoundError:
print(f"Could not find file {template_filename}", file=stderr)
return 2
return 0


if __name__ == "__main__":
exit(main())
39 changes: 38 additions & 1 deletion devtools/pip-tools/pr_tool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import re

from argparse import ArgumentParser, Namespace, ArgumentTypeError
from os import environ, chdir
from os import environ, chdir, path
from pprint import pprint
from sys import stdout, stderr
from time import sleep
Expand All @@ -38,6 +38,7 @@
update_links,
guess_section,
)
from license_utils import generate_license_file

from check_sources.main import main_program as check_sources
from . import backport
Expand Down Expand Up @@ -348,6 +349,29 @@ def get_plain_label_list(label_list):
return [x["name"].lower() for x in label_list]


def get_git_file_modified(repo, path):
for item in repo.index.diff(None):
if path == item.b_path:
return True
return False


def update_license_file(repo):
license_filename = "LICENSE.txt"
license_file_path = f"{repo.working_tree_dir}/{license_filename}"
license_template = "LICENSE.template"
license_template_path = f"{repo.working_tree_dir}/{license_template}"
if not path.isfile(license_template_path):
print(f"skipped, template file '{license_template}' not found.")
return None
generate_license_file(license_template_path, license_file_path)
if get_git_file_modified(repo, license_filename):
print(f"Updating {license_filename}")
repo.git.commit("-m", f"Update {license_filename}", license_file_path)
return True
return None


def get_changelog_section(pr):
labels = get_plain_label_list(pr["labels"])
if "documentation" in labels:
Expand Down Expand Up @@ -411,6 +435,7 @@ def parse_cmdline_args():
help="ignore (required) github status checks",
)
changelog_parser = subparsers.add_parser("add-changelog")
license_parser = subparsers.add_parser("update-license")
merge_parser = subparsers.add_parser("merge")
merge_parser.add_argument(
"--skip-merge",
Expand Down Expand Up @@ -509,12 +534,18 @@ def merge_pr(

original_commit = repo.head.commit
try:
git_modified = False
if update_license_file(repo):
git_modified = True
if check_changelog_entry(repo, pr):
print("Keeping existing changelog entry.")
else:
print("Adding changelog entry")
if not add_changelog_entry(repo, pr):
raise Exception("Adding changelog failed")
git_modified = True

if git_modified:
repo.git.push()

if check_merge_is_possible(repo, pr):
Expand Down Expand Up @@ -542,6 +573,7 @@ def merge_pr(
repo.git.rebase("--abort")
raise e

update_license_file(repo)
print("Adding changelog entry again")
if not add_changelog_entry(repo, pr):
raise Exception("Adding changelog failed")
Expand Down Expand Up @@ -743,6 +775,11 @@ def main():
ret = False

handle_ret(ret)
elif args.subcommand == "update-license":
if update_license_file(repo) in [None, True]:
exit(0)
else:
exit(1)
elif args.subcommand == "add-changelog":
if check_changelog_entry(repo, pr_data):
print("Already have Changelog for this PR")
Expand Down
1 change: 1 addition & 0 deletions devtools/pip-tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"pr-tool=pr_tool.main:main",
"update-changelog-links=changelog_utils.update_links:main",
"add-changelog-entry=changelog_utils.add_entry:main",
"update-license=license_utils.update_license:main",
]
},
)
47 changes: 0 additions & 47 deletions devtools/update-license-file.sh

This file was deleted.

3 changes: 2 additions & 1 deletion third-party/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Each Library will be put into its own subdirectory with `git subtree` and will h
A typical git subtree command would look like this : `git subtree add --prefix destination/path https://www.yourgithost.com/your/repo yourbranch(usuallymaster) --squash`

Make sure the library license is covert by `LICENSE.txt`. At best, the library is also licensed as AGPL-3. If it is not already covered, a section needs to be added for it in `LICENSE.template`.
Also execute `devtools/update-license-file.sh` to update `LICENSE.txt`, and commit new version.
Executing `pr-tool update-license` will renew the `LICENSE.txt` file.
This will also happen automatically when merging the PR.

### What to add?

Expand Down

0 comments on commit a3eac86

Please sign in to comment.