'Extract content of <script>...</script> from the problem.xml file, and exec it in the
context of this problem.  Provides ability to randomize problems, and also set
variables for problem answer checking.
Problem XML goes to Python execution context. Runs everything in script tags.'

def _extract_context(self, tree):

    context = {}
    context['seed'] = self.seed
    context['anonymous_student_id'] = self.capa_system.anonymous_student_id
    all_code = ''
    python_path = []
    for script in tree.findall('.//script'):
        stype = script.get('type')
        if stype:
            if ('javascript' in stype):
                continue
            if ('perl' in stype):
                continue
        for d in self._extract_system_path(script):
            if ((d not in python_path) and os.path.exists(d)):
                python_path.append(d)
        XMLESC = {'&apos;': "'", '&quot;': '"'}
        code = unescape(script.text, XMLESC)
        all_code += code
    extra_files = []
    if all_code:
        zip_lib = self.capa_system.get_python_lib_zip()
        if (zip_lib is not None):
            extra_files.append(('python_lib.zip', zip_lib))
            python_path.append('python_lib.zip')
        try:
            safe_exec(all_code, context, random_seed=self.seed, python_path=python_path, extra_files=extra_files, cache=self.capa_system.cache, slug=self.problem_id, unsafely=self.capa_system.can_execute_unsafe_code())
        except Exception as err:
            log.exception(('Error    while    execing    script    code:    ' + all_code))
            msg = ('Error    while    executing    script    code:    %s' % str(err).replace('<', '&lt;'))
            raise responsetypes.LoncapaProblemError(msg)
    context['script_code'] = all_code
    context['python_path'] = python_path
    context['extra_files'] = (extra_files or None)
    return context
