Esempio n. 1
0
  def extra_context(reportclass, request, *args, **kwargs):
    try: constraint = int(request.session['constraint'])
    except: constraint = 15

    # Synchronize the scenario table with the settings
    Scenario.syncWithSettings()

    # Loop over all fixtures of all apps and directories
    fixtures = set()
    folders = list(settings.FIXTURE_DIRS)
    for app in get_apps():
      if app.__name__.startswith('django'): continue
      folders.append(os.path.join(os.path.dirname(app.__file__), 'fixtures'))
    for f in folders:
      try:
        for root, dirs, files in os.walk(f):
          for i in files:
            if i.endswith('.json'):
              fixtures.add(i.split('.')[0])
      except:
        pass # Silently ignore failures
    fixtures = sorted(fixtures)

    # Send to template
    return {'capacityconstrained': constraint & 4,
            'materialconstrained': constraint & 2,
            'leadtimeconstrained': constraint & 1,
            'fenceconstrained': constraint & 8,
            'scenarios': Scenario.objects.all(),
            'fixtures': fixtures,
            'openbravo': 'freppledb.openbravo' in settings.INSTALLED_APPS,
            'openerp': 'freppledb.openerp' in settings.INSTALLED_APPS,
            }
Esempio n. 2
0
  def extra_context(reportclass, request, *args, **kwargs):
    try: constraint = int(request.session['constraint'])
    except: constraint = 15

    # Synchronize the scenario table with the settings
    Scenario.syncWithSettings()

    # Loop over all fixtures of all apps and directories
    fixtures = set()
    folders = list(settings.FIXTURE_DIRS)
    for app in get_apps():
      if app.__name__.startswith('django'): continue
      folders.append(os.path.join(os.path.dirname(app.__file__), 'fixtures'))
    for f in folders:
      try:
        for root, dirs, files in os.walk(f):
          for i in files:
            if i.endswith('.json'):
              fixtures.add(i.split('.')[0])
      except:
        pass # Silently ignore failures
    fixtures = sorted(fixtures)

    # Send to template
    return {'capacityconstrained': constraint & 4,
            'materialconstrained': constraint & 2,
            'leadtimeconstrained': constraint & 1,
            'fenceconstrained': constraint & 8,
            'scenarios': Scenario.objects.all(),
            'fixtures': fixtures,
            'openbravo': 'freppledb.openbravo' in settings.INSTALLED_APPS,
            'openerp': 'freppledb.openerp' in settings.INSTALLED_APPS,
            }
Esempio n. 3
0
  def handle(self, *args, **options):
    # Make sure the debug flag is not set!
    # When it is set, the django database wrapper collects a list of all sql
    # statements executed and their timings. This consumes plenty of memory
    # and cpu time.
    tmp_debug = settings.DEBUG
    settings.DEBUG = False

    # Pick up options
    if 'force' in options: force = options['force']
    else: force = False
    test = 'FREPPLE_TEST' in os.environ
    if 'user' in options and options['user']:
      try: user = User.objects.all().get(username=options['user'])
      except: raise CommandError("User '%s' not found" % options['user'] )
    else:
      user = None

    # Initialize the task
    now = datetime.now()
    task = None
    if 'task' in options and options['task']:
      try: task = Task.objects.all().get(pk=options['task'])
      except: raise CommandError("Task identifier not found")
      if task.started or task.finished or task.status != "Waiting" or task.name != 'copy scenario':
        raise CommandError("Invalid task identifier")
      task.status = '0%'
      task.started = now
    else:
      task = Task(name='copy scenario', submitted=now, started=now, status='0%', user=user)
    task.save()

    # Synchronize the scenario table with the settings
    Scenario.syncWithSettings()

    # Validate the arguments
    destinationscenario = None
    try:
      if len(args) != 2:
        raise CommandError("Command takes exactly 2 arguments.")
      task.arguments = "%s %s" % (args[0], args[1])
      task.save()
      source = args[0]
      try:
        sourcescenario = Scenario.objects.get(pk=source)
      except:
        raise CommandError("No source database defined with name '%s'" % source)
      destination = args[1]
      try:
        destinationscenario = Scenario.objects.get(pk=destination)
      except:
        raise CommandError("No destination database defined with name '%s'" % destination)
      if source == destination:
        raise CommandError("Can't copy a schema on itself")
      if settings.DATABASES[source]['ENGINE'] != settings.DATABASES[destination]['ENGINE']:
        raise CommandError("Source and destination scenarios have a different engine")
      if sourcescenario.status != u'In use':
        raise CommandError("Source scenario is not in use")
      if destinationscenario.status != u'Free' and not force:
        raise CommandError("Destination scenario is not free")

      # Logging message - always logging in the default database
      destinationscenario.status = u'Busy'
      destinationscenario.save()

      # Copying the data
      if settings.DATABASES[source]['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
        ret = os.system("pg_dump -c -U%s -Fp %s%s%s | psql -U%s %s%s%s" % (
          settings.DATABASES[source]['USER'],
          settings.DATABASES[source]['HOST'] and ("-h %s " % settings.DATABASES[source]['HOST']) or '',
          settings.DATABASES[source]['PORT'] and ("-p %s " % settings.DATABASES[source]['PORT']) or '',
          test and settings.DATABASES[source]['TEST_NAME'] or settings.DATABASES[source]['NAME'],
          settings.DATABASES[destination]['USER'],
          settings.DATABASES[destination]['HOST'] and ("-h %s " % settings.DATABASES[destination]['HOST']) or '',
          settings.DATABASES[destination]['PORT'] and ("-p %s " % settings.DATABASES[destination]['PORT']) or '',
          test and settings.DATABASES[destination]['TEST_NAME'] or settings.DATABASES[destination]['NAME'],
          ))
        if ret: raise Exception('Exit code of the database copy command is %d' % ret)
      elif settings.DATABASES[source]['ENGINE'] == 'django.db.backends.sqlite3':
        # A plain copy of the database file
        if test:
          shutil.copy2(settings.DATABASES[source]['TEST_NAME'], settings.DATABASES[destination]['TEST_NAME'])
        else:
          shutil.copy2(settings.DATABASES[source]['NAME'], settings.DATABASES[destination]['NAME'])
      elif settings.DATABASES[source]['ENGINE'] == 'django.db.backends.mysql':
        ret = os.system("mysqldump %s --password=%s --user=%s %s%s--quick --compress --extended-insert --add-drop-table | mysql %s --password=%s --user=%s %s%s" % (
          test and settings.DATABASES[source]['TEST_NAME'] or settings.DATABASES[source]['NAME'],
          settings.DATABASES[source]['PASSWORD'],
          settings.DATABASES[source]['USER'],
          settings.DATABASES[source]['HOST'] and ("--host=%s " % settings.DATABASES[source]['HOST']) or '',
          settings.DATABASES[source]['PORT'] and ("--port=%s " % settings.DATABASES[source]['PORT']) or '',
          test and settings.DATABASES[destination]['TEST_NAME'] or settings.DATABASES[destination]['NAME'],
          settings.DATABASES[destination]['PASSWORD'],
          settings.DATABASES[destination]['USER'],
          settings.DATABASES[destination]['HOST'] and ("--host=%s " % settings.DATABASES[destination]['HOST']) or '',
          settings.DATABASES[destination]['PORT'] and ("--port=%s " % settings.DATABASES[destination]['PORT']) or '',
          ))
        if ret: raise Exception('Exit code of the database copy command is %d' % ret)
      elif settings.DATABASES[source]['ENGINE'] == 'django.db.backends.oracle':
        try:
          try: os.unlink(os.path.join(settings.FREPPLE_LOGDIR,'frepple.dmp'))
          except: pass
          ret = os.system("expdp %s/%s@//%s:%s/%s schemas=%s directory=frepple_logdir nologfile=Y dumpfile=frepple.dmp" % (
            test and settings.DATABASES[source]['TEST_USER'] or settings.DATABASES[source]['USER'],
            settings.DATABASES[source]['PASSWORD'],
            settings.DATABASES[source]['HOST'] or 'localhost',
            settings.DATABASES[source]['PORT'] or '1521',
            test and settings.DATABASES[source]['TEST_NAME'] or settings.DATABASES[source]['NAME'],
            test and settings.DATABASES[source]['TEST_USER'] or settings.DATABASES[source]['USER'],
            ))
          if ret: raise Exception('Exit code of the database export command is %d' % ret)
          ret = os.system("impdp %s/%s@//%s:%s/%s remap_schema=%s:%s table_exists_action=replace directory=frepple_logdir nologfile=Y dumpfile=frepple.dmp" % (
            test and settings.DATABASES[destination]['TEST_USER'] or settings.DATABASES[destination]['USER'],
            settings.DATABASES[destination]['PASSWORD'],
            settings.DATABASES[destination]['HOST'],
            settings.DATABASES[destination]['PORT'],
            test and settings.DATABASES[destination]['TEST_NAME'] or settings.DATABASES[destination]['NAME'],
            test and settings.DATABASES[source]['TEST_USER'] or settings.DATABASES[source]['USER'],
            test and settings.DATABASES[destination]['TEST_USER'] or settings.DATABASES[destination]['USER'],
            ))
          if ret: raise Exception('Exit code of the database import command is %d' % ret)
        finally:
          try: os.unlink(os.path.join(settings.FREPPLE_LOGDIR,'frepple.dmp'))
          except: pass
      else:
        raise Exception('Copy command not supported for database engine %s' % settings.DATABASES[source]['ENGINE'])

      # Update the scenario table
      destinationscenario.status = 'In use'
      destinationscenario.lastrefresh = datetime.today()
      if 'description' in options:
        destinationscenario.description = options['description']
      else:
        destinationscenario.description = "Copied from scenario '%s'" % source
      destinationscenario.save()

      # Logging message
      task.status = 'Done'
      task.finished = datetime.now()

    except Exception as e:
      if task:
        task.status = 'Failed'
        task.message = '%s' % e
        task.finished = datetime.now()
      if destinationscenario and destinationscenario.status == u'Busy':
        destinationscenario.status = u'Free'
        destinationscenario.save()
      raise e

    finally:
      if task: task.save()
      settings.DEBUG = tmp_debug
Esempio n. 4
0
  def handle(self, *args, **options):
    # Make sure the debug flag is not set!
    # When it is set, the django database wrapper collects a list of all sql
    # statements executed and their timings. This consumes plenty of memory
    # and cpu time.
    tmp_debug = settings.DEBUG
    settings.DEBUG = False

    # Pick up options
    if 'force' in options:
      force = options['force']
    else:
      force = False
    test = 'FREPPLE_TEST' in os.environ
    if 'user' in options and options['user']:
      try:
        user = User.objects.all().get(username=options['user'])
      except:
        raise CommandError("User '%s' not found" % options['user'] )
    else:
      user = None

    # Initialize the task
    now = datetime.now()
    task = None
    if 'task' in options and options['task']:
      try:
        task = Task.objects.all().get(pk=options['task'])
      except:
        raise CommandError("Task identifier not found")
      if task.started or task.finished or task.status != "Waiting" or task.name != 'copy scenario':
        raise CommandError("Invalid task identifier")
      task.status = '0%'
      task.started = now
    else:
      task = Task(name='copy scenario', submitted=now, started=now, status='0%', user=user)
    task.save()

    # Synchronize the scenario table with the settings
    Scenario.syncWithSettings()

    # Validate the arguments
    destinationscenario = None
    try:
      if len(args) != 2:
        raise CommandError("Command takes exactly 2 arguments.")
      task.arguments = "%s %s" % (args[0], args[1])
      task.save()
      source = args[0]
      try:
        sourcescenario = Scenario.objects.get(pk=source)
      except:
        raise CommandError("No source database defined with name '%s'" % source)
      destination = args[1]
      try:
        destinationscenario = Scenario.objects.get(pk=destination)
      except:
        raise CommandError("No destination database defined with name '%s'" % destination)
      if source == destination:
        raise CommandError("Can't copy a schema on itself")
      if settings.DATABASES[source]['ENGINE'] != settings.DATABASES[destination]['ENGINE']:
        raise CommandError("Source and destination scenarios have a different engine")
      if sourcescenario.status != 'In use':
        raise CommandError("Source scenario is not in use")
      if destinationscenario.status != 'Free' and not force:
        raise CommandError("Destination scenario is not free")

      # Logging message - always logging in the default database
      destinationscenario.status = 'Busy'
      destinationscenario.save()

      # Copying the data
      if settings.DATABASES[source]['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
        # Commenting the next line is a little more secure, but requires you to create a .pgpass file.
        os.environ['PGPASSWORD'] = settings.DATABASES[source]['PASSWORD']
        ret = os.system("pg_dump -c -U%s -Fp %s%s%s | psql -U%s %s%s%s" % (
          settings.DATABASES[source]['USER'],
          settings.DATABASES[source]['HOST'] and ("-h %s " % settings.DATABASES[source]['HOST']) or '',
          settings.DATABASES[source]['PORT'] and ("-p %s " % settings.DATABASES[source]['PORT']) or '',
          test and settings.DATABASES[source]['TEST']['NAME'] or settings.DATABASES[source]['NAME'],
          settings.DATABASES[destination]['USER'],
          settings.DATABASES[destination]['HOST'] and ("-h %s " % settings.DATABASES[destination]['HOST']) or '',
          settings.DATABASES[destination]['PORT'] and ("-p %s " % settings.DATABASES[destination]['PORT']) or '',
          test and settings.DATABASES[destination]['TEST']['NAME'] or settings.DATABASES[destination]['NAME'],
          ))
        if ret:
          raise Exception('Exit code of the database copy command is %d' % ret)
      elif settings.DATABASES[source]['ENGINE'] == 'django.db.backends.sqlite3':
        # A plain copy of the database file
        if test:
          shutil.copy2(settings.DATABASES[source]['TEST']['NAME'], settings.DATABASES[destination]['TEST']['NAME'])
        else:
          shutil.copy2(settings.DATABASES[source]['NAME'], settings.DATABASES[destination]['NAME'])
      else:
        raise Exception('Copy command not supported for database engine %s' % settings.DATABASES[source]['ENGINE'])

      # Update the scenario table
      destinationscenario.status = 'In use'
      destinationscenario.lastrefresh = datetime.today()
      if 'description' in options:
        destinationscenario.description = options['description']
      else:
        destinationscenario.description = "Copied from scenario '%s'" % source
      destinationscenario.save()

      # Logging message
      task.status = 'Done'
      task.finished = datetime.now()

    except Exception as e:
      if task:
        task.status = 'Failed'
        task.message = '%s' % e
        task.finished = datetime.now()
      if destinationscenario and destinationscenario.status == 'Busy':
        destinationscenario.status = 'Free'
        destinationscenario.save()
      raise e

    finally:
      if task:
        task.save()
      settings.DEBUG = tmp_debug
Esempio n. 5
0
    def handle(self, *args, **options):
        # Make sure the debug flag is not set!
        # When it is set, the django database wrapper collects a list of all sql
        # statements executed and their timings. This consumes plenty of memory
        # and cpu time.
        tmp_debug = settings.DEBUG
        settings.DEBUG = False

        # Pick up options
        if 'force' in options: force = options['force']
        else: force = False
        test = 'FREPPLE_TEST' in os.environ
        if 'user' in options and options['user']:
            try:
                user = User.objects.all().get(username=options['user'])
            except:
                raise CommandError("User '%s' not found" % options['user'])
        else:
            user = None

        # Initialize the task
        now = datetime.now()
        task = None
        if 'task' in options and options['task']:
            try:
                task = Task.objects.all().get(pk=options['task'])
            except:
                raise CommandError("Task identifier not found")
            if task.started or task.finished or task.status != "Waiting" or task.name != 'copy scenario':
                raise CommandError("Invalid task identifier")
            task.status = '0%'
            task.started = now
        else:
            task = Task(name='copy scenario',
                        submitted=now,
                        started=now,
                        status='0%',
                        user=user)
        task.save()

        # Synchronize the scenario table with the settings
        Scenario.syncWithSettings()

        # Validate the arguments
        destinationscenario = None
        try:
            if len(args) != 2:
                raise CommandError("Command takes exactly 2 arguments.")
            task.arguments = "%s %s" % (args[0], args[1])
            task.save()
            source = args[0]
            try:
                sourcescenario = Scenario.objects.get(pk=source)
            except:
                raise CommandError(
                    "No source database defined with name '%s'" % source)
            destination = args[1]
            try:
                destinationscenario = Scenario.objects.get(pk=destination)
            except:
                raise CommandError(
                    "No destination database defined with name '%s'" %
                    destination)
            if source == destination:
                raise CommandError("Can't copy a schema on itself")
            if settings.DATABASES[source]['ENGINE'] != settings.DATABASES[
                    destination]['ENGINE']:
                raise CommandError(
                    "Source and destination scenarios have a different engine")
            if sourcescenario.status != u'In use':
                raise CommandError("Source scenario is not in use")
            if destinationscenario.status != u'Free' and not force:
                raise CommandError("Destination scenario is not free")

            # Logging message - always logging in the default database
            destinationscenario.status = u'Busy'
            destinationscenario.save()

            # Copying the data
            if settings.DATABASES[source][
                    'ENGINE'] == 'django.db.backends.postgresql_psycopg2':
                ret = os.system(
                    "pg_dump -c -U%s -Fp %s%s%s | psql -U%s %s%s%s" % (
                        settings.DATABASES[source]['USER'],
                        settings.DATABASES[source]['HOST'] and
                        ("-h %s " % settings.DATABASES[source]['HOST']) or '',
                        settings.DATABASES[source]['PORT'] and
                        ("-p %s " % settings.DATABASES[source]['PORT']) or '',
                        test and settings.DATABASES[source]['TEST_NAME']
                        or settings.DATABASES[source]['NAME'],
                        settings.DATABASES[destination]['USER'],
                        settings.DATABASES[destination]['HOST'] and
                        ("-h %s " % settings.DATABASES[destination]['HOST'])
                        or '',
                        settings.DATABASES[destination]['PORT'] and
                        ("-p %s " % settings.DATABASES[destination]['PORT'])
                        or '',
                        test and settings.DATABASES[destination]['TEST_NAME']
                        or settings.DATABASES[destination]['NAME'],
                    ))
                if ret:
                    raise Exception(
                        'Exit code of the database copy command is %d' % ret)
            elif settings.DATABASES[source][
                    'ENGINE'] == 'django.db.backends.sqlite3':
                # A plain copy of the database file
                if test:
                    shutil.copy2(settings.DATABASES[source]['TEST_NAME'],
                                 settings.DATABASES[destination]['TEST_NAME'])
                else:
                    shutil.copy2(settings.DATABASES[source]['NAME'],
                                 settings.DATABASES[destination]['NAME'])
            elif settings.DATABASES[source][
                    'ENGINE'] == 'django.db.backends.mysql':
                ret = os.system(
                    "mysqldump %s --password=%s --user=%s %s%s--quick --compress --extended-insert --add-drop-table | mysql %s --password=%s --user=%s %s%s"
                    % (
                        test and settings.DATABASES[source]['TEST_NAME']
                        or settings.DATABASES[source]['NAME'],
                        settings.DATABASES[source]['PASSWORD'],
                        settings.DATABASES[source]['USER'],
                        settings.DATABASES[source]['HOST'] and
                        ("--host=%s " % settings.DATABASES[source]['HOST'])
                        or '',
                        settings.DATABASES[source]['PORT'] and
                        ("--port=%s " % settings.DATABASES[source]['PORT'])
                        or '',
                        test and settings.DATABASES[destination]['TEST_NAME']
                        or settings.DATABASES[destination]['NAME'],
                        settings.DATABASES[destination]['PASSWORD'],
                        settings.DATABASES[destination]['USER'],
                        settings.DATABASES[destination]['HOST'] and
                        ("--host=%s " %
                         settings.DATABASES[destination]['HOST']) or '',
                        settings.DATABASES[destination]['PORT'] and
                        ("--port=%s " %
                         settings.DATABASES[destination]['PORT']) or '',
                    ))
                if ret:
                    raise Exception(
                        'Exit code of the database copy command is %d' % ret)
            elif settings.DATABASES[source][
                    'ENGINE'] == 'django.db.backends.oracle':
                try:
                    try:
                        os.unlink(
                            os.path.join(settings.FREPPLE_LOGDIR,
                                         'frepple.dmp'))
                    except:
                        pass
                    ret = os.system(
                        "expdp %s/%s@//%s:%s/%s schemas=%s directory=frepple_logdir nologfile=Y dumpfile=frepple.dmp"
                        % (
                            test and settings.DATABASES[source]['TEST_USER']
                            or settings.DATABASES[source]['USER'],
                            settings.DATABASES[source]['PASSWORD'],
                            settings.DATABASES[source]['HOST'] or 'localhost',
                            settings.DATABASES[source]['PORT'] or '1521',
                            test and settings.DATABASES[source]['TEST_NAME']
                            or settings.DATABASES[source]['NAME'],
                            test and settings.DATABASES[source]['TEST_USER']
                            or settings.DATABASES[source]['USER'],
                        ))
                    if ret:
                        raise Exception(
                            'Exit code of the database export command is %d' %
                            ret)
                    ret = os.system(
                        "impdp %s/%s@//%s:%s/%s remap_schema=%s:%s table_exists_action=replace directory=frepple_logdir nologfile=Y dumpfile=frepple.dmp"
                        % (
                            test
                            and settings.DATABASES[destination]['TEST_USER']
                            or settings.DATABASES[destination]['USER'],
                            settings.DATABASES[destination]['PASSWORD'],
                            settings.DATABASES[destination]['HOST'],
                            settings.DATABASES[destination]['PORT'],
                            test
                            and settings.DATABASES[destination]['TEST_NAME']
                            or settings.DATABASES[destination]['NAME'],
                            test and settings.DATABASES[source]['TEST_USER']
                            or settings.DATABASES[source]['USER'],
                            test
                            and settings.DATABASES[destination]['TEST_USER']
                            or settings.DATABASES[destination]['USER'],
                        ))
                    if ret:
                        raise Exception(
                            'Exit code of the database import command is %d' %
                            ret)
                finally:
                    try:
                        os.unlink(
                            os.path.join(settings.FREPPLE_LOGDIR,
                                         'frepple.dmp'))
                    except:
                        pass
            else:
                raise Exception(
                    'Copy command not supported for database engine %s' %
                    settings.DATABASES[source]['ENGINE'])

            # Update the scenario table
            destinationscenario.status = 'In use'
            destinationscenario.lastrefresh = datetime.today()
            if 'description' in options:
                destinationscenario.description = options['description']
            else:
                destinationscenario.description = "Copied from scenario '%s'" % source
            destinationscenario.save()

            # Logging message
            task.status = 'Done'
            task.finished = datetime.now()

        except Exception as e:
            if task:
                task.status = 'Failed'
                task.message = '%s' % e
                task.finished = datetime.now()
            if destinationscenario and destinationscenario.status == u'Busy':
                destinationscenario.status = u'Free'
                destinationscenario.save()
            raise e

        finally:
            if task: task.save()
            settings.DEBUG = tmp_debug