def build_cache(self, modules, excluded_packages, non_binary_packages):
        wheels_folder = os.path.join(self.cache_folder, 'wheels')
        wheels_install_folder = os.path.join(self.cache_folder, 'dependencies')
        cache_zip_file = os.path.join(self.cache_folder, 'cache.zip')
        cache_metadata_file = os.path.join(self.cache_folder, 'metadata.json')
        packages = DependencyManager.get_dependency_packages_list(
            modules, excluded_packages)
        packages_root = os.path.join('.python_packages', 'lib', 'python3.6',
                                     'site-packages')

        if not DependencyManager.check_cache(cache_metadata_file,
                                             cache_zip_file, packages):
            cache_pkg = AzurePythonPackageArchive()
            self.log.info(
                "Cached packages not found or requirements were changed.")

            # If cache check fails, wipe all previous wheels, installations etc
            if os.path.exists(self.cache_folder):
                self.log.info("Removing cache folder...")
                shutil.rmtree(self.cache_folder)

            self.log.info("Preparing non binary wheels...")
            DependencyManager.prepare_non_binary_wheels(
                non_binary_packages, wheels_folder)

            self.log.info("Downloading wheels...")
            DependencyManager.download_wheels(packages, wheels_folder)

            self.log.info("Installing wheels...")
            DependencyManager.install_wheels(wheels_folder,
                                             wheels_install_folder)

            for root, _, files in os.walk(wheels_install_folder):
                arc_prefix = os.path.relpath(root, wheels_install_folder)
                for f in files:
                    dest_path = os.path.join(packages_root, arc_prefix, f)

                    if f.endswith('.pyc') or f.endswith('.c'):
                        continue
                    f_path = os.path.join(root, f)

                    cache_pkg.add_file(f_path, dest_path)

            self.log.info('Saving cache zip file...')
            cache_pkg.close()
            with open(cache_zip_file, 'wb') as fout:
                fout.write(cache_pkg.get_stream().read())

            self.log.info("Removing temporary folders...")
            shutil.rmtree(wheels_folder)
            shutil.rmtree(wheels_install_folder)

            self.log.info("Updating metadata file...")
            DependencyManager.create_cache_metadata(cache_metadata_file,
                                                    cache_zip_file, packages)

        return cache_zip_file
    def test_create_cache_metadata(self):
        bench = tempfile.mkdtemp()
        self.addCleanup(lambda: shutil.rmtree(bench))

        tmp_metadata = os.path.join(bench, 'metadata.json')
        DependencyManager.create_cache_metadata(tmp_metadata, self.test_zip,
                                                self.test_packages)

        with open(self.test_metadata, 'rt') as f:
            test_json = json.load(f)
        with open(tmp_metadata, 'rt') as f:
            tmp_json = json.load(f)
        self.assertTrue(test_json == tmp_json)
    def build(self, policy, modules, non_binary_packages, excluded_packages, queue_name=None,):

        wheels_folder = os.path.join(self.cache_folder, 'wheels')
        wheels_install_folder = os.path.join(self.cache_folder, 'dependencies')

        packages = \
            DependencyManager.get_dependency_packages_list(modules, excluded_packages)

        if not DependencyManager.check_cache(self.cache_folder, wheels_install_folder, packages):
            self.log.info("Cached packages not found or requirements were changed.")
            # If cache check fails, wipe all previous wheels, installations etc
            if os.path.exists(self.cache_folder):
                self.log.info("Removing cache folder...")
                shutil.rmtree(self.cache_folder)

            self.log.info("Preparing non binary wheels...")
            DependencyManager.prepare_non_binary_wheels(non_binary_packages, wheels_folder)

            self.log.info("Downloading wheels...")
            DependencyManager.download_wheels(packages, wheels_folder)

            self.log.info("Installing wheels...")
            DependencyManager.install_wheels(wheels_folder, wheels_install_folder)

            self.log.info("Updating metadata file...")
            DependencyManager.create_cache_metadata(self.cache_folder,
                                                    wheels_install_folder,
                                                    packages)

        for root, _, files in os.walk(wheels_install_folder):
            arc_prefix = os.path.relpath(root, wheels_install_folder)
            for f in files:
                dest_path = os.path.join(arc_prefix, f)

                if f.endswith('.pyc') or f.endswith('.c'):
                    continue
                f_path = os.path.join(root, f)

                self.pkg.add_file(f_path, dest_path)

        exclude = os.path.normpath('/cache/') + os.path.sep
        self.pkg.add_modules(lambda f: (exclude in f),
                             *[m.replace('-', '_') for m in modules])

        # add config and policy
        self._add_functions_required_files(policy, queue_name)

        # generate and add auth
        s = local_session(Session)
        self.pkg.add_contents(dest=self.name + '/auth.json', contents=s.get_functions_auth_string())
Beispiel #4
0
    def build(
        self,
        policy,
        modules,
        non_binary_packages,
        excluded_packages,
        queue_name=None,
    ):

        wheels_folder = os.path.join(self.cache_folder, 'wheels')
        wheels_install_folder = os.path.join(self.cache_folder, 'dependencies')

        cache_zip_file = os.path.join(self.cache_folder, 'cache.zip')
        cache_metadata_file = os.path.join(self.cache_folder, 'metadata.json')

        packages = \
            DependencyManager.get_dependency_packages_list(modules, excluded_packages)

        if not DependencyManager.check_cache(cache_metadata_file,
                                             cache_zip_file, packages):
            cache_pkg = PythonPackageArchive()
            self.log.info(
                "Cached packages not found or requirements were changed.")
            # If cache check fails, wipe all previous wheels, installations etc
            if os.path.exists(self.cache_folder):
                self.log.info("Removing cache folder...")
                shutil.rmtree(self.cache_folder)

            self.log.info("Preparing non binary wheels...")
            DependencyManager.prepare_non_binary_wheels(
                non_binary_packages, wheels_folder)

            self.log.info("Downloading wheels...")
            DependencyManager.download_wheels(packages, wheels_folder)

            self.log.info("Installing wheels...")
            DependencyManager.install_wheels(wheels_folder,
                                             wheels_install_folder)

            for root, _, files in os.walk(wheels_install_folder):
                arc_prefix = os.path.relpath(root, wheels_install_folder)
                for f in files:
                    dest_path = os.path.join(arc_prefix, f)

                    if f.endswith('.pyc') or f.endswith('.c'):
                        continue
                    f_path = os.path.join(root, f)

                    cache_pkg.add_file(f_path, dest_path)

            self.log.info('Saving cache zip file...')
            cache_pkg.close()
            with open(cache_zip_file, 'wb') as fout:
                fout.write(cache_pkg.get_stream().read())

            self.log.info("Removing temporary folders...")
            shutil.rmtree(wheels_folder)
            shutil.rmtree(wheels_install_folder)

            self.log.info("Updating metadata file...")
            DependencyManager.create_cache_metadata(cache_metadata_file,
                                                    cache_zip_file, packages)

        self.pkg = PythonPackageArchive(cache_file=cache_zip_file)

        exclude = os.path.normpath('/cache/') + os.path.sep
        self.pkg.add_modules(lambda f: (exclude in f),
                             [m.replace('-', '_') for m in modules])

        # add config and policy
        self._add_functions_required_files(policy, queue_name)
    def build(
        self,
        policy,
        modules,
        non_binary_packages,
        excluded_packages,
        queue_name=None,
    ):

        wheels_folder = os.path.join(self.cache_folder, 'wheels')
        wheels_install_folder = os.path.join(self.cache_folder, 'dependencies')

        packages = \
            DependencyManager.get_dependency_packages_list(modules, excluded_packages)

        if not DependencyManager.check_cache(self.cache_folder,
                                             wheels_install_folder, packages):
            self.log.info(
                "Cached packages not found or requirements were changed.")
            # If cache check fails, wipe all previous wheels, installations etc
            if os.path.exists(self.cache_folder):
                self.log.info("Removing cache folder...")
                shutil.rmtree(self.cache_folder)

            self.log.info("Preparing non binary wheels...")
            DependencyManager.prepare_non_binary_wheels(
                non_binary_packages, wheels_folder)

            self.log.info("Downloading wheels...")
            DependencyManager.download_wheels(packages, wheels_folder)

            self.log.info("Installing wheels...")
            DependencyManager.install_wheels(wheels_folder,
                                             wheels_install_folder)

            self.log.info("Updating metadata file...")
            DependencyManager.create_cache_metadata(self.cache_folder,
                                                    wheels_install_folder,
                                                    packages)

        for root, _, files in os.walk(wheels_install_folder):
            arc_prefix = os.path.relpath(root, wheels_install_folder)
            for f in files:
                dest_path = os.path.join(arc_prefix, f)

                if f.endswith('.pyc') or f.endswith('.c'):
                    continue
                f_path = os.path.join(root, f)

                self.pkg.add_file(f_path, dest_path)

        exclude = os.path.normpath('/cache/') + os.path.sep
        self.pkg.add_modules(lambda f: (exclude in f),
                             *[m.replace('-', '_') for m in modules])

        # add config and policy
        self._add_functions_required_files(policy, queue_name)

        # generate and add auth
        s = local_session(Session)
        self.pkg.add_contents(dest=self.name + '/auth.json',
                              contents=s.get_functions_auth_string())