-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathModuleKind.qll
More file actions
45 lines (41 loc) · 1.11 KB
/
ModuleKind.qll
File metadata and controls
45 lines (41 loc) · 1.11 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
import python
private import semmle.python.types.ModuleObject
private predicate is_normal_module(ModuleObject m) {
m instanceof BuiltinModuleObject
or
m instanceof PackageObject
or
exists(ImportingStmt i | m.importedAs(i.getAnImportedModuleName()))
or
m.getName().matches("%\\_\\_init\\_\\_")
}
private predicate is_script(ModuleObject m) {
not is_normal_module(m) and
(
m.getModule().getFile().getExtension() != ".py"
or
exists(If i, Name name, StringLiteral main, Cmpop op |
i.getScope() = m.getModule() and
op instanceof Eq and
i.getTest().(Compare).compares(name, op, main) and
name.getId() = "__name__" and
main.getText() = "__main__"
)
)
}
private predicate is_plugin(ModuleObject m) {
// This needs refining but is sufficient for our present needs.
not is_normal_module(m) and
not is_script(m)
}
/**
* Gets the kind for module `m` will be one of
* "module", "script" or "plugin"
*/
string getKindForModule(ModuleObject m) {
is_normal_module(m) and result = "module"
or
is_script(m) and result = "script"
or
is_plugin(m) and result = "plugin"
}