Repository navigation
Expand file tree
/
Copy pathPluginSearchPath.cs
More file actions
79 lines (68 loc) · 2.67 KB
/
Copy pathPluginSearchPath.cs
File metadata and controls
79 lines (68 loc) · 2.67 KB
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
using System;
using System.Collections.Generic;
using System.IO;
namespace OpenTap.Python;
public class PluginSearchPath : ValidatingObject
{
const int MAX_ENTRY_COUNT = 100;
string searchPath;
[Display("Search Path", "A search path for finding the Python based plugin modules.", Order:1)]
[DirectoryPath]
public string SearchPath
{
get => searchPath;
set => searchPath = Path.GetFullPath(value);
}
[Display("Enabled", "Enable or disable the search path.", Order:0)]
public bool Enabled { get; set; } = true;
public PluginSearchPath()
{
Rules.Add(() =>
{
if(!string.IsNullOrEmpty(SearchPath))
return Directory.Exists(Path.GetFullPath(SearchPath));
return false;
}, "This search path does not exist.", nameof(SearchPath));
Rules.Add(CheckDirectorySizeConstraint, "The directory or its sub-directory(s) could not be accessed or they contain more than 100 files. (Maximum file count: 100)", nameof(SearchPath));
}
internal bool CheckDirectorySizeConstraint()
{
// check the existence of the search path first
if (!string.IsNullOrWhiteSpace(SearchPath) && Directory.Exists(Path.GetFullPath(SearchPath)))
{
try
{
var startingDir = new DirectoryInfo(SearchPath);
Queue<DirectoryInfo> dirQueue = new Queue<DirectoryInfo>();
dirQueue.Enqueue(startingDir);
int totalFileSystemEntryCount = 0;
while (dirQueue.Count != 0)
{
DirectoryInfo currentDir = dirQueue.Dequeue();
if (currentDir.Name == "bin" || currentDir.Name == "obj" || File.Exists(Path.Combine(currentDir.FullName, "__init__.py")))
continue;
// count this dir
totalFileSystemEntryCount++;
// count the files inside the dir
totalFileSystemEntryCount += currentDir.GetFiles().Length;
if (totalFileSystemEntryCount > MAX_ENTRY_COUNT)
return false;
else
{
foreach (DirectoryInfo subDir in currentDir.GetDirectories())
{
dirQueue.Enqueue(subDir);
}
}
}
}
catch (Exception e)
{
log.Error(e.Message);
return false;
}
}
return true;
}
static readonly TraceSource log = Log.CreateSource("Python");
}