Skip to content
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ ALL_SRC = $(shell find . -name '*.go' \
-type f | \
sort)

# All .sh or .py or Makefile or .mk files that should be auto-formatted and linted.
SCRIPTS_SRC = $(shell find . \( -name '*.sh' -o -name '*.py' -o -name 'Makefile*' \) \
-type f | \
sort)

# ALL_PKGS is used with 'nocover' and 'goleak'
ALL_PKGS = $(shell echo $(dir $(ALL_SRC)) | tr ' ' '\n' | sort -u)

Expand Down Expand Up @@ -185,11 +190,11 @@ fmt: $(GOFUMPT)
@echo Running gofumpt on ALL_SRC ...
@$(GOFUMPT) -e -l -w $(ALL_SRC)
@echo Running updateLicense.py on ALL_SRC ...
@./scripts/updateLicense.py $(ALL_SRC)
@./scripts/updateLicense.py $(ALL_SRC) $(SCRIPTS_SRC)

.PHONY: lint
lint: $(LINT) goleak
@./scripts/updateLicense.py $(ALL_SRC) > $(FMT_LOG)
@./scripts/updateLicense.py $(ALL_SRC) $(SCRIPTS_SRC) > $(FMT_LOG)
@./scripts/import-order-cleanup.py -o stdout -t $(ALL_SRC) > $(IMPORT_LOG)
@[ ! -s "$(FMT_LOG)" -a ! -s "$(IMPORT_LOG)" ] || (echo "License check or import ordering failures, run 'make fmt'" | cat - $(FMT_LOG) $(IMPORT_LOG) && false)
./scripts/check-semconv-version.sh
Expand Down
59 changes: 49 additions & 10 deletions scripts/updateLicense.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@
LICENSE_BLOB = """Copyright (c) %d The Jaeger Authors.
SPDX-License-Identifier: Apache-2.0""" % CURRENT_YEAR

LICENSE_BLOB_LINES_GO = [
('// ' + l).strip() + '\n' for l in LICENSE_BLOB.split('\n')
]
def get_license_blob_lines(comment_prefix):
return [
(comment_prefix + ' ' + l).strip() + '\n' for l in LICENSE_BLOB.split('\n')
]

LICENSE_BLOB_LINES_GO = get_license_blob_lines('//')

LICENSE_BLOB_LINES_SHELL = get_license_blob_lines('#')

LICENSE_BLOB_LINES_MAKEFILE = get_license_blob_lines('#')

LICENSE_BLOB_LINES_PYTHON = get_license_blob_lines('#')

COPYRIGHT_RE = re.compile(r'Copyright \(c\) (\d+)', re.I)

SHEBANG_RE = re.compile(r'^#!\s*/[^\s]+')

def update_go_license(name, force=False):
def update_license(name, license_lines):
with open(name) as f:
orig_lines = list(f)
lines = list(orig_lines)
Expand Down Expand Up @@ -50,18 +60,29 @@ def update_go_license(name, force=False):

# print('found=%s, changed=%s, jaeger=%s' % (found, changed, jaeger))

first_line = lines[0]
shebang_match = SHEBANG_RE.match(first_line)

def replace(header_lines):
if 'Code generated by' in lines[0]:

if 'Code generated by' in first_line:
lines[1:1] = ['\n'] + header_lines
elif shebang_match:
lines[1:1] = header_lines
else:
lines[0:0] = header_lines

if not found:
replace(LICENSE_BLOB_LINES_GO + ['\n'])
# depend on file type
if(shebang_match):
replace(['\n'] + license_lines)
else:
replace(license_lines + ['\n'])

changed = True
else:
if not jaeger:
replace(LICENSE_BLOB_LINES_GO[0])
replace(license_lines[0])
changed = True

if changed:
Expand All @@ -70,16 +91,34 @@ def replace(header_lines):
f.write(line)
print(name)

def get_license_type(file):
ext_map = {
'.go' : LICENSE_BLOB_LINES_GO,
'.mk' : LICENSE_BLOB_LINES_MAKEFILE,
'Makefile' : LICENSE_BLOB_LINES_MAKEFILE,
'.py' : LICENSE_BLOB_LINES_PYTHON,
'.sh' : LICENSE_BLOB_LINES_SHELL,
}

license_type = None

for ext, license in ext_map.items():
if file.endswith(ext):
license_type = license
break

return license_type

def main():
if len(sys.argv) == 1:
print('USAGE: %s FILE ...' % sys.argv[0])
sys.exit(1)

for name in sys.argv[1:]:
if name.endswith('.go'):
license_type = get_license_type(name)
if license_type:
try:
update_go_license(name)
update_license(name, license_type)
except Exception as error:
logger.error('Failed to process file %s', name)
logger.exception(error)
Expand Down