summaryrefslogtreecommitdiff
path: root/server/problems.py
diff options
context:
space:
mode:
Diffstat (limited to 'server/problems.py')
-rw-r--r--server/problems.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/server/problems.py b/server/problems.py
new file mode 100644
index 0000000..7fb2606
--- /dev/null
+++ b/server/problems.py
@@ -0,0 +1,83 @@
+# coding=utf-8
+
+import sys
+import importlib.machinery
+import threading
+
+#sys.path.append('/home/aless/job/codeq/source/codeq-problems/')
+_path_prefix = '/home/aless/job/codeq/source/codeq-problems/'
+_module_loading_lock = threading.RLock() # TODO: make a more fine-grained locking import machinery
+
+def load_module(fullname):
+# return importlib.import_module(fullname)
+ with _module_loading_lock:
+ mod = sys.modules.get(fullname, None)
+ if mod is None:
+ parts = fullname.split('.')
+ d = _path_prefix + '/'.join(parts[0:-1])
+ ff = importlib.machinery.FileFinder(d, (importlib.machinery.SourceFileLoader, ['.py']))
+ spec = ff.find_spec(fullname)
+ mod = type(sys)(fullname)
+ mod.__loader__ = spec.loader
+ mod.__package__ = spec.parent
+ mod.__spec__ = spec
+ if spec.has_location:
+ mod.__file__ = spec.origin
+ mod.__cached__ = spec.cached
+ sys.modules[fullname] = mod
+ try:
+ spec.loader.exec_module(mod)
+ except:
+ try:
+ del sys.modules[fullname]
+ except KeyError:
+ pass
+ raise
+ return mod
+
+def load_problem(language, problem_group, problem, tail_module):
+ return load_module('{0}.problems.{1}.{2}.{3}'.format(language, problem_group, problem, tail_module))
+
+def load_facts(language, fact_module):
+ return load_module('{0}.facts.{1}'.format(language, fact_module))
+
+def load_problems(language, tuples, tail_module):
+ modules = []
+ for problem_group, problem in tuples:
+ mod = '{0}.problems.{1}.{2}.{3}'.format(language, problem_group, problem, tail_module)
+ print('importing {}'.format(mod))
+ modules.append(load_module(mod))
+ return modules
+
+def get_facts(language, problem_module):
+ try:
+ facts = problem_module.facts
+ except AttributeError as e:
+ return None
+ if facts is None:
+ return None
+ module = load_facts(language, facts)
+ if module:
+ try:
+ return module.facts
+ except AttributeError as e:
+ return None
+ return None
+
+def solutions_for_problems(language, tuples):
+ if not tuples:
+ return ''
+ modules = load_problems(language, tuples, 'common')
+ solutions = set()
+ facts = set()
+ for module in modules:
+ try:
+ solution = module.solution
+ except AttributeError as me:
+ pass
+ else:
+ solutions.add(solution)
+ f = get_facts(language, module)
+ if f:
+ facts.add(f)
+ return '\n'.join(facts) + '\n' + '\n'.join(solutions)