示例#1
0
 def test_call(self):
     src = textwrap.dedent("""
     var j = {
       a: 1,
       b: {
         c: 2,
         d: x
       }
     };
     var k = 'hello world';
     var f = 'foobar';
     """).strip()
     result = repr_walker(es5(src))
     self.assertEqual(repr(es5(src)), result)
    def test_no_resolve(self):
        # a simple test to show that an obfuscator without the initial
        # loading run executed (i.e. the one with the required handlers)
        # with the resolve added to the dispatcher will not crash, but
        # simply not have any effect.

        tree = es5(
            dedent("""
        (function() {
          var foo = 1;
          var bar = 2;
        })(this);
        """).strip())
        unparser = Unparser()
        obfuscator = Obfuscator()
        dispatcher = Dispatcher(
            unparser.definitions, token_handler_str_default, {
                Space: layout_handler_space_minimum,
                RequiredSpace: layout_handler_space_minimum,
                OpenBlock: layout_handler_openbrace,
                CloseBlock: layout_handler_closebrace,
                EndStatement: layout_handler_semicolon,
            }, {
                Resolve: obfuscator.resolve,
            })
        # see that the manually constructed minimum output works.
        self.assertEqual("(function(){var foo=1;var bar=2;})(this);",
                         ''.join(c.text for c in walk(dispatcher, tree)))
    def test_obfuscate_no_global_recursive(self):
        node = es5(
            dedent("""
        (function named(param1, param2) {
          param1 = param1 * param2 - param2;
          param2--;
          if (param2 < 0) {
            return named(param1, param2);
          }
          return param1;
        })();
        """).strip())

        self.assertEqual(
            dedent("""
        (function named(b, a) {
          b = b * a - a;
          a--;
          if (a < 0) {
            return named(b, a);
          }
          return b;
        })();
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='  '),
                obfuscate(obfuscate_globals=False),
            ))(node)))
    def test_obfuscate_try_catch_shadowed(self):
        node = es5(
            dedent("""
        var value = 1;
        try {
          console.log(value);
          throw Error("welp");
        }
        catch (value) {
          console.log(value);
        }
        """).strip())

        self.assertEqual(
            dedent("""
        var a = 1;
        try {
            console.log(a);
            throw Error("welp");
        }
        catch (a) {
            console.log(a);
        }
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='    '),
                obfuscate(obfuscate_globals=True),
            ))(node)))
    def test_functions_in_lists(self):
        node = es5(
            dedent("""
        (function main(root) {
          root.exports = [
            (function(module, exports) {
              module.exports = {};
            }),
            (function(module, exports) {
              exports.fun = 1;
            }),
          ];
        })(this);
        """).strip())

        self.assertEqual(
            dedent("""
        (function main(a) {
          a.exports = [(function(a, b) {
            a.exports = {};
          }), (function(b, a) {
            a.fun = 1;
          })];
        })(this);
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='  '),
                obfuscate(),
            ))(node)))
示例#6
0
    def test_various_nested(self):
        result = repr_walker.walk(es5(textwrap.dedent("""
        var j = function(x) {
          return {
            a: 1,
            b: {
              c: 2,
              d: x
            }
          };
        }
        """).strip()), indent=2, pos=True, omit=(
            'op', 'lexpos', 'colno', 'lineno', 'left'))
        self.assertEqual(textwrap.dedent("""
        <ES5Program @1:1 ?children=[
          <VarStatement @1:1 ?children=[
            <VarDecl @1:5 identifier=<Identifier @1:5 value='j'>, initializer=\
<FuncExpr @1:9 elements=[
              <Return @2:3 expr=<Object @2:10 properties=[
                <Assign @3:6 right=<Number @3:8 value='1'>>,
                <Assign @4:6 right=<Object @4:8 properties=[
                  <Assign @5:8 right=<Number @5:10 value='2'>>,
                  <Assign @6:8 right=<Identifier @6:10 value='x'>>
                ]>>
              ]>>
            ], identifier=None, parameters=[
              <Identifier @1:18 value='x'>
            ]>>
          ]>
        ]>
        """).strip(), result)
示例#7
0
    def parse_sources(self, html):
        """
        Function called to parse html

        Parses resources and prepare the resource analysis. Retrieves the script
        tags that have text (inline script) and calls another method to handle
        script includes and parses the html page (makes a soup).

        :param html: the html body of the page to parse
        :return: none
        """
        soup = BeautifulSoup(html, 'html.parser')
        scripts = soup.find_all("script")
        styles = soup.find_all("style")

        for script in scripts:
            # If it is an inline script, format it and adds it to tree list
            if script.text:
                # Submitting a flag for each script encountered
                self.submit_flag(Flag('inline_script', script))
                try:
                    self.trees.append(es5(script.text))
                except ECMASyntaxError:
                    pass
            # Else it is an include script, just extract the source directly
            else:
                self.extract_inline_script_source(script)
        # Extract the sources in inline style (fonts, style dynamic stylesheets)
        self.styles = styles
        for style in styles:
            self.submit_flag(Flag('inline_style', style))
            self.extract_inline_style_source(style)
示例#8
0
def bake_js(source_dir="js5", dest_file="script5.js"):
    create_baked_directory()
    fn = os.path.join(
        os.path.dirname(__file__),
        "..",
        "static",
        "baked",
        str(get_build_number()),
        dest_file,
    )
    if not os.path.exists(fn):
        js_content = ""
        for sfn in get_js_file_list(source_dir):
            jsfile = open(os.path.join(os.path.dirname(__file__), "..", sfn))
            js_content += jsfile.read() + "\n"
            jsfile.close()

        o = open(fn, "w")
        # Pylint disabled for next line because pylint is buggy about the es5 function
        o.write(minify_print(es5(js_content))) # pylint: disable=not-callable
        if config.has("sentry_frontend_dsn"):
            sentry_frontend_dsn = config.get("sentry_frontend_dsn")
            o.write(
                'if (window.Sentry) {'
                    'window.Sentry.init({'
                        f'dsn: "{ sentry_frontend_dsn }",'
                        'tunnel: "/sentry_tunnel",'
                    '});'
                '}'
            )
        o.close()
示例#9
0
    def test_karma_test_files_located(self):
        karma_config = karma.build_base_config()
        karma_config['files'] = ['example/package/lib.js']
        spec = Spec(
            karma_config=karma_config,
            build_dir=mkdtemp(self),
            rjs_loader_plugin_registry=get(RJS_LOADER_PLUGIN_REGISTRY_NAME),
            export_module_names=['preexported'],
            test_module_paths_map={
                'example/package/tests/test_some_module':
                    '/src/example/package/tests/test_some_module.js',
                'example/package/tests/some_test_data':
                    '/src/example/package/tests/some_test_data.js',
            },
        )

        with pretty_logging(stream=StringIO()):
            karma_requirejs(spec)

        with open(spec['karma_requirejs_test_script'], encoding='utf-8') as fd:
            script = es5(fd.read())

        # this is the node for the json in the build file
        deps = json.loads(str(script.children()[0].children()[0].initializer))
        tests = json.loads(str(script.children()[1].children()[0].initializer))

        self.assertEqual(['example/package/tests/test_some_module'], tests)
        self.assertEqual(
            ['preexported', 'example/package/tests/some_test_data'], deps)
示例#10
0
 def test_dynamic_define_dynamic_amd_require(self):
     self.assertEqual(2, len(list(interrogate.yield_module_imports_nodes(
         es5("""
         require([foobar, 'foobar'], function(dyn, static) {
         });
         """)
     ))))
示例#11
0
 def test_identifier_extract_typical(self):
     with open(resource_filename('calmjs.testing', join(
             'names', 'typical.js')), encoding='utf-8') as fd:
         tree = es5(fd.read())
     for obj_node in interrogate.shallow_filter(
             tree, lambda node: isinstance(node, Object)):
         self.assertEqual('typical', interrogate.to_identifier(
             obj_node.properties[0].left))
    def test_obfuscate_try_catch_scope_inside_catch(self):
        node = es5(
            dedent("""
        var value = 100;
        (function() {
          var dummy = 0;
          console.log(value);
          value = 1;
          try {
            console.log(value);
            throw Error("welp");
          }
          catch (exc) {
            var value = 2;
            var welped = value;
            welped = 'welped';
            (function() {
              console.log(exc);
              console.log(value);
              console.log(welped);
            })();
          }
          value = 3;
        })();
        console.log(value);
        """).strip())

        # note that dummy -> c because exc is a local, not counted in
        # the priority at the parent scope.
        self.assertEqual(
            dedent("""
        var value = 100;
        (function() {
          var c = 0;
          console.log(a);
          a = 1;
          try {
            console.log(a);
            throw Error("welp");
          }
          catch (d) {
            var a = 2;
            var b = a;
            b = 'welped';
            (function() {
              console.log(d);
              console.log(a);
              console.log(b);
            })();
          }
          a = 3;
        })();
        console.log(value);
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='  '),
                obfuscate(),
            ))(node)))
示例#13
0
 def test_dynamic_define_dynamic_cjs_require(self):
     self.assertEqual(2, len(list(interrogate.yield_module_imports_nodes(
         es5("""
         var foobar = 'module';
         var mod = require(foobar);
         var mod2 = require('foobar');
         var invalid = require(foobar, 'foobar');
         """)
     ))))
示例#14
0
 def gen_js_script_nodes(self, node_type, scripts):
     res = []
     for script in scripts:
         program = es5(script)
         walker = Walker()
         for node in walker.filter(
                 program, lambda node: (isinstance(node, node_type))):
             res.append(node)
     return res
示例#15
0
    def test_base(self):
        # simply just test the basic functions... while the top level
        # readme covers this, just ensure this covers it
        from calmjs.parse import es5

        src = u'var a;'
        self.assertTrue(isinstance(es5(src), asttypes.Node))
        self.assertEqual(es5.pretty_print(src).strip(), src)
        self.assertEqual(es5.minify_print(src), src)
        self.assertEqual(es5.minify_print(src, True, True), src)
示例#16
0
def parse_publication(soups: Soups):
    stash = {}
    content = None
    target_script = None
    for script in soups.body.find_all("script"):
        try:
            if "Fusion.globalContent" in script.contents[0]:
                target_script = script.contents[0]
                break
        except:
            pass

    if target_script:
        for x in Walker().filter(
            es5(target_script), lambda node: isinstance(node, Assign)
        ):
            if str(x.left) == "Fusion.globalContent":
                content = json.loads(str(x.right))["content_elements"]
                break

        publication_text = [x["content"] for x in content if x.get("type") == "text"]
        publication_text_html = [
            x["content"] for x in content if x.get("type") == "raw_html"
        ]
        for html in publication_text_html:
            soup = BeautifulSoup(html, "html.parser")
            publication_text += list(soup.stripped_strings)
        stash["publication_text"] = "\n".join(publication_text)
        stash["image_urls"] = [x["url"] for x in content if x.get("type") == "image"]

    return {
        "version": soups.snapshot.snapshot_at,
        "site_id": soups.snapshot.site_id,
        "canonical_url": soups.snapshot.url,
        "published_at": P.parse_published_at(soups),
        "first_seen_at": soups.snapshot.first_seen_at,
        "last_updated_at": soups.snapshot.last_updated_at,
        "title": soups.body.find("title").text,
        "publication_text": stash.get("publication_text", ""),
        "author": None,
        "connect_from": None,
        "data": {
            "urls": P.parse_external_links(soups),
            "image_urls": stash.get("image_urls", []),
            "hashtags": [],
            "keywords": [],
            "tags": [],
            "metadata": {
                "metatags": soups.metatags,
                **soups.metadata,
                "ga-id": parse_ga_id(soups),
            },
            "comments": [],
        },
    }
示例#17
0
    def test_artifact_verify_extra_artifacts_with_build_dir(self):
        # this one is provided only as convenience; this may be useful
        # for builders that construct a partial artifacts but using a
        # test rule that doesn't provide some requirements, or for
        # testing whether inclusion of that other artifact will cause
        # interference with the expected functionality of the artifact
        # to be tested with.

        extra_js = join(mkdtemp(self), 'extra.js')
        extra_test = join(mkdtemp(self), 'test_extra.js')

        with open(extra_js, 'w') as fd:
            fd.write('var extra = {value: "artifact"};')

        with open(extra_test, 'w') as fd:
            fd.write(dedent("""
            'use strict';

            describe('emulated extra test', function() {
                it('extra artifact provided', function() {
                    expect(window.extra.value).to.equal("artifact");
                });
            });
            """.strip()))

        build_dir = mkdtemp(self)
        stub_stdouts(self)
        rt = self.setup_karma_artifact_runtime()
        # remove the fail test.
        reg = root_registry.get('calmjs.dev.module.tests')
        reg.records['calmjs.dev.tests'].pop('calmjs/dev/tests/test_fail', '')
        # inject our extra test to ensure the artifact that got added
        # still gets tested.
        reg.records['calmjs.dev.tests'][
            'calmjs/dev/tests/test_extra'] = extra_test
        self.assertTrue(rt([
            '-vv',
            '--artifact', extra_js,
            '--build-dir', build_dir,
            '-u', 'calmjs.dev',
            'calmjs.dev',
        ]))
        stderr = sys.stderr.getvalue()
        self.assertIn("specified artifact '%s' found" % extra_js, stderr)
        self.assertIn("artifact.js' found", stderr)

        with codecs.open(
                join(build_dir, 'karma.conf.js'), encoding='utf8') as fd:
            rawconf = es5(fd.read())

        # manually and lazily extract the configuration portion
        config = json.loads(str(
            rawconf.children()[0].expr.right.elements[0].expr.args.items[0]))
        # the extra specified artifact must be before the rest.
        self.assertEqual(config['files'][0], extra_js)
示例#18
0
    def read_js_object(js_script):
        """
        Reads the JS input (js_script, a string), and return a dictionary of
        variables definded in the JS.
        """
        def visit(node):
            if isinstance(node, asttypes.Program):
                d = {}
                for child in node:
                    if not isinstance(child, asttypes.VarStatement):
                        raise ValueError("All statements should be var statements")
                    key, val = visit(child)
                    d[key] = val
                return d
            elif isinstance(node, asttypes.VarStatement):
                return visit(node.children()[0])
            elif isinstance(node, asttypes.VarDecl):
                return (visit(node.identifier), visit(node.initializer))
            elif isinstance(node, asttypes.Object):
                d = {}
                for property in node:
                    key = visit(property.left)
                    value = visit(property.right)
                    d[key] = value
                return d
            elif isinstance(node, asttypes.BinOp):
                # simple constant folding
                if node.op == '+':
                    if isinstance(node.left, asttypes.String) and isinstance(node.right, asttypes.String):
                        return visit(node.left) + visit(node.right)
                    elif isinstance(node.left, asttypes.Number) and isinstance(node.right, asttypes.Number):
                        return visit(node.left) + visit(node.right)
                    else:
                        raise ValueError("Cannot + on anything other than two literals")
                else:
                    raise ValueError("Cannot do operator '%s'" % node.op)

            elif isinstance(node, asttypes.String) or isinstance(node, asttypes.Number):
                return literal_eval(node.value)
            elif isinstance(node, asttypes.Array):
                return [visit(x) for x in node]
            elif isinstance(node, asttypes.Null):
                return None
            elif isinstance(node, asttypes.Boolean):
                if str(node) == "false":
                    return False
                else:
                    return True
            elif isinstance(node, asttypes.Identifier):
                return node.value
            else:
                raise Exception("Unhandled node: %r" % node)
        return visit(es5(js_script))
示例#19
0
 def test_basic(self):
     result = repr_walker.walk(es5(textwrap.dedent("""
     var o = {
       a: 1,
       b: 2
     };
     """)), pos=True)
     self.assertEqual(
         result, "<ES5Program @2:1 ?children=[<VarStatement @2:1 "
         "?children=[<VarDecl @2:5 identifier=<Identifier @2:5 value='o'>, "
         "initializer=<Object @2:9 properties=[<Assign @3:4 left="
         "<PropIdentifier @3:3 value='a'>, op=':', right=<Number @3:6 "
         "value='1'>>, <Assign @4:4 left=<PropIdentifier @4:3 value='b'>, "
         "op=':', right=<Number @4:6 value='2'>>]>>]>]>"
     )
示例#20
0
 def test_depth_0(self):
     result = repr_walker.walk(es5(textwrap.dedent("""
     var j = function(x) {
       return {
         a: 1,
         b: {
           c: 2,
           d: x
         }
       };
     }
     """).strip()), omit=(), depth=0, indent=2)
     self.assertEqual(textwrap.dedent("""
     <ES5Program ...>
     """).strip(), result)
示例#21
0
 def test_extract_func_call_eval_instruction(self):
     """
     Aims to test if eval instruction properly generate unsafe-eval directive
     and raise correct flags
     :return:
     """
     # ------------------------------- #
     # ----------- ARRANGE ----------- #
     # ------------------------------- #
     self.res_sorter.report_generator = ReportGenerator()
     self.test_sorter.report_generator = ReportGenerator()
     script = """
         eval('2*3;')
         
         var m = 3;
         var f = new Function('a', 'return a');
         
         document.getElementsByTagName("body").style.cssText = "background-color:pink;font-size:55px;border:2px dashed green;color:white;"
         myStyle.insertRule('#blanc { color: white }', 0);
     """
     # Getting the node from the script
     nodes = []
     walker = Walker()
     for node in walker.filter(
             es5(script), lambda node: (isinstance(node, FunctionCall))):
         nodes.append(node)
     # Adding unsafe-eval directives for each relevant directive
     self.res_sorter.directives_sources['script-src'].add("'unsafe-eval'")
     self.res_sorter.directives_sources['style-src'].add("'unsafe-eval'")
     # Adding flag into test report generator
     flag_eval = Flag('eval_script', nodes[0])
     flag_insert_rule = Flag('eval_style', nodes[2])
     self.res_sorter.report_generator.flags.append(flag_eval)
     self.res_sorter.report_generator.flags.append(flag_insert_rule)
     # ------------------------------- #
     # ------------- ACT ------------- #
     # ------------------------------- #
     for node in nodes:
         instruction = self.test_sorter.get_node_instruction(node)
         self.test_sorter.extract_func_call_eval_instruction(
             node, instruction)
     # ------------------------------- #
     # ----------- ASSERT ------------ #
     # ------------------------------- #
     assert (self.res_sorter.directives_sources ==
             self.test_sorter.directives_sources)
     assert (set(self.res_sorter.report_generator.flags) == set(
         self.test_sorter.report_generator.flags))
    def test_obfuscate_no_shadow_funcname_not_mapped(self):
        node = es5(
            dedent("""
        function a(arg) {
        }
        """).strip())

        self.assertEqual(
            dedent("""
        function a(b) {
        }
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='  '),
                obfuscate(obfuscate_globals=False, shadow_funcname=False),
            ))(node)))
示例#23
0
    def test_repr(self):
        result = repr(es5(textwrap.dedent("""
        var j = null;

          var  k = this;
        """).strip()))
        self.assertEqual(textwrap.dedent("""
        <ES5Program @1:1 ?children=[
          <VarStatement @1:1 ?children=[
            <VarDecl @1:5 identifier=<Identifier ...>, initializer=<Null ...>>
          ]>,
          <VarStatement @3:3 ?children=[
            <VarDecl @3:8 identifier=<Identifier ...>, initializer=<This ...>>
          ]>
        ]>
        """).strip(), result)
示例#24
0
def parse_publication(soups):
    stash = {}
    stash["title"] = soups.body.find("title").text
    declarations = es5(soups.body.find_all("script")[-10].contents[0])
    info_node = list(Walker().filter(
        declarations,
        lambda x: isinstance(x, Assign) and str(x.left) == "articleInfo",
    ))[0]
    content_node = list(
        filter(
            lambda p: isinstance(p, Assign) and str(p.left) == "content",
            info_node.right.properties,
        ))[0]
    content_text = re.search(r"\'(.*)\'.slice",
                             str(content_node.right)).group(1)

    content_soup = BeautifulSoup(json.loads(html.unescape(content_text)),
                                 "html.parser")
    stash["publication_text"] = "\n".join(content_soup.stripped_strings)
    stash["image_urls"] = [x["src"] for x in content_soup.find_all("img")]

    return {
        "version": soups.snapshot.snapshot_at,
        "site_id": soups.snapshot.site_id,
        "canonical_url": soups.snapshot.url,
        "published_at": P.parse_published_at(soups),
        "first_seen_at": soups.snapshot.first_seen_at,
        "last_updated_at": soups.snapshot.last_updated_at,
        "title": stash["title"],
        "publication_text": stash.get("publication_text", ""),
        "author": None,
        "connect_from": None,
        "data": {
            "urls": P.parse_external_links(soups),
            "image_urls": stash.get("image_urls", ""),
            "hashtags": [],
            "keywords": [],
            "tags": [],
            "metadata": {
                "metatags": soups.metatags,
                **soups.metadata,
                "ga-id": parse_ga_id(soups),
            },
            "comments": [],
        },
    }
示例#25
0
 def test_indented_omitted(self):
     result = repr_walker.walk(es5(textwrap.dedent("""
     var o = {
       a: 1,
       b: 2
     };
     """).strip()), indent=2, pos=True, omit=(
         'op', 'right', 'lexpos', 'colno', 'lineno', 'identifier'))
     self.assertEqual(textwrap.dedent("""
     <ES5Program @1:1 ?children=[
       <VarStatement @1:1 ?children=[
         <VarDecl @1:5 initializer=<Object @1:9 properties=[
           <Assign @2:4 left=<PropIdentifier @2:3 value='a'>>,
           <Assign @3:4 left=<PropIdentifier @3:3 value='b'>>
         ]>>
       ]>
     ]>
     """).strip(), result)
    def test_multiple_reuse(self):
        tree = es5(
            dedent("""
        (function() {
          var foo = 1;
          var bar = 2;
          bar = 3;
        })(this);
        """).strip())
        obfuscator_unparser = Unparser(rules=(
            minimum_rules,
            obfuscate(),
        ))

        self.assertEqual(
            ''.join(c.text for c in obfuscator_unparser(tree)),
            ''.join(c.text for c in obfuscator_unparser(tree)),
        )
示例#27
0
 def test_identifier_extract_unusual(self):
     answers = [
         '\'"\'',
         '\'\"\'',
         "\'\"\'",
         "\n",
         "\t",
         r'\\ ',
         "\u3042",
         "\u3042",
         "    ",
     ]
     with open(resource_filename('calmjs.testing', join(
             'names', 'unusual.js')), encoding='utf-8') as fd:
         tree = es5(fd.read())
     for obj_node, answer in zip(interrogate.shallow_filter(
             tree, lambda node: isinstance(node, Object)), answers):
         self.assertEqual(answer, interrogate.to_identifier(
             obj_node.properties[0].left))
    def test_obfuscate_try_catch_vardeclare_inside_catch(self):
        node = es5(
            dedent("""
        var value = 100;
        (function() {
          console.log(value);
          value = 1;
          try {
            console.log(value);
            throw Error("welp");
          }
          catch (exc) {
            var value = 2;
            console.log(value);
          }
          value = 3;
        })();
        console.log(value);
        """).strip())

        self.assertEqual(
            dedent("""
        var value = 100;
        (function() {
          console.log(a);
          a = 1;
          try {
            console.log(a);
            throw Error("welp");
          }
          catch (b) {
            var a = 2;
            console.log(a);
          }
          a = 3;
        })();
        console.log(value);
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='  '),
                obfuscate(),
            ))(node)))
示例#29
0
 def test_depth_2(self):
     result = repr_walker.walk(es5(textwrap.dedent("""
     var j = function(x) {
       return {
         a: 1,
         b: {
           c: 2,
           d: x
         }
       };
     }
     """).strip()), omit=(
         'op', 'left', 'lexpos', 'identifier'), depth=2, indent=2)
     self.assertEqual(textwrap.dedent("""
     <ES5Program ?children=[
       <VarStatement ?children=[
         <VarDecl ...>
       ], colno=1, lineno=1>
     ], colno=1, lineno=1>
     """).strip(), result)
    def test_no_shadow_children(self):
        node = es5(
            dedent("""
        var some_value = 1;
        (function(param) {
          a.is_not_declared_in_this_file(param);
        })();
        """).strip())

        self.assertEqual(
            dedent("""
        var b = 1;
        (function(b) {
            a.is_not_declared_in_this_file(b);
        })();
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='    '),
                obfuscate(obfuscate_globals=True),
            ))(node)))
示例#31
0
def bake_js(source_dir="js5", dest_file="script5.js"):
    create_baked_directory()
    fn = os.path.join(
        os.path.dirname(__file__),
        "..",
        "static",
        "baked",
        str(get_build_number()),
        dest_file,
    )
    if not os.path.exists(fn):
        js_content = ""
        for sfn in get_js_file_list(source_dir):
            jsfile = open(os.path.join(os.path.dirname(__file__), "..", sfn))
            js_content += jsfile.read() + "\n"
            jsfile.close()

        o = open(fn, "w")
        # Pylint disabled for next line because pylint is buggy about the es5 function
        o.write(minify_print(es5(js_content), obfuscate=True))  # pylint: disable=not-callable
        o.close()
    def test_obfuscate_skip(self):
        node = es5(
            dedent("""
        (function(a_param) {
          var a_local = a_param;
        })();
        """).strip())

        self.assertEqual(
            dedent("""
        (function(c) {
            var d = c;
        })();
        """).lstrip(), ''.join(c.text for c in Unparser(rules=(
                default_rules,
                indent(indent_str='    '),
                obfuscate(reserved_keywords=(
                    'a',
                    'b',
                )),
            ))(node)))
示例#33
0
    def assemble_spec_config(self, **kw):
        # for the assemble related tests.
        tmpdir = utils.mkdtemp(self)
        build_dir = utils.mkdtemp(self)
        rjs = toolchain.RJSToolchain()

        export_target = join(build_dir, 'export.js')
        build_manifest_path = join(build_dir, 'build.js')
        node_config_js = join(build_dir, 'node_config.js')
        requirejs_config_js = join(build_dir, 'requirejs_config.js')

        with open(join(tmpdir, 'r.js'), 'w'):
            pass

        with open(join(build_dir, 'module1.js'), 'w') as fd:
            fd.write(
                "define(['jquery', 'underscore', 'some.pylike.module'], "
                "function(jquery, underscore, module) {"
                "});"
            )

        with open(join(build_dir, 'module2.js'), 'w') as fd:
            fd.write(
                "define(['module1', 'underscore'], "
                "function(module1, underscore) {"
                "});"
            )

        with open(join(build_dir, 'module3.js'), 'w') as fd:
            fd.write(
                "'use strict';\n"
                "var $ = require('jquery');\n"
                "var _ = require('underscore');\n"
                "var module2 = require('module2');\n"
            )

        spec = Spec(
            build_dir=build_dir,
            export_target=export_target,
            build_manifest_path=build_manifest_path,
            node_config_js=node_config_js,
            requirejs_config_js=requirejs_config_js,
            transpiled_modpaths={
                'module1': 'module1',
                'module2': 'module2',
                'module3': 'module3',
            },
            # these are not actually transpiled sources, but will fit
            # with the purposes of this test.
            transpiled_targetpaths={
                'module1': 'module1.js',
                'module2': 'module2.js',
                'module3': 'module3.js',
            },
            # the "bundled" names were specified to be omitted.
            bundled_modpaths={},
            bundled_targetpaths={},
            plugins_modpaths={},
            plugins_targetpaths={},
            export_module_names=['module1', 'module2', 'module3'],
            **kw
        )
        spec[rjs.rjs_bin_key] = join(tmpdir, 'r.js')
        rjs.assemble(spec)

        # the main config file
        # check that they all exists
        self.assertTrue(exists(build_manifest_path))
        self.assertTrue(exists(node_config_js))
        self.assertTrue(exists(requirejs_config_js))

        # only checking the build_manifest version, as the node config
        # version is not that much different.
        with open(build_manifest_path) as fd:
            build_tree = es5(fd.read())

        # this is the node for the json in the build file
        build_js = json.loads(str(build_tree.children()[0].expr.expr))

        with open(requirejs_config_js) as fd:
            config_tree = es5(fd.read())

        # this is the node for json in the config file
        config_js = json.loads(str(
            config_tree.children()[0].expr.expr.identifier.children(
            )[2].children()[0].initializer.expr
        ))

        return build_js, config_js
示例#34
0
 def test_dynamic_amd_define_require_yield_node(self):
     self.assertEqual(2, len(list(interrogate.yield_module_imports_nodes(
         es5(requirejs_dynamic_define)))))
示例#35
0
 def test_yield_imports_basic(self):
     self.assertEqual([
         'mod1',
         "name/mod/mod2",
     ], sorted(set(interrogate.yield_module_imports(es5(
         commonjs_require)))))