Skip to content

Commit

Permalink
Revert usage of PyCharm as frontend (SonarSource#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaladin authored and andrea-guarino-sonarsource committed Aug 19, 2019
1 parent 58a1337 commit 6c69290
Show file tree
Hide file tree
Showing 81 changed files with 1,073 additions and 3,116 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ Desktop.ini
# ---- Sonar
.sonar
.scannerwork

pycharm-community-*
14 changes: 0 additions & 14 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pipeline {
SONARSOURCE_QA = 'true'
MAVEN_TOOL = 'Maven 3.6.x'
JDK_VERSION = 'Java 11'
PYCHARM_VERSION = '2019.1.3'
}
stages {
stage('Notify') {
Expand Down Expand Up @@ -55,19 +54,6 @@ pipeline {
steps {
withMaven(maven: MAVEN_TOOL) {
mavenSetBuildVersion()
sh "curl -L -O https://download-cf.jetbrains.com/python/pycharm-community-${PYCHARM_VERSION}.tar.gz"
sh "tar xzf pycharm-community-${PYCHARM_VERSION}.tar.gz"
sh "rm pycharm-community-${PYCHARM_VERSION}.tar.gz"
dir("pycharm-community-$PYCHARM_VERSION/lib") {
runMaven(JDK_VERSION,"install:install-file -Dfile=extensions.jar -DgroupId=com.jetbrains.pycharm -DartifactId=extensions -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=openapi.jar -DgroupId=com.jetbrains.pycharm -DartifactId=openapi -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=platform-api.jar -DgroupId=com.jetbrains.pycharm -DartifactId=platform-api -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=platform-impl.jar -DgroupId=com.jetbrains.pycharm -DartifactId=platform-impl -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=pycharm.jar -DgroupId=com.jetbrains.pycharm -DartifactId=pycharm -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=pycharm-pydev.jar -DgroupId=com.jetbrains.pycharm -DartifactId=pycharm-pydev -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=resources_en.jar -DgroupId=com.jetbrains.pycharm -DartifactId=resources_en -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
runMaven(JDK_VERSION,"install:install-file -Dfile=util.jar -DgroupId=com.jetbrains.pycharm -DartifactId=util -Dversion=${PYCHARM_VERSION} -Dpackaging=jar")
}
runMaven(JDK_VERSION,"clean install -Dskip.its=true")
}
}
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,3 @@ SonarPython is a code analyzer for Python projects.
Copyright 2011-2018 SonarSource.

Licensed under the [GNU Lesser General Public License, Version 3.0](http://www.gnu.org/licenses/lgpl.txt)

## Build

Download required dependency:

> ./tools/download-pycharm.sh
Build the project using Maven:

> mvn clean install
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void project_level() {
assertThat(getProjectMeasureAsInt(NCLOC)).isEqualTo(6);
assertThat(getProjectMeasureAsInt(LINES)).isEqualTo(13);
assertThat(getProjectMeasureAsInt(FILES)).isEqualTo(2);
assertThat(getProjectMeasureAsInt(STATEMENTS)).isEqualTo(6);
assertThat(getProjectMeasureAsInt(STATEMENTS)).isEqualTo(5);
assertThat(getProjectMeasureAsInt(FUNCTIONS)).isEqualTo(1);
assertThat(getProjectMeasureAsInt(CLASSES)).isEqualTo(0);
// Documentation
Expand Down Expand Up @@ -114,7 +114,7 @@ public void directory_level() {
assertThat(getDirectoryMeasureAsInt(NCLOC)).isEqualTo(6);
assertThat(getDirectoryMeasureAsInt(LINES)).isEqualTo(13);
assertThat(getDirectoryMeasureAsInt(FILES)).isEqualTo(2);
assertThat(getDirectoryMeasureAsInt(STATEMENTS)).isEqualTo(6);
assertThat(getDirectoryMeasureAsInt(STATEMENTS)).isEqualTo(5);
assertThat(getDirectoryMeasureAsInt(FUNCTIONS)).isEqualTo(1);
assertThat(getDirectoryMeasureAsInt(CLASSES)).isEqualTo(0);
// Documentation
Expand All @@ -140,7 +140,7 @@ public void file_level() {
assertThat(getFileMeasureAsInt(NCLOC)).isEqualTo(1);
assertThat(getFileMeasureAsInt(LINES)).isEqualTo(6);
assertThat(getFileMeasureAsInt(FILES)).isEqualTo(1);
assertThat(getFileMeasureAsInt(STATEMENTS)).isEqualTo(2);
assertThat(getFileMeasureAsInt(STATEMENTS)).isEqualTo(1);
assertThat(getFileMeasureAsInt(FUNCTIONS)).isEqualTo(1);
assertThat(getFileMeasureAsInt(CLASSES)).isEqualTo(0);
// Documentation
Expand Down
10 changes: 0 additions & 10 deletions its/ruling/src/test/resources/expected/python-S100.json
Original file line number Diff line number Diff line change
Expand Up @@ -1539,15 +1539,6 @@
743,
762,
826,
911,
915,
926,
938,
941,
960,
969,
975,
982,
1116,
1137,
1151,
Expand Down Expand Up @@ -2401,7 +2392,6 @@
149,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
162,
165,
171,
Expand Down
7 changes: 0 additions & 7 deletions its/ruling/src/test/resources/expected/python-S108.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
112,
144,
],
'project:numpy-1.16.4/numpy/core/einsumfunc.py':[
832,
836,
],
'project:numpy-1.16.4/numpy/core/records.py':[
179,
],
Expand Down Expand Up @@ -128,9 +124,6 @@
'project:twisted-12.1.0/twisted/internet/_threadedselect.py':[
281,
],
'project:twisted-12.1.0/twisted/internet/tcp.py':[
562,
],
'project:twisted-12.1.0/twisted/mail/test/test_pop3.py':[
72,
],
Expand Down
22 changes: 22 additions & 0 deletions its/ruling/src/test/resources/expected/python-S1542.json
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,15 @@
],
'project:twisted-12.1.0/twisted/internet/base.py':[
856,
911,
915,
926,
938,
941,
960,
969,
975,
982,
],
'project:twisted-12.1.0/twisted/internet/default.py':[
17,
Expand Down Expand Up @@ -1606,6 +1615,15 @@
'project:twisted-12.1.0/twisted/internet/inotify.py':[
99,
],
'project:twisted-12.1.0/twisted/internet/iocpreactor/reactor.py':[
172,
184,
193,
205,
],
'project:twisted-12.1.0/twisted/internet/iocpreactor/tcp.py':[
145,
],
'project:twisted-12.1.0/twisted/internet/main.py':[
20,
],
Expand Down Expand Up @@ -2249,6 +2267,7 @@
264,
],
'project:twisted-12.1.0/twisted/protocols/ident.py':[
149,
153,
],
'project:twisted-12.1.0/twisted/protocols/loopback.py':[
Expand Down Expand Up @@ -2846,6 +2865,9 @@
1065,
1108,
1141,
1374,
1382,
1390,
],
'project:twisted-12.1.0/twisted/test/test_iutils.py':[
67,
Expand Down
2 changes: 0 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
</developers>

<modules>
<module>python-frontend</module>
<module>python-squid</module>
<module>python-checks</module>
<module>sonar-python-plugin</module>
Expand Down Expand Up @@ -96,7 +95,6 @@
<sonarlint-core.version>3.0.0.1140</sonarlint-core.version>
<sslr.version>1.23</sslr.version>
<sslr.xpath.version>1.22</sslr.xpath.version>
<pycharm.version>2019.1.3</pycharm.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
*/
package org.sonar.python.checks;

import com.intellij.lang.ASTNode;
import com.jetbrains.python.PyStubElementTypes;
import com.jetbrains.python.psi.PyFunction;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AstNodeType;
import java.util.Collections;
import java.util.Set;
import org.sonar.check.RuleProperty;
import org.sonar.python.api.PythonGrammar;

public abstract class AbstractFunctionNameCheck extends AbstractNameCheck {

private static final String DEFAULT = "^[a-z_][a-z0-9_]{2,}$";
private static final String MESSAGE = "Rename %s \"%s\" to match the regular expression %s.";

@RuleProperty(
key = "format",
Expand All @@ -38,26 +42,25 @@ protected String format() {
}

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(PyStubElementTypes.FUNCTION_DECLARATION, ctx -> {
PyFunction node = (PyFunction) ctx.syntaxNode();
if (!shouldCheckFunctionDeclaration(node)) {
return;
}
ASTNode nameNode = node.getNameNode();
if (nameNode == null) {
return;
}
String name = nameNode.getText();
if (!pattern().matcher(name).matches()) {
String message = String.format("Rename %s \"%s\" to match the regular expression %s.", typeName(), name, format);
ctx.addIssue(nameNode.getPsi(), message);
}
});
public Set<AstNodeType> subscribedKinds() {
return Collections.singleton(PythonGrammar.FUNCDEF);
}

@Override
public void visitNode(AstNode astNode) {
if (!shouldCheckFunctionDeclaration(astNode)) {
return;
}
AstNode nameNode = astNode.getFirstChild(PythonGrammar.FUNCNAME);
String name = nameNode.getTokenValue();
if (!pattern().matcher(name).matches()) {
String message = String.format(MESSAGE, typeName(), name, this.format);
addIssue(nameNode, message);
}
}

public abstract String typeName();

public abstract boolean shouldCheckFunctionDeclaration(PyFunction function);
public abstract boolean shouldCheckFunctionDeclaration(AstNode astNode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@
*/
package org.sonar.python.checks;

import com.intellij.lang.ASTNode;
import com.intellij.psi.tree.IElementType;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyFileElementType;
import com.jetbrains.python.psi.PyStatement;
import com.jetbrains.python.psi.PyStatementList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AstNodeType;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.python.PythonCheck;
import org.sonar.python.SubscriptionContext;
import org.sonar.python.api.PythonGrammar;

@Rule(key = "S1763")
@Rule(key = AfterJumpStatementCheck.CHECK_KEY)
public class AfterJumpStatementCheck extends PythonCheck {

private static final Set<IElementType> JUMP_TYPES = new HashSet<>(Arrays.asList(
PyElementTypes.RETURN_STATEMENT,
PyElementTypes.RAISE_STATEMENT,
PyElementTypes.BREAK_STATEMENT,
PyElementTypes.CONTINUE_STATEMENT
));
public static final String CHECK_KEY = "S1763";

private static final String MESSAGE = "Remove the code after this \"%s\".";

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(PyElementTypes.STATEMENT_LIST, ctx -> {
PyStatementList statementList = (PyStatementList) ctx.syntaxNode();
checkStatements(ctx, Arrays.asList(statementList.getStatements()));
});
context.registerSyntaxNodeConsumer(PyFileElementType.INSTANCE, ctx -> {
PyFile pyFile = (PyFile) ctx.syntaxNode();
checkStatements(ctx, pyFile.getStatements());
});
public Set<AstNodeType> subscribedKinds() {
return immutableSet(
PythonGrammar.RETURN_STMT,
PythonGrammar.RAISE_STMT,
PythonGrammar.BREAK_STMT,
PythonGrammar.CONTINUE_STMT
);
}

private static void checkStatements(SubscriptionContext ctx, List<PyStatement> statements) {
for (PyStatement statement : statements.subList(0, Math.max(statements.size() - 1, 0))) {
if (JUMP_TYPES.contains(statement.getNode().getElementType())) {
ASTNode keyword = statement.getNode().findLeafElementAt(0);
ctx.addIssue(keyword.getPsi(), String.format(
"Refactor this piece of code to not have any dead code after this \"%s\".", keyword.getText()));
@Override
public void visitNode(AstNode node) {
AstNode simpleStatement = node.getParent();

AstNode nextSibling = simpleStatement.getNextSibling();
if (nextSibling != null && nextSibling.getNextSibling() != null) {
raiseIssue(node);
return;
}

AstNode stmtList = simpleStatement.getParent();
if (stmtList.getParent().is(PythonGrammar.STATEMENT)){
nextSibling = stmtList.getParent().getNextSibling();
if (nextSibling != null && nextSibling.getNextSibling() != null){
raiseIssue(node);
}
}
}

private void raiseIssue(AstNode node) {
addIssue(node, String.format(MESSAGE, node.getTokenValue()));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
*/
package org.sonar.python.checks;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AstNodeType;
import java.util.Collections;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.python.PythonCheck;
import org.sonar.python.SubscriptionContext;
import org.sonar.python.api.PythonTokenType;

@Rule(key = "S1717")
public class BackslashInStringCheck extends PythonCheck {
Expand All @@ -34,17 +34,13 @@ public class BackslashInStringCheck extends PythonCheck {
private static final String VALID_ESCAPED_CHARACTERS = "abfnrtvxnNrtuU\\'\"0123456789\n\r";

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(PyElementTypes.STRING_LITERAL_EXPRESSION, ctx -> {
PyStringLiteralExpression expression = (PyStringLiteralExpression) ctx.syntaxNode();
for (ASTNode stringNode : expression.getStringNodes()) {
checkLiteral(ctx, stringNode.getPsi());
}
});
public Set<AstNodeType> subscribedKinds() {
return Collections.singleton(PythonTokenType.STRING);
}

public void checkLiteral(SubscriptionContext ctx, PsiElement literal) {
String string = literal.getNode().getText();
@Override
public void visitNode(AstNode node) {
String string = node.getTokenOriginalValue();
int length = string.length();
boolean isEscaped = false;
boolean inPrefix = true;
Expand All @@ -58,7 +54,7 @@ public void checkLiteral(SubscriptionContext ctx, PsiElement literal) {
}
} else {
if (isEscaped && VALID_ESCAPED_CHARACTERS.indexOf(c) == -1 && !isBackslashedSpaceAfterInlineMarkup(isThreeQuotes, string, i, c)) {
ctx.addIssue(literal, MESSAGE);
addIssue(node, MESSAGE);
}
isEscaped = c == '\\' && !isEscaped;
}
Expand Down
Loading

0 comments on commit 6c69290

Please sign in to comment.