|
| 1 | +// Copyright(c) Microsoft Corporation |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the License); you may not use |
| 5 | +// this file except in compliance with the License. You may obtain a copy of the |
| 6 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 9 | +// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY |
| 10 | +// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 11 | +// MERCHANTABILITY OR NON-INFRINGEMENT. |
| 12 | +// |
| 13 | +// See the Apache Version 2.0 License for specific language governing |
| 14 | +// permissions and limitations under the License. |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Diagnostics; |
| 18 | +using System.IO; |
| 19 | +using System.Security.Cryptography; |
| 20 | +using System.Text; |
| 21 | +using Microsoft.Python.Core; |
| 22 | +using Microsoft.Python.Core.IO; |
| 23 | +using Microsoft.Python.Core.Logging; |
| 24 | +using Microsoft.Python.Core.OS; |
| 25 | + |
| 26 | +namespace Microsoft.Python.Analysis.Caching { |
| 27 | + internal static class CacheFolders { |
| 28 | + public static string GetCacheFilePath(string root, string moduleName, string content, IFileSystem fs) { |
| 29 | + // Folder for all module versions and variants |
| 30 | + // {root}/module_name/content_hash.pyi |
| 31 | + var folder = Path.Combine(root, moduleName); |
| 32 | + |
| 33 | + var filePath = Path.Combine(root, folder, $"{FileNameFromContent(content)}.pyi"); |
| 34 | + if (fs.StringComparison == StringComparison.Ordinal) { |
| 35 | + filePath = filePath.ToLowerInvariant(); |
| 36 | + } |
| 37 | + |
| 38 | + return filePath; |
| 39 | + } |
| 40 | + |
| 41 | + public static string GetCacheFolder(IServiceContainer services) { |
| 42 | + var platform = services.GetService<IOSPlatform>(); |
| 43 | + var logger = services.GetService<ILogger>(); |
| 44 | + |
| 45 | + // Default. Not ideal on all platforms, but used as a fall back. |
| 46 | + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 47 | + var plsSubfolder = $"Microsoft{Path.DirectorySeparatorChar}Python Language Server"; |
| 48 | + var defaultCachePath = Path.Combine(localAppData, plsSubfolder); |
| 49 | + |
| 50 | + string cachePath = null; |
| 51 | + try { |
| 52 | + const string homeVarName = "HOME"; |
| 53 | + var homeFolderPath = Environment.GetEnvironmentVariable(homeVarName); |
| 54 | + |
| 55 | + if(platform.IsWindows) { |
| 56 | + cachePath = defaultCachePath; |
| 57 | + } |
| 58 | + |
| 59 | + if (platform.IsMac) { |
| 60 | + if (CheckVariableSet(homeVarName, homeFolderPath, logger) |
| 61 | + && CheckPathRooted(homeVarName, homeFolderPath, logger) |
| 62 | + && !string.IsNullOrWhiteSpace(homeFolderPath)) { |
| 63 | + cachePath = Path.Combine(homeFolderPath, "Library/Caches", plsSubfolder); |
| 64 | + } else { |
| 65 | + logger?.Log(TraceEventType.Warning, Resources.EnvVariablePathNotRooted.FormatInvariant(homeVarName)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (platform.IsLinux) { |
| 70 | + const string xdgCacheVarName = "XDG_CACHE_HOME"; |
| 71 | + var xdgCacheHomePath = Environment.GetEnvironmentVariable(xdgCacheVarName); |
| 72 | + |
| 73 | + if (!string.IsNullOrWhiteSpace(xdgCacheHomePath) |
| 74 | + && CheckPathRooted(xdgCacheVarName, xdgCacheHomePath, logger)) { |
| 75 | + cachePath = Path.Combine(xdgCacheVarName, plsSubfolder); |
| 76 | + } else if (!string.IsNullOrWhiteSpace(homeFolderPath) |
| 77 | + && CheckVariableSet(homeVarName, homeFolderPath, logger) |
| 78 | + && CheckPathRooted(homeVarName, homeFolderPath, logger)) { |
| 79 | + cachePath = Path.Combine(homeFolderPath, ".cache", plsSubfolder); |
| 80 | + } else { |
| 81 | + logger?.Log(TraceEventType.Warning, Resources.EnvVariablePathNotRooted.FormatInvariant(homeVarName)); |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (Exception ex) when (!ex.IsCriticalException()) { |
| 85 | + logger?.Log(TraceEventType.Warning, Resources.UnableToDetermineCachePathException.FormatInvariant(ex.Message, defaultCachePath)); |
| 86 | + } |
| 87 | + |
| 88 | + // Default is same as Windows. Not ideal on all platforms, but it is a fallback anyway. |
| 89 | + if (cachePath == null) { |
| 90 | + logger?.Log(TraceEventType.Warning, Resources.UnableToDetermineCachePath.FormatInvariant(defaultCachePath)); |
| 91 | + cachePath = defaultCachePath; |
| 92 | + } |
| 93 | + |
| 94 | + logger?.Log(TraceEventType.Information, Resources.AnalysisCachePath.FormatInvariant(cachePath)); |
| 95 | + return cachePath; |
| 96 | + } |
| 97 | + |
| 98 | + public static string FileNameFromContent(string content) { |
| 99 | + // File name depends on the content so we can distinguish between different versions. |
| 100 | + var hash = SHA256.Create(); |
| 101 | + return Convert |
| 102 | + .ToBase64String(hash.ComputeHash(new UTF8Encoding(false).GetBytes(content))) |
| 103 | + .Replace('/', '_').Replace('+', '-'); |
| 104 | + } |
| 105 | + |
| 106 | + public static string GetAnalysisCacheFilePath(string analysisRootFolder, string moduleName, string content, IFileSystem fs) |
| 107 | + => GetCacheFilePath(analysisRootFolder, moduleName, content, fs); |
| 108 | + |
| 109 | + private static bool CheckPathRooted(string varName, string path, ILogger logger) { |
| 110 | + if (!string.IsNullOrWhiteSpace(path) && Path.IsPathRooted(path)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + logger?.Log(TraceEventType.Warning, Resources.EnvVariablePathNotRooted.FormatInvariant(varName)); |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + private static bool CheckVariableSet(string varName, string value, ILogger logger) { |
| 119 | + if (!string.IsNullOrWhiteSpace(value)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + logger?.Log(TraceEventType.Warning, Resources.EnvVariableNotSet.FormatInvariant(varName)); |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments