def sync(client=None, force=False):
    """ 
    Let's face it... pushing this stuff to S3 is messy. A lot of different 
    things need to be calculated for each file and they have to be in a certain 
    order as some variables rely on others.
    """
    # create client connection
    if client is None:
        client = backends.client()

    client.open()
    client.serve_remote = True

    #
    # sync joined media
    #

    for joinfile, sourcefiles in JOINED.iteritems():
        filedata = combine_files(joinfile, sourcefiles, client)
        if filedata is None:
            # combine_files() is only interested in CSS/JS files.
            continue
        filedata, dirname = filedata

        content_type = mimetypes.guess_type(joinfile)[0] or 'application/octet-stream'

        remote_path = joinfile
        if dirname:
            remote_path = "%s/%s" % (dirname, remote_path)

        if client.process_and_put(filedata, content_type, remote_path, force=force):
            print "[%s] %s" % (content_type, remote_path)

    #
    # sync static media
    #

    for dirname in os.listdir(client.media_root):

        dirpath = os.path.abspath(os.path.join(client.media_root, dirname))

        if os.path.isdir(dirpath):

            for filename in listdir_recursive(dirpath):

                # calculate local and remote paths
                filepath = os.path.join(dirpath, filename)
                remote_path = "%s/%s" % (dirname, filename)

                content_type = mimetypes.guess_type(filepath)[0] or 'application/octet-stream'

                if not is_syncable_file(os.path.basename(filename)) or not os.path.isfile(filepath):
                    continue # hidden file or directory, do not upload

                filedata = open(filepath, 'rb').read()

                if client.process_and_put(filedata, content_type, remote_path, force=force):
                    print "[%s] %s" % (content_type, remote_path)

    client.close()
    def render(self, context):
        media_url = self.get_media_url(context)

        if SERVE_REMOTE and self.path in JOINED:
            # Serving from S3/Cloud Files.
            return self.scripttag(media_url, JS_PATH, self.path)
        elif not SERVE_REMOTE and EMULATE_COMBO:
            # Don't split the combo file into its component files. Emulate
            # the combo behavior, but generate/serve it locally. Useful for
            # testing combo JS before deploying.
            return self.scripttag(media_url, JS_PATH, self.path)
        else:
            # If this is a combo file seen in the JOINED key on the
            # MEDIASYNC dict, break it apart into its component files and
            # write separate <link> tags for each.
            filenames = JOINED.get(self.path, (self.path,))
            return ' '.join((self.scripttag(media_url, JS_PATH, fn) for fn in filenames))
def _find_combo_match(path):
    """
    Calculate the key to check the MEDIASYNC['JOINED'] dict for, perform the
    lookup, and return the matching key string if a match is found. If no
    match is found, return None instead.
    """
    key_str = _form_key_str(path)
    if not key_str:
        # _form_key_str() says this isn't even a CSS/JS file.
        return None

    if not JOINED.has_key(key_str):
        # No combo file match found. Must be an single file.
        return None
    else:
        # Combo match found, return the JOINED key.
        return key_str