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

devonfw / IDEasy / 34104600373

07 Sep 2026 09:11AM UTC coverage: 73.878% (-0.003%) from 73.881%
34104600373

push

github

web-flow
#2190: IDEasy destroys my python installation (#2384)

Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>

5560 of 8378 branches covered (66.36%)

Branch coverage included in aggregate %.

14486 of 18756 relevant lines covered (77.23%)

3.29 hits per line

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

66.36
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 getInstalledVersion(Path toolPath) {
100

101
    VersionIdentifier version = super.getInstalledVersion(toolPath);
4✔
102
    if (version == null) {
2✔
103
      // the virtual environment can be recreated by uv or python and then the version file is lost, so we ask the
104
      // installation itself instead of reporting that python is not installed - see
105
      // https://github.com/devonfw/IDEasy/issues/2190
106
      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
    }
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 = binPath.resolve(getBinaryName());
5✔
150
    if (!Files.exists(binaryPath)) {
5✔
151
      binaryPath = binPath.resolve(getBinaryName() + ".exe");
6✔
152
    }
153
    if (!Files.exists(binaryPath)) {
5✔
154
      LOG.debug("Python binary does not exist in {}.", binPath);
4✔
155
      return null;
2✔
156
    }
157
    String output = this.context.newProcess().runAndGetSingleOutput(IdeLogLevel.DEBUG, binaryPath.toString(), "--version");
14✔
158
    if (output == null) {
2!
159
      return null;
×
160
    }
161
    String version = output.trim();
3✔
162
    int lastSpace = version.lastIndexOf(' ');
4✔
163
    if (lastSpace >= 0) {
2!
164
      version = version.substring(lastSpace + 1);
6✔
165
    }
166
    if (!version.isEmpty() && Character.isDigit(version.charAt(0))) {
8!
167
      return VersionIdentifier.of(version);
3✔
168
    }
169
    LOG.debug("Could not parse version from output '{}' of {}.", output, binaryPath);
×
170
    return null;
×
171
  }
172

173
  @Override
174
  public ToolRepository getToolRepository() {
175

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

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

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

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

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

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