use of com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc in project Pydev by fabioz.
the class Flake8Analysis method createFlake8Process.
/**
* Creates the flake8 process and starts getting its output.
*/
void createFlake8Process(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
String flake8Executable = FileUtils.getFileAbsolutePath(flake8Location);
String target = location.toOSString();
ArrayList<String> cmdList = new ArrayList<String>();
cmdList.add(flake8Executable);
String userArgs = StringUtils.replaceNewLines(Flake8Preferences.getFlake8Args(resource), " ");
List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
// run flake8 in project location
IProject project = resource.getProject();
if (project == null || !project.isAccessible()) {
// If the project is no longer valid, we can't do much.
Log.log("Unable to run flake8 in: " + target + ". Project not available (" + project + ").");
return;
}
File workingDir = project.getLocation().toFile();
for (String s : userArgsAsList) {
if (s.startsWith("--format=")) {
// ignore that as we'll add the '--format' as needed ourselves.
continue;
}
cmdList.add(s);
}
cmdList.add("--format=default");
cmdList.add(target);
String[] args = cmdList.toArray(new String[0]);
// run executable command (flake8 or flake8.bat or flake8.exe)
WriteToStreamHelper.write("Flake8: Executing command line:", out, (Object) args);
IPythonNature nature = PythonNature.getPythonNature(project);
ICallback0<Process> launchProcessCallback = () -> {
SimpleRunner simpleRunner = new SimpleRunner();
final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature, null, null);
Process process = r.o1;
return process;
};
this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, project, true);
this.processWatchDoc.start();
}
use of com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc in project Pydev by fabioz.
the class MypyAnalysis method createMypyProcess.
/**
* Creates the mypy process and starts getting its output.
*/
void createMypyProcess(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
String mypyExecutable = FileUtils.getFileAbsolutePath(mypyLocation);
String target = location.toOSString();
ArrayList<String> cmdList = new ArrayList<String>();
cmdList.add(mypyExecutable);
String userArgs = StringUtils.replaceNewLines(MypyPreferences.getMypyArgs(resource), " ");
List<String> userArgsAsList = new ArrayList<>(Arrays.asList(ProcessUtils.parseArguments(userArgs)));
if (!userArgsAsList.contains("--show-column-numbers")) {
userArgsAsList.add("--show-column-numbers");
}
boolean foundFollowImports = false;
boolean foundCacheDir = false;
for (String arg : userArgsAsList) {
if (arg.startsWith("--follow-imports=silent")) {
foundFollowImports = true;
}
if (arg.startsWith("--cache-dir")) {
foundCacheDir = true;
}
}
if (!foundFollowImports) {
// We just want warnings for the current file.
userArgsAsList.add("--follow-imports=silent");
}
// run mypy in project location
IProject project = resource.getProject();
if (project == null || !project.isAccessible()) {
// If the project is no longer valid, we can't do much.
Log.log("Unable to run mypy in: " + target + ". Project not available (" + project + ").");
return;
}
File workingDir = project.getLocation().toFile();
if (!foundCacheDir) {
// Set a cache dir if one is not given.
userArgsAsList.add("--cache-dir=" + new File(workingDir, ".mypy_cache").toString());
}
cmdList.addAll(userArgsAsList);
cmdList.add(target);
String[] args = cmdList.toArray(new String[0]);
// run executable command (mypy or mypy.bat or mypy.exe)
WriteToStreamHelper.write("Mypy: Executing command line:", out, (Object) args);
IPythonNature nature = PythonNature.getPythonNature(project);
ICallback<String[], String[]> updateEnv = null;
if (MypyPreferences.getAddProjectFoldersToMyPyPath(resource)) {
Collection<String> addToMypyPath = new HashSet<String>();
IModulesManager[] managersInvolved = nature.getAstManager().getModulesManager().getManagersInvolved(false);
for (IModulesManager iModulesManager : managersInvolved) {
for (String s : StringUtils.split(iModulesManager.getNature().getPythonPathNature().getOnlyProjectPythonPathStr(true), "|")) {
if (!s.isEmpty()) {
addToMypyPath.add(s);
}
}
}
if (addToMypyPath.size() > 0) {
updateEnv = new ICallback<String[], String[]>() {
@Override
public String[] call(String[] arg) {
for (int i = 0; i < arg.length; i++) {
// Update var
if (arg[i].startsWith("MYPYPATH=")) {
arg[i] = arg[i] + SimpleRunner.getPythonPathSeparator() + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath);
return arg;
}
}
// Create new var.
return ArrayUtils.concatArrays(arg, new String[] { "MYPYPATH=" + StringUtils.join(SimpleRunner.getPythonPathSeparator(), addToMypyPath) });
}
};
}
}
final ICallback<String[], String[]> finalUpdateEnv = updateEnv;
ICallback0<Process> launchProcessCallback = () -> {
SimpleRunner simpleRunner = new SimpleRunner();
final Tuple<Process, String> r = simpleRunner.run(args, workingDir, nature, null, finalUpdateEnv);
Process process = r.o1;
return process;
};
this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, project, true);
this.processWatchDoc.start();
}
use of com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc in project Pydev by fabioz.
the class PyLintAnalysis method createPyLintProcess.
/**
* Creates the pylint process and starts getting its output.
*/
void createPyLintProcess(IExternalCodeAnalysisStream out) throws CoreException, MisconfigurationException, PythonNatureWithoutProjectException {
String script = FileUtils.getFileAbsolutePath(pyLintLocation);
String target = FileUtils.getFileAbsolutePath(new File(location.toOSString()));
// check whether lint.py module or pylint executable has been specified
boolean isPyScript = script.endsWith(".py") || script.endsWith(".pyw");
ArrayList<String> cmdList = new ArrayList<String>();
// pylint executable
if (!isPyScript) {
cmdList.add(script);
}
// user args
String userArgs = StringUtils.replaceNewLines(PyLintPreferences.getPyLintArgs(resource), " ");
StringTokenizer tokenizer2 = new StringTokenizer(userArgs);
while (tokenizer2.hasMoreTokens()) {
String token = tokenizer2.nextToken();
if (token.equals("--output-format=parseable")) {
continue;
}
if (token.startsWith("--msg-template=")) {
continue;
}
if (token.startsWith("--output-format=")) {
continue;
}
cmdList.add(token);
}
cmdList.add("--output-format=text");
cmdList.add("--msg-template='{C}:{line:3d},{column:2d}: ({symbol}) {msg}'");
// target file to be linted
cmdList.add(target);
String[] args = cmdList.toArray(new String[0]);
// run pylint in project location
IProject project = resource.getProject();
File workingDir = project.getLocation().toFile();
ICallback0<Process> launchProcessCallback;
if (isPyScript) {
// run Python script (lint.py) with the interpreter of current project
PythonNature nature = PythonNature.getPythonNature(project);
if (nature == null) {
Throwable e = new RuntimeException("PyLint ERROR: Nature not configured for: " + project);
Log.log(e);
return;
}
launchProcessCallback = () -> {
String interpreter;
try {
interpreter = nature.getProjectInterpreter().getExecutableOrJar();
} catch (Exception e) {
throw new RuntimeException(e);
}
WriteToStreamHelper.write("PyLint: Executing command line:", out, script, args);
SimplePythonRunner runner = new SimplePythonRunner();
String[] parameters = SimplePythonRunner.preparePythonCallParameters(interpreter, script, args);
Tuple<Process, String> r = runner.run(parameters, workingDir, nature, monitor);
return r.o1;
};
} else {
// run executable command (pylint or pylint.bat or pylint.exe)
launchProcessCallback = () -> {
WriteToStreamHelper.write("PyLint: Executing command line:", out, (Object) args);
SimpleRunner simpleRunner = new SimpleRunner();
Tuple<Process, String> r = simpleRunner.run(args, workingDir, PythonNature.getPythonNature(project), null);
return r.o1;
};
}
this.processWatchDoc = new ExternalAnalizerProcessWatchDoc(out, monitor, this, launchProcessCallback, null, false);
this.processWatchDoc.start();
}
Aggregations