Search in sources :

Example 1 with SimpleRunner

use of org.python.pydev.ast.runners.SimpleRunner 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 SimpleRunner

use of org.python.pydev.ast.runners.SimpleRunner 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 SimpleRunner

use of org.python.pydev.ast.runners.SimpleRunner in project Pydev by fabioz.

the class InterpreterInfo method obtainUserSitePackages.

@Override
public String obtainUserSitePackages(IInterpreterManager interpreterManager) {
    if (userSitePackages == null) {
        SimpleRunner simpleRunner = new SimpleRunner();
        Tuple<String, String> output = simpleRunner.runAndGetOutput(new String[] { executableOrJar, "-m", "site", PlatformUtils.isWindowsPlatform() ? "--user-site" : "--user-base" }, new File(executableOrJar).getParentFile(), new SystemPythonNature(interpreterManager, this), null, "utf-8");
        userSitePackages = output.o1.trim();
    }
    return userSitePackages;
}
Also used : SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 4 with SimpleRunner

use of org.python.pydev.ast.runners.SimpleRunner in project Pydev by fabioz.

the class CondaPackageManager method listCondaEnvironments.

public static List<File> listCondaEnvironments(File condaExecutable) {
    List<File> lst = new ArrayList<>();
    String encoding = "utf-8";
    Tuple<String, String> output = new SimpleRunner().runAndGetOutput(new String[] { condaExecutable.toString(), "env", "list", "--json" }, null, null, null, encoding);
    Log.logInfo(output.o1);
    JsonObject jsonOutput = JsonValue.readFrom(output.o1).asObject();
    JsonArray envs = jsonOutput.get("envs").asArray();
    for (JsonValue env : envs.values()) {
        lst.add(new File(env.asString()));
    }
    return lst;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) ArrayList(java.util.ArrayList) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) File(java.io.File)

Example 5 with SimpleRunner

use of org.python.pydev.ast.runners.SimpleRunner in project Pydev by fabioz.

the class CondaPackageManager method manage.

public void manage(String[] initialCommands, boolean autoRun, File workingDir) {
    final File condaExecutable;
    try {
        condaExecutable = PyDevCondaPreferences.findCondaExecutable(interpreterInfo);
    } catch (UnableToFindExecutableException e) {
        Log.log(e);
        PyDialogHelpers.openException("Unable to find conda", e);
        return;
    }
    ProcessWindow processWindow = new ProcessWindow(UIUtils.getActiveShell()) {

        @Override
        protected void configureShell(Shell shell) {
            super.configureShell(shell);
            shell.setText("Manage conda");
        }

        @Override
        protected String[] getAvailableCommands() {
            List<String> lst = new ArrayList<>(Arrays.asList(initialCommands));
            final String prefixDir = new File(interpreterInfo.getExecutableOrJar()).getParent();
            String prefixInfo = " -p " + prefixDir;
            for (int i = 0; i < lst.size(); i++) {
                String existing = lst.get(i);
                if (!existing.contains("-p")) {
                    existing = existing.trim();
                    if (existing.startsWith("install") || existing.startsWith("uninstall") || existing.startsWith("upgrade") || existing.startsWith("update") || existing.startsWith("clean") || existing.startsWith("list") || existing.startsWith("package") || existing.startsWith("remove") || existing.startsWith("search")) {
                        existing += prefixInfo;
                    }
                    lst.set(i, existing);
                }
            }
            return ArrayUtils.concatArrays(lst.toArray(new String[0]), new String[] { "install -p " + prefixDir + " <package>", "uninstall -p " + prefixDir + " <package>" });
        }

        @Override
        protected String getSeeURL() {
            return "https://conda.io/docs/commands.html";
        }

        @Override
        public Tuple<Process, String> createProcess(String[] arguments) {
            clearOutput();
            AbstractShell.restartAllShells();
            String[] cmdLine = ArrayUtils.concatArrays(new String[] { condaExecutable.toString() }, arguments);
            return new SimpleRunner().run(cmdLine, workingDir, null, null);
        }
    };
    if (workingDir == null) {
        workingDir = condaExecutable.getParentFile();
    }
    processWindow.setParameters(null, null, condaExecutable, workingDir);
    processWindow.setAutoRun(autoRun);
    processWindow.open();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) AbstractShell(org.python.pydev.ast.codecompletion.shell.AbstractShell) SimpleRunner(org.python.pydev.ast.runners.SimpleRunner) UnableToFindExecutableException(org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException) ArrayList(java.util.ArrayList) File(java.io.File) ProcessWindow(org.python.pydev.process_window.ProcessWindow)

Aggregations

File (java.io.File)10 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)10 ArrayList (java.util.ArrayList)7 UnableToFindExecutableException (org.python.pydev.core.IInterpreterInfo.UnableToFindExecutableException)4 ExternalAnalizerProcessWatchDoc (com.python.pydev.analysis.external.ExternalAnalizerProcessWatchDoc)3 IFile (org.eclipse.core.resources.IFile)3 IProject (org.eclipse.core.resources.IProject)3 IPythonNature (org.python.pydev.core.IPythonNature)3 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)3 Shell (org.eclipse.swt.widgets.Shell)2 AbstractShell (org.python.pydev.ast.codecompletion.shell.AbstractShell)2 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)2 MisconfigurationException (org.python.pydev.core.MisconfigurationException)2 JsonArray (org.python.pydev.json.eclipsesource.JsonArray)2 JsonObject (org.python.pydev.json.eclipsesource.JsonObject)2 JsonValue (org.python.pydev.json.eclipsesource.JsonValue)2 ProcessWindow (org.python.pydev.process_window.ProcessWindow)2 Tuple (org.python.pydev.shared_core.structure.Tuple)2 HashSet (java.util.HashSet)1 List (java.util.List)1