cloudposse-archives / copyright-header

© Copyright Header is a utility to manipulate software licenses on source code.
https://cloudposse.com/accelerate
GNU General Public License v3.0
181 stars 61 forks source link

Duplicates license in files containing # -*- coding: utf-8 -*- #39

Open bedge opened 7 years ago

bedge commented 7 years ago

Subject says it all.

#!/usr/bin/env python

# Copyright (c) blah

# -*- coding: utf-8 -*-

# Copyright (c) blah

Used: copyright-header --guess-extension --license-file LICENSE.txt --add-path . --output-dir .

Test case:

%> cat test.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

%> copyright-header --guess-extension --license-file LICENSE.txt --add-path . --output-dir .
SKIP ./LICENSE.txt; excluded
UPDATE ./test.py [output-dir ./.]

%> ls -lrt
total 8
-rw-r--r--+ 1 bedge admin 198 Nov  6 13:09 LICENSE.txt
-rw-r--r--+ 1 bedge admin 463 Nov  6 13:10 test.py

%> cat test.py
#!/usr/bin/env python

# Copyright (c) blah

# -*- coding: UTF-8 -*-

# Copyright (c) blah
bedge commented 7 years ago

Note also that this is the recommended encoding from https://docs.python.org/2.3/whatsnew/section-encodings.html https://www.python.org/dev/peps/pep-0263/#defining-the-encoding

bedge commented 7 years ago

Was able to work around this by specifying an alternate syntax file:

diff --git a/contrib/syntax.yml b/contrib/syntax.yml
index 8b6cc75..7f99efc 100644
--- a/contrib/syntax.yml
+++ b/contrib/syntax.yml
@@ -39,7 +39,7 @@ perl:
 # vim: set fileencoding=<encoding name> :
 python:
   ext: ['.py']
-  after: ['^#!', '^#.*coding:', '^#.*coding=', '^#.*fileencoding=']
+  after: ['^#!']
   comment:
     open:   '\n'
     close:  '\n'

However, IMO it's a bug as it can inject the license more than once in a source file.

I agree that the original after... line above covers more cases, but it should only insert on the first match.