• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 36534428096

29 Sep 2026 07:03AM UTC coverage: 73.865% (-0.3%) from 74.13%
36534428096

push

github

web-flow
#2293: rework spyder as ide tool commandlet (#2333)

5732 of 8665 branches covered (66.15%)

Branch coverage included in aggregate %.

15101 of 19539 relevant lines covered (77.29%)

3.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.0
cli/src/main/java/com/devonfw/tools/ide/tool/python/Python.java
1
package com.devonfw.tools.ide.tool.python;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.nio.file.StandardCopyOption;
6
import java.util.List;
7
import java.util.Set;
8

9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11

12
import com.devonfw.tools.ide.cli.CliException;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.io.FileAccess;
16
import com.devonfw.tools.ide.log.IdeLogLevel;
17
import com.devonfw.tools.ide.process.EnvironmentContext;
18
import com.devonfw.tools.ide.process.ProcessContext;
19
import com.devonfw.tools.ide.process.ProcessErrorHandling;
20
import com.devonfw.tools.ide.process.ProcessMode;
21
import com.devonfw.tools.ide.process.ProcessResult;
22
import com.devonfw.tools.ide.tool.AbstractLocalToolCommandlet;
23
import com.devonfw.tools.ide.tool.ToolInstallRequest;
24
import com.devonfw.tools.ide.tool.ToolInstallation;
25
import com.devonfw.tools.ide.tool.repository.ToolRepository;
26
import com.devonfw.tools.ide.tool.uv.Uv;
27
import com.devonfw.tools.ide.version.VersionIdentifier;
28

29
/**
30
 * {@link AbstractToolCommandlet} for <a href="https://www.python.org/">python</a>.
31
 */
32
public class Python extends AbstractLocalToolCommandlet {
33

34
  private static final Logger LOG = LoggerFactory.getLogger(Python.class);
3 ✔
35

36
  private static final VersionIdentifier PYTHON_MIN_VERSION = VersionIdentifier.of("3.8.2");
4 ✔
37

38
  /** The folder created by {@code uv venv} inside the software folder before it is renamed to the python installation. */
39
  static final String VENV_FOLDER = ".venv";
40

41
  private static final String FILE_PYVENV_CFG = "pyvenv.cfg";
42

43
  private static final String PYVENV_CFG_VERSION_INFO = "version_info";
44

45
  /**
46
   * The constructor.
47
   *
48
   * @param context the {@link IdeContext}.
49
   */
50
  public Python(IdeContext context) {
51

52
    super(context, "python", Set.of(Tag.PYTHON));
6 ✔
53
  }
1 ✔
54

55
  @Override
56
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
57

58
    VersionIdentifier resolvedVersion = request.getRequested().getResolvedVersion();
4 ✔
59
    if (resolvedVersion.compareVersion(PYTHON_MIN_VERSION).isLess()) {
5 !
60
      throw new CliException("Python version must be at least " + this.PYTHON_MIN_VERSION);
×
61
    }
62

63
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
64
    if (Files.exists(installationPath)) {
5 !
65
      fileAccess.backup(installationPath);
×
66
    }
67
    Path softwarePath = installationPath.getParent();
3 ✔
68
    Path venvPath = softwarePath.resolve(VENV_FOLDER);
4 ✔
69

70
    fileAccess.delete(venvPath);
3 ✔
71

72
    Uv uv = this.context.getCommandletManager().getCommandlet(Uv.class);
7 ✔
73

74
    uv.installPython(softwarePath, resolvedVersion, request.getProcessContext());
6 ✔
75
    renameVenvFolderToPython(fileAccess, softwarePath, installationPath);
5 ✔
76
    this.context.writeVersionFile(resolvedVersion, installationPath);
5 ✔
77
    createWindowsSymlinkBinFolder(fileAccess, installationPath);
4 ✔
78
    LOG.debug("Installed {} in version {} at {}", this.tool, resolvedVersion, installationPath);
18 ✔
79
  }
1 ✔
80

81
  @Override
82
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean additionalInstallation) {
83

84
    super.setEnvironment(environmentContext, toolInstallation, additionalInstallation);
5 ✔
85
    environmentContext.withEnvVar("VIRTUAL_ENV", toolInstallation.rootDir().toString());
7 ✔
86
    environmentContext.withEnvVar("UV_PROJECT_ENVIRONMENT", toolInstallation.rootDir().toString());
7 ✔
87
  }
1 ✔
88

89
  @Override
90
  protected boolean isIgnoreSoftwareRepo() {
91

92
    return true;
2 ✔
93
  }
94

95
  @Override
96
  protected boolean isIgnoreMissingSoftwareVersionFile() {
97

98
    // https://github.com/devonfw/IDEasy/issues/2190
99
    return true;
×
100
  }
101

102
  @Override
103
  protected VersionIdentifier computeInstalledVersionFromLocalSoftwareFolder() {
104

105
    Path toolPath = getToolPath();
3 ✔
106
    VersionIdentifier version = readVersionFromPyvenvCfg(toolPath);
4 ✔
107
    if (version == null) {
2 !
108
      version = readVersionFromInterpreter(toolPath);
4 ✔
109
    }
110
    if (version != null) {
2 !
111
      LOG.debug("Determined version {} of python from the installation at {}.", version, toolPath);
5 ✔
112
    }
113
    return version;
2 ✔
114
  }
115

116
  /**
117
   * @param installationPath the {@link Path} to the virtual environment.
118
   * @return the {@link VersionIdentifier} from the {@code version_info} entry of {@code pyvenv.cfg} or {@code null} if not available or not precise enough.
119
   */
120
  private VersionIdentifier readVersionFromPyvenvCfg(Path installationPath) {
121

122
    Path pyvenvCfg = installationPath.resolve(FILE_PYVENV_CFG);
4 ✔
123
    if (!Files.exists(pyvenvCfg)) {
5 !
124
      return null;
2 ✔
125
    }
126
    String content = this.context.getFileAccess().readFileContent(pyvenvCfg);
×
127
    for (String line : content.split("\\R")) {
×
128
      String[] keyAndValue = line.split("=", 2);
×
129
      if ((keyAndValue.length == 2) && keyAndValue[0].trim().equals(PYVENV_CFG_VERSION_INFO)) {
×
130
        String value = keyAndValue[1].trim();
×
131
        // uv only writes the minor version (e.g. "3.13") for its own interpreters what is too imprecise for us
132
        if (value.chars().filter(c -> c == '.').count() >= 2) {
×
133
          return VersionIdentifier.of(value);
×
134
        }
135
        LOG.debug("Ignoring imprecise version {} from {}.", value, pyvenvCfg);
×
136
      }
137
    }
138
    return null;
×
139
  }
140

141
  /**
142
   * @param installationPath the {@link Path} to the virtual environment.
143
   * @return the {@link VersionIdentifier} reported by the installed python interpreter or {@code null} if it could not be determined.
144
   */
145
  private VersionIdentifier readVersionFromInterpreter(Path installationPath) {
146

147
    Path binPath = this.context.getFileAccess().getBinPath(installationPath);
6 ✔
148
    Path binaryPath = this.context.getPath().findBinary(binPath.resolve(getBinaryName()));
9 ✔
149
    if (!Files.exists(binaryPath)) {
5 !
150
      LOG.debug("Python binary does not exist in {}.", binPath);
×
151
      return null;
×
152
    }
153
    ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.NONE).withPathEntry(binPath);
8 ✔
154
    ProcessResult result = runTool(pc, ProcessMode.DEFAULT_CAPTURE, List.of("--version"));
7 ✔
155
    String output = result.getSingleOutput(IdeLogLevel.DEBUG);
4 ✔
156
    if (output == null) {
2 !
157
      return null;
×
158
    }
159
    String version = output.trim();
3 ✔
160
    int lastSpace = version.lastIndexOf(' ');
4 ✔
161
    if (lastSpace >= 0) {
2 !
162
      version = version.substring(lastSpace + 1);
6 ✔
163
    }
164
    if (!version.isEmpty() && Character.isDigit(version.charAt(0))) {
8 !
165
      return VersionIdentifier.of(version);
3 ✔
166
    }
167
    LOG.debug("Could not parse version from output '{}' of {}.", output, binPath);
×
168
    return null;
×
169
  }
170

171
  @Override
172
  public ToolRepository getToolRepository() {
173

174
    return this.context.getPythonRepository();
4 ✔
175
  }
176

177
  /**
178
   * Creates a symlink from the "Scripts" folder to the "bin" folder on Windows systems. This is necessary for compatibility with tools that expect a "bin"
179
   * directory.
180
   *
181
   * @param fileAccess the {@link FileAccess} utility for file operations.
182
   * @param installationPath the path where Python is installed.
183
   */
184
  private void createWindowsSymlinkBinFolder(FileAccess fileAccess, Path installationPath) {
185

186
    if (!this.context.getSystemInfo().isWindows()) {
5 !
187
      return;
1 ✔
188
    }
189
    Path scriptsPath = installationPath.resolve("Scripts");
×
190
    Path binPath = installationPath.resolve("bin");
×
191
    fileAccess.symlink(scriptsPath, binPath);
×
192
  }
×
193

194
  /**
195
   * Renames the ".venv" folder into the installation path (Python).
196
   *
197
   * @param fileAccess the {@link FileAccess} utility for file operations.
198
   * @param softwarePath the path where the software is installed.
199
   * @param installationPath the target path where the ".venv" folder should be moved.
200
   */
201
  private void renameVenvFolderToPython(FileAccess fileAccess, Path softwarePath, Path installationPath) {
202

203
    Path venvPath = softwarePath.resolve(VENV_FOLDER);
4 ✔
204
    fileAccess.move(venvPath, installationPath, StandardCopyOption.REPLACE_EXISTING);
10 ✔
205
  }
1 ✔
206

207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc