def _copy_py_file(self, srcpath, dstpath):
        """Copy a python source file into the bundle.

        This method copes the contents of a python source file into the bundle.
        Since browsers usually expect strings in utf-8 format, it will try to
        detect source files in other encodings and transparently convert them
        to utf-8.
        """
        # XXX TODO: copy in chunks, like shutil would do?
        with open(srcpath, "rb") as f_src:
            data = f_src.read()
        # Look for the encoding marker in the first two lines of the file.
        lines = data.split(b"\n", 2)
        encoding = None
        for i in range(2):
            if i >= len(lines):
                break
            if lines[i].startswith(b"#"):
                match = re.search(b"coding[:=]\\s*([-\\w.]+)", lines[i])
                if match is not None:
                    encoding = match.group(1)
                    encodingName = encoding.decode("ascii")
                    try:
                        codecs.lookup(encodingName)
                    except LookupError:
                        encoding = None
                    break
        # Write normalized data to output file.
        with open(dstpath, "wb") as f_dst:
            if encoding is None:
                f_dst.write(data)
            else:
                for j in range(i):
                    f_dst.write(lines[j])
                    f_dst.write(b"\n")
                f_dst.write(lines[i].replace(encoding, b"utf-8"))
                f_dst.write(b"\n")
                for j in range(i + 1, len(lines)):
                    f_dst.write(lines[j].decode(encodingName).encode("utf8"))
                    if j < len(lines) - 1:
                        f_dst.write(b"\n")
Copy a python source file into the bundle.

        This method copes the contents of a python source file into the bundle.
        Since browsers usually expect strings in utf-8 format, it will try to
        detect source files in other encodings and transparently convert them
        to utf-8.
        
tools/module_bundler.py