Search in sources :

Example 1 with ExternalAnalizerProcessWatchDoc

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();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 2 with ExternalAnalizerProcessWatchDoc

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();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) IModulesManager(org.python.pydev.core.IModulesManager) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 3 with ExternalAnalizerProcessWatchDoc

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();
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) PythonNature(org.python.pydev.plugin.nature.PythonNature) ArrayList(java.util.ArrayList) ExternalAnalizerProcessWatchDoc(com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) StringTokenizer(java.util.StringTokenizer) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

ExternalAnalizerProcessWatchDoc (com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 IProject (org.eclipse.core.resources.IProject)3 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)3 IPythonNature (org.python.pydev.core.IPythonNature)2 Tuple (org.python.pydev.shared_core.structure.Tuple)2 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 CoreException (org.eclipse.core.runtime.CoreException)1 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)1 IModulesManager (org.python.pydev.core.IModulesManager)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 PythonNatureWithoutProjectException (org.python.pydev.core.PythonNatureWithoutProjectException)1 PythonNature (org.python.pydev.plugin.nature.PythonNature)1