@@ -73,7 +73,7 @@ public static PythonLibraryPath Parse(string s) {
73
73
/// <param name="library">Root of the standard library.</param>
74
74
/// <returns>A list of search paths for the interpreter.</returns>
75
75
/// <remarks>New in 2.2, moved in 3.3</remarks>
76
- public static List < PythonLibraryPath > GetDefaultDatabaseSearchPaths ( string library ) {
76
+ public static List < PythonLibraryPath > GetDefaultSearchPaths ( string library ) {
77
77
var result = new List < PythonLibraryPath > ( ) ;
78
78
if ( ! Directory . Exists ( library ) ) {
79
79
return result ;
@@ -95,41 +95,24 @@ public static List<PythonLibraryPath> GetDefaultDatabaseSearchPaths(string libra
95
95
}
96
96
97
97
/// <summary>
98
- /// Gets the set of search paths for the specified factory as
99
- /// efficiently as possible. This may involve executing the
100
- /// interpreter, and may cache the paths for retrieval later.
98
+ /// Gets the set of search paths for the specified factory.
101
99
/// </summary>
102
- public static async Task < IList < PythonLibraryPath > > GetDatabaseSearchPathsAsync ( InterpreterConfiguration config , string cachePath ) {
100
+ public static async Task < IList < PythonLibraryPath > > GetSearchPathsAsync ( InterpreterConfiguration config ) {
103
101
for ( int retries = 5 ; retries > 0 ; -- retries ) {
104
- List < PythonLibraryPath > paths ;
105
- if ( ! string . IsNullOrEmpty ( cachePath ) ) {
106
- paths = GetCachedDatabaseSearchPaths ( cachePath ) ;
107
- if ( paths != null && paths . Count > 2 ) {
108
- return paths ;
109
- }
110
- }
111
-
112
102
try {
113
- paths = await GetUncachedDatabaseSearchPathsAsync ( config . InterpreterPath ) ;
114
- if ( ! string . IsNullOrEmpty ( cachePath ) ) {
115
- WriteDatabaseSearchPaths ( cachePath , paths ) ;
116
- }
117
- return paths ;
103
+ return await GetSearchPathsFromInterpreterAsync ( config . InterpreterPath ) ;
118
104
} catch ( InvalidOperationException ) {
119
105
// Failed to get paths
120
106
break ;
121
- } catch ( UnauthorizedAccessException ) {
122
- // Failed to write paths - sleep and then loop
123
- Thread . Sleep ( 50 ) ;
124
- } catch ( IOException ) {
125
- // Failed to write paths - sleep and then loop
107
+ } catch ( Exception e ) when ( e is IOException || e is UnauthorizedAccessException ) {
108
+ // Failed to get paths due to IO exception - sleep and then loop
126
109
Thread . Sleep ( 50 ) ;
127
110
}
128
111
}
129
112
130
113
var ospy = PathUtils . FindFile ( config . LibraryPath , "os.py" ) ;
131
114
if ( ! string . IsNullOrEmpty ( ospy ) ) {
132
- return GetDefaultDatabaseSearchPaths ( IOPath . GetDirectoryName ( ospy ) ) ;
115
+ return GetDefaultSearchPaths ( IOPath . GetDirectoryName ( ospy ) ) ;
133
116
}
134
117
135
118
return Array . Empty < PythonLibraryPath > ( ) ;
@@ -141,7 +124,7 @@ public static async Task<IList<PythonLibraryPath>> GetDatabaseSearchPathsAsync(I
141
124
/// <param name="interpreter">Path to the interpreter.</param>
142
125
/// <returns>A list of search paths for the interpreter.</returns>
143
126
/// <remarks>Added in 2.2, moved in 3.3</remarks>
144
- public static async Task < List < PythonLibraryPath > > GetUncachedDatabaseSearchPathsAsync ( string interpreter ) {
127
+ public static async Task < List < PythonLibraryPath > > GetSearchPathsFromInterpreterAsync ( string interpreter ) {
145
128
// sys.path will include the working directory, so we make an empty
146
129
// path that we can filter out later
147
130
var tempWorkingDir = IOPath . Combine ( IOPath . GetTempPath ( ) , IOPath . GetRandomFileName ( ) ) ;
@@ -193,53 +176,5 @@ public static async Task<List<PythonLibraryPath>> GetUncachedDatabaseSearchPaths
193
176
}
194
177
} ) . Where ( p => p != null ) . ToList ( ) ;
195
178
}
196
-
197
- /// <summary>
198
- /// Gets the set of search paths that were last saved for a database.
199
- /// </summary>
200
- /// <param name="databasePath">Path containing the database.</param>
201
- /// <returns>The cached list of search paths.</returns>
202
- /// <remarks>Added in 2.2, moved in 3.3</remarks>
203
- public static List < PythonLibraryPath > GetCachedDatabaseSearchPaths ( string cachePath ) {
204
- if ( ! File . Exists ( cachePath ) ) {
205
- return null ;
206
- }
207
-
208
- try {
209
- var result = new List < PythonLibraryPath > ( ) ;
210
- using ( var file = File . OpenText ( cachePath ) ) {
211
- string line ;
212
- while ( ( line = file . ReadLine ( ) ) != null ) {
213
- try {
214
- result . Add ( Parse ( line ) ) ;
215
- } catch ( ArgumentException ) {
216
- Debug . Fail ( "Invalid search path: " + ( line ?? "<null>" ) ) ;
217
- } catch ( FormatException ) {
218
- Debug . Fail ( "Invalid format for search path: " + line ) ;
219
- }
220
- }
221
- }
222
-
223
- return result ;
224
- } catch ( IOException ) {
225
- return null ;
226
- }
227
- }
228
-
229
- /// <summary>
230
- /// Saves search paths for a database.
231
- /// </summary>
232
- /// <param name="databasePath">The path to the database.</param>
233
- /// <param name="paths">The list of search paths.</param>
234
- /// <remarks>Added in 2.2, moved in 3.3</remarks>
235
- public static void WriteDatabaseSearchPaths ( string cachePath , IEnumerable < PythonLibraryPath > paths ) {
236
- Directory . CreateDirectory ( IOPath . GetDirectoryName ( cachePath ) ) ;
237
- using ( var file = new StreamWriter ( cachePath ) ) {
238
- foreach ( var path in paths ) {
239
- file . WriteLine ( path . ToString ( ) ) ;
240
- }
241
- }
242
- }
243
-
244
179
}
245
180
}
0 commit comments