-
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: Gemfile info extraction #120
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d16901
InfoExtractor: Add spec_references field
satwikkansal a348113
Info: Add example_values field.
satwikkansal 5ffd9b8
info_extraction: Add Info classes
satwikkansal 299c5e2
info_extractors: Add package.json extractor
satwikkansal d23329a
info_extractors: Extract info from Gemfile
satwikkansal 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) |
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,30 @@ | ||
from gemfileparser import GemfileParser | ||
|
||
from coala_quickstart.info_extraction.InfoExtractor import InfoExtractor | ||
from coala_quickstart.info_extraction.Information import ( | ||
ProjectDependencyInfo, VersionInfo) | ||
|
||
|
||
class GemfileInfoExtractor(InfoExtractor): | ||
supported_files = ("Gemfile",) | ||
|
||
spec_references = ["https://gitlab.com/coala/GSoC-2017/issues/167", ] | ||
supported_information_kinds = ( | ||
ProjectDependencyInfo, VersionInfo) | ||
|
||
def parse_file(self, fname, file_content): | ||
parser = GemfileParser(fname) | ||
return parser.parse() | ||
|
||
def find_information(self, fname, parsed_file): | ||
results = [] | ||
for grp, deps in parsed_file.items(): | ||
for dep in deps: | ||
results.append( | ||
ProjectDependencyInfo( | ||
fname, | ||
dep.name, | ||
version=VersionInfo(fname, dep.requirement), | ||
url=dep.source)) | ||
|
||
return results |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
coala_bears~=0.11 | ||
coala_utils~=0.4 | ||
gemfileparser~=0.6.2 |
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,72 @@ | ||
import os | ||
import unittest | ||
|
||
from coala_quickstart.info_extractors.GemfileInfoExtractor import ( | ||
GemfileInfoExtractor) | ||
from coala_quickstart.info_extraction.Information import ( | ||
ProjectDependencyInfo, VersionInfo) | ||
from tests.TestUtilities import generate_files | ||
|
||
|
||
test_file = """ | ||
source "https://rubygems.org" | ||
|
||
gem "puppet-lint", "2.1.1" | ||
gem "rubocop", "0.47.1" | ||
gem "scss_lint", require: false | ||
gem "RedCloth", :require => "redcloth" | ||
gem "omniauth", ">= 0.2.6", :git => "git://github.com/intridea/omniauth.git" | ||
|
||
group :assets do | ||
gem 'some-gem', source: "https://gems.example.com" | ||
end | ||
gem "rspec-rails", ">= 2.6.1", :group => [:development, :test] | ||
|
||
# Some comment | ||
# gem "not_to_consider" | ||
|
||
group :development, :test, :cucumber do | ||
gem "rspec-rails", "~> 2.0.0" | ||
gem "ruby-debug19", :platforms => :mri_19 | ||
end | ||
""" | ||
|
||
|
||
class GemfileInfoExtractorTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.current_dir = os.getcwd() | ||
|
||
def test_extracted_information(self): | ||
|
||
with generate_files( | ||
["Gemfile"], | ||
[test_file], | ||
self.current_dir) as gen_file: | ||
|
||
self.uut = GemfileInfoExtractor( | ||
["Gemfile"], | ||
self.current_dir) | ||
|
||
extracted_info = self.uut.extract_information() | ||
extracted_info = extracted_info[ | ||
os.path.normcase("Gemfile")] | ||
|
||
information_types = extracted_info.keys() | ||
|
||
self.assertIn("ProjectDependencyInfo", information_types) | ||
dep_info = extracted_info["ProjectDependencyInfo"] | ||
self.assertEqual(len(dep_info), 9) | ||
|
||
gems = [('some-gem', ''), ('puppet-lint', '2.1.1'), | ||
('rubocop', '0.47.1'), ('scss_lint', ''), ('RedCloth', ''), | ||
('rspec-rails', '>= 2.6.1'), ('rspec-rails', '~> 2.0.0'), | ||
('ruby-debug19', ''), ('omniauth', '>= 0.2.6')] | ||
|
||
deps = [(d.value, d.version.value) for d in dep_info] | ||
self.assertNotIn(("not_to_consider", ""), deps) | ||
for gem in gems: | ||
self.assertIn(gem, deps) | ||
|
||
source_urls = [d.url for d in dep_info] | ||
self.assertIn("https://gems.example.com", source_urls) |
Oops, something went wrong.
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.
Again, a gemfile spec (official or unofficial) would be awesome