Skip to content

Instantly share code, notes, and snippets.

@emesik
Created October 13, 2010 19:21
Show Gist options
  • Save emesik/624684 to your computer and use it in GitHub Desktop.
Save emesik/624684 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.http import HttpResponseNotFound
from django.views.static import serve as django_serve
import os
def _do_rec_sync(local_root, remote_path, local_path, update):
if os.path.exists(local_path) and (os.path.isdir(local_path) or not update):
return
parent_local_path = os.path.normpath(os.path.join(local_path, '..'))
parent_remote_path = os.path.normpath(os.path.join(remote_path, '..'))
_do_rec_sync(local_root, parent_remote_path, parent_local_path, update)
args = ['rsync', '-dtpq', remote_path, local_path]
os.spawnvp(os.P_WAIT, 'rsync', args)
def _synchronize(local_root, path, remote_root, update):
local_path = os.path.join(local_root, path)
remote_path = os.path.join(remote_root, path)
_do_rec_sync(local_root, remote_path, local_path, update)
def serve(request, path, document_root=None, show_indexes=False, remote_root=None, update=False):
local_name = os.path.join(document_root, path)
if not os.path.exists(document_root):
return HttpResponseNotFound()
if os.path.exists(local_name) and not update:
# The path exists and we don't have to update. This is the only situation when
# we just serve a file.
return django_serve(request, path, document_root=document_root, show_indexes=show_indexes)
# Otherwise, we must sync from remote.
_synchronize(document_root, path, remote_root, update)
return django_serve(request, path, document_root=document_root, show_indexes=show_indexes)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django_sfsync.serve', {
'document_root': settings.MEDIA_ROOT,
'remote_root': '[email protected]:/srv/www/example.com/myproject/webroot/media/',
'update': True,
}),
)
@emesik
Copy link
Author

emesik commented Oct 13, 2010

Ever wanted to keep the devel server updated with a copy from the stable one?

Using this code and rsync you will update all the static files in-flight. You only have to keep track on the database.

The update parameters specifies if existing files should be updated or skipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment