chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OpenTap.Authentication;
 
namespace OpenTap.Package
{
    /// <summary>
    /// A client interface for a package repository. Implementations include <see cref="FilePackageRepository"/> and <see cref="HttpPackageRepository"/>.
    /// </summary>
    public interface IPackageRepository
    {
        /// <summary>
        /// The url of the repository.
        /// </summary>
        string Url { get; }
 
        /// <summary>
        /// Downloads a package from this repository to a file.
        /// </summary>
        /// <param name="package">The package to download.</param>
        /// <param name="destination">The destination path where the package should be stored.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the download.</param>
        void DownloadPackage(IPackageIdentifier package, string destination, CancellationToken cancellationToken);
 
        /// <summary>
        /// Get all names of packages.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the download.</param>
        /// <param name="compatibleWith">Any packages that the package to download must be compatible with.</param>
        /// <returns>An array of package names.</returns>
        string[] GetPackageNames(CancellationToken cancellationToken, params IPackageIdentifier[] compatibleWith);
 
        /// <summary>
        /// Returns all package version information about a package.
        /// </summary>
        /// <param name="packageName">The name package to retrieve version info about.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the download.</param>
        /// <param name="compatibleWith">Any packages that the package to download must be compatible with.</param>
        /// <returns>An array of package versions.</returns>
        PackageVersion[] GetPackageVersions(string packageName, CancellationToken cancellationToken, params IPackageIdentifier[] compatibleWith);
 
        /// <summary>
        /// This returns the latest version of a package that matches a number of specified parameters.
        /// If multiple packages have that same version number they all will be returned.
        /// </summary>
        /// <param name="package">A package identifier. If not specified, packages with any name will be returned.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the download.</param>
        /// <param name="compatibleWith">Any packages that the package to download must be compatible with.</param>
        /// <returns>An array of package definitions <see cref="PackageDef"/>.</returns>
        PackageDef[] GetPackages(PackageSpecifier package, CancellationToken cancellationToken, params IPackageIdentifier[] compatibleWith);
 
        /// <summary>
        /// Returns a list of all packages that have an updated version.
        /// </summary>
        /// <param name="packages"></param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the download.</param>
        /// <returns>An array of package definitions <see cref="PackageDef"/>.</returns>
        PackageDef[] CheckForUpdates(IPackageIdentifier[] packages, CancellationToken cancellationToken);
    }
 
    /// <summary>
    /// Represents a version of a package. Objects of this type is returned by<see cref="IPackageRepository.GetPackageVersions"/>.
    /// </summary>
    [DebuggerDisplay("{Name} : {Version.ToString()}")]
    public class PackageVersion : PackageIdentifier, IEquatable<PackageVersion>
    {
        internal bool IsUnlisted { get; private set; }
        internal static PackageVersion FromDictionary(Dictionary<string, object> dict)
        {
            var name = dict["Name"] as string;
            Enum.TryParse(dict["Architecture"] as string, out CpuArchitecture arch);
            var version = SemanticVersion.Parse(dict["Version"] as string);
            var os = dict["OS"] as string;
            var date = (DateTime)dict["Date"];
            var licenses = new List<string>();
            if (dict.TryGetValue("LicenseRequired", out var licenseRequired))
            {
                if (licenseRequired is string license)
                    licenses.Add(license);
                else if (licenseRequired is string[] licenseArray)
                    licenses.AddRange(licenseArray);
            }
 
            return new PackageVersion(name, version, os, arch, date, licenses)
            {
                IsUnlisted = dict.TryGetValue("IsUnlisted", out var unlisted) && unlisted is bool b && b
            };
        }
        
        /// <summary>
        /// Initializes a new instance of a PackageVersion.
        /// </summary>
        public PackageVersion(string name, SemanticVersion version, string os, CpuArchitecture architechture, DateTime date, List<string> licenses) : base(name, version, architechture, os)
        {
            this.Date = date;
            this.Licenses = licenses;
        }
 
        internal PackageVersion()
        {
 
        }
 
        /// <summary>
        /// The date that the package was build.
        /// </summary>
        public DateTime Date { get; set; }
 
        /// <summary>
        /// License(s) required to use this package.
        /// </summary>
        public List<string> Licenses { get; set; }
 
        /// <summary>
        /// Compares this PackageVersion with another.
        /// </summary>
        public bool Equals(PackageVersion other)
        {
            return (this as PackageIdentifier).Equals(other);
        }
    }
 
    internal static class PackageRepositoryExtension
    {
        public static void DownloadPackage(this IPackageRepository repository, IPackageIdentifier package, string destination)
        {
            repository.DownloadPackage(package, destination, TapThread.Current.AbortToken);
        }
 
        public static string[] GetPackageNames(this IPackageRepository repository, params IPackageIdentifier[] compatibleWith)
        {
            return repository.GetPackageNames(TapThread.Current.AbortToken, compatibleWith);
        }
 
        public static PackageVersion[] GetPackageVersions(this IPackageRepository repository, string packageName, params IPackageIdentifier[] compatibleWith)
        {
            return repository.GetPackageVersions(packageName, TapThread.Current.AbortToken, compatibleWith);
        }
 
        public static PackageDef[] GetPackages(this IPackageRepository repository, PackageSpecifier package, params IPackageIdentifier[] compatibleWith)
        {
            return repository.GetPackages(package, TapThread.Current.AbortToken, compatibleWith);
        }
 
        public static PackageDef[] CheckForUpdates(this IPackageRepository repository, IPackageIdentifier[] packages)
        {
            return repository.CheckForUpdates(packages, TapThread.Current.AbortToken);
        }
    }
 
    internal class PackageRepositoryHelpers
    {
        private static TraceSource log = Log.CreateSource("PackageRepository");
 
        static void ParallelTryForEach<TSource>(IEnumerable<TSource> source, Action<TSource> body)
        {
            try
            {
                Parallel.ForEach(source, body);
            }
            catch (AggregateException ex)
            {
                foreach (var inner in ex.InnerExceptions)
                {
                    log.Info(inner.Message);
                    log.Debug(inner);
                }
            }
        }
 
        internal static List<PackageDef> GetPackageNameAndVersionFromAllRepos(List<IPackageRepository> repositories,
            PackageSpecifier id, params IPackageIdentifier[] compatibleWith)
        {
            var list = new List<PackageDef>();
 
            ParallelTryForEach(repositories, repo =>
            {
                if (repo is HttpPackageRepository httprepo)
                {
                    var parameters = HttpPackageRepository.GetQueryParameters(version: id.Version, os: id.OS,
                        architecture: id.Architecture, distinctName: true);
                    
                    var repoClient = HttpPackageRepository.GetAuthenticatedClient(new Uri(httprepo.Url, UriKind.Absolute));
                    var result = repoClient.Query(parameters, CancellationToken.None, "name", "version");
 
                    var packages = result.Select(p => new PackageDef()
                    {
                        Name = p["name"] as string,
                        Version = SemanticVersion.Parse(p["version"] as string),
                        PackageSource = new HttpRepositoryPackageDefSource() { RepositoryUrl = httprepo.Url }
                    });
 
                    lock (list)
                    {
                        list.AddRange(packages);
                    }
                }
                else
                {
                    var packages = repo.GetPackages(id, compatibleWith);
                    lock (list)
                    {
                        list.AddRange(packages);
                    }
                }
            });
            return list;
        }
 
        internal static List<PackageDef> GetPackagesFromAllRepos(List<IPackageRepository> repositories,
            PackageSpecifier id, params IPackageIdentifier[] compatibleWith)
        {
            var list = new List<PackageDef>();
 
            ParallelTryForEach(repositories, repo =>
            {
                var packages = repo.GetPackages(id, compatibleWith);
                lock (list)
                {
                    list.AddRange(packages);
                }
            });
 
            return list;
        }
 
        internal static List<PackageVersion> GetAllVersionsFromAllRepos(List<IPackageRepository> repositories,
            string packageName, params IPackageIdentifier[] compatibleWith)
        {
            var list = new List<PackageVersion>();
            ParallelTryForEach(repositories, repo =>
            {
                var packages = repo.GetPackageVersions(packageName, compatibleWith);
                lock (list)
                {
                    list.AddRange(packages);
                }
            });
            return list;
        }
 
        /// <summary>
        /// Returns FilePackageRepository if either of the following is true:
        /// - Url is explicitly defined with file:/// 
        /// - The url is relative and directory exists
        /// - Starts with classic windows absolute path like 'C:/'
        /// - Starts with '\\'
        /// Otherwise returns HttpPackageRepository
        /// </summary>
        /// <param name="url">url to be determined to be file path or http path</param>
        /// <returns>Determined repository type</returns>
        internal static IPackageRepository DetermineRepositoryType(string url)
        {
            if (registeredRepositories.TryGetValue(url, out var repo))
                return repo;
            if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri uri))
            {
                if(uri.IsAbsoluteUri)
                {
                    switch(uri.Scheme)
                    {
                        case "http":
                        case "https":
                            return new HttpPackageRepository(url);
                        case "file":
                            return new FilePackageRepository(url);
                        default:
                            throw new NotSupportedException($"Scheme {uri.Scheme} is not supported as a package repository ({url}).");
                    }
                }
 
                if (!Directory.Exists(url))
                {
                    try
                    {
                        var repo2 = new HttpPackageRepository(url);
                        if (repo2.Version != null)
                            return repo2;
                    }
                    catch
                    {
                        // probably not an http repo
                    }
                }
 
                // If the url starts with a slash, and we are not on windows, it could be an absolute path
                if (OperatingSystem.Current != OperatingSystem.Windows && url.StartsWith("/") && Directory.Exists(url))
                {
                    return new FilePackageRepository(url);
                }
 
                if (!string.IsNullOrWhiteSpace(AuthenticationSettings.Current.BaseAddress))
                    return DetermineRepositoryType(new Uri(new Uri(AuthenticationSettings.Current.BaseAddress), url).AbsoluteUri);
                    
                // This is a relative URI, and it's scheme cannot be determined. The best we can do is guess.
                // If the path contains any invalid path chars, it cannot be a file repository
                // Trailing slashes don't matter. Simplify the logic by stripping them
                url = url.TrimEnd('/');
                var canBeFile = Path.GetInvalidPathChars().Any(url.Contains) == false;
                if (canBeFile)
                {
                    try
                    {
                        // GetFullPath detects issues not detected by GetInvalidPathChars
                        _ = Path.GetFullPath(url);
                    }
                    catch
                    {
                        canBeFile = false;
                    }
                }
 
                var canBeUrl = Uri.IsWellFormedUriString("https://" + url, UriKind.Absolute);
                if (canBeFile && canBeUrl)
                {
                    // The URI is ambiguous. Assume it is a http repository if it ends with something that looks like a top-level domain
                    var segments = url.Split('.');
                    var topLevel = segments.LastOrDefault();
                    if (segments.Count() > 1 && !string.IsNullOrWhiteSpace(topLevel))
                    {
                        canBeFile = false;
                    }
                }
                
                if (canBeFile) return new FilePackageRepository(Path.GetFullPath(url));
                if (canBeUrl) return new HttpPackageRepository("http://" + url);
            }
 
            throw new NotSupportedException($"Unable to determine repository type of '{url}'. Try specifying a scheme using 'http://' or 'file:///'.");
        }
 
        static Dictionary<string, IPackageRepository> registeredRepositories = new Dictionary<string, IPackageRepository>();
 
        internal static void RegisterRepository(IPackageRepository repo)
        {
            registeredRepositories[repo.Url] = repo;
        }
    }
}