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

devonfw / IDEasy / 33068437017

27 Aug 2026 11:40AM UTC coverage: 73.571% (-0.06%) from 73.632%
33068437017

Pull #2384

github

web-flow
Merge f2ce49d13 into d9844a272
Pull Request #2384: #2190: Restore missing software version file instead of only logging it

5386 of 8164 branches covered (65.97%)

Branch coverage included in aggregate %.

14159 of 18402 relevant lines covered (76.94%)

3.28 hits per line

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

57.94
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.Set;
7

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

11
import com.devonfw.tools.ide.cli.CliException;
12
import com.devonfw.tools.ide.common.Tag;
13
import com.devonfw.tools.ide.context.IdeContext;
14
import com.devonfw.tools.ide.io.FileAccess;
15
import com.devonfw.tools.ide.log.IdeLogLevel;
16
import com.devonfw.tools.ide.process.EnvironmentContext;
17
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
18
import com.devonfw.tools.ide.tool.ToolCommandlet;
19
import com.devonfw.tools.ide.tool.ToolInstallRequest;
20
import com.devonfw.tools.ide.tool.ToolInstallation;
21
import com.devonfw.tools.ide.tool.repository.ToolRepository;
22
import com.devonfw.tools.ide.tool.uv.Uv;
23
import com.devonfw.tools.ide.version.VersionIdentifier;
24

25
/**
26
 * {@link ToolCommandlet} for <a href="https://www.python.org/">python</a>.
27
 */
28
public class Python extends LocalToolCommandlet {
29

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

32
  private static final VersionIdentifier PYTHON_MIN_VERSION = VersionIdentifier.of("3.8.2");
4✔
33

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

37
  private static final String FILE_PYVENV_CFG = "pyvenv.cfg";
38

39
  private static final String PYVENV_CFG_VERSION_INFO = "version_info";
40

41
  /**
42
   * The constructor.
43
   *
44
   * @param context the {@link IdeContext}.
45
   */
46
  public Python(IdeContext context) {
47

48
    super(context, "python", Set.of(Tag.PYTHON));
6✔
49
  }
1✔
50

51
  @Override
52
  protected void performToolInstallation(ToolInstallRequest request, Path installationPath) {
53

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

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

66
    fileAccess.delete(venvPath);
3✔
67

68
    Uv uv = this.context.getCommandletManager().getCommandlet(Uv.class);
7✔
69

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

77
  @Override
78
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean additionalInstallation) {
79

80
    super.setEnvironment(environmentContext, toolInstallation, additionalInstallation);
5✔
81
    environmentContext.withEnvVar("VIRTUAL_ENV", toolInstallation.rootDir().toString());
7✔
82
    environmentContext.withEnvVar("UV_PROJECT_ENVIRONMENT", toolInstallation.rootDir().toString());
7✔
83
  }
1✔
84

85
  @Override
86
  protected boolean isIgnoreSoftwareRepo() {
87

88
    return true;
2✔
89
  }
90

91
  @Override
92
  protected boolean isIgnoreMissingSoftwareVersionFile() {
93

94
    // https://github.com/devonfw/IDEasy/issues/2190
95
    return true;
2✔
96
  }
97

98
  @Override
99
  protected VersionIdentifier detectInstalledVersion(Path installationPath, VersionIdentifier resolvedVersion) {
100

101
    VersionIdentifier version = readVersionFromPyvenvCfg(installationPath);
4✔
102
    if (version == null) {
2!
103
      version = readVersionFromInterpreter(installationPath);
4✔
104
    }
105
    if (version == null) {
2!
106
      LOG.warn("Could not detect the installed version of python at {} - assuming {}.", installationPath, resolvedVersion);
×
107
      return resolvedVersion;
×
108
    }
109
    return version;
2✔
110
  }
111

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

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

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

143
    Path binPath = this.context.getFileAccess().getBinPath(installationPath);
6✔
144
    Path binaryPath = binPath.resolve(getBinaryName());
5✔
145
    if (!Files.exists(binaryPath)) {
5!
146
      binaryPath = binPath.resolve(getBinaryName() + ".exe");
×
147
    }
148
    if (!Files.exists(binaryPath)) {
5!
149
      LOG.debug("Python binary does not exist in {}.", binPath);
×
150
      return null;
×
151
    }
152
    String output = this.context.newProcess().runAndGetSingleOutput(IdeLogLevel.DEBUG, binaryPath.toString(), "--version");
14✔
153
    if (output == null) {
2!
154
      return null;
×
155
    }
156
    String version = output.trim();
3✔
157
    int lastSpace = version.lastIndexOf(' ');
4✔
158
    if (lastSpace >= 0) {
2!
159
      version = version.substring(lastSpace + 1);
6✔
160
    }
161
    if (!version.isEmpty() && Character.isDigit(version.charAt(0))) {
8!
162
      return VersionIdentifier.of(version);
3✔
163
    }
164
    LOG.debug("Could not parse version from output '{}' of {}.", output, binaryPath);
×
165
    return null;
×
166
  }
167

168
  @Override
169
  public ToolRepository getToolRepository() {
170

171
    return this.context.getPythonRepository();
4✔
172
  }
173

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

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

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

200
    Path venvPath = softwarePath.resolve(VENV_FOLDER);
4✔
201
    fileAccess.move(venvPath, installationPath, StandardCopyOption.REPLACE_EXISTING);
10✔
202
  }
1✔
203

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