Skip to content

Instantly share code, notes, and snippets.

@roadsideseb
Created January 24, 2013 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roadsideseb/4621178 to your computer and use it in GitHub Desktop.
Save roadsideseb/4621178 to your computer and use it in GitHub Desktop.
This is an unfinished managment command that I use when customising one of Oscar's core apps. It simply copies over all the files of the app (excluding subdirectories) and leaves the rest to you. I'd like to improve it at some point and make it part of Oscar but I didn't have the time for that yet. The command in action might look like this: ```…
import os
import imp
import shutil
import pkgutil
import logging
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from oscar import get_core_apps
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('oscar_override_app')
class Command(BaseCommand):
args = '<oscar_app> <your_app>'
help = 'For testing the content of order emails'
option_list = BaseCommand.option_list + (
make_option('--copy-migrations',
action='store_true',
dest='copy_migrations',
default=False,
help='Copies the migrations from Oscar instead of creating '
'generating an initial migration.'),
#TODO: need additional options
# 1) overwrite existing files in specified "new_app"
# 2) recursively add modules included int he package
)
def handle(self, *args, **options):
try:
oscar_app, new_app = args
except ValueError:
raise CommandError(
'Overwriting an app requires an app and a target name'
)
oscar_app = 'oscar.apps.%s' % oscar_app
if oscar_app not in get_core_apps():
raise CommandError(
'Oscar app %s could not be found' % oscar_app
)
parts = new_app.split('.')
path = None
for part in parts:
try:
f, path, desc = imp.find_module(part, path and [path] or None)
except ImportError:
path = os.path.join(path, part)
if not os.path.exists(path):
os.mkdir(path)
# touch the init
init = os.path.join(path, '__init__.py')
if not os.path.exists(init):
open(init, 'w').close()
new_app_path = path
oscar, module = oscar_app.split('.', 1)
f, path, desc = imp.find_module('oscar', None)
path = os.path.join(path, module.replace('.', '/'))
for loader, name, ispkg in pkgutil.walk_packages([path]):
if ispkg:
#TODO this need to handle sub-packages at some point
logger.info("skipping package '%s'", name)
continue
if name == "abstract_models":
continue
oscar_file = '%s/%s.py' % (path, name)
new_file = '%s/%s.py' % (new_app_path, name)
if not os.path.exists(new_file):
shutil.copy(oscar_file, new_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment