-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from rafalnowak/pylint
Processor for Pylint
- Loading branch information
Showing
25 changed files
with
664 additions
and
24 deletions.
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
11 changes: 11 additions & 0 deletions
11
src/main/java/pl/touk/sputnik/processor/pylint/PylintException.java
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,11 @@ | ||
package pl.touk.sputnik.processor.pylint; | ||
|
||
public class PylintException extends RuntimeException { | ||
public PylintException(String message) { | ||
super(message); | ||
} | ||
|
||
public PylintException(String message, Throwable t) { | ||
super(message, t); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/pl/touk/sputnik/processor/pylint/PylintExecutor.java
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,47 @@ | ||
package pl.touk.sputnik.processor.pylint; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Lists; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.exec.ExternalProcess; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
public class PylintExecutor { | ||
private static final String PYLINT_EXECUTABLE = "pylint"; | ||
private static final String PYLINT_OUTPUT_FORMAT = "--output-format=json"; | ||
private static final String PYLINT_RCFILE_NAME = "--rcfile="; | ||
|
||
private String rcfileName; | ||
|
||
public PylintExecutor(@Nullable String rcfileName) { | ||
this.rcfileName = rcfileName; | ||
} | ||
|
||
public String runOnFile(String filePath) { | ||
log.info("Review on file: " + filePath); | ||
return new ExternalProcess().executeCommand(buildParams(filePath)); | ||
} | ||
|
||
@NotNull | ||
private String[] buildParams(String filePath) { | ||
List<String> basicPylintArgs = ImmutableList.of( | ||
PYLINT_EXECUTABLE, | ||
PYLINT_OUTPUT_FORMAT); | ||
List<String> rcfileNameArg = getRcfileNameAsList(); | ||
List<String> filePathArg = ImmutableList.of(filePath); | ||
List<String> allArgs = Lists.newArrayList(Iterables.concat(basicPylintArgs, rcfileNameArg, filePathArg)); | ||
return allArgs.toArray(new String[allArgs.size()]); | ||
} | ||
|
||
private List<String> getRcfileNameAsList() { | ||
if (rcfileName == null) { | ||
return ImmutableList.of(); | ||
} | ||
return ImmutableList.of(PYLINT_RCFILE_NAME + rcfileName); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/pl/touk/sputnik/processor/pylint/PylintProcessor.java
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,45 @@ | ||
package pl.touk.sputnik.processor.pylint; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
import pl.touk.sputnik.processor.tools.externalprocess.ExternalProcessResultParser; | ||
import pl.touk.sputnik.processor.tools.externalprocess.ProcessorRunningExternalProcess; | ||
import pl.touk.sputnik.review.filter.FileFilter; | ||
import pl.touk.sputnik.review.filter.PythonFilter; | ||
|
||
import java.io.File; | ||
|
||
@Slf4j | ||
public class PylintProcessor extends ProcessorRunningExternalProcess { | ||
|
||
private PylintExecutor pylintExecutor; | ||
private PylintResultParser pylintResultParser; | ||
|
||
public PylintProcessor(Configuration configuration) { | ||
pylintExecutor = new PylintExecutor(configuration.getProperty(GeneralOption.PYLINT_RCFILE)); | ||
pylintResultParser = new PylintResultParser(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getName() { | ||
return "Pylint"; | ||
} | ||
|
||
@Override | ||
public FileFilter getReviewFileFilter() { | ||
return new PythonFilter(); | ||
} | ||
|
||
@Override | ||
public ExternalProcessResultParser getParser() { | ||
return pylintResultParser; | ||
} | ||
|
||
@Override | ||
public String getOutputFromExternalProcess(File fileToReview) { | ||
return pylintExecutor.runOnFile(fileToReview.getAbsolutePath()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/pl/touk/sputnik/processor/pylint/PylintProcessorFactory.java
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,17 @@ | ||
package pl.touk.sputnik.processor.pylint; | ||
|
||
import pl.touk.sputnik.configuration.Configuration; | ||
import pl.touk.sputnik.configuration.GeneralOption; | ||
import pl.touk.sputnik.processor.ReviewProcessorFactory; | ||
|
||
public class PylintProcessorFactory implements ReviewProcessorFactory<PylintProcessor> { | ||
@Override | ||
public boolean isEnabled(Configuration configuration) { | ||
return Boolean.valueOf(configuration.getProperty(GeneralOption.PYLINT_ENABLED)); | ||
} | ||
|
||
@Override | ||
public PylintProcessor create(Configuration configuration) { | ||
return new PylintProcessor(configuration); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/pl/touk/sputnik/processor/pylint/PylintResultParser.java
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,68 @@ | ||
package pl.touk.sputnik.processor.pylint; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.commons.lang3.StringUtils; | ||
import pl.touk.sputnik.processor.tools.externalprocess.ExternalProcessResultParser; | ||
import pl.touk.sputnik.processor.pylint.json.PylintMessage; | ||
import pl.touk.sputnik.processor.pylint.json.PylintMessages; | ||
import pl.touk.sputnik.review.Severity; | ||
import pl.touk.sputnik.review.Violation; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class PylintResultParser implements ExternalProcessResultParser { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private static final String PYLINT_ERROR = "error"; | ||
private static final String PYLINT_FATAL = "fatal"; | ||
private static final String PYLINT_WARNING = "warning"; | ||
private static final String PYLINT_CONVENTION = "convention"; | ||
private static final String PYLINT_REFACTOR = "refactor"; | ||
|
||
@Override | ||
public List<Violation> parse(String pylintOutput) { | ||
if (StringUtils.isEmpty(pylintOutput)) { | ||
return Collections.emptyList(); | ||
} | ||
try { | ||
List<Violation> violations = new ArrayList<>(); | ||
PylintMessages messages = objectMapper.readValue(removeHeaderFromPylintOutput(pylintOutput), PylintMessages.class); | ||
for (PylintMessage message : messages) { | ||
Violation violation = new Violation(message.getPath(), | ||
message.getLine(), | ||
formatViolationMessageFromPylint(message), | ||
pylintMessageTypeToSeverity(message.getMessage(), message.getType())); | ||
violations.add(violation); | ||
} | ||
return violations; | ||
} catch (IOException e) { | ||
throw new PylintException("Error when converting from json format", e); | ||
} | ||
} | ||
|
||
private String removeHeaderFromPylintOutput(String pylintOutput) { | ||
return pylintOutput.replace("No config file found, using default configuration", ""); | ||
} | ||
|
||
private String formatViolationMessageFromPylint(PylintMessage message) { | ||
return message.getMessage() + " [" + message.getSymbol() + "]"; | ||
} | ||
|
||
private Severity pylintMessageTypeToSeverity(String message, String messageType) { | ||
if (PYLINT_ERROR.equals(messageType)) { | ||
return Severity.ERROR; | ||
} else if (PYLINT_WARNING.equals(messageType)) { | ||
return Severity.WARNING; | ||
} else if (PYLINT_CONVENTION.equals(messageType) || PYLINT_REFACTOR.equals(messageType)) { | ||
return Severity.INFO; | ||
} else if (PYLINT_FATAL.equals(messageType)) { | ||
throw new PylintException("Fatal error from pylint (" + message + ")"); | ||
} else { | ||
throw new PylintException("Unknown message type returned by pylint (type = " + messageType + ")"); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/pl/touk/sputnik/processor/pylint/json/PylintMessage.java
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,17 @@ | ||
package pl.touk.sputnik.processor.pylint.json; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
public class PylintMessage { | ||
private String message; | ||
private String obj; | ||
private int column; | ||
private String path; | ||
private int line; | ||
private String type; | ||
private String symbol; | ||
private String module; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/pl/touk/sputnik/processor/pylint/json/PylintMessages.java
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,6 @@ | ||
package pl.touk.sputnik.processor.pylint.json; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class PylintMessages extends ArrayList<PylintMessage> { | ||
} |
9 changes: 9 additions & 0 deletions
9
...ain/java/pl/touk/sputnik/processor/tools/externalprocess/ExternalProcessResultParser.java
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,9 @@ | ||
package pl.touk.sputnik.processor.tools.externalprocess; | ||
|
||
import pl.touk.sputnik.review.Violation; | ||
|
||
import java.util.List; | ||
|
||
public interface ExternalProcessResultParser { | ||
List<Violation> parse(String output); | ||
} |
34 changes: 34 additions & 0 deletions
34
...java/pl/touk/sputnik/processor/tools/externalprocess/ProcessorRunningExternalProcess.java
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,34 @@ | ||
package pl.touk.sputnik.processor.tools.externalprocess; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import pl.touk.sputnik.review.Review; | ||
import pl.touk.sputnik.review.ReviewProcessor; | ||
import pl.touk.sputnik.review.ReviewResult; | ||
import pl.touk.sputnik.review.Violation; | ||
import pl.touk.sputnik.review.filter.FileFilter; | ||
import pl.touk.sputnik.review.transformer.IOFileTransformer; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public abstract class ProcessorRunningExternalProcess implements ReviewProcessor { | ||
|
||
@Override | ||
@NotNull | ||
public ReviewResult process(@NotNull Review review) { | ||
ReviewResult result = new ReviewResult(); | ||
List<File> files = review.getFiles(getReviewFileFilter(), new IOFileTransformer()); | ||
for (File file : files) { | ||
for (Violation violation : getParser().parse(getOutputFromExternalProcess(file))) { | ||
result.add(violation); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public abstract FileFilter getReviewFileFilter(); | ||
|
||
public abstract ExternalProcessResultParser getParser(); | ||
|
||
public abstract String getOutputFromExternalProcess(File fileToReview); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/pl/touk/sputnik/processor/tslint/TSLintException.java
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,11 @@ | ||
package pl.touk.sputnik.processor.tslint; | ||
|
||
public class TSLintException extends RuntimeException { | ||
public TSLintException(String message) { | ||
super(message); | ||
} | ||
|
||
public TSLintException(String message, Throwable t) { | ||
super(message, t); | ||
} | ||
} |
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
Oops, something went wrong.