Hi,
I have been bashing my head trying to work this out. This code creates
the new theme fine:

[EMAIL PROTECTED]:~/Web/django_projects/webtheme$ export
DJANGO_SETTINGS_MODULE DJANGO_SETTINGS_MODULE=webtheme.settings
[EMAIL PROTECTED]:~/Web/django_projects/webtheme$ django-admin.py shell
Python 2.5.2 (r252:60911, May  7 2008, 15:19:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from webtheme import settings
>>> from webtheme.thememaker import themegen
>>> from webtheme.thememaker.models import Theme
>>> bundle = Theme.objects.get(pk=126)
>>> proccessor = themegen.ProcessTarball(bundle.themename, 
>>> bundle.get_userfile_filename())
>>> proccessor.process_tarball()
>>>

but this code in views.py does not:

# Local imports
from webtheme import settings
from webtheme.thememaker import themegen
from webtheme.thememaker.models import Theme

def process(request, theme_id):
    try:
       # hit the database for the object
       bundle = Theme.objects.get(pk=theme_id)
       # instantiate the processor class
       proccessor = themegen.ProcessTarball(bundle.themename,
bundle.get_userfile_filename())
       # let fly !
       proccessor.process_tarball()
       # redirect to the download
       return HttpResponseRedirect(settings.MEDIA_URL + 'themes/download/')
    except Theme.DoesNotExist:
       raise Http404

I *think* this might have something to do with permissions as the user
ian runs the django shell but www-data the apache process. i have
changed permissions and ownership but still no joy. Any ideas would be
great (themegen.py is attached)
-- 
http://ianlawrence.info

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

# Python imports
import os
import tarfile
from shutil import copyfileobj, rmtree, copytree, copyfile
import time
from tempfile import mkdtemp
import pdb
# Django imports
from django.http import HttpResponse, HttpResponseServerError
# Local imports 
from webtheme import settings

class ProcessTarball(object):
     #==== utility variable =========
     path = os.path
     
     #==== Pass in the theme name and the modifications to __init__ ====================               
     def __init__(self, themename = "Customized",  userfile = None):
          self.userfile = userfile
          self.themename = themename

     #==== Auxiliary Funcs ====================
     def _which_to_update(self, newfiles, dir):
        """Returns which files to update, ie. those images that exist in the
        original theme AND in the custom tarball."""
    
        oldfiles = set(os.listdir(self.path.join(dir, settings._IMAGES_DIR)))
    
        return oldfiles.intersection(set(newfiles))

     def _clean_path(self):
      """Delete all entries from 'path' that are older than 'lifecycle' seconds"""
    
      oldpwd = os.getcwd()
      cleanpath = settings._THEME_TMP
      lifecycle = settings._LIFECYCLE
      os.chdir(cleanpath)
    
      threshold = time.time() - lifecycle
    
      for entry in os.listdir("."):
          if self.path.getctime(entry) < threshold:
              try:
                  rmtree(entry)
              except OSError: 
                  os.remove(entry)
    
      os.chdir(oldpwd)

      #==== Theme Customization Funcs ===========

     def _update_name(self, themename, dir):
      fp = file(self.path.join(dir, settings._EDC_FILE), "r+")

      edc_contents = fp.read()

      fp.seek(0)

      fp.write('#define THEME_NAME "%s"\n' % themename)
      fp.write(edc_contents)

      fp.close()

     def _update_images(self, userfile, dir):
      """Cherry-picks the soon to be updated images from the received tarball
      and override those in dir"""
      try:
          file = open(userfile, 'r')
          custom_tgz = tarfile.open("", "r", file)
      except:
                  userfile.close()
                  return None

      # What did the user sent to us?
      custom_files = [self.path.basename(a) for a in custom_tgz.getnames()]

      # We'll extract those, and only those, images that exist in the original
      # theme and were provided by the user in his tarball
      files_to_update = self._which_to_update(custom_files, dir)

      img_path = self.path.join(dir, settings._IMAGES_DIR)

      skipped = set()

      for fn in files_to_update:
          # Get a TarInfo object that represents "fn"
          member = custom_tgz.getmember(self.path.join(settings._IMAGES_DIR, fn))

          # We'll refuse to extract anything that looks too big. Watch for
          # huge malicious files that could eat up space in our server.
          if member.size > settings._MAXFILESIZE:
              skipped.add(fn)
          else:
              # Note that extracting our files this way we are not subject
              # to absolute or ../.. paths in the untrusted tarball
              fp = open(self.path.join(img_path, fn), "wb")
              try:
                  copyfileobj(custom_tgz.extractfile(member), fp)
              except:
                  skipped.add(fn)
              fp.close()

      custom_tgz.close()
     # file.close()

      return files_to_update.difference(skipped)
    
     #==== Public Functions ====================

     def process_tarball(self):
        try: 
            if not self.userfile:
               return HttpResponseServerError('No user file')
    
            # Remove old build trees
            self._clean_path()
    
            # Generate a new one
            tmp_dir = mkdtemp(prefix="", dir=settings._THEME_TMP)
            tmp_hash = self.path.basename(tmp_dir)
            build_dir = self.path.join(tmp_dir, "build")
            copytree(settings._THEME_PATH, build_dir)
    
            # Replace images with those in the user overlay
            
            updated_files = self._update_images(self.userfile, build_dir)
    
            # Patch EDC file with new theme name
            self._update_name(self.themename, build_dir)
    
            if not updated_files:
                return HttpResponseServerError('No update file')
    
            # Assemble theme name that will match our build_dir hash
            edj_name = tmp_hash + settings._EDJ_SUFFIX
    
            # Re-compile the theme
            rc = os.system("cd %s; PATH=$PATH:%s edje_cc %s %s > %s 2>&1" %
                           (build_dir, settings._EDJE_PATH, settings._EDC_FILE, edj_name, settings._EDJE_CC_LOG))
            if rc:
                return HttpResponseServerError('Unable to compile')
    
    
            # clean-up
            self._clean_path()
     
            # Copy new theme to PUBLIC_PATH
            try:
                copyfile(self.path.join(build_dir, edj_name),
                         self.path.join(settings._PUBLIC_PATH, edj_name))
            except:
                return HttpResponseServerError('Unable to compile')

        except:
             return HttpResponseServerError('Server Error')

Reply via email to