def _resolve(self, scriptName, foundInScript = None):
        '''
        the center piece.
        recursively resolve dependencies of scripts
        return them as a flat list, sorted according to ancestral relationships
        '''
        resolved = self.dependencies.get(scriptName, None)
        if resolved is not None:
            return resolved

        scriptFile = self.files.get(scriptName)

        if not scriptFile:
            msg = 'script %s not found' % scriptName
            if foundInScript:
                msg += ' while resolving %s' % foundInScript
            raise JSResolverError, msg

        declared = scriptFile.declaredDependencies()

        resolved = set()

        for decl in declared:
            resolutionStep = (scriptName, decl)
            if resolutionStep in self.reentrantResolution:
                if foundInScript:
                    scapegoat = foundInScript
                else:
                    scapegoat = decl
                raise JSResolverError, 'circular dependency involving %s and %s' % (scriptName, scapegoat)
            self.reentrantResolution.add(resolutionStep)
            # resolved.add(decl)      # add the declared script ...
            resolved.update(self._resolve(decl, foundInScript = scriptName)) # and its dependencies, if any

        resolved = list(resolved)
        # now it's time for sorting hierarchically... Since circular dependencies are excluded,
        # ancestors will always have fewer dependencies than descendants, so sorting by the
        # number of dependencies will give us the desired order.
        resolved.sort(key = lambda x : len(self.dependencies[x]))
        resolved.append(scriptName)
        self.dependencies[scriptName] = resolved
        return resolved

        the center piece.
        recursively resolve dependencies of scripts
        return them as a flat list, sorted according to ancestral relationships
        
recipes/Python/498265_Server_side_JavaScript_dependency/recipe-498265.py