Add new fingerprint plugin - #1576
Conversation
Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
Codecov Report
@@ Coverage Diff @@
## develop #1576 +/- ##
==========================================
- Coverage 84.35% 82.3% -2.06%
==========================================
Files 124 126 +2
Lines 14594 14659 +65
==========================================
- Hits 12311 12065 -246
- Misses 2283 2594 +311
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## develop #1576 +/- ##
===========================================
- Coverage 81.7% 81.16% -0.54%
===========================================
Files 126 125 -1
Lines 15333 15478 +145
===========================================
+ Hits 12528 12563 +35
- Misses 2805 2915 +110
Continue to review full report at Codecov.
|
Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
| options = [ | ||
| CommandLineOption(('-f', '--fingerprint'), | ||
| is_flag=True, default=False, | ||
| help='Scan <input> to generate fingerprint.', |
There was a problem hiding this comment.
We might want to be a bit more descriptive here and state why kind(s) of fingerprints the -f flag will generate.
There was a problem hiding this comment.
@MaJuRG Okay. Since it is a very descriptive answer to your question, it would not be the right place to keep it IMHO. Can you please suggest me how should I frame it?
There was a problem hiding this comment.
Sure. It does not have to be in-depth, but even just adding the name of the fingerprint or something like ... to generate simhash fingerprints for use in similarity matching. Something like that.
There was a problem hiding this comment.
Sure. will change it.
| import hashlib | ||
|
|
||
| hash_length = 128 | ||
| shingle_length = 3 |
There was a problem hiding this comment.
Standard convention for constants like these should be all caps: HASH_LENGTH, SHINGLE_LENGTH to distinguish them from other variables.
There was a problem hiding this comment.
Sure. Will change it.
| { | ||
| "path": "factorial-test.java", | ||
| "type": "file", | ||
| "fingerprint": "00000001110011111011010000110111101011001110110100111111011111011000111001000001100001001111010011011000011001110011000100100111", |
There was a problem hiding this comment.
is there another way to store this fingerprint, or does it have to be a binary string? The length of these fingerprints is unsightly IMO.
There was a problem hiding this comment.
@arnav-mandal1234 Store the fingerprint as a hex string instead of a binary string
There was a problem hiding this comment.
We just want to calcuate the fingerprints for each file in scancode-toolkit. When it comes to the actual comparison aspect, that will be handled in deltacode (calculating the hamming distance, etc). I do not think we have to move any code over, correct? The additions you made were simply to:
- create the fingerprint for each file
- add it to scan output
When we go to actually check two separate scan output files, deltacode will be doing those comparisons. Does this make sense?
There was a problem hiding this comment.
| def test_generate_fingerprint1(self): | ||
| test_file = self.get_test_loc('fingerprint/fingerprint-test1.java') | ||
| result = generate_fingerprint(test_file) | ||
| assert result == '01001110010000101101100011000000111011010110011010010011011001010100100001100110010000100101010001010001001000010000010000010111' |
There was a problem hiding this comment.
See comment above. Is there another way to store this fingerprint?
|
@pombredanne @JonoYang Your review would be welcome as well. |
| weighted_list = get_weightedlist(token_list) | ||
| fingerprint = process_weightedlist(weighted_list) | ||
|
|
||
| return "".join(str(bit) for bit in fingerprint) |
There was a problem hiding this comment.
Style note: we use single quotes ' when declaring strings.
| { | ||
| "path": "factorial-test.java", | ||
| "type": "file", | ||
| "fingerprint": "00000001110011111011010000110111101011001110110100111111011111011000111001000001100001001111010011011000011001110011000100100111", |
There was a problem hiding this comment.
@arnav-mandal1234 Store the fingerprint as a hex string instead of a binary string
Signed-off-by: Li <li@nexb.com>
pombredanne
left a comment
There was a problem hiding this comment.
A few general questions: do we want this as a built-in plugin that is always available? or is this an external plugin?
Also I am sure that cluecode is not the right module for this: until now this was about copyrights and "greps" only.
I made several comments inline too.
| @@ -0,0 +1,60 @@ | |||
| # | |||
| # Copyright (c) 2018 nexB Inc. and others. All rights reserved. | |||
There was a problem hiding this comment.
We are in 2019 alright. But having no year is probably best
There was a problem hiding this comment.
Sure will change this.
| return fingerprint | ||
|
|
||
| def get_scanner(self, **kwargs): | ||
| from scancode.api import get_fingerprint |
There was a problem hiding this comment.
The scancode.api module is a legacy wart. Avoid using this for this code. Instead put it all in this same module here.
There was a problem hiding this comment.
@pombredanne should I shift it to the commoncode/fingerprint.py?
|
|
||
| def generate_fingerprint(location): | ||
| """ | ||
| Return fingerprint of the file at `location`. |
There was a problem hiding this comment.
Theer 4 extra spaces to remove in from the Return
There was a problem hiding this comment.
Sure will change this.
| """ | ||
| Return fingerprint of the file at `location`. | ||
| """ | ||
| token_list = get_tokenlist(location) |
There was a problem hiding this comment.
why do you qualify each variable and functions with a type?
You should have instead tokens = get_tokens(location) here and in other similar places below and elsewhere
There was a problem hiding this comment.
Okay. will change all the similar variable and function names.
|
|
||
| def get_tokenlist(location): | ||
| """ | ||
| Return a list of tokens for the file at `location`. |
There was a problem hiding this comment.
Okay. Will do that.
|
|
||
| from commoncode.filetype import get_last_modified_date | ||
| from commoncode.hash import multi_checksums | ||
| from commoncode.fingerprint import generate_fingerprint |
There was a problem hiding this comment.
The import needs to be properly ordered
There was a problem hiding this comment.
In sorted order?
There was a problem hiding this comment.
Yes sorted alphabetically
There was a problem hiding this comment.
Okay. will change it.
| result['is_script'] = bool(collector.is_script) | ||
| return result | ||
|
|
||
| def get_fingerprint(location, **kwargs): |
There was a problem hiding this comment.
Do not put this code in this module. Keep it with your plugin instead
There was a problem hiding this comment.
Sure will keep this in commoncode/fingerprint.py
| @@ -0,0 +1,15 @@ | |||
| public class Factorial | |||
There was a problem hiding this comment.
Can you include real code and text and data files as tests rather than only made up ones?
There was a problem hiding this comment.
Sorry I didn't get you. Please explain. :)
There was a problem hiding this comment.
He means you should use actual source found in the wild, like code from an open source repository.
This is a better representation of files scancode will actually come across in the wild.
There should also be a verity of these, differing in lines of code AND language.
There was a problem hiding this comment.
Okay. I will change it then. :)
| @@ -0,0 +1,114 @@ | |||
| # | |||
There was a problem hiding this comment.
Why is this under commoncode when your plugin is in cluecode?
There was a problem hiding this comment.
Actually, all the main functions are in commoncode i.e. fingerprint.py. And the plugin is in cluecode. what should be the structure? should I move everything to commoncode?
Signed-off-by: Li <li@nexb.com>
…#253 Signed-off-by: Li <li@nexb.com>
Signed-off-by: Li <li@nexb.com>
Unicode string literals are simply converted into string literals, which are always Unicode in Python 3. Unicode raw strings (in which Python does not auto-escape backslashes) are converted to raw strings. In Python 3, raw strings are always Unicode. Signed-off-by: Abhishek Kumar <abhishek.kasyap09@gmail.com>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
range() is now its own generator-like class in Python 3 and was a list in Python2. We now handle what is a generator-like object properly on both versions. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
For now run only the commoncode tests on Python 2.7 to tune the setup Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
These are in the form of: First Last <email> (c) year Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
Add new License rules and licenses
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
…rithm Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
Signed-off-by: arnav-mandal1234 <arnav.mandal1234@gmail.com>
…4/scancode-toolkit into gsoc_fingerprint
|
@arnav-mandal1234 there seems to be something rather wrong with this PR and its 6710 modified files! is there some issues with your rebasing? |
@pombredanne Something went wrong. I had opened a new PR with the same changes and it got merged. :) |
|
@pombredanne This can be closed. #1651 was merged and was the replacement for this. |
Signed-off-by: arnav-mandal1234 arnav.mandal1234@gmail.com