peterbe / django-static

Template tags for better serving static files from templates in Django
BSD 2-Clause "Simplified" License
193 stars 28 forks source link

django-static try overwrite target of symlink #25

Closed jeanlucmongrain closed 7 years ago

jeanlucmongrain commented 12 years ago

I have set my deployment this way: the code is in /usr/local/src/$project. Gunicorn is running under a non-root user. the path are set that the source where django-static get it's static file is in a different directory than slimmed, compiled or simply symlinked file.

It can only create new content and never write original files.

But, I have this weird behavior I've seed 5-6 times, where django-static try to open a symlink as a regular file and write data into it.

It might be caused by some manipulation I made. Sometimes I replace:

{% slimall %}
<script src="file1.js"></script>
<script src="file2.js"></script>
{% endslimall %}

by:

<script src="{% staticfile "file1.js" %}"></script>
<script src="{% staticfile "file1.js" %}"></script>

so I get unmodified .js and can troubleshoot javascript errors easily.

when the problem happens this is raised exception:

Traceback (most recent call last):

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/core/handlers/base.py", line 100, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/usr/local/src/radiopirate/horfunk/radiopirate/views/__init__.py", line 20, in index
   return render_to_response(template, context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/shortcuts/__init__.py", line 20, in render_to_response
   return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/loader.py", line 183, in render_to_string
   return t.render(Context(dictionary))

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 173, in render
   return self._render(context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 167, in _render
   return self.nodelist.render(context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 796, in render
   bits.append(self.render_node(node, context))

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 809, in render_node
   return node.render(context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/loader_tags.py", line 125, in render
   return compiled_parent._render(context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 167, in _render
   return self.nodelist.render(context)

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 796, in render
   bits.append(self.render_node(node, context))

 File "/usr/local/src/radiopirate/eggs/Django-1.2.7-py2.7.egg/django/template/__init__.py", line 809, in render_node
   return node.render(context)

 File "/usr/local/src/radiopirate/lib/python2.7/site-packages/django_static-1.5.5-py2.7.egg/django_static/templatetags/django_static.py", line 208, in render
   symlink_if_possible=self.symlink_if_possible)

 File "/usr/local/src/radiopirate/lib/python2.7/site-packages/django_static-1.5.5-py2.7.egg/django_static/templatetags/django_static.py", line 555, in _static_file
   codecs.open(new_filepath, 'w', 'utf-8').write(content)

 File "/usr/local/src/radiopirate/lib/python2.7/codecs.py", line 881, in open
   file = __builtin__.open(filename, mode, buffering)

IOError: [Errno 13] Permission denied: u'/var/lib/radiopirate/cache/css/blueprint/screen.1318293843.css'

the symlink point to elsewhere and is not writable by gunicorn user

jeanlucmongrain commented 12 years ago

I found it, it is caused when you first have this:

{% staticfile "abc.js" %}

and replace it after template had been rendered by:

{% slimfile "abc.js" %}

fix:

https://github.com/bclermont/django-static/commit/fb21cb00b167cb7d5ee1d70e8e6229e356639638