Esempio n. 1
0
    def generate_main(self, main_filename, boilerplate_contents):
        """Generate the contents of the __main__.py file

        We take the module that is specified as the main entry point,
        and insert some boilerplate to invoke import helper code.

        Returns:
            A StoredResource
        """
        # Read main source file, in unknown encoding.  We use latin-1
        # here, but any single-byte encoding that doesn't raise errors
        # would work.
        with io.open(main_filename, 'rt', encoding='latin-1') as main_file:
            original_content = main_file.read()

        # Find a good place to insert the boilerplate, which is the
        # first line that is not a comment, blank line, doc comment,
        # or future import.
        match = re.match(_boilerplate_insertion_regex, original_content)
        assert match, original_content
        assert (len(match.group('before')) + len(match.group('after'))) == \
                len(original_content), (match, original_content)
        new_content = (match.group('before') + boilerplate_contents +
                       match.group('after'))

        # Insert boilerplate (might be beginning, middle or end)
        encoded_content = new_content.encode('latin-1')
        return stored_resource.StoredContent('__main__.py',
                                             self.timestamp_tuple,
                                             encoded_content)
Esempio n. 2
0
    def generate_main(self, main_filename, boilerplate_contents):
        """Generate the contents of the __main__.py file

        We take the module that is specified as the main entry point,
        and insert some boilerplate to invoke import helper code.

        Returns:
            A StoredResource
        """
        # Read main source file, in unknown encoding.  We use latin-1
        # here, but any single-byte encoding that doesn't raise errors
        # would work.
        output_lines = []
        with io.open(main_filename, 'rt', encoding='latin-1') as main_file:
            output_lines = list(main_file)

        # Find a good place to insert the boilerplate, which is the
        # first line that is not a comment, blank line, or future
        # import.
        skip_regex = re.compile(
            '''(#.*)|(\\s+)|(from\\s+__future__\\s+import)''')
        idx = 0
        while idx < len(output_lines):
            if not skip_regex.match(output_lines[idx]):
                break
            idx += 1

        # Insert boilerplate (might be beginning, middle or end)
        output_lines[idx:idx] = [boilerplate_contents]
        contents = ''.join(output_lines).encode('latin-1')
        return stored_resource.StoredContent('__main__.py', contents)
Esempio n. 3
0
def fetch_support_file(name):
    """Read a file from the runtime package

    Args:
        name: filename in runtime package's directory

    Returns:
        A StoredResource representing the content of that file
    """
    stored_filename = os.path.join(_subpar_package, 'runtime', name)
    content = pkgutil.get_data(_subpar_package, 'runtime/' + name)
    # content is None means the file wasn't found.  content == '' is
    # valid, it means the file was found and was empty.
    if content is None:
        raise error.Error(
            'Internal error: Can\'t find runtime support file [%s]' % name)
    return stored_resource.StoredContent(stored_filename, content)
Esempio n. 4
0
    def write_zip_data(self, temp_parfile, stored_resources):
        """Write the second part of a parfile, consisting of ZIP data

        Args:
            stored_resources: A dictionary mapping relative path to the
            content to store at that path.
        """

        logging.debug('Storing Files...')
        with contextlib.closing(
                zipfile.ZipFile(temp_parfile, 'w', self.compression)) as z:
            manifest_hash = hashlib.sha256()
            items = sorted(stored_resources.items())
            for relative_path, resource in items:
                assert resource.zipinfo.filename == relative_path
                resource.store(z)
                _update_hash(manifest_hash, resource)

            logging.debug(
                "Hash calculated for manifest: %s", manifest_hash.hexdigest())
            hash_file = stored_resource.StoredContent(
                "UNPAR_MANIFEST", self.timestamp_tuple,
                manifest_hash.hexdigest())
            hash_file.store(z)
Esempio n. 5
0
 def test_StoredContent(self):
     expected_content = b'Contents of foo/bar'
     name = 'foo/bar'
     resource = stored_resource.StoredContent(name, expected_content)
     self._write_and_check(resource, name, expected_content)