Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 9116b93

Browse files
authored
Remove path cache file (#653)
Closes #652.
1 parent 18a27b3 commit 9116b93

File tree

4 files changed

+10
-79
lines changed

4 files changed

+10
-79
lines changed

src/Analysis/Ast/Impl/Modules/Definitions/IModuleCache.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace Microsoft.Python.Analysis.Modules {
2121
public interface IModuleCache {
22-
string SearchPathCachePath { get; }
2322
Task<IDocument> ImportFromCacheAsync(string name, CancellationToken cancellationToken);
2423
string GetCacheFilePath(string filePath);
2524
string ReadCachedModule(string filePath);

src/Analysis/Ast/Impl/Modules/ModuleCache.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ public ModuleCache(IPythonInterpreter interpreter, IServiceContainer services) {
4242
_fs = services.GetService<IFileSystem>();
4343
_log = services.GetService<ILogger>();
4444
_skipCache = string.IsNullOrEmpty(_interpreter.Configuration.DatabasePath);
45-
SearchPathCachePath = Path.Combine(_interpreter.Configuration.DatabasePath, $"database{_interpreter.Configuration.Version}.path");
4645
}
4746

48-
public string SearchPathCachePath { get; }
49-
5047
public async Task<IDocument> ImportFromCacheAsync(string name, CancellationToken cancellationToken) {
5148
if (string.IsNullOrEmpty(ModuleCachePath)) {
5249
return null;

src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ private async Task<IReadOnlyList<string>> GetInterpreterSearchPathsAsync(Cancell
127127
return Array.Empty<string>();
128128
}
129129

130-
_log?.Log(TraceEventType.Information, "GetCurrentSearchPaths", Configuration.InterpreterPath, ModuleCache.SearchPathCachePath);
130+
_log?.Log(TraceEventType.Information, "GetCurrentSearchPaths", Configuration.InterpreterPath);
131131
try {
132-
var paths = await PythonLibraryPath.GetDatabaseSearchPathsAsync(Configuration, ModuleCache.SearchPathCachePath);
132+
var paths = await PythonLibraryPath.GetSearchPathsAsync(Configuration);
133133
cancellationToken.ThrowIfCancellationRequested();
134134
return paths.MaybeEnumerate().Select(p => p.Path).ToArray();
135135
} catch (InvalidOperationException) {

src/Analysis/Core/Impl/Interpreter/PythonLibraryPath.cs

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static PythonLibraryPath Parse(string s) {
7373
/// <param name="library">Root of the standard library.</param>
7474
/// <returns>A list of search paths for the interpreter.</returns>
7575
/// <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) {
7777
var result = new List<PythonLibraryPath>();
7878
if (!Directory.Exists(library)) {
7979
return result;
@@ -95,41 +95,24 @@ public static List<PythonLibraryPath> GetDefaultDatabaseSearchPaths(string libra
9595
}
9696

9797
/// <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.
10199
/// </summary>
102-
public static async Task<IList<PythonLibraryPath>> GetDatabaseSearchPathsAsync(InterpreterConfiguration config, string cachePath) {
100+
public static async Task<IList<PythonLibraryPath>> GetSearchPathsAsync(InterpreterConfiguration config) {
103101
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-
112102
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);
118104
} catch (InvalidOperationException) {
119105
// Failed to get paths
120106
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
126109
Thread.Sleep(50);
127110
}
128111
}
129112

130113
var ospy = PathUtils.FindFile(config.LibraryPath, "os.py");
131114
if (!string.IsNullOrEmpty(ospy)) {
132-
return GetDefaultDatabaseSearchPaths(IOPath.GetDirectoryName(ospy));
115+
return GetDefaultSearchPaths(IOPath.GetDirectoryName(ospy));
133116
}
134117

135118
return Array.Empty<PythonLibraryPath>();
@@ -141,7 +124,7 @@ public static async Task<IList<PythonLibraryPath>> GetDatabaseSearchPathsAsync(I
141124
/// <param name="interpreter">Path to the interpreter.</param>
142125
/// <returns>A list of search paths for the interpreter.</returns>
143126
/// <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) {
145128
// sys.path will include the working directory, so we make an empty
146129
// path that we can filter out later
147130
var tempWorkingDir = IOPath.Combine(IOPath.GetTempPath(), IOPath.GetRandomFileName());
@@ -193,53 +176,5 @@ public static async Task<List<PythonLibraryPath>> GetUncachedDatabaseSearchPaths
193176
}
194177
}).Where(p => p != null).ToList();
195178
}
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-
244179
}
245180
}

0 commit comments

Comments
 (0)