Commit 45ab302c authored by François Ferry's avatar François Ferry
Browse files

feat(update-licence-dates): add a new command to update licence date in all py files.

parent c40c8e05faff
Pipeline #28518 failed with stage
in 2 minutes and 37 seconds
......@@ -11,7 +11,7 @@ CLI pattern looks like:
::
GITLAB_TOKEN=... python3 doctor_hg.py [-h] [-a] [-i] [-m] [-n NUMBER] [-q QUERY] {fix-README,add-tox,add-pytest-deprecated-warnings-cmd,replace-set-attributes-by-set-cw}
GITLAB_TOKEN=... python3 doctor_hg.py [-h] [-a] [-i] [-m] [-n NUMBER] [-q QUERY] {fix-README,add-tox,add-pytest-deprecated-warnings-cmd,replace-set-attributes-by-set-cw,update-licence-dates}
Example:
......@@ -72,6 +72,14 @@ This command will:
- retrieve all the Python files inside the cube
- for each file, replace all occurences of set_attributes calls by set_cw calls
update-licence-dates
--------------------
This command will:
- retrieve all the Python files inside the cube
- for each file, if it has a licence part in comments, extend the licence end date to
the current year.
Command CLI arguments
---------------------
......
......@@ -10,6 +10,7 @@ from cube_doctor.transforms.add_pytest_deprecated_warnings import (
CaptureDeprecatedWarnings,
)
from cube_doctor.transforms.readme_rst import READMERst
from cube_doctor.transforms.udate_cube_licence_dates import UpdateCubeLicenceDates
def hg_command(dir, command):
......@@ -54,6 +55,7 @@ commands = {
"add-pypi-publish": AddPypiPublish().workflow,
"add-deb-publish": AddDebPublish().workflow,
"replace-set-attributes": ReplaceSetAttributesWithSetCW().workflow,
"update-licence-dates": UpdateCubeLicenceDates().workflow,
}
if __name__ == "__main__":
......
import re
from datetime import date
from redbaron import RedBaron
from cube_doctor import Command
class UpdateCubeLicenceDates(Command):
BRANCH_NAME = "topic/default/update-licence-dates"
commit_message = "docs(licence): update licence dates"
base_query = "?project lgg:is_cube true"
licence_regex = re.compile(
r"# copyright (?P<start>\d{4})-(?P<end>\d{4}) LOGILAB S\.A\."
)
no_end_licence_regex = re.compile(r"# copyright (?P<start>\d{4}) LOGILAB S\.A\.")
def modify_code(self, cube, repo, root_files, branches):
today_year = date.today().year
for f in repo.all_python_files():
file_content_before = repo.read_file(f)
match = re.search(self.licence_regex, file_content_before)
if match:
old_licence = (
f"# copyright {match['start']}-{match['end']} LOGILAB S.A."
)
else:
match = re.search(self.no_end_licence_regex, file_content_before)
if not match:
continue
old_licence = f"# copyright {match['start']} LOGILAB S.A."
new_licence = f"# copyright {match['start']}-{today_year} LOGILAB S.A."
red = RedBaron(file_content_before)
for target in red("comment", lambda x: x.value.startswith(old_licence)):
target.value = target.value.replace(old_licence, new_licence)
repo.write_file(f, red.dumps())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment