Package madrona :: Package common :: Package management :: Package commands :: Module install_media
[hide private]

Source Code for Module madrona.common.management.commands.install_media

  1  from django.core.management.base import BaseCommand, CommandError 
  2  from django.conf import settings 
  3  from optparse import make_option 
  4  import os 
  5  import sys 
  6  import glob 
  7  import shutil 
  8  try: 
  9      set 
 10  except NameError: 
 11      from sets import Set as set # Python 2.3 fallback 
 12   
13 -class Command(BaseCommand):
14 media_dirs = ['media'] 15 ignore_apps = ['django.contrib.admin'] 16 exclude = ['CVS', '.*', '*~'] 17 option_list = BaseCommand.option_list + ( 18 make_option('--media-root', default=settings.MEDIA_ROOT, dest='media_root', metavar='DIR', 19 help="Specifies the root directory in which to collect media files."), 20 make_option('-n', '--dry-run', action='store_true', dest='dry_run', 21 help="Do everything except modify the filesystem."), 22 make_option('-a', '--admin', action='store_true', dest='admin', 23 help="Include django.contrib.admin static media files."), 24 make_option('-d', '--dir', action='append', default=media_dirs, dest='media_dirs', metavar='NAME', 25 help="Specifies the name of the media directory to look for in each app."), 26 make_option('-e', '--exclude', action='append', default=exclude, dest='exclude', metavar='PATTERNS', 27 help="A space-delimited list of glob-style patterns to ignore. Use multiple times to add more."), 28 make_option('-f', '--force', action='store_true', dest='force_compress', 29 help="Force django-compress to re-create the compressed media files."), 30 make_option('-l', '--link', action='store_true', dest='link', 31 help="Create a symbolic link to each file instead of copying.") 32 ) 33 help = 'Collect media files into a single media directory.' 34
35 - def handle(self, *app_labels, **options):
36 self.dry_run = options.get('dry_run', False) 37 self.include_admin = options.get('admin', False) 38 self.media_root = options.get('media_root', settings.MEDIA_ROOT) 39 self.force_compress = options.get('force_compress', False) 40 41 madrona_media_dir = self.get_madrona_dir() 42 project_media_dir = self.get_project_dir() 43 admin_media_dir = self.get_admin_dir() 44 45 if self.dry_run: 46 print " DRY RUN! NO FILES WILL BE MODIFIED." 47 48 if os.path.abspath(os.path.realpath(madrona_media_dir)) == os.path.abspath(os.path.realpath(self.media_root)) or \ 49 os.path.abspath(os.path.realpath(project_media_dir)) == os.path.abspath(os.path.realpath(self.media_root)): 50 raise Exception("Your MEDIA_ROOT setting has to be a directory other than your madrona or project media folder!") 51 52 if self.include_admin: 53 self.copy_media_to_root(admin_media_dir) 54 self.copy_media_to_root(madrona_media_dir) 55 self.copy_media_to_root(project_media_dir) 56 57 self.compile_media() 58 59 self.remove_uncompressed_media() 60 61 self.change_mediaroot_owner() 62 63 if settings.AWS_USE_S3_MEDIA: 64 self.copy_mediaroot_to_s3()
65
66 - def get_madrona_dir(self):
67 # We know madrona/media is relative to this file 68 madrona_media_dir = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),'..','..','..','media')) 69 return madrona_media_dir
70
71 - def get_project_dir(self):
72 # project media may at the same level as the project base dir 73 trydir = os.path.realpath(os.path.join(settings.BASE_DIR, '..', 'media')) 74 if not os.path.exists(trydir): 75 # ... or it may be a subdir 76 trydir = os.path.realpath(os.path.join(settings.BASE_DIR, 'media')) 77 projdir = trydir 78 return projdir
79
80 - def get_admin_dir(self):
81 # django admin is relative to django source 82 import django 83 return os.path.join(django.__path__[0], 'contrib', 'admin', 'static')
84
85 - def copy_media_to_root(self, source_dir):
86 if self.dry_run: 87 print " This would copy %s to %s" % (source_dir, self.media_root) 88 return 89 90 print " Copying %s to %s" % (source_dir, self.media_root) 91 from distutils.dir_util import copy_tree 92 copy_tree(source_dir, self.media_root) 93 94 return
95
96 - def compile_media(self):
97 if self.dry_run: 98 print " This would compile all the media assets in %s" % (self.media_root) 99 return 100 101 force_msg = '' 102 if self.force_compress: 103 force_msg = "--force" 104 105 print " Compiling media using synccompress %s" % force_msg 106 from django.core.management import call_command 107 call_command('synccompress', force=self.force_compress) 108 return
109
111 if self.dry_run: 112 print " This would remove the media assets that were alredy compiled/compressed" 113 return 114 115 print " Removing uncompressed media (not yet implemented)" 116 return
117
118 - def change_mediaroot_owner(self):
119 if self.dry_run: 120 print " This would change the ownership of MEDIA_ROOT to the WSGI_USER" 121 return 122 123 if settings.WSGI_USER: 124 print " Changing %s ownership to user '%s'" % (self.media_root, settings.WSGI_USER) 125 126 try: 127 from pwd import getpwnam 128 uid = getpwnam(settings.WSGI_USER)[2] 129 except KeyError: 130 print " **** WARNING: UID for user %s can't be found; %s ownership not changing" % \ 131 (settings.WSGI_USER, self.media_root) 132 return 133 134 try: 135 os.chown(self.media_root, uid, -1) 136 for root, dirs, files in os.walk(self.media_root): 137 for m in dirs: 138 os.chown(os.path.join(root, m), uid, -1) 139 for m in files: 140 os.chown(os.path.join(root, m), uid, -1) 141 except OSError: 142 print " **** WARNING: You don't have the permissions to change ownership of %s" % self.media_root 143 print " **** Perhaps try running the install_media command as root?" 144 print " **** OR" 145 print " **** Try 'sudo chown -R %s %s'" % (settings.WSGI_USER, self.media_root) 146 return 147 148 else: 149 print " Ownership of %s not altered (WSGI_USER not set)" % (self.media_root)
150
151 - def copy_mediaroot_to_s3(self):
152 if settings.AWS_USE_S3_MEDIA and \ 153 settings.AWS_MEDIA_BUCKET and \ 154 settings.AWS_ACCESS_KEY and \ 155 settings.AWS_SECRET_KEY: 156 pass 157 else: 158 print " AWS_USE_S3_MEDIA and associated settings are not found; Files will not be uploaded to S3" 159 return None 160 161 if self.dry_run: 162 print " This would publish all the media in %s to your S3 bucket at %s and be accessible at url %s" % \ 163 (self.media_root, settings.AWS_MEDIA_BUCKET, settings.MEDIA_URL) 164 return 165 166 if self.media_root[-1] != '/': 167 root = self.media_root + "/" 168 else: 169 root = self.media_root 170 171 from madrona.common import s3 172 173 for top, dirs, files in os.walk(root): 174 for nm in files: 175 fpath = os.path.join(top, nm) 176 key = fpath.replace(root,'') 177 print " -- Uploading %s to %s " % (fpath, key) 178 s3.upload_to_s3(fpath, key) 179 180 return
181