|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2019 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Package implementing the various conda related modules. |
|
8 """ |
|
9 |
|
10 import json |
|
11 |
|
12 from PyQt6.QtCore import QCoreApplication, QProcess |
|
13 |
|
14 import Preferences |
|
15 |
|
16 __CondaVersion = () |
|
17 __CondaVersionStr = "" |
|
18 __CondaRootPrefix = "" |
|
19 __CondaUserConfig = "" |
|
20 |
|
21 __initialized = False |
|
22 |
|
23 |
|
24 def __initializeCondaInterface(): |
|
25 """ |
|
26 Private module function to (re-)initialize the conda interface. |
|
27 """ |
|
28 global __CondaVersionStr, __CondaVersion, __CondaRootPrefix |
|
29 global __CondaUserConfig, __initialized |
|
30 |
|
31 if not __initialized: |
|
32 exe = Preferences.getConda("CondaExecutable") |
|
33 if not exe: |
|
34 exe = "conda" |
|
35 |
|
36 proc = QProcess() |
|
37 proc.start(exe, ["info", "--json"]) |
|
38 if not proc.waitForStarted(msecs=15000): |
|
39 __CondaVersionStr = QCoreApplication.translate( |
|
40 "CondaInterface", |
|
41 '<conda not found or not configured.>') |
|
42 else: |
|
43 proc.waitForFinished(msecs=15000) |
|
44 output = str(proc.readAllStandardOutput(), |
|
45 Preferences.getSystem("IOEncoding"), |
|
46 'replace').strip() |
|
47 try: |
|
48 jsonDict = json.loads(output) |
|
49 except Exception: |
|
50 __CondaVersionStr = QCoreApplication.translate( |
|
51 "CondaInterface", |
|
52 '<conda returned invalid data.>') |
|
53 return |
|
54 |
|
55 if "error" in jsonDict: |
|
56 __CondaVersionStr = QCoreApplication.translate( |
|
57 "CondaInterface", |
|
58 '<conda returned an error: {0}.>').format( |
|
59 jsonDict["error"]) |
|
60 else: |
|
61 __CondaVersionStr = jsonDict["conda_version"] |
|
62 __CondaVersion = tuple( |
|
63 int(i) for i in __CondaVersionStr.split(".") |
|
64 ) |
|
65 __CondaRootPrefix = jsonDict["root_prefix"] |
|
66 __CondaUserConfig = jsonDict.get("user_rc_path") |
|
67 if __CondaUserConfig is None: |
|
68 __CondaUserConfig = jsonDict.get("rc_path") |
|
69 |
|
70 __initialized = True |
|
71 |
|
72 |
|
73 def condaVersion(): |
|
74 """ |
|
75 Module function to get the conda version. |
|
76 |
|
77 @return tuple containing the conda version |
|
78 @rtype tuple of (int, int, int) |
|
79 """ |
|
80 __initializeCondaInterface() |
|
81 return __CondaVersion |
|
82 |
|
83 |
|
84 def condaVersionStr(): |
|
85 """ |
|
86 Module function to get the conda version as a string. |
|
87 |
|
88 @return conda version as a string |
|
89 @rtype str |
|
90 """ |
|
91 __initializeCondaInterface() |
|
92 return __CondaVersionStr |
|
93 |
|
94 |
|
95 def rootPrefix(): |
|
96 """ |
|
97 Module function to get the root prefix. |
|
98 |
|
99 @return root prefix |
|
100 @rtype str |
|
101 """ |
|
102 __initializeCondaInterface() |
|
103 return __CondaRootPrefix |
|
104 |
|
105 |
|
106 def userConfiguration(): |
|
107 """ |
|
108 Module function to get the path of the user configuration file. |
|
109 |
|
110 @return path of the user configuration file |
|
111 @rtype str |
|
112 """ |
|
113 __initializeCondaInterface() |
|
114 return __CondaUserConfig |
|
115 |
|
116 |
|
117 def isCondaAvailable(): |
|
118 """ |
|
119 Module function to check the availability of conda. |
|
120 |
|
121 @return flag indicating conda availability |
|
122 @rtype bool |
|
123 """ |
|
124 __initializeCondaInterface() |
|
125 return bool(__CondaVersion) |
|
126 |
|
127 |
|
128 def resetInterface(): |
|
129 """ |
|
130 Module function to reset the conda interface. |
|
131 """ |
|
132 global __initialized |
|
133 |
|
134 __initialized = False |