-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgii_module_local.py
59 lines (47 loc) · 1.54 KB
/
cgii_module_local.py
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
# sys_shelve_importer.py
import imp
import os
import shelve
import sys
import requests
session = requests.Session()
session.auth = ("admin", "12345")
class LocalDirFinder:
def __init__(self, url, subdir):
self.url = '{}/{}'.format(url, subdir)
def find_module(self, fullname, path=None):
if fullname.split('.')[0] != "testproject":
return None
print("Assuming project path is right")
return LocalDirLoader(self.url, fullname.split('.')[1:])
class LocalDirLoader:
"""Load source for modules from shelve databases."""
def __init__(self, url, path):
self.path = "/".join(path)
self.url = url + '/' + self.path + '.py'
def get_source_for_path(self, path):
r = session.get(self.url)
if r.status_code != 200:
# ideally should treat this as module with __init__.py
return ""
else:
return r.text
def load_module(self, fullname):
source = self.get_source_for_path(fullname)
if fullname in sys.modules:
mod = sys.modules[fullname]
else:
mod = sys.modules.setdefault(
fullname,
imp.new_module(fullname)
)
# Set a few properties required by PEP 302
mod.__file__ = 'testproject'
mod.__name__ = fullname
mod.__path__ = '/dummy/path'
mod.__loader__ = self
mod.__package__ = fullname
print('execing source...')
exec(source, mod.__dict__)
print('done')
return mod