def testBinaryImport(self):
        zip_io = StringIO()
        zip_f = zipfile.ZipFile(zip_io, 'w')
        zip_f.writestr('testa/a.so', '')
        zip_f.close()
        self.assertRaises(SystemError, CompressImporter, zip_f)

        temp_path = tempfile.mkdtemp(prefix='tmp_pyodps')
        lib_path = os.path.join(temp_path, 'mylib')
        os.makedirs(lib_path)

        lib_dict = dict()
        try:
            six_path = os.path.join(
                os.path.dirname(os.path.abspath(six.__file__)), 'six.py')
            shutil.copy(six_path, os.path.join(lib_path, 'five.py'))
            dummy_bin = open(os.path.join(lib_path, 'dummy.so'), 'w')
            dummy_bin.close()

            lib_files = ['five.py', 'dummy.so']
            lib_dict = dict((os.path.join(lib_path, fn),
                             open(os.path.join(lib_path, fn), 'r'))
                            for fn in lib_files)

            importer.ALLOW_BINARY = False
            self.assertRaises(SystemError, CompressImporter, lib_dict)

            importer.ALLOW_BINARY = True
            sys.meta_path.append(CompressImporter(lib_dict))
            import five
            self.assertEqual(list(to_binary('abc')),
                             list(five.binary_type(to_binary('abc'))))
        finally:
            [f.close() for f in six.itervalues(lib_dict)]
            shutil.rmtree(temp_path)
        def _normalize_counter(json_str):
            obj = json.loads(json_str)
            for v in six.itervalues(obj):
                if 'counters' not in v:
                    continue
                v['counters'] = sorted(v['counters'], key=lambda item: item['name'])

            return json.dumps(obj, sort_keys=True)
        def _normalize_counter(json_str):
            obj = json.loads(json_str)
            for v in six.itervalues(obj):
                if 'counters' not in v:
                    continue
                v['counters'] = sorted(v['counters'],
                                       key=lambda item: item['name'])

            return json.dumps(obj, sort_keys=True)
Exemplo n.º 4
0
    def load_script():
        global _script_content
        if _script_content is None:
            # load static resources, .min.* come first.
            def load_static_resources(ext):
                file_dict = load_static_text_files('ui/*.min.' + ext)
                file_names = set([fn.replace('.min.' + ext, '') for fn in six.iterkeys(file_dict)])
                file_dict.update(load_static_text_files('ui/*.' + ext, lambda fn: '.min.' not in fn and
                                                                                  not fn.replace('.' + ext, '') in file_names))
                return file_dict

            minify_scripts()

            js_dict = load_static_resources('js') if in_ipython_frontend() else None

            if js_dict:
                css_dict = load_static_resources('css') if in_ipython_frontend() else None

                css_contents = ''
                if css_dict:
                    css_contents = '\n'.join(six.itervalues(css_dict))

                common_js_file = None
                if COMMON_JS + '.min.js' in js_dict:
                    common_js_file = COMMON_JS + '.min.js'
                elif COMMON_JS + '.js' in js_dict:
                    common_js_file = COMMON_JS + '.js'

                js_contents = ''

                if common_js_file:
                    js_common = js_dict.pop(common_js_file)
                    js_contents += '\n' + js_common.replace('##WIDGET_NUM##', str(len(js_dict))) \
                        .replace('##MAX_SCRIPT_LOAD_SEC##', str(MAX_SCRIPT_LOAD_SEC)) + '\n'

                if css_contents:
                    js_contents += '\n' + CSS_REGISTER_JS.replace('##CSS_STR##', quote(css_contents)) + '\n'
                js_contents += '\n'.join(six.itervalues(js_dict))
                _script_content = js_contents
            else:
                _script_content = None
        return _script_content
Exemplo n.º 5
0
    def test_html_notify(self):
        with setup_kernel() as client:
            client.execute('from odps.ui import html_notify')
            shell_msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = shell_msg['content']
            self.assertEqual(content['status'], 'ok')

            msg_id = client.execute('html_notify("TestMessage")')
            iopub_data = self.grab_iopub_comm(client, msg_id)
            assert any(u"TestMessage" in json.dumps(l)
                       for l in six.itervalues(iopub_data))

            shell_msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = shell_msg['content']
            self.assertEqual(content['status'], 'ok')
Exemplo n.º 6
0
    def test_html_notify(self):
        with setup_kernel() as client:
            client.execute('from odps.ui import html_notify')
            shell_msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = shell_msg['content']
            self.assertEqual(content['status'], 'ok')

            msg_id = client.execute('html_notify("TestMessage")')
            iopub_data = self.grab_iopub_comm(client, msg_id)
            assert any(u"TestMessage" in json.dumps(l)
                       for l in six.itervalues(iopub_data))

            shell_msg = client.get_shell_msg(block=True, timeout=TIMEOUT)
            content = shell_msg['content']
            self.assertEqual(content['status'], 'ok')
    def testBinaryImport(self):
        zip_io = StringIO()
        zip_f = zipfile.ZipFile(zip_io, 'w')
        zip_f.writestr('testa/a.so', '')
        zip_f.close()

        zip_io.seek(0)
        self.assertRaises(SystemError, CompressImporter,
                          zipfile.ZipFile(zip_io, 'r'))

        try:
            zip_io.seek(0)
            CompressImporter(zipfile.ZipFile(zip_io, 'r'),
                             extract=True,
                             _match_version=False)
            self.assertTrue(os.path.exists(CompressImporter._extract_path))
        finally:
            shutil.rmtree(CompressImporter._extract_path)
            CompressImporter._extract_path = None

        six_path = os.path.join(os.path.dirname(os.path.abspath(six.__file__)),
                                'six.py')

        temp_path = tempfile.mkdtemp(prefix='tmp-pyodps-')
        lib_path = os.path.join(temp_path, 'mylib')
        os.makedirs(lib_path)

        lib_dict = dict()
        try:
            with open(os.path.join(lib_path, '__init__.py'), 'w'):
                pass
            shutil.copy(six_path, os.path.join(lib_path, 'fake_six.py'))
            dummy_bin = open(os.path.join(lib_path, 'dummy.so'), 'w')
            dummy_bin.close()

            sub_lib_path = os.path.join(lib_path, 'sub_path')
            os.makedirs(sub_lib_path)

            with open(os.path.join(sub_lib_path, '__init__.py'), 'w'):
                pass
            shutil.copy(six_path, os.path.join(sub_lib_path, 'fake_six.py'))

            lib_files = [
                '__init__.py', 'fake_six.py', 'dummy.so',
                os.path.join('sub_path', '__init__.py'),
                os.path.join('sub_path', 'fake_six.py')
            ]
            lib_dict = dict((os.path.join(lib_path, fn),
                             open(os.path.join(lib_path, fn), 'r'))
                            for fn in lib_files)

            importer.ALLOW_BINARY = False
            self.assertRaises(SystemError, CompressImporter, lib_dict)

            importer.ALLOW_BINARY = True
            importer_obj = CompressImporter(lib_dict)
            sys.meta_path.append(importer_obj)
            from mylib import fake_six
            self.assertEqual(list(to_binary('abc')),
                             list(fake_six.binary_type(to_binary('abc'))))
            self.assertRaises(ImportError, __import__, 'sub_path', fromlist=[])
        finally:
            [f.close() for f in six.itervalues(lib_dict)]
            shutil.rmtree(temp_path)