Exemplo n.º 1
0
 def test_StoredFile(self):
     expected_content = b'Contents of foo/bar'
     name = 'foo/bar'
     f = test_utils.temp_file(expected_content)
     resource = stored_resource.StoredFile(
         name, self.date_time_tuple, f.name)
     self._write_and_check(resource, name, expected_content)
Exemplo n.º 2
0
 def test_write_zip_data(self):
     par = self._construct()
     with par.create_temp_parfile() as t:
         resource = stored_resource.StoredFile(
             os.path.basename(self.main_file.name), self.main_file.name)
         resources = {resource.stored_filename: resource}
         par.write_zip_data(t, resources)
     self.assertTrue(zipfile.is_zipfile(t.name))
Exemplo n.º 3
0
    def scan_manifest(self, manifest):
        """Return a dict of StoredResources based on an input manifest.

        Returns:
            A dict of store_filename to StoredResource
        """

        # Extend the list of import roots to include workspace roots
        top_roots = set()
        for stored_path in manifest.keys():
            if '/' in stored_path:  # Zip file paths use / on all platforms
                top_dir = stored_path.split('/', 1)[0]
                if top_dir not in top_roots:
                    top_roots.add(top_dir)
        import_roots = list(self.import_roots) + sorted(top_roots)

        # Include some files that every .par file needs at runtime
        stored_resources = {}
        for support_file in _runtime_support_files:
            resource = fetch_support_file(support_file, self.timestamp_tuple)
            stored_filename = resource.zipinfo.filename
            stored_resources[stored_filename] = resource

        # Scan manifest
        for stored_path, local_path in manifest.items():
            if local_path is None:
                stored_resources[stored_path] = stored_resource.EmptyFile(
                    stored_path, self.timestamp_tuple)
            else:
                stored_resources[stored_path] = stored_resource.StoredFile(
                    stored_path, self.timestamp_tuple, local_path)

        # Copy main entry point to well-known name
        if '__main__.py' in stored_resources:
            raise error.Error(
                ('Configuration error for [%s]: Manifest file included a '
                 'file named __main__.py, which is not allowed') %
                self.manifest_filename)
        stored_resources['__main__.py'] = self.generate_main(
            self.main_filename, self.generate_boilerplate(import_roots))

        # Add an __init__.py for each parent package of the support files
        for stored_filename in _runtime_init_files:
            if stored_filename in stored_resources:
                logging.debug('Skipping __init__.py already present [%s]',
                              stored_filename)
                continue
            stored_resources[stored_filename] = stored_resource.EmptyFile(
                stored_filename, self.timestamp_tuple)

        return stored_resources
Exemplo n.º 4
0
    def test_write_zip_data(self):
        # Create simple .par file
        par = self._construct()
        with par.create_temp_parfile() as output_file:
            stored_name = os.path.basename(self.main_file.name)
            resource = stored_resource.StoredFile(stored_name,
                                                  self.date_time_tuple,
                                                  self.main_file.name)
            resources = {resource.zipinfo.filename: resource}
            par.write_zip_data(output_file, resources)
        output_file.close()

        # Check that it's a valid zipfile
        self.assertTrue(zipfile.is_zipfile(output_file.name))

        # Check that the file was stored correctly
        z = zipfile.ZipFile(output_file.name)
        zipinfo = z.getinfo(stored_name)
        self.assertEqual(zipinfo.date_time, self.date_time_tuple)