-
Notifications
You must be signed in to change notification settings - Fork 76
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
gsoc: package.json info extraction #118
Merged
rultor
merged 4 commits into
coala:master
from
satwikkansal:pakcage_json_info_extraction
Jun 26, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from coala_quickstart.info_extraction.Info import Info | ||
|
||
|
||
class LicenseUsedInfo(Info): | ||
description = "License of the project." | ||
value_type = (str,) | ||
example_values = ["MIT", "GPL-3", "Apache-2.0"] | ||
|
||
|
||
class VersionInfo(Info): | ||
description = "Version information, see http://semver.org/" | ||
value_type = (str,) | ||
example_values = [">=1.2.7", "~1.2.3", "^0.2"] | ||
|
||
|
||
class ProjectDependencyInfo(Info): | ||
description = "Dependency of the project." | ||
value_type = (str,) | ||
example_values = ['some_npm_package_name', 'some_gem_name', 'other_dep'] | ||
|
||
def __init__(self, | ||
source, | ||
value, | ||
extractor=None, | ||
version=None, | ||
url=''): | ||
super().__init__(source, value, extractor, version=version, url=url) | ||
|
||
|
||
class PathsInfo(Info): | ||
description = "File path globs mentioned in the file." | ||
value_type = ([str],) | ||
example_values = ["**.py", "dev/tests/**"] | ||
|
||
|
||
class IncludePathsInfo(PathsInfo): | ||
description = "Target files to perform analysis." | ||
|
||
|
||
class IgnorePathsInfo(PathsInfo): | ||
description = "Files to ignore during analysis." | ||
|
||
|
||
class ManFilesInfo(Info): | ||
description = "Filenames to put in place for the man program to find." | ||
value_type = (str, [str]) | ||
example_values = ["./man/doc.1", ["./man/foo.1", "./man/bar.1"]] | ||
|
||
def __init__(self, | ||
source, | ||
value, | ||
extractor=None, | ||
keyword=""): | ||
""" | ||
:param keyword: Primary keyword for ``man`` command that would display | ||
the man pages provided in value argument. | ||
""" | ||
super().__init__(source, value, extractor, keyword=keyword) |
61 changes: 61 additions & 0 deletions
61
coala_quickstart/info_extractors/PackageJSONInfoExtractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
import logging | ||
|
||
from coala_quickstart.info_extraction.InfoExtractor import InfoExtractor | ||
from coala_quickstart.info_extraction.Information import ( | ||
LicenseUsedInfo, ProjectDependencyInfo, IncludePathsInfo, ManFilesInfo, | ||
VersionInfo) | ||
|
||
|
||
class PackageJSONInfoExtractor(InfoExtractor): | ||
supported_file_globs = ("package.json",) | ||
|
||
spec_references = [ | ||
"https://docs.npmjs.com/files/package.json", | ||
"https://gitlab.com/coala/GSoC-2017/issues/167"] | ||
|
||
supported_info_kinds = ( | ||
LicenseUsedInfo, | ||
ProjectDependencyInfo, | ||
IncludePathsInfo, | ||
ManFilesInfo) | ||
|
||
def parse_file(self, fname, file_content): | ||
parsed_file = {} | ||
|
||
try: | ||
parsed_file = json.loads(file_content) | ||
except Exception: | ||
logging.warning("Error while parsing the file {}".format(fname)) | ||
|
||
return parsed_file | ||
|
||
def find_information(self, fname, parsed_file): | ||
results = [] | ||
|
||
if parsed_file.get("license"): | ||
results.append( | ||
LicenseUsedInfo(fname, parsed_file["license"])) | ||
|
||
if parsed_file.get("dependencies"): | ||
for package_name, version_range in ( | ||
parsed_file["dependencies"].items()): | ||
results.append( | ||
ProjectDependencyInfo( | ||
fname, | ||
package_name, | ||
self.__class__.__name__, | ||
VersionInfo(fname, version_range))) | ||
|
||
if parsed_file.get("files"): | ||
results.append( | ||
IncludePathsInfo(fname, parsed_file["files"])) | ||
|
||
if parsed_file.get("man"): | ||
results.append( | ||
ManFilesInfo( | ||
fname, | ||
parsed_file["man"], | ||
keyword=parsed_file.get("name"))) | ||
|
||
return results |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,11 @@ def find_information(self, fname, parsed_file): | |
class InfoA(Info): | ||
description = 'Information A' | ||
value_type = (str, int) | ||
example_values = ['coala', 420] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hehe |
||
|
||
class InfoB(Info): | ||
description = "Info class without value_type" | ||
example_values = [["literally", "anything"]] | ||
|
||
self.info_a = InfoA( | ||
'source_file', | ||
|
@@ -43,7 +45,8 @@ def test_main(self): | |
self.assertEqual(self.base_info.name, 'Info') | ||
self.assertEqual(self.base_info.value, 'base_info_value') | ||
self.assertEqual(self.base_info.source, 'source_file') | ||
self.assertEqual(self.base_info.description, 'Some information') | ||
self.assertEqual(self.base_info.description, 'Some Description.') | ||
self.assertEqual(len(self.base_info.example_values), 0) | ||
self.assertIsInstance(self.base_info.extractor, InfoExtractor) | ||
|
||
def test_derived_instances(self): | ||
|
@@ -52,6 +55,7 @@ def test_derived_instances(self): | |
self.assertEqual(self.info_a.source, 'source_file') | ||
self.assertEqual(self.info_a.extra_param, 'extra_param_value') | ||
self.assertEqual(self.info_a.description, 'Information A') | ||
self.assertEqual(self.info_a.example_values, ['coala', 420]) | ||
|
||
def test_value_type(self): | ||
with self.assertRaisesRegexp( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
import unittest | ||
|
||
from coala_quickstart.info_extractors.PackageJSONInfoExtractor import ( | ||
PackageJSONInfoExtractor) | ||
from coala_quickstart.info_extraction.Information import ( | ||
LicenseUsedInfo, ProjectDependencyInfo, IncludePathsInfo, ManFilesInfo, | ||
VersionInfo) | ||
from tests.TestUtilities import generate_files | ||
|
||
|
||
test_file = """ { | ||
"name": "awesome-packages", | ||
"version": "0.8.0", | ||
"license": "MIT", | ||
"dependencies": { | ||
"coffeelint": "~1", | ||
"ramllint": ">=1.2.2 <1.2.4" | ||
}, | ||
"files": ["dist"], | ||
"man" : ["./man/foo.1", "./man/bar.1"] | ||
} | ||
""" | ||
|
||
invalid_test_file = """ | ||
Some content that is not JSON! | ||
""" | ||
|
||
class PackageJSONInfoExtractorTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.current_dir = os.getcwd() | ||
|
||
def test_extracted_information(self): | ||
|
||
with generate_files( | ||
["package.json"], | ||
[test_file], | ||
self.current_dir) as gen_file: | ||
|
||
self.uut = PackageJSONInfoExtractor( | ||
["package.json"], | ||
self.current_dir) | ||
|
||
extracted_information = self.uut.extract_information() | ||
extracted_information = extracted_information["package.json"] | ||
|
||
information_types = extracted_information.keys() | ||
|
||
self.assertIn("LicenseUsedInfo", information_types) | ||
license_info = extracted_information["LicenseUsedInfo"] | ||
self.assertEqual(len(license_info), 1) | ||
self.assertEqual(license_info[0].value, "MIT") | ||
|
||
self.assertIn("ProjectDependencyInfo", information_types) | ||
dep_info = extracted_information["ProjectDependencyInfo"] | ||
self.assertEqual(len(dep_info), 2) | ||
self.assertIn(dep_info[0].value, ["coffeelint", "ramllint"]) | ||
self.assertIsInstance(dep_info[0].version, VersionInfo) | ||
self.assertIn( | ||
dep_info[0].version.value, ["~1", ">=1.2.2 <1.2.4"]) | ||
|
||
self.assertIn("ManFilesInfo", information_types) | ||
man_paths_info = extracted_information["ManFilesInfo"] | ||
self.assertEqual(len(man_paths_info), 1) | ||
self.assertEqual(man_paths_info[0].value, ["./man/foo.1", "./man/bar.1"]) | ||
|
||
self.assertIn("IncludePathsInfo", information_types) | ||
include_paths_info = extracted_information["IncludePathsInfo"] | ||
self.assertEqual(len(include_paths_info), 1) | ||
self.assertEqual(include_paths_info[0].value, ["dist"]) | ||
|
||
def test_invalid_files(self): | ||
|
||
with generate_files( | ||
["package.json"], | ||
[invalid_test_file], | ||
self.current_dir) as gen_file: | ||
|
||
self.uut = PackageJSONInfoExtractor( | ||
["package.json"], | ||
self.current_dir) | ||
extracted_information = self.uut.extract_information() | ||
self.assertEqual(extracted_information, {}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quick question: the
.
in the glob isn't "match any character", right? Do we need to escape it? I don't quite remember how our globbing works :DThe simplest way to test would be to create a file called "packagexjson" and see if it's picked up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
matches only.
, just verified :)packagexjson
fails for glob "package.json"and
package.json
falis for escaped glob "package/.json"