|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Copyright 2022 Sushant Kumar ([email protected]) |
| 6 | +SPDX-License-Identifier: GPL-2.0 |
| 7 | +This program is free software; you can redistribute it and/or |
| 8 | +modify it under the terms of the GNU General Public License |
| 9 | +version 2 as published by the Free Software Foundation. |
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +You should have received a copy of the GNU General Public License along |
| 15 | +with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | +""" |
| 18 | + |
| 19 | +__author__ = 'Sushant Kumar' |
| 20 | + |
| 21 | + |
| 22 | +import argparse |
| 23 | + |
| 24 | +from atarashi.agents.atarashiAgent import AtarashiAgent |
| 25 | +from atarashi.libs.initialmatch import spdx_identifer |
| 26 | +from linearsvc import linearsvc |
| 27 | + |
| 28 | + |
| 29 | +class Linearsvc(AtarashiAgent): |
| 30 | + |
| 31 | + def __init__(self, licenseList): |
| 32 | + super().__init__(licenseList) |
| 33 | + |
| 34 | + def predict_shortname(self, processed_comment): |
| 35 | + ''' |
| 36 | + :param filePath: extracted and preprocessed comment |
| 37 | + :return: Returns the predicted license's short name |
| 38 | + ''' |
| 39 | + |
| 40 | + processed_comment = [processed_comment] |
| 41 | + classifier = linearsvc(processed_comment) |
| 42 | + predictor = classifier.classify() |
| 43 | + return predictor.predict(processed_comment) |
| 44 | + |
| 45 | + def scan(self, filePath): |
| 46 | + ''' |
| 47 | + Read the content of filename, extract the comments and preprocess them. |
| 48 | + Find the predicted short name for the preprocessed file. |
| 49 | + :param filePath: Path of the file to scan |
| 50 | + :return: Returns the license's short name |
| 51 | + ''' |
| 52 | + |
| 53 | + match = [] |
| 54 | + |
| 55 | + with open(filePath) as file: |
| 56 | + raw_data = file.read() |
| 57 | + |
| 58 | + spdx_identifers = spdx_identifer(raw_data, |
| 59 | + self.licenseList['shortname']) |
| 60 | + if spdx_identifers: |
| 61 | + match.extend(spdx_identifers) |
| 62 | + else: |
| 63 | + processed_comment = super().loadFile(filePath) |
| 64 | + license_name = self.predict_shortname(processed_comment) |
| 65 | + |
| 66 | + match.append({ |
| 67 | + 'shortname': str(license_name[0]), |
| 68 | + 'sim_score': 1.0, |
| 69 | + 'sim_type': 'linearsvc', |
| 70 | + 'description': '', |
| 71 | + }) |
| 72 | + return match |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + |
| 77 | + parser = argparse.ArgumentParser() |
| 78 | + parser.add_argument('processedLicenseList', |
| 79 | + help='Specify the processed license list file') |
| 80 | + parser.add_argument('inputFile', |
| 81 | + help='Specify the input file which needs to be scanned' |
| 82 | + ) |
| 83 | + |
| 84 | + args = parser.parse_args() |
| 85 | + |
| 86 | + licenseList = args.processedLicenseList |
| 87 | + filename = args.inputFile |
| 88 | + |
| 89 | + scanner = Linearsvc(licenseList) |
| 90 | + scanner.scan(filename) |
0 commit comments