Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bears/general: Add LicenseCheckBear #1442

Merged
merged 1 commit into from
Mar 8, 2017
Merged

bears/general: Add LicenseCheckBear #1442

merged 1 commit into from
Mar 8, 2017

Conversation

yash-nisar
Copy link
Member

@yash-nisar yash-nisar commented Feb 18, 2017

Closes #823

For short term contributors: we understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!

Checklist

  • I read the commit guidelines and I've followed
    them.
  • I ran coala over my code locally. (All commits have to pass
    individually.
    It is not sufficient to have "fixup commits" on your PR,
    our bot will still report the issues for the previous commit.) You will
    likely receive a lot of bot comments and build failures if coala does not
    pass on every single commit!

After you submit your pull request, DO NOT click the 'Update Branch' button.
When asked for a rebase, consult coala.io/rebase
instead.

Please consider helping us by reviewing other peoples pull requests as well:

The more you review, the more your score will grow at coala.io and we will
review your PRs faster!

@yash-nisar
Copy link
Member Author

@Makman2 Appveyor build failing. However the circle ci and travis builds have passed. I have added the dependencies in .travis.yml and deps.sh. What could be the reason ?

@yash-nisar
Copy link
Member Author

@Makman2 @adtac Please have a look at this when you are free. :)

"""
args = ()
if licensecheck_lines != 60:
args += ('--lines', str(licensecheck_lines))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just give this directly, there's no need for a conditional statement.


@staticmethod
def create_arguments(filename, file, config_file,
licensecheck_lines: int = 60,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No spaces around the = (see also the other lines below)

@@ -0,0 +1,78 @@
from queue import Queue
import os.path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually just do import os :)

Any particular reasons for import os.path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk why I imported os.path 😆

def test_license(self):
file_contents = [load_testfile(
get_testfile_path('apache_license_2.py'))]
test_file_path = get_testfile_path('apache_license_2.py')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this one line above and reuse test_file_path

self.uut,
file_contents,
[Result.from_values('LicenseCheckBear',
test_file_path + ': Apache (v2.0)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this will return a result all the time? I thought this bear is something like "Make sure all files have this particular license; if not, return a result" and that it'll be quiet if a file is okay?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this a helper bear for other bears?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike other bears that check validity,

This bear attempts to determine the license that applies to each file passed to
it, by searching the start of the file for text belonging to various
licenses.

It will return a result all the time to determine not just the license but also the type of license.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is then definitely suited for a dependency bear imo, and we should add a bear that uses that information. Like a LicenseConsistencyBear or so^^

Copy link
Member

@adtac adtac Feb 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd never enable a bear that always returns results - it'll just clutter the output window. This should definitely be a dependency bear. And in that case, you should use HiddenResults instead of Results.

Copy link
Member Author

@yash-nisar yash-nisar Feb 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of a native bear, I can silence the results in the run method but how do I silence the results given by the licensecheck tool in this case ? @adtac @Makman2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@linter currently doesn't support yielding HiddenResults currently, you maybe want to implement support for it^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, till the time yielding HiddenResult is implemented for linter bears, what do we do with this ?

@yash-nisar
Copy link
Member Author

Done @adtac

:param licensecheck_check:
Specify a pattern indicating which files should be checked.
:param licensecheck_copyright:
Display the file's copyright
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see what this is good for or even what it does

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were actually planning to write a bear that would check for copyright notices in source files as mentioned here. However we have wrapped this functionality in a tool called as licensecheck which determines the license that applies to each file and checks for copyright notices too (displays it if present).

Specify that files/directories matching the regular expression
should be ignored when checking files.
:param licensecheck_check:
Specify a pattern indicating which files should be checked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add examples? Solely from this description I'd not know how to use it

Copy link
Member Author

@yash-nisar yash-nisar Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this parameter too IMO because the user can explicitly specify it either by the --files argument or in the .coafile with the help of globs

for license information.
:param licensecheck_ignore:
Specify that files/directories matching the regular expression
should be ignored when checking files.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be given, coala does ignoring and we shouldn't repeat the functionality as a setting

args += ('--lines', str(licensecheck_lines))
if licensecheck_copyright:
args += ('--copyright',)
return args + (filename,)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W292 no newline at end of file'

PycodestyleBear (W292), severity NORMAL, section autopep8.

args += ('--lines', str(licensecheck_lines))
if licensecheck_copyright:
args += ('--copyright',)
return args + (filename,)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/bears/general/LicenseCheckBear.py
+++ b/bears/general/LicenseCheckBear.py
@@ -34,4 +34,4 @@
         args += ('--lines', str(licensecheck_lines))
         if licensecheck_copyright:
             args += ('--copyright',)
-        return args + (filename,)+        return args + (filename,)

@yash-nisar
Copy link
Member Author

Surprisingly, everything is green again. Over to you, @Makman2 @jayvdb.


@linter(executable='licensecheck',
output_format='regex',
output_regex=r'(?P<filename>.*): (?P<copyright>.*)UNKNOWN',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unknown capturing groups, please remove them as coala will otherwise raise a warning on execution^^

class LicenseCheckBear:
"""
Attempts to check the given file for a license, by searching the start
of the file for text belonging to various licenses. For Ubuntu/Debian
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would layout the docs like this:

Attempts to check the given file for a license, by searching the start
of the file for text belonging to various licenses.

For Ubuntu/Debian ...

As the Ubuntu/Debian part is detailed :) Rule of thumb: The first line/sentence is the short description which shall describe the bear as short as possible, everything following an empty line is detailed description :)

Attempts to check the given file for a license, by searching the start
of the file for text belonging to various licenses. For Ubuntu/Debian
users, the licensecheck_lines option has to be used in accordance with the
licensecheck_tail option.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enclose licensecheck_lines and licensecheck_tail in double backticks :)

:param licensecheck_lines:
Specify how many lines of the file header should be parsed
for license information. Set to 0 to parse the whole
file (and ignore licensecheck_tail).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enclose licensecheck_tail in double backticks

file (and ignore licensecheck_tail).
:param licensecheck_tail:
Specify how many bytes to parse at end of file. Set to 0 to
avoid parsing from end of file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rephrase to

Set to 0 to disable parsing from end of file.

avoid parsing from end of file.
"""
return '--lines', str(licensecheck_lines), '--tail', \
str(licensecheck_tail), filename
Copy link
Member

@Makman2 Makman2 Mar 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use braces instead of \:

return ('--lines', ..., ..., 
        'folllowing-args', ...)

# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding some code to these files?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(to some of them)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some code too and update the regex. :D

def add_two_numbers(a, b):
return a + b

print(add_two_numbers(4, 5))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E305 expected 2 blank lines after class or function definition, found 1'

PycodestyleBear (E305), severity NORMAL, section autopep8.

@yash-nisar
Copy link
Member Author

Done @Makman2 :)

of the file for text belonging to various licenses.

For Ubuntu/Debian users, the `licensecheck_lines` option has to be used in
accordance with the `licensecheck_tail` option.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use double backticks, and I'm not sure if RST handles single ones properly. Could you change that everywhere? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry. :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. :)

file (and ignore `licensecheck_tail`).
:param licensecheck_tail:
Specify how many bytes to parse at end of file. Set to 0 to disable
parsing from end of file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation off

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I forget that. 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. :)

'No license found.',
file=get_testfile_path('no_license.py'))],
filename=get_testfile_path('no_license.py'),
settings={'licensecheck_lines': 'int = 10'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int = 10 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean just '10'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will check the first 10 lines and parse 5000 bytes from the bottom of the file. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the int is invalid, isn't it? it's parsed as a string then^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @Makman2 , you are absolutely correct, actually I had copied that statement from the docs and added quotes because of some error in haste. Have filed an issue for the same. :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed*


@linter(executable='licensecheck',
output_format='regex',
output_regex=r'(^(.*/)*)([^/]*): .*UNKNOWN$',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for paths I would use .*, as this may not work on windows ;)

Copy link
Member

@Makman2 Makman2 Mar 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we maybe need some regex composition library, where you could do something like regexes.path + r': .*UNKNOWN$')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just an idea, don't seach for one ;D)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought initially but we don't have support for windows so..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now our bear is future proof. :D

@yash-nisar
Copy link
Member Author

Done @Makman2 :D

@Makman2
Copy link
Member

Makman2 commented Mar 8, 2017

ack 01d4f24

@Makman2
Copy link
Member

Makman2 commented Mar 8, 2017

@rultor merge

@rultor
Copy link

rultor commented Mar 8, 2017

@rultor merge

@Makman2 OK, I'll try to merge now. You can check the progress of the merge here

@rultor rultor merged commit 01d4f24 into coala:master Mar 8, 2017
@rultor
Copy link

rultor commented Mar 8, 2017

@rultor merge

@Makman2 Done! FYI, the full log is here (took me 2min)


@linter(executable='licensecheck',
output_format='regex',
output_regex=r'.*: .*UNKNOWN$',
Copy link
Member

@jayvdb jayvdb Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh.
The first .* should have been something like [^:]+.

@yash-nisar yash-nisar deleted the test branch March 9, 2017 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

CopyrightBear / LicenseCheckBear
10 participants