Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 11 additions & 81 deletions mediafixtures/management/commands/use_media_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,23 @@
import os
import sys
import shutil
from shutil import Error, WindowsError, copy2, copystat

from distutils import dir_util
from django.conf import settings
from django.utils.importlib import import_module
from django.core.management.base import NoArgsCommand
from django.core.management import base as management

class Command(NoArgsCommand):

class Command(management.AppCommand):
"""
Command that copies the media-fixtures from "fixtures/media/" form every app
to the media/ directory of the project.
Copy files from "fixtures/media/" directory of the given applications in to
the directory specified by the settings.MEDIA_ROOT.

Usefull alongside initial_data.* fixtures, so that you can have DB-fixtures
and matching media-file fixtures.
"""

help = "TODO"

def handle(self, *args, **options):
self.apps = []

print "\nSearch for apps with media fixtures..."

apps = settings.INSTALLED_APPS
for app in apps:
mod = import_module(app)
mod_path = os.path.dirname(mod.__file__)
location = os.path.join(mod_path, 'fixtures', 'media')

if os.path.isdir(location):
print " - " + app
copytree(location, settings.MEDIA_ROOT)

print ""


def copytree(src, dst, symlinks = False, ignore = None):
"""
Copied from Python 2.6 to add the following line:

if not os.path.isdir(dst):

this way, we can copy/overwrite existing directories. Very important here

If anyone knows a better way, say it
"""
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()

if not os.path.isdir(dst):
os.makedirs(dst)

errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks, ignore)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.extend((src, dst, str(why)))
if errors:
raise Error, errors





def handle_app(self, app, **options):
path = os.path.join(os.path.dirname(app.__file__), 'fixtures', 'media')

if os.path.isdir(path):
dir_util.copy_tree(path, settings.MEDIA_ROOT)
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from setuptools import setup, find_packages


setup(
name='mediafixtures',
version='1.0',
description='Collects media-files as fixtures from django apps.',
long_description=open('README.rst').read(),
author='Leander Hanwald',
author_email='leander@hanwald.de',
url='http://github.com/gamemine/mediafixtures/',
license='MIT',
packages=find_packages(),
include_package_data=True,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)