160 self.tr("<p>The conda executable returned an error.</p>" |
164 self.tr("<p>The conda executable returned an error.</p>" |
161 "<p>{0}</p>").format(jsonDict["message"])) |
165 "<p>{0}</p>").format(jsonDict["message"])) |
162 return False |
166 return False |
163 |
167 |
164 return jsonDict["success"] |
168 return jsonDict["success"] |
|
169 |
|
170 def getCondaEnvironmentsList(self, listPrefixes=False): |
|
171 """ |
|
172 Public method to get a list of all Conda environments. |
|
173 |
|
174 @param listPrefixes flag indicating to return prefixes instead of names |
|
175 @type bool |
|
176 @return list of environment names or prefixes |
|
177 @rtype list of str |
|
178 """ |
|
179 # TODO: return environment name and prefix |
|
180 # TODO: return root environment only, if writable ('root_prefix', 'root_writable') |
|
181 exe = Preferences.getConda("CondaExecutable") |
|
182 if not exe: |
|
183 exe = "conda" |
|
184 |
|
185 environmentsList = [] |
|
186 |
|
187 proc = QProcess() |
|
188 proc.start(exe, ["info", "--json"]) |
|
189 if proc.waitForStarted(15000): |
|
190 if proc.waitForFinished(15000): |
|
191 output = str(proc.readAllStandardOutput(), |
|
192 Preferences.getSystem("IOEncoding"), |
|
193 'replace').strip() |
|
194 try: |
|
195 jsonDict = json.loads(output) |
|
196 except Exception: |
|
197 jsonDict = {} |
|
198 |
|
199 if "envs" in jsonDict: |
|
200 if listPrefixes: |
|
201 environmentsList = jsonDict["envs"][:] |
|
202 else: |
|
203 environmentsList = [ |
|
204 os.path.basename(e) for e in jsonDict["envs"]] |
|
205 |
|
206 return environmentsList |
|
207 |
|
208 ####################################################################### |
|
209 ## package related methods below |
|
210 ####################################################################### |
|
211 |
|
212 def getInstalledPackages(self, name="", prefix=""): |
|
213 """ |
|
214 Public method to get a list of installed packages of a conda |
|
215 environment. |
|
216 |
|
217 @param name name of the environment |
|
218 @type str |
|
219 @param prefix prefix of the environment |
|
220 @type str |
|
221 @return list of installed packages. Each entry is a tuple containing |
|
222 the package name, version and build. |
|
223 @rtype list of tuples of (str, str, str) |
|
224 @rtype bool |
|
225 @exception RuntimeError raised to indicate an error in parameters |
|
226 |
|
227 Note: only one of name or prefix must be given. |
|
228 """ |
|
229 if name and prefix: |
|
230 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
231 |
|
232 if not name and not prefix: |
|
233 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
234 |
|
235 args = [ |
|
236 "list", |
|
237 "--json", |
|
238 ] |
|
239 if name: |
|
240 args.extend(["--name", name]) |
|
241 elif prefix: |
|
242 args.extend(["--prefix", prefix]) |
|
243 |
|
244 exe = Preferences.getConda("CondaExecutable") |
|
245 if not exe: |
|
246 exe = "conda" |
|
247 |
|
248 packages = [] |
|
249 |
|
250 proc = QProcess() |
|
251 proc.start(exe, args) |
|
252 if proc.waitForStarted(15000): |
|
253 if proc.waitForFinished(15000): |
|
254 output = str(proc.readAllStandardOutput(), |
|
255 Preferences.getSystem("IOEncoding"), |
|
256 'replace').strip() |
|
257 try: |
|
258 jsonList = json.loads(output) |
|
259 except Exception: |
|
260 jsonList = [] |
|
261 |
|
262 for package in jsonList: |
|
263 if isinstance(package, dict): |
|
264 packages.append(( |
|
265 package["name"], |
|
266 package["version"], |
|
267 package["build_string"] |
|
268 )) |
|
269 else: |
|
270 packages.append(tuple(package.rsplit("-", 2))) |
|
271 |
|
272 return packages |
|
273 |
|
274 def getUpdateablePackages(self, name="", prefix=""): |
|
275 """ |
|
276 Public method to get a list of updateable packages of a conda |
|
277 environment. |
|
278 |
|
279 @param name name of the environment |
|
280 @type str |
|
281 @param prefix prefix of the environment |
|
282 @type str |
|
283 @return list of installed packages. Each entry is a tuple containing |
|
284 the package name, version and build. |
|
285 @rtype list of tuples of (str, str, str) |
|
286 @rtype bool |
|
287 @exception RuntimeError raised to indicate an error in parameters |
|
288 |
|
289 Note: only one of name or prefix must be given. |
|
290 """ |
|
291 if name and prefix: |
|
292 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
293 |
|
294 if not name and not prefix: |
|
295 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
296 |
|
297 args = [ |
|
298 "update", |
|
299 "--json", |
|
300 "--quiet", |
|
301 "--all", |
|
302 "--dry-run", |
|
303 ] |
|
304 if name: |
|
305 args.extend(["--name", name]) |
|
306 elif prefix: |
|
307 args.extend(["--prefix", prefix]) |
|
308 |
|
309 exe = Preferences.getConda("CondaExecutable") |
|
310 if not exe: |
|
311 exe = "conda" |
|
312 |
|
313 packages = [] |
|
314 |
|
315 proc = QProcess() |
|
316 proc.start(exe, args) |
|
317 if proc.waitForStarted(15000): |
|
318 if proc.waitForFinished(15000): |
|
319 output = str(proc.readAllStandardOutput(), |
|
320 Preferences.getSystem("IOEncoding"), |
|
321 'replace').strip() |
|
322 try: |
|
323 jsonDict = json.loads(output) |
|
324 except Exception: |
|
325 jsonDict = {} |
|
326 |
|
327 if "actions" in jsonDict and "LINK" in jsonDict["actions"]: |
|
328 for linkEntry in jsonDict["actions"]["LINK"]: |
|
329 if isinstance(linkEntry, dict): |
|
330 packages.append(( |
|
331 linkEntry["name"], |
|
332 linkEntry["version"], |
|
333 linkEntry["build_string"] |
|
334 )) |
|
335 else: |
|
336 package = linkEntry.split()[0] |
|
337 packages.append(tuple(package.rsplit("-", 2))) |
|
338 |
|
339 return packages |
|
340 |
|
341 def updatePackages(self, packages, name="", prefix=""): |
|
342 """ |
|
343 Public method to update packages of a conda environment. |
|
344 |
|
345 @param packages list of package names to be updated |
|
346 @type list of str |
|
347 @param name name of the environment |
|
348 @type str |
|
349 @param prefix prefix of the environment |
|
350 @type str |
|
351 @return list of installed packages. Each entry is a tuple containing |
|
352 the package name, version and build. |
|
353 @rtype list of tuples of (str, str, str) |
|
354 @rtype bool |
|
355 @exception RuntimeError raised to indicate an error in parameters |
|
356 |
|
357 Note: only one of name or prefix must be given. |
|
358 """ |
|
359 if name and prefix: |
|
360 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
361 |
|
362 if not name and not prefix: |
|
363 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
364 |
|
365 # TODO: not implemented yet |
|
366 |
|
367 def updateAllPackages(self, name="", prefix=""): |
|
368 """ |
|
369 Public method to update all packages of a conda environment. |
|
370 |
|
371 @param name name of the environment |
|
372 @type str |
|
373 @param prefix prefix of the environment |
|
374 @type str |
|
375 @return list of installed packages. Each entry is a tuple containing |
|
376 the package name, version and build. |
|
377 @rtype list of tuples of (str, str, str) |
|
378 @rtype bool |
|
379 @exception RuntimeError raised to indicate an error in parameters |
|
380 |
|
381 Note: only one of name or prefix must be given. |
|
382 """ |
|
383 if name and prefix: |
|
384 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
385 |
|
386 if not name and not prefix: |
|
387 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
388 |
|
389 # TODO: not implemented yet |
|
390 |
|
391 def installPackages(self, packages, name="", prefix=""): |
|
392 """ |
|
393 Public method to install packages into a conda environment. |
|
394 |
|
395 @param packages list of package names to be installed |
|
396 @type list of str |
|
397 @param name name of the environment |
|
398 @type str |
|
399 @param prefix prefix of the environment |
|
400 @type str |
|
401 @return list of installed packages. Each entry is a tuple containing |
|
402 the package name, version and build. |
|
403 @rtype list of tuples of (str, str, str) |
|
404 @rtype bool |
|
405 @exception RuntimeError raised to indicate an error in parameters |
|
406 |
|
407 Note: only one of name or prefix must be given. |
|
408 """ |
|
409 if name and prefix: |
|
410 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
411 |
|
412 if not name and not prefix: |
|
413 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
414 |
|
415 # TODO: not implemented yet |
|
416 |
|
417 def uninstallPackages(self, packages, name="", prefix=""): |
|
418 """ |
|
419 Public method to uninstall packages of a conda environment. |
|
420 |
|
421 @param packages list of package names to be uninstalled |
|
422 @type list of str |
|
423 @param name name of the environment |
|
424 @type str |
|
425 @param prefix prefix of the environment |
|
426 @type str |
|
427 @return list of installed packages. Each entry is a tuple containing |
|
428 the package name, version and build. |
|
429 @rtype list of tuples of (str, str, str) |
|
430 @rtype bool |
|
431 @exception RuntimeError raised to indicate an error in parameters |
|
432 |
|
433 Note: only one of name or prefix must be given. |
|
434 """ |
|
435 if name and prefix: |
|
436 raise RuntimeError("Only one of 'name' or 'prefix' must be given.") |
|
437 |
|
438 if not name and not prefix: |
|
439 raise RuntimeError("One of 'name' or 'prefix' must be given.") |
|
440 |
|
441 # TODO: not implemented yet |