<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _finalize(self):
 """ command to run at the end of sprinter's run """

        self.logger.info("Finalizing...")
        self.write_manifest()

        if self.directory.rewrite_config:
            # always ensure .rc is written (sourcing .env)
            self.directory.add_to_rc('')
            # prepend brew for global installs
            if system.is_osx() and self.main_manifest.is_affirmative('config', 'use_global_packagemanagers'):
                self.directory.add_to_env('__sprinter_prepend_path "%s" PATH' % '/usr/local/bin')
            self.directory.add_to_env('__sprinter_prepend_path "%s" PATH' % self.directory.bin_path())
            self.directory.add_to_env('__sprinter_prepend_path "%s" LIBRARY_PATH' % self.directory.lib_path())
            self.directory.add_to_env('__sprinter_prepend_path "%s" C_INCLUDE_PATH' % self.directory.include_path())
            self.directory.finalize()

        self.injections.commit()
        self.global_injections.commit()

        if not os.path.exists(os.path.join(self.root, ".global")):
            self.logger.debug("Global directory doesn't exist! creating...")
            os.makedirs(os.path.join(self.root, ".global"))

        self.logger.debug("Writing shell util file...")
        with open(self.shell_util_path, 'w+') as fh:
            fh.write(shell_utils_template)

        if self.error_occured:
            raise SprinterException("Error occured!")

        if self.message_success():
            self.logger.info(self.message_success())

        self.logger.info("Done!")
        self.logger.info("NOTE: Please remember to open new shells/terminals to use the modified environment")