Example #1
0
def update(new_info):
    """
    Update the info.

    :param new_info: Either a dict containing the new info or a path/url
                     to a json file containing the new info.
    """

    if isinstance(new_info, basestring):
        # lazy import
        import mozfile
        import json
        f = mozfile.load(new_info)
        new_info = json.loads(f.read())
        f.close()

    info.update(new_info)
    sanitize(info)
    globals().update(info)

    # convenience data for os access
    for os_name in choices['os']:
        globals()['is' + os_name.title()] = info['os'] == os_name
    # unix is special
    if isLinux or isBsd:  # noqa
        globals()['isUnix'] = True
Example #2
0
    def test_basic(self):
        """ Test mozhttpd can serve files """

        tempdir = tempfile.mkdtemp()

        # sizes is a dict of the form: name -> [size, binary_string, filepath]
        sizes = {'small': [128], 'large': [16384]}

        for k in sizes.keys():
            # Generate random binary string
            sizes[k].append(os.urandom(sizes[k][0]))

            # Add path of file with binary string to list
            fpath = os.path.join(tempdir, k)
            sizes[k].append(fpath)

            # Write binary string to file
            with open(fpath, 'wb') as f:
                f.write(sizes[k][1])

        server = mozhttpd.MozHttpd(docroot=tempdir)
        server.start()
        server_url = server.get_url()

        # Retrieve file and check contents matchup
        for k in sizes.keys():
            retrieved_content = mozfile.load(server_url + k).read()
            self.assertEqual(retrieved_content, sizes[k][1])

        # Cleanup tempdir and related files
        mozfile.rmtree(tempdir)
Example #3
0
def update(new_info):
    """
    Update the info.

    :param new_info: Either a dict containing the new info or a path/url
                     to a json file containing the new info.
    """

    if isinstance(new_info, basestring):
        # lazy import
        import mozfile
        import json
        f = mozfile.load(new_info)
        new_info = json.loads(f.read())
        f.close()

    info.update(new_info)
    sanitize(info)
    globals().update(info)

    # convenience data for os access
    for os_name in choices['os']:
        globals()['is' + os_name.title()] = info['os'] == os_name
    # unix is special
    if isLinux or isBsd:
        globals()['isUnix'] = True
Example #4
0
def update(new_info):
    """
    Update the info.

    :param new_info: Either a dict containing the new info or a path/url
                     to a json file containing the new info.
    """
    from six import string_types

    if isinstance(new_info, string_types):
        # lazy import
        import mozfile
        import json

        f = mozfile.load(new_info)
        new_info = json.loads(f.read())
        f.close()

    info.update(new_info)
    sanitize(info)
    globals().update(info)

    # convenience data for os access
    for os_name in choices["os"]:
        globals()["is" + os_name.title()] = info["os"] == os_name
    # unix is special
    if isLinux or isBsd:  # noqa
        globals()["isUnix"] = True
Example #5
0
    def test_basic(self):
        """ Test mozhttpd can serve files """

        tempdir = tempfile.mkdtemp()

        # sizes is a dict of the form: name -> [size, binary_string, filepath]
        sizes = {'small': [128], 'large': [16384]}

        for k in sizes.keys():
            # Generate random binary string
            sizes[k].append(os.urandom(sizes[k][0]))

            # Add path of file with binary string to list
            fpath = os.path.join(tempdir, k)
            sizes[k].append(fpath)

            # Write binary string to file
            with open(fpath, 'wb') as f:
                f.write(sizes[k][1])

        server = mozhttpd.MozHttpd(docroot=tempdir)
        server.start()
        server_url = server.get_url()

        # Retrieve file and check contents matchup
        for k in sizes.keys():
            retrieved_content = mozfile.load(server_url + k).read()
            self.assertEqual(retrieved_content, sizes[k][1])

        # Cleanup tempdir and related files
        mozfile.rmtree(tempdir)
Example #6
0
def test_basic(httpd_url, files):
    """Test that mozhttpd can serve files."""

    # Retrieve file and check contents matchup
    for name, binary_string in files:
        retrieved_content = mozfile.load(httpd_url + name).read()
        assert retrieved_content == binary_string
Example #7
0
    def read_prefs(cls, path, pref_setter="user_pref", interpolation=None):
        """
        Read preferences from (e.g.) prefs.js

        :param path: The path to the preference file to read.
        :param pref_setter: The name of the function used to set preferences
                            in the preference file.
        :param interpolation: If provided, a dict that will be passed
                              to str.format to interpolate preference values.
        """

        marker = "##//"  # magical marker
        lines = [i.strip() for i in mozfile.load(path).readlines()]
        _lines = []
        for line in lines:
            # decode bytes in case of URL processing
            if isinstance(line, bytes):
                line = line.decode()
            if not line.startswith(pref_setter):
                continue
            if "//" in line:
                line = line.replace("//", marker)
            _lines.append(line)
        string = "\n".join(_lines)

        # skip trailing comments
        processed_tokens = []
        f_obj = StringIO(string)
        for token in tokenize.generate_tokens(f_obj.readline):
            if token[0] == tokenize.COMMENT:
                continue
            processed_tokens.append(
                token[:2]
            )  # [:2] gets around http://bugs.python.org/issue9974
        string = tokenize.untokenize(processed_tokens)

        retval = []

        def pref(a, b):
            if interpolation and isinstance(b, string_types):
                b = b.format(**interpolation)
            retval.append((a, b))

        lines = [i.strip().rstrip(";") for i in string.split("\n") if i.strip()]

        _globals = {"retval": retval, "true": True, "false": False}
        _globals[pref_setter] = pref
        for line in lines:
            try:
                eval(line, _globals, {})
            except SyntaxError:
                print(line)
                raise

        # de-magic the marker
        for index, (key, value) in enumerate(retval):
            if isinstance(value, string_types) and marker in value:
                retval[index] = (key, value.replace(marker, "//"))

        return retval
Example #8
0
    def read_prefs(cls, path, pref_setter='user_pref', interpolation=None):
        """
        Read preferences from (e.g.) prefs.js

        :param path: The path to the preference file to read.
        :param pref_setter: The name of the function used to set preferences
                            in the preference file.
        :param interpolation: If provided, a dict that will be passed
                              to str.format to interpolate preference values.
        """

        comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/', re.MULTILINE)

        marker = '##//' # magical marker
        lines = [i.strip() for i in mozfile.load(path).readlines() if i.strip()]
        _lines = []
        for line in lines:
            if line.startswith(('#', '//')):
                continue
            if '//' in line:
                line = line.replace('//', marker)
            _lines.append(line)
        string = '\n'.join(_lines)
        string = re.sub(comment, '', string)

        # skip trailing comments
        processed_tokens = []
        f_obj = StringIO(string)
        for token in tokenize.generate_tokens(f_obj.readline):
            if token[0] == tokenize.COMMENT:
                continue
            processed_tokens.append(token[:2]) # [:2] gets around http://bugs.python.org/issue9974
        string = tokenize.untokenize(processed_tokens)

        retval = []
        def pref(a, b):
            if interpolation and isinstance(b, basestring):
                b = b.format(**interpolation)
            retval.append((a, b))
        lines = [i.strip().rstrip(';') for i in string.split('\n') if i.strip()]

        _globals = {'retval': retval, 'true': True, 'false': False}
        _globals[pref_setter] = pref
        for line in lines:
            try:
                eval(line, _globals, {})
            except SyntaxError:
                print line
                raise

        # de-magic the marker
        for index, (key, value) in enumerate(retval):
            if isinstance(value, basestring) and marker in value:
                retval[index] = (key, value.replace(marker, '//'))

        return retval
Example #9
0
    def read_prefs(cls, path, pref_setter="user_pref", interpolation=None):
        """
        Read preferences from (e.g.) prefs.js

        :param path: The path to the preference file to read.
        :param pref_setter: The name of the function used to set preferences
                            in the preference file.
        :param interpolation: If provided, a dict that will be passed
                              to str.format to interpolate preference values.
        """

        marker = "##//"  # magical marker
        lines = [i.strip() for i in mozfile.load(path).readlines()]
        _lines = []
        for line in lines:
            if not line.startswith(pref_setter):
                continue
            if "//" in line:
                line = line.replace("//", marker)
            _lines.append(line)
        string = "\n".join(_lines)

        # skip trailing comments
        processed_tokens = []
        f_obj = StringIO(string)
        for token in tokenize.generate_tokens(f_obj.readline):
            if token[0] == tokenize.COMMENT:
                continue
            processed_tokens.append(token[:2])  # [:2] gets around http://bugs.python.org/issue9974
        string = tokenize.untokenize(processed_tokens)

        retval = []

        def pref(a, b):
            if interpolation and isinstance(b, basestring):
                b = b.format(**interpolation)
            retval.append((a, b))

        lines = [i.strip().rstrip(";") for i in string.split("\n") if i.strip()]

        _globals = {"retval": retval, "true": True, "false": False}
        _globals[pref_setter] = pref
        for line in lines:
            try:
                eval(line, _globals, {})
            except SyntaxError:
                print line
                raise

        # de-magic the marker
        for index, (key, value) in enumerate(retval):
            if isinstance(value, basestring) and marker in value:
                retval[index] = (key, value.replace(marker, "//"))

        return retval
Example #10
0
    def read_prefs(cls, path, pref_setter='user_pref'):
        """read preferences from (e.g.) prefs.js"""

        comment = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/',
                             re.MULTILINE)

        marker = '##//'  # magical marker
        lines = [
            i.strip() for i in mozfile.load(path).readlines() if i.strip()
        ]
        _lines = []
        for line in lines:
            if line.startswith(('#', '//')):
                continue
            if '//' in line:
                line = line.replace('//', marker)
            _lines.append(line)
        string = '\n'.join(_lines)
        string = re.sub(comment, '', string)

        # skip trailing comments
        processed_tokens = []
        f_obj = StringIO(string)
        for token in tokenize.generate_tokens(f_obj.readline):
            if token[0] == tokenize.COMMENT:
                continue
            processed_tokens.append(
                token[:2])  # [:2] gets around http://bugs.python.org/issue9974
        string = tokenize.untokenize(processed_tokens)

        retval = []

        def pref(a, b):
            retval.append((a, b))

        lines = [
            i.strip().rstrip(';') for i in string.split('\n') if i.strip()
        ]

        _globals = {'retval': retval, 'true': True, 'false': False}
        _globals[pref_setter] = pref
        for line in lines:
            try:
                eval(line, _globals, {})
            except SyntaxError:
                print line
                raise

        # de-magic the marker
        for index, (key, value) in enumerate(retval):
            if isinstance(value, basestring) and marker in value:
                retval[index] = (key, value.replace(marker, '//'))

        return retval
Example #11
0
    def read_prefs(cls, path, pref_setter='user_pref', interpolation=None):
        """
        Read preferences from (e.g.) prefs.js

        :param path: The path to the preference file to read.
        :param pref_setter: The name of the function used to set preferences
                            in the preference file.
        :param interpolation: If provided, a dict that will be passed
                              to str.format to interpolate preference values.
        """

        marker = '##//' # magical marker
        lines = [i.strip() for i in mozfile.load(path).readlines() if i.strip()]
        _lines = []
        for line in lines:
            if line.startswith(('#', '//')):
                continue
            if '//' in line:
                line = line.replace('//', marker)
            _lines.append(line)
        string = '\n'.join(_lines)

        # skip trailing comments
        processed_tokens = []
        f_obj = StringIO(string)
        for token in tokenize.generate_tokens(f_obj.readline):
            if token[0] == tokenize.COMMENT:
                continue
            processed_tokens.append(token[:2]) # [:2] gets around http://bugs.python.org/issue9974
        string = tokenize.untokenize(processed_tokens)

        retval = []
        def pref(a, b):
            if interpolation and isinstance(b, basestring):
                b = b.format(**interpolation)
            retval.append((a, b))
        lines = [i.strip().rstrip(';') for i in string.split('\n') if i.strip()]

        _globals = {'retval': retval, 'true': True, 'false': False}
        _globals[pref_setter] = pref
        for line in lines:
            try:
                eval(line, _globals, {})
            except SyntaxError:
                print line
                raise

        # de-magic the marker
        for index, (key, value) in enumerate(retval):
            if isinstance(value, basestring) and marker in value:
                retval[index] = (key, value.replace(marker, '//'))

        return retval
Example #12
0
    def test_file_path(self):
        """test loading from file path"""
        try:
            # create a temporary file
            tmp = tempfile.NamedTemporaryFile(delete=False)
            tmp.write('foo bar')
            tmp.close()

            # read the file
            contents = file(tmp.name).read()
            self.assertEqual(contents, 'foo bar')

            # read the file with load and a file path
            self.assertEqual(load(tmp.name).read(), contents)

            # read the file with load and a file URL
            self.assertEqual(load('file://%s' % tmp.name).read(), contents)
        finally:
            # remove the tempfile
            if os.path.exists(tmp.name):
                os.remove(tmp.name)
Example #13
0
    def test_file_path(self):
        """test loading from file path"""
        try:
            # create a temporary file
            tmp = tempfile.NamedTemporaryFile(delete=False)
            tmp.write('foo bar')
            tmp.close()

            # read the file
            contents = file(tmp.name).read()
            self.assertEqual(contents, 'foo bar')

            # read the file with load and a file path
            self.assertEqual(load(tmp.name).read(), contents)

            # read the file with load and a file URL
            self.assertEqual(load('file://%s' % tmp.name).read(), contents)
        finally:
            # remove the tempfile
            if os.path.exists(tmp.name):
                os.remove(tmp.name)
Example #14
0
    def read_ini(cls, path, section=None):
        """read preferences from an .ini file"""

        parser = ConfigParser()
        parser.readfp(mozfile.load(path))

        if section:
            if section not in parser.sections():
                raise PreferencesReadError("No section '%s' in %s" % (section, path))
            retval = parser.items(section, raw=True)
        else:
            retval = parser.defaults().items()

        # cast the preferences since .ini is just strings
        return [(i, cls.cast(j)) for i, j in retval]
Example #15
0
    def read_ini(cls, path, section=None):
        """read preferences from an .ini file"""

        parser = ConfigParser()
        parser.optionxform = str
        parser.readfp(mozfile.load(path))

        if section:
            if section not in parser.sections():
                raise PreferencesReadError("No section '%s' in %s" % (section, path))
            retval = parser.items(section, raw=True)
        else:
            retval = parser.defaults().items()

        # cast the preferences since .ini is just strings
        return [(i, cls.cast(j)) for i, j in retval]
Example #16
0
    def read_json(cls, path):
        """read preferences from a JSON blob"""

        prefs = json.loads(mozfile.load(path).read())

        if type(prefs) not in [list, dict]:
            raise PreferencesReadError("Malformed preferences: %s" % path)
        if isinstance(prefs, list):
            if [i for i in prefs if type(i) != list or len(i) != 2]:
                raise PreferencesReadError("Malformed preferences: %s" % path)
            values = [i[1] for i in prefs]
        elif isinstance(prefs, dict):
            values = prefs.values()
        else:
            raise PreferencesReadError("Malformed preferences: %s" % path)
        types = (bool, basestring, int)
        if [i for i in values if not [isinstance(i, j) for j in types]]:
            raise PreferencesReadError("Only bool, string, and int values allowed")
        return prefs
Example #17
0
    def read_json(cls, path):
        """read preferences from a JSON blob"""

        prefs = json.loads(mozfile.load(path).read())

        if type(prefs) not in [list, dict]:
            raise PreferencesReadError("Malformed preferences: %s" % path)
        if isinstance(prefs, list):
            if [i for i in prefs if type(i) != list or len(i) != 2]:
                raise PreferencesReadError("Malformed preferences: %s" % path)
            values = [i[1] for i in prefs]
        elif isinstance(prefs, dict):
            values = prefs.values()
        else:
            raise PreferencesReadError("Malformed preferences: %s" % path)
        types = (bool, string_types, int)
        if [i for i in values if not any([isinstance(i, j) for j in types])]:
            raise PreferencesReadError("Only bool, string, and int values allowed")
        return prefs
Example #18
0
def update(new_info):
    """Update the info.
    new_info can either be a dict or a path/url
    to a json file containing a dict."""

    if isinstance(new_info, basestring):
        f = mozfile.load(new_info)
        new_info = json.loads(f.read())
        f.close()

    info.update(new_info)
    sanitize(info)
    globals().update(info)

    # convenience data for os access
    for os_name in choices['os']:
        globals()['is' + os_name.title()] = info['os'] == os_name
    # unix is special
    if isLinux or isBsd:
        globals()['isUnix'] = True
Example #19
0
    def test_http(self):
        """test with mozhttpd and a http:// URL"""

        def example(request):
            """example request handler"""
            body = 'example'
            return (200, {'Content-type': 'text/plain',
                          'Content-length': len(body)
                          }, body)

        host = '127.0.0.1'
        httpd = mozhttpd.MozHttpd(host=host,
                                  urlhandlers=[{'method': 'GET',
                                                'path': '.*',
                                                'function': example}])
        try:
            httpd.start(block=False)
            content = load(httpd.get_url()).read()
            self.assertEqual(content, 'example')
        finally:
            httpd.stop()
Example #20
0
def test_http(httpd_url):
    """Test with WebTestHttpd and a http:// URL."""
    content = load(httpd_url).read()
    assert content == b"example"
Example #21
0
def test_file_url(temporary_file):
    """Test loading from a file URL."""
    assert load("file://%s" % temporary_file).read() == "hello world"
Example #22
0
def test_file_path(temporary_file):
    """Test loading from a file path."""
    assert load(temporary_file).read() == "hello world"