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

devonfw / IDEasy / 35624018318

21 Sep 2026 04:11PM UTC coverage: 74.213% (-0.005%) from 74.218%
35624018318

Pull #2438

github

web-flow
Merge 09d8a4569 into a6809790c
Pull Request #2438: #2422: cleanup determine installed edition and version and restore software version file

5691 of 8522 branches covered (66.78%)

Branch coverage included in aggregate %.

14783 of 19066 relevant lines covered (77.54%)

3.32 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.LocalToolCommandlet;
23
import com.devonfw.tools.ide.tool.ToolCommandlet;
24
import com.devonfw.tools.ide.tool.ToolInstallRequest;
25
import com.devonfw.tools.ide.tool.ToolInstallation;
26
import com.devonfw.tools.ide.tool.repository.ToolRepository;
27
import com.devonfw.tools.ide.tool.uv.Uv;
28
import com.devonfw.tools.ide.version.VersionIdentifier;
29

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

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

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

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

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

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

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

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

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

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

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

71
    fileAccess.delete(venvPath);
3✔
72

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

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

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

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

90
  @Override
91
  protected boolean isIgnoreSoftwareRepo() {
92

93
    return true;
2✔
94
  }
95

96
  @Override
97
  protected boolean isIgnoreMissingSoftwareVersionFile() {
98

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

103
  @Override
104
  protected VersionIdentifier computeInstalledVersionFromLocalSoftwareFolder() {
105

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

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

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

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

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

172
  @Override
173
  public ToolRepository getToolRepository() {
174

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

178
  /**
179
   * 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"
180
   * directory.
181
   *
182
   * @param fileAccess the {@link FileAccess} utility for file operations.
183
   * @param installationPath the path where Python is installed.
184
   */
185
  private void createWindowsSymlinkBinFolder(FileAccess fileAccess, Path installationPath) {
186

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

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

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

208
}
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