Skip to content

Instantly share code, notes, and snippets.

@xxl007
Forked from roadsideseb/oscar_override_app.py
Last active August 29, 2015 14:06
Show Gist options
  • Save xxl007/4d64d2490e7118c2b536 to your computer and use it in GitHub Desktop.
Save xxl007/4d64d2490e7118c2b536 to your computer and use it in GitHub Desktop.
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