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
//            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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
 
namespace OpenTap.Package
{
    /// <summary> Type of dependency issue. </summary>
    public enum DependencyIssueType
    {
        /// <summary> No issue noted (Usually notes an installed package). </summary>
        None,
        /// <summary> The dependency is missing. </summary>
        Missing,
        /// <summary> Incompatible version installed. </summary>
        [Display("Incompatible Version")]
        IncompatibleVersion,
        /// <summary> One of the dependencies has a issue.</summary>
        [Display("Dependency Missing")]
        DependencyMissing
    }
 
    /// <summary>
    /// Model object for a dependency issue.
    /// </summary>
    public struct DependencyIssue
    {
        /// <summary>
        /// Issue package, eg the missing package.
        /// </summary>
        public string PackageName { get; set; }
        /// <summary>
        /// Expected version.
        /// </summary>
        public VersionSpecifier ExpectedVersion { get; set; }
 
        /// <summary>
        /// The version available.
        /// </summary>
        public SemanticVersion LoadedVersion { get; set; }
        
        /// <summary> Denotes which kind of dependency issue it is. </summary>
        public DependencyIssueType IssueType { get; set; }
    }
 
    /// <summary>
    /// Algorithm for calculate nth degree dependency issues.
    /// </summary>
    public class DependencyAnalyzer
    {
        /// <summary>
        /// Broken packages from the packages used to build the object.
        /// </summary>
        public ReadOnlyCollection<PackageDef> BrokenPackages { get; set; }
        Dictionary<string, PackageDef> packagesLookup;
        Dictionary<PackageDef, List<PackageDef>> dependers;
        private DependencyAnalyzer() { }
 
        /// <summary>
        /// Returns the issues for a given package.
        /// </summary>
        /// <param name="pkg"></param>
        /// <returns></returns>
        public List<DependencyIssue> GetIssues(PackageDef pkg)
        {
            List<DependencyIssue> brokenDependencies = new List<DependencyIssue>();
            foreach (var dep in pkg.Dependencies)
            {
                if (packagesLookup.ContainsKey(dep.Name) == false)
                {
                    brokenDependencies.Add(new DependencyIssue() { PackageName = dep.Name, ExpectedVersion = dep.Version, IssueType = DependencyIssueType.Missing });
                    continue;
                }
                
                var installed = packagesLookup[dep.Name];
                var iv = installed.Version;
                var depbase = new DependencyIssue { PackageName = dep.Name, ExpectedVersion = dep.Version, LoadedVersion = iv };
                if (installed.Version == null)
                { // missing
                    depbase.IssueType = DependencyIssueType.Missing;
                }
                else if (!dep.Version.IsCompatible(iv))
                { // incompatible
                    depbase.IssueType = DependencyIssueType.IncompatibleVersion;
                }
                else if (BrokenPackages.Any(pkg2 => pkg2.Name == dep.Name))
                { // broken dependency
                    depbase.IssueType = DependencyIssueType.DependencyMissing;
                }
                else continue;
                brokenDependencies.Add(depbase);
            }
            return brokenDependencies;
        }
        /// <summary> Creates a new dependency analyzer that only shows things related to important_packages. </summary>
        /// <param name="important_packages"></param>
        /// <returns></returns>
        public DependencyAnalyzer FilterRelated(List<PackageDef> important_packages)
        {
            HashSet<string> visited = new HashSet<string>();
            Stack<string> stack = new Stack<string>(important_packages.Select(pkg => pkg.Name));
            // moving down.
            while (stack.Count > 0)
            {
                var top = stack.Pop();
                visited.Add(top);
                var pkg = packagesLookup[top];
                foreach (var dep in pkg.Dependencies)
                {
                    if (visited.Contains(dep.Name) == false)
                    {
                        visited.Add(dep.Name);
                        stack.Push(dep.Name);
                    }
                }
            }
 
            // moving up
            HashSet<string> visited2 = new HashSet<string>();
            Stack<string> stack2 = new Stack<string>(important_packages.Select(pkg => pkg.Name));
            // moving down.
            while (stack2.Count > 0)
            {
                var top = stack2.Pop();
                visited2.Add(top);
                var pkg = packagesLookup[top];
                foreach (var dep in dependers[pkg])
                {
                    if (visited2.Contains(dep.Name) == false)
                    {
                        visited2.Add(dep.Name);
                        stack2.Push(dep.Name);
                    }
                }
            }
 
            visited = new HashSet<string>(visited.Concat(visited2));
 
 
            return new DependencyAnalyzer
            {
                BrokenPackages = BrokenPackages.Where(pkg => visited.Contains(pkg.Name)).ToList().AsReadOnly(),
                packagesLookup = packagesLookup,
                dependers = dependers
            };
        }
 
        /// <summary>
        ///  Builds a DependencyTree based on the dependencies between the packages.
        /// </summary>
        /// <param name="packages"></param>
        /// <returns></returns>
        public static DependencyAnalyzer BuildAnalyzerContext(List<PackageDef> packages)
        {
            Dictionary<string, PackageDef> packagesLookup = packages.GroupBy(p => p.Name).Select(p => p.First()).ToDictionary(pkg => pkg.Name);
            Dictionary<PackageDef, List<PackageDef>> dependers = packages.ToDictionary(pkg => pkg, pkg => new List<PackageDef>());
            HashSet<PackageDef> broken_packages = new HashSet<PackageDef>();
            foreach (var package in packages)
            {
                foreach (var dep in package.Dependencies)
                {
                    if (false == packagesLookup.ContainsKey(dep.Name))
                    { // missing: create placeholder. null means missing.
                        packagesLookup[dep.Name] = new PackageDef { Dependencies = new List<PackageDependency>(), Name = dep.Name, Version = null, Files = new List<PackageFile>() };
                        dependers[packagesLookup[dep.Name]] = new List<PackageDef>();
                    }
 
                    var loadedpackage = packagesLookup[dep.Name];
                    if (false == dep.Version.IsCompatible(loadedpackage.Version) || null == loadedpackage.Version)
                    {
                        broken_packages.Add(package);
                    }
 
                    dependers[packagesLookup[dep.Name]].Add(package);
                }
            }
 
            // find nth order broken packages
            // if a dependency is broken, the depender is also broken.
            Stack<PackageDef> newBroken = new Stack<PackageDef>(broken_packages);
            while (newBroken.Count > 0)
            {
                var item = newBroken.Pop();
                var deps = dependers[item];
                foreach (var dep in deps)
                {
                    if (!broken_packages.Contains(dep))
                    {
                        broken_packages.Add(dep);
                        newBroken.Push(dep);
                    }
                }
            }
 
            DependencyAnalyzer deptree = new DependencyAnalyzer();
            deptree.BrokenPackages = broken_packages.ToList().AsReadOnly();
            deptree.packagesLookup = packagesLookup;
            deptree.dependers = dependers;
            return deptree;
        }
    }
}