#!/usr/bin/env python # import sys import os def insert_at(srclines, pos, tag, style): if style == '/*': srclines.insert(pos, '/* SPDX-License-Identifier: %s */\n' %tag) else: srclines.insert(pos, '%s SPDX-License-Identifier: %s\n' %(style, tag)) return True def handle_c(srclines, tag): return insert_at(srclines, 0, tag, '//') def is_script(srclines): for line in srclines: line.lstrip(); if line.startswith('#!'): return True; return False; def handle_asm(srclines, tag): # Stupid search for a proper style to comment the SPDX tag pos = 0 style = None for line in srclines: pos += 1 line.lstrip(); if line.startswith(';;;'): style = ';;;' elif line.startswith(';;'): style = ';;' elif line.startswith(';'): style = ';' elif line.startswith('|'): style = '|' elif line.startswith('!'): style = '!' elif line.startswith('//'): style = '//' elif line.find("/*") >= 0: style = '/*' else: # default to /* if we have no clue style = '/*' return insert_at(srclines, 0, tag, style) return False def handle_sh(srclines, tag): return insert_at(srclines, 1, tag, '#') tf = open(sys.argv[1]) for entry in tf.readlines(): if len(entry.strip()) == 0: continue nr, fname, tag = entry.strip().split(',') # FIXME: Use a proper encoder fname = fname.replace('%2C',',') fname = fname.replace('%2B','+') if tag == 'NOTAG': print("Skipping %s" %fname) continue if not os.path.isfile(fname): print("FAIL: File %s does not exist anymore" %fname) continue bname = os.path.basename(fname) srclines = open(fname).readlines() done = False for line in srclines: if line.find('SPDX-License-Identifier') >= 0: done = True break if done: print("SPDX id exists already in %s" %fname) continue ok = False if fname.endswith('.h') or fname.endswith('.lds'): ok = insert_at(srclines, 0, tag, '/*') elif fname.endswith('.cpp'): ok = insert_at(srclines, 0, tag, '//') elif fname.endswith('.c') or fname.endswith('.uc'): ok = handle_c(srclines, tag) elif fname.endswith('.S'): ok = handle_asm(srclines, tag) elif fname.endswith('.cocci'): ok = insert_at(srclines, 0, tag, '//') elif fname.endswith('.dts') or fname.endswith('.dtsi'): ok = insert_at(srclines, 0, tag, '//') elif fname.endswith('.py') or fname.endswith('.tc') or fname.endswith('.sh') or fname.endswith('.pl'): ok = insert_at(srclines, 1, tag, '#') elif bname.startswith('Makefile') or bname.startswith('Kconfig') or bname.startswith('Kbuild'): ok = insert_at(srclines, 0, tag, '#') elif is_script(srclines): ok = insert_at(srclines, 1, tag, "#") else: print("Unhandled or ignored file %s" %fname) continue if ok: open(fname, 'w').writelines(srclines) print("Inserted %s into %s" %(tag, fname)) else: print("FAIL: No place for insertion found %s" %fname)