summaryrefslogtreecommitdiff
path: root/server/problems.py
blob: 7fb2606e64d4b434c15f2e8f8cc22901b215d33a (plain)
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
80
81
82
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)