Skip to content

Commit

Permalink
Add selection between last year and range of years
Browse files Browse the repository at this point in the history
Some organizations prefer to list the last year an author touched a file only
  • Loading branch information
emzeat committed Jan 9, 2024
1 parent 5bff9af commit 51a453c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _WIP_
using headers as defined for the licenses
* Make detection of comment style based on suffix case insensitive
* Fix handling of files with unicode BOM
* Allow selection of latest year vs range of years

v2.6.2
------
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ A sample configuration is given below with each option annotated for explanation
// of an author correctly.
// if both 'from_git' and 'years' have been specified, the 'years' ranges
// will be merged.
// if 'latest_year_only' is true only the latest year in the range will be used
"years": [1970, 2023],
// only list the latest year an author made a change not the range of years
"latest_year_only": false,
// for some licenses an additional company name which is different from the
// author may be wanted. This can be specified here.
"company": "the authors",
Expand Down Expand Up @@ -261,7 +264,7 @@ A sample configuration is given below with each option annotated for explanation
// specifies the title to put at the top of each file. Only choice right now
// is "filename".
// Set to false to leave out the title altogether.
"title": "filename",
"title": "filename",
// specifies a custom title to put at the top of the file header.
// In case both 'title' and 'custom_title' have been specified, the
// 'custom_title' takes precedence.
Expand Down
18 changes: 13 additions & 5 deletions license_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,14 @@ def force_newline(input: str, newline='\n') -> str:
return Tool.force_newline(input).replace('\n', newline)

def bump(self, filename: pathlib.PurePath,
keep_license: bool = True, title: Title = None, keep_authors: bool = True) -> str:
keep_license: bool = True, title: Title = None, keep_authors: bool = True, latest_year_only: bool = False) -> str:
"""
Reads a file and returns the bumped contents
:filename: The file to be bumped
:keep_license: If an existing license should be retained or replaced with the new default
:title: The title to use in the header
:keep_authors: If any existing authors should be retained or replaced with the new default
:latest_year_only: Only lists the last year a file was touched
returns a tuple of detected language and bumped contents
"""
parsed = ParsedHeader(filename)
Expand Down Expand Up @@ -611,6 +612,9 @@ def bump(self, filename: pathlib.PurePath,
parsed.authors = list(seen_authors.values())
else:
parsed.authors = [latest_author]
if latest_year_only:
for author in parsed.authors:
author.year_from = author.year_to

license_text = None
if keep_license:
Expand All @@ -633,16 +637,18 @@ def bump(self, filename: pathlib.PurePath,
return parsed.style, output

def bump_inplace(self, filename: pathlib.PurePath, keep_license: bool = True,
title: Title = None, simulate: bool = False, keep_authors: bool = True) -> bool:
title: Title = None, simulate: bool = False, keep_authors: bool = True, latest_year_only: bool = False) -> bool:
"""
Bumps the license header of a given file
:filename: The file to be bumped
:keep_license: If an existing license should be retained or replaced with the new default
:title: The title to use in the header
:simulate: Perform a dry run not applying any changes
:keep_authors: If any existing authors should be retained or replaced with the new default
:latest_year_only: Only lists the last year a file was touched
"""
_, bumped = self.bump(filename, keep_license=keep_license, title=title, keep_authors=keep_authors)
_, bumped = self.bump(filename, keep_license=keep_license, title=title,
keep_authors=keep_authors, latest_year_only=latest_year_only)
if bumped:
filename = str(filename) + \
'.license_bumped' if simulate else filename
Expand Down Expand Up @@ -784,6 +790,7 @@ def main():
'author': {
'from_git': True,
'years': [1970, DateUtils.current_year()],
'latest_year_only': False,
'name': '<author here>',
'company': 'the authors',
'aliases': {
Expand Down Expand Up @@ -948,7 +955,7 @@ def try_shorten(path: pathlib.Path):
sys.exit(2)
if 'years' in config_author:
years = config_author['years']
if len(years) < 1 or len(years) > 2:
if not isinstance(years, list) or len(years) < 1 or len(years) > 2:
logging.fatal(f"Please provide the 'years' attribute as [from] or pair [from, to]: {years}")
sys.exit(2)
try:
Expand Down Expand Up @@ -976,10 +983,11 @@ def try_shorten(path: pathlib.Path):
tool = Tool(license, author, company, aliases, lines_after_license)
keep_license = not args.force_license and not config.get('force_license', False)
keep_authors = not config.get('force_author', False)
latest_year_only = config_author.get('latest_year_only', False)

logging.debug(f"Processing '{file_rel}'")
try:
if tool.bump_inplace(file, keep_license=keep_license, keep_authors=keep_authors, title=title, simulate=args.dry_run):
if tool.bump_inplace(file, keep_license=keep_license, keep_authors=keep_authors, latest_year_only=latest_year_only, title=title, simulate=args.dry_run):
return True
except UnicodeDecodeError as error:
logging.warning(f"Failed to decode {file_rel}: {error}")
Expand Down
22 changes: 22 additions & 0 deletions test/package_pinned_author_single_year.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
diff --git a/code.cpp b/code.cpp
index 9114b7f..85e48ac 100644
--- a/code.cpp
+++ b/code.cpp
@@ -1,3 +1,17 @@
+/*
+ * code.cpp
+ *
+ * Copyright (c) 2022 Test Author
+ * All rights reserved.
+ *
+ * Unauthorized copying of this file, via any medium is strictly prohibited.
+ * This file is a proprietary and confidential part of a software product
+ * by Umbrella Inc or part of its connected software and documentation.
+ *
+ * ANY FILE BEARING THIS HEADER SHALL NOT BE RELEASED TO THE
+ * PUBLIC, BE USED, BE MODIFIED OR BE DISTRIBUTED IN ANY WAY
+ * WITHOUT WRITTEN PERMISSION OF UMBRELLA INC.
+ */

#include <iostream>

12 changes: 12 additions & 0 deletions test/package_pinned_author_single_year.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"author": {
"years": [
1980
],
"latest_year_only": true,
"from_git": true,
"company": "Umbrella Inc"
},
"license": "Proprietary",
"force_license": true
}
25 changes: 25 additions & 0 deletions test/package_pinned_author_single_year.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From 5b43dbd60b2355884c3e5176c2c5f02ee93e6982 Mon Sep 17 00:00:00 2001
From: Test Author <[email protected]>
Date: Wed, 26 Jan 2022 07:55:44 +0100
Subject: [PATCH] Create main

---
code.cpp | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 code.cpp

diff --git a/code.cpp b/code.cpp
new file mode 100644
index 0000000..f53d6b0
--- /dev/null
+++ b/code.cpp
@@ -0,0 +1,7 @@
+
+#include <iostream>
+
+int main(int argc, char* argv[]){
+ std::cout << "Hello World" << std::endl;
+ return 0;
+}
--
2.34.1

0 comments on commit 51a453c

Please sign in to comment.