<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def publish(self, target="S3", sitename=None, purge_files=True):
 """ To publish programatically :param target: Where to pusblish at, S3 :param sitename: The site name :param purge_files: if True, it will delete old files :return: """

        self.build()

        endpoint = self.config.get("hosting.%s" % target)
        if target.upper() == "S3":
            p = publisher.S3Website(sitename=sitename or self.config.get("sitename"),
                                    aws_access_key_id=endpoint.get("aws_access_key_id"),
                                    aws_secret_access_key=endpoint.get("aws_secret_access_key"),
                                    region=endpoint.get("aws_region"))
            if not p.website_exists:
                if p.create_website() is True:
                    # Need to give it enough time to create it
                    # Should be a one time thing
                    time.sleep(10)
                    p.create_www_website()

            p.create_manifest_from_s3_files()

            if purge_files:
                exclude_files = endpoint.get("purge_exclude_files", [])
                p.purge_files(exclude_files=exclude_files)

            p.upload(self.build_dir)
            return p.website_endpoint_url