1
+ import sys
2
+ import pip
3
+
4
+
5
+ class IntermediateModule :
6
+ """Module for paths like `github_com.nvbn`."""
7
+
8
+ def __init__ (self , fullname ):
9
+ self .__package__ = fullname
10
+ self .__path__ = fullname .split ('.' )
11
+ self .__name__ = fullname
12
+
13
+
14
+ class GithubComFinder :
15
+ """Handles `github_com....` modules."""
16
+
17
+ def find_module (self , module_name , package_path ):
18
+ if module_name .startswith ('github_com' ):
19
+ return GithubComLoader ()
20
+
21
+
22
+ class GithubComLoader :
23
+ """Installs and imports modules from github."""
24
+
25
+ def _is_installed (self , fullname ):
26
+ try :
27
+ self ._import_module (fullname )
28
+ return True
29
+ except ImportError :
30
+ return False
31
+
32
+ def _import_module (self , fullname ):
33
+ actual_name = '.' .join (fullname .split ('.' )[2 :])
34
+ return __import__ (actual_name )
35
+
36
+ def _install_module (self , fullname ):
37
+ if not self ._is_installed (fullname ):
38
+ url = fullname .replace ('.' , '/' ) \
39
+ .replace ('github_com' , 'git+https://github.com' , 1 )
40
+ pip .main (['install' , url ])
41
+
42
+ def _is_repository_path (self , fullname ):
43
+ return fullname .count ('.' ) == 2
44
+
45
+ def _is_intermediate_path (self , fullname ):
46
+ return fullname .count ('.' ) < 2
47
+
48
+ def load_module (self , fullname ):
49
+ if self ._is_repository_path (fullname ):
50
+ self ._install_module (fullname )
51
+
52
+ if self ._is_intermediate_path (fullname ):
53
+ module = IntermediateModule (fullname )
54
+ else :
55
+ module = self ._import_module (fullname )
56
+
57
+ sys .modules [fullname ] = module
58
+
59
+
60
+ sys .meta_path .append (GithubComFinder ())
0 commit comments