本文整理汇总了Python中freppledb.execute.models.Task.arguments方法的典型用法代码示例。如果您正苦于以下问题:Python Task.arguments方法的具体用法?Python Task.arguments怎么用?Python Task.arguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freppledb.execute.models.Task
的用法示例。
在下文中一共展示了Task.arguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'restore database':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='restore database', submitted=now, started=now, status='0%', user=user)
task.arguments = args and args[0] or None
task.save(using=database)
# Validate options
if not args:
raise CommandError("No dump file specified")
if not os.path.isfile(os.path.join(settings.FREPPLE_LOGDIR, args[0])):
raise CommandError("Dump file not found")
# Run the restore command
# Commenting the next line is a little more secure, but requires you to create a .pgpass file.
if settings.DATABASES[database]['PASSWORD']:
os.environ['PGPASSWORD'] = settings.DATABASES[database]['PASSWORD']
cmd = [ "psql", ]
if settings.DATABASES[database]['USER']:
cmd.append("--username=%s" % settings.DATABASES[database]['USER'])
if settings.DATABASES[database]['HOST']:
cmd.append("--host=%s" % settings.DATABASES[database]['HOST'])
if settings.DATABASES[database]['PORT']:
cmd.append("--port=%s " % settings.DATABASES[database]['PORT'])
cmd.append(settings.DATABASES[database]['NAME'])
cmd.append('<%s' % os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, args[0])))
ret = subprocess.call(cmd, shell=True) # Shell needs to be True in order to interpret the < character
if ret:
raise Exception("Run of run psql failed")
# Task update
# We need to recreate a new task record, since the previous one is lost during the restoration.
task = Task(
name='restore database', submitted=task.submitted, started=task.started,
arguments=task.arguments, status='Done', finished=datetime.now(),
user=task.user
)
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
# Commit it all, even in case of exceptions
if task:
task.save(using=database)
示例2: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
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:
#.........这里部分代码省略.........
示例3: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if not database in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
transaction.enter_transaction_management(using=database)
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'restore database':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='restore database', submitted=now, started=now, status='0%', user=user)
task.arguments = args and args[0] or None
task.save(using=database)
transaction.commit(using=database)
# Validate options
if not args:
raise CommandError("No dump file specified")
if not os.path.isfile(os.path.join(settings.FREPPLE_LOGDIR, args[0])):
raise CommandError("Dump file not found")
# Run the restore command
if settings.DATABASES[database]['ENGINE'] == 'django.db.backends.sqlite3':
# SQLITE
shutil.copy2(os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, args[0])), settings.DATABASES[database]['NAME'])
elif settings.DATABASES[database]['ENGINE'] == 'django.db.backends.mysql':
# MYSQL
cmd = [
'mysql',
'--password=%s' % settings.DATABASES[database]['PASSWORD'],
'--user=%s' % settings.DATABASES[database]['USER']
]
if settings.DATABASES[database]['HOST']:
cmd.append("--host=%s " % settings.DATABASES[database]['HOST'])
if settings.DATABASES[database]['PORT']:
cmd.append("--port=%s " % settings.DATABASES[database]['PORT'])
cmd.append(settings.DATABASES[database]['NAME'])
cmd.append('<%s' % os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, args[0])))
ret = subprocess.call(cmd, shell=True) # Shell needs to be True in order to interpret the < character
if ret:
raise Exception("Run of mysql failed")
elif settings.DATABASES[database]['ENGINE'] == 'django.db.backends.oracle':
# ORACLE
if settings.DATABASES[database]['HOST'] and settings.DATABASES[database]['PORT']:
# The setting 'NAME' contains the SID name
dsn = "%s/%[email protected]//%s:%s/%s" % (
settings.DATABASES[database]['USER'],
settings.DATABASES[database]['PASSWORD'],
settings.DATABASES[database]['HOST'],
settings.DATABASES[database]['PORT'],
settings.DATABASES[database]['NAME']
)
else:
# The setting 'NAME' contains the TNS name
dsn = "%s/%[email protected]%s" % (
settings.DATABASES[database]['USER'],
settings.DATABASES[database]['PASSWORD'],
settings.DATABASES[database]['NAME']
)
cmd = [
"impdp",
dsn,
"table_exists_action=replace",
"nologfile=Y",
"directory=frepple_logdir",
"dumpfile=%s" % args[0]
]
ret = subprocess.call(cmd)
if ret:
raise Exception("Run of impdp failed")
elif settings.DATABASES[database]['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
# POSTGRESQL
# Commenting the next line is a little more secure, but requires you to create a .pgpass file.
os.environ['PGPASSWORD'] = settings.DATABASES[database]['PASSWORD']
cmd = [ "psql", '--username=%s' % settings.DATABASES[database]['USER'] ]
if settings.DATABASES[database]['HOST']:
cmd.append("--host=%s" % settings.DATABASES[database]['HOST'])
if settings.DATABASES[database]['PORT']:
#.........这里部分代码省略.........
示例4: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if not database in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
transaction.enter_transaction_management(using=database)
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'load XML file':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='load XML file', submitted=now, started=now, status='0%', user=user)
task.arguments = ' '.join(['"%s"' % i for i in args])
task.save(using=database)
transaction.commit(using=database)
if not args:
raise CommandError("No XML input file given")
# Execute
# TODO: if frePPLe is available as a module, we don't really need to spawn another process.
os.environ['FREPPLE_HOME'] = settings.FREPPLE_HOME.replace('\\', '\\\\')
os.environ['FREPPLE_APP'] = settings.FREPPLE_APP
os.environ['FREPPLE_DATABASE'] = database
os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP
os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME
if 'DJANGO_SETTINGS_MODULE' not in os.environ.keys():
os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings'
if os.path.exists(os.path.join(os.environ['FREPPLE_HOME'], 'python27.zip')):
# For the py2exe executable
os.environ['PYTHONPATH'] = os.path.join(os.environ['FREPPLE_HOME'], 'python27.zip') + ';' + os.path.normpath(os.environ['FREPPLE_APP'])
else:
# Other executables
os.environ['PYTHONPATH'] = os.path.normpath(os.environ['FREPPLE_APP'])
cmdline = [ '"%s"' % i for i in args ]
cmdline.insert(0, 'frepple')
cmdline.append( '"%s"' % os.path.join(settings.FREPPLE_APP, 'freppledb', 'execute', 'loadxml.py') )
ret = os.system(' '.join(cmdline))
if ret:
raise Exception('Exit code of the batch run is %d' % ret)
# Task update
task.status = 'Done'
task.finished = datetime.now()
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
if task:
task.save(using=database)
try:
transaction.commit(using=database)
except:
pass
transaction.leave_transaction_management(using=database)
示例5: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **options):
# Pick up the options
now = datetime.now()
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
timestamp = now.strftime("%Y%m%d%H%M%S")
if database == DEFAULT_DB_ALIAS:
logfile = 'frepple-%s.log' % timestamp
else:
logfile = 'frepple_%s-%s.log' % (database, timestamp)
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('runplan', 'frepple_run'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.logfile = logfile
else:
task = Task(name='runplan', submitted=now, started=now, status='0%', user=user, logfile=logfile)
# Validate options
if 'constraint' in options:
constraint = int(options['constraint'])
if constraint < 0 or constraint > 15:
raise ValueError("Invalid constraint: %s" % options['constraint'])
else:
constraint = 15
if 'plantype' in options:
plantype = int(options['plantype'])
else:
plantype = 1
# Reset environment variables
# TODO avoid having to delete the environment variables. Use options directly?
PlanTaskRegistry.autodiscover()
for i in PlanTaskRegistry.reg:
if 'env' in options:
# Options specified
if i.label and i.label[0] in os.environ:
del os.environ[i.label[0]]
elif i.label:
# No options specified - default to activate them all
os.environ[i.label[0]] = '1'
# Set environment variables
if options['env']:
task.arguments = "--constraint=%d --plantype=%d --env=%s" % (constraint, plantype, options['env'])
for i in options['env'].split(','):
j = i.split('=')
if len(j) == 1:
os.environ[j[0]] = '1'
else:
os.environ[j[0]] = j[1]
else:
task.arguments = "--constraint=%d --plantype=%d" % (constraint, plantype)
if options['background']:
task.arguments += " --background"
# Log task
# Different from the other tasks the frepple engine will write the processid
task.save(using=database)
# Locate commands.py
import freppledb.common.commands
cmd = freppledb.common.commands.__file__
def setlimits():
import resource
if settings.MAXMEMORYSIZE:
resource.setrlimit(
resource.RLIMIT_AS,
(settings.MAXMEMORYSIZE * 1024 * 1024, (settings.MAXMEMORYSIZE + 10) * 1024 * 1024)
)
if settings.MAXCPUTIME:
resource.setrlimit(
resource.RLIMIT_CPU,
(settings.MAXCPUTIME, settings.MAXCPUTIME + 5)
)
# Limiting the file size is a bit tricky as this limit not only applies to the log
# file, but also to temp files during the export
#.........这里部分代码省略.........
示例6: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **options):
# Pick up the options
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if not database in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try: user = User.objects.all().using(database).get(username=options['user'])
except: raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
transaction.enter_transaction_management(managed=False, using=database)
transaction.managed(False, using=database)
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try: task = Task.objects.all().using(database).get(pk=options['task'])
except: raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'generate plan':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='generate plan', submitted=now, started=now, status='0%', user=user)
# Validate options
if 'constraint' in options:
constraint = int(options['constraint'])
if constraint < 0 or constraint > 15:
raise ValueError("Invalid constraint: %s" % options['constraint'])
else: constraint = 15
if 'plantype' in options:
plantype = int(options['plantype'])
if plantype < 1 or plantype > 2:
raise ValueError("Invalid plan type: %s" % options['plantype'])
else: plantype = 1
# Log task
task.arguments = "--constraint=%d --plantype=%d" % (constraint, plantype)
task.save(using=database)
transaction.commit(using=database)
# Locate commands.py
cmd = None
for app in settings.INSTALLED_APPS:
mod = import_module(app)
if os.path.exists(os.path.join(os.path.dirname(mod.__file__),'commands.py')):
cmd = os.path.join(os.path.dirname(mod.__file__),'commands.py')
break
if not cmd: raise Exception("Can't locate commands.py")
# Execute
os.environ['FREPPLE_PLANTYPE'] = str(plantype)
os.environ['FREPPLE_CONSTRAINT'] = str(constraint)
os.environ['FREPPLE_TASKID'] = str(task.id)
os.environ['FREPPLE_DATABASE'] = database
os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP
if os.path.isfile(os.path.join(settings.FREPPLE_HOME,'libfrepple.so')):
os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings'
if os.path.exists(os.path.join(settings.FREPPLE_HOME,'python27.zip')):
# For the py2exe executable
os.environ['PYTHONPATH'] = os.path.join(settings.FREPPLE_HOME,'python27.zip') + os.pathsep + os.path.normpath(settings.FREPPLE_APP)
else:
# Other executables
os.environ['PYTHONPATH'] = os.path.normpath(settings.FREPPLE_APP)
ret = os.system('frepple "%s"' % cmd.replace('\\','\\\\'))
if ret != 0 and ret != 2:
# Return code 0 is a successful run
# Return code is 2 is a run cancelled by a user. That's shown in the status field.
raise Exception('Failed with exit code %d' % ret)
# Task update
task.status = 'Done'
task.finished = datetime.now()
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
if task: task.save(using=database)
try: transaction.commit(using=database)
except: pass
transaction.leave_transaction_management(using=database)
示例7: LaunchTask
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def LaunchTask(request, action):
# Allow only post
if request.method != 'POST':
raise Http404('Only post requests allowed')
# Parse the posted parameters as arguments for an asynchronous task to add to the queue. TODO MAKE MODULAR WITH SEPERATE TASK CLASS
worker_database = request.database
try:
now = datetime.now()
# A
if action == 'generate plan':
constraint = 0
for value in request.POST.getlist('constraint'):
try: constraint += int(value)
except: pass
task = Task(name='generate plan', submitted=now, status='Waiting', user=request.user)
task.arguments = "--constraint=%s --plantype=%s" % (constraint, request.POST.get('plantype'))
task.save(using=request.database)
# Update the session object TODO REPLACE WITH PREFERENCE INFO
request.session['plantype'] = request.POST.get('plantype')
request.session['constraint'] = constraint
# B
elif action == 'generate model':
task = Task(name='generate model', submitted=now, status='Waiting', user=request.user)
task.arguments = "--cluster=%s --demand=%s --forecast_per_item=%s --level=%s --resource=%s " \
"--resource_size=%s --components=%s --components_per=%s --deliver_lt=%s --procure_lt=%s" % (
request.POST['clusters'], request.POST['demands'], request.POST['fcst'], request.POST['levels'],
request.POST['rsrc_number'], request.POST['rsrc_size'], request.POST['components'],
request.POST['components_per'], request.POST['deliver_lt'], request.POST['procure_lt']
)
task.save(using=request.database)
# C
elif action == 'empty database':
task = Task(name='empty database', submitted=now, status='Waiting', user=request.user)
task.save(using=request.database)
# D
elif action == 'load dataset':
task = Task(name='load dataset', submitted=now, status='Waiting', user=request.user, arguments=request.POST['datafile'])
task.save(using=request.database)
# E
elif action == 'manage scenarios':
worker_database = DEFAULT_DB_ALIAS
if 'copy' in request.POST:
source = request.POST.get('source', DEFAULT_DB_ALIAS)
for sc in Scenario.objects.all():
if request.POST.get(sc.name,'off') == 'on' and sc.status == u'Free':
task = Task(name='copy scenario', submitted=now, status='Waiting', user=request.user, arguments="%s %s" % (source, sc.name))
task.save()
elif 'release' in request.POST:
# Note: release is immediate and synchronous.
for sc in Scenario.objects.all():
if request.POST.get(sc.name,'off') == u'on' and sc.status != u'Free':
sc.status = u'Free'
sc.lastrefresh = now
sc.save()
if request.database == sc.name:
# Erasing the database that is currently selected.
request.prefix = ''
elif 'update' in request.POST:
# Note: update is immediate and synchronous.
for sc in Scenario.objects.all():
if request.POST.get(sc.name, 'off') == 'on':
sc.description = request.POST.get('description',None)
sc.save()
else:
raise Http404('Invalid scenario task')
# F
elif action == 'backup database':
task = Task(name='backup database', submitted=now, status='Waiting', user=request.user)
task.save(using=request.database)
# G
elif action == 'generate buckets':
task = Task(name='generate buckets', submitted=now, status='Waiting', user=request.user)
task.arguments = "--start=%s --end=%s --weekstart=%s" % (
request.POST['start'], request.POST['end'], request.POST['weekstart']
)
task.save(using=request.database)
# H
elif action == 'exportworkbook':
return exportWorkbook(request)
# I
elif action == 'importworkbook':
return importWorkbook(request)
# J
elif action == 'openbravo_import' and 'freppledb.openbravo' in settings.INSTALLED_APPS:
task = Task(name='Openbravo import', submitted=now, status='Waiting', user=request.user)
task.arguments = "--delta=%s" % request.POST['delta']
task.save(using=request.database)
# K
elif action == 'openbravo_export' and 'freppledb.openbravo' in settings.INSTALLED_APPS:
task = Task(name='Openbravo export', submitted=now, status='Waiting', user=request.user)
task.save(using=request.database)
# L
elif action == 'openerp_import' and 'freppledb.openerp' in settings.INSTALLED_APPS:
task = Task(name='OpenERP import', submitted=now, status='Waiting', user=request.user)
task.arguments = "--delta=%s" % request.POST['delta']
task.save(using=request.database)
# M
elif action == 'openerp_export' and 'freppledb.openerp' in settings.INSTALLED_APPS:
task = Task(name='OpenERP export', submitted=now, status='Waiting', user=request.user)
#.........这里部分代码省略.........
示例8: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **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
force = options['force']
test = 'FREPPLE_TEST' in os.environ
if options['user']:
try:
user = User.objects.all().get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
# Synchronize the scenario table with the settings
Scenario.syncWithSettings()
# Initialize the task
source = options['source']
try:
sourcescenario = Scenario.objects.using(DEFAULT_DB_ALIAS).get(pk=source)
except:
raise CommandError("No source database defined with name '%s'" % source)
now = datetime.now()
task = None
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(source).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_copy', 'scenario_copy'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='scenario_copy', submitted=now, started=now, status='0%', user=user)
task.processid = os.getpid()
task.save(using=source)
# Validate the arguments
destination = options['destination']
destinationscenario = None
try:
task.arguments = "%s %s" % (source, destination)
if options['description']:
task.arguments += '--description="%s"' % options['description'].replace('"', '\\"')
if force:
task.arguments += " --force"
task.save(using=source)
try:
destinationscenario = Scenario.objects.using(DEFAULT_DB_ALIAS).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(using=DEFAULT_DB_ALIAS)
# Copying the data
# Commenting the next line is a little more secure, but requires you to create a .pgpass file.
if settings.DATABASES[source]['PASSWORD']:
os.environ['PGPASSWORD'] = settings.DATABASES[source]['PASSWORD']
if os.name == 'nt':
# On windows restoring with pg_restore over a pipe is broken :-(
cmd = "pg_dump -c -Fp %s%s%s%s | psql %s%s%s%s"
else:
cmd = "pg_dump -Fc %s%s%s%s | pg_restore -n public -Fc -c --if-exists %s%s%s -d %s"
commandline = cmd % (
settings.DATABASES[source]['USER'] and ("-U %s " % settings.DATABASES[source]['USER']) or '',
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'] and ("-U %s " % settings.DATABASES[destination]['USER']) or '',
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'],
)
with subprocess.Popen(commandline, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) as p:
try:
task.processid = p.pid
task.save(using=source)
p.wait()
except:
p.kill()
p.wait()
# Consider the destination database free again
destinationscenario.status = 'Free'
#.........这里部分代码省略.........
示例9: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **options):
# Pick up the options
now = datetime.now()
self.database = options['database']
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
timestamp = now.strftime("%Y%m%d%H%M%S")
if self.database == DEFAULT_DB_ALIAS:
logfile = 'importworkbook-%s.log' % timestamp
else:
logfile = 'importworkbook_%s-%s.log' % (self.database, timestamp)
task = None
try:
setattr(_thread_locals, 'database', self.database)
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_importworkbook', 'importworkbook'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='importworkbook', submitted=now, started=now, status='0%', user=self.user)
task.arguments = ' '.join(options['file'])
task.save(using=self.database)
all_models = [ (ct.model_class(), ct.pk) for ct in ContentType.objects.all() if ct.model_class() ]
try:
with transaction.atomic(using=self.database):
# Find all models in the workbook
for file in filename:
wb = load_workbook(filename=file, read_only=True, data_only=True)
models = []
for ws_name in wb.sheetnames:
# Find the model
model = None
contenttype_id = None
for m, ct in all_models:
if matchesModelName(ws_name, m):
model = m
contenttype_id = ct
break
if not model or model in EXCLUDE_FROM_BULK_OPERATIONS:
print(force_text(_("Ignoring data in worksheet: %s") % ws_name))
# yield '<div class="alert alert-warning">' + force_text(_("Ignoring data in worksheet: %s") % ws_name) + '</div>'
elif not self.user.has_perm('%s.%s' % (model._meta.app_label, get_permission_codename('add', model._meta))):
# Check permissions
print(force_text(_("You don't permissions to add: %s") % ws_name))
# yield '<div class="alert alert-danger">' + force_text(_("You don't permissions to add: %s") % ws_name) + '</div>'
else:
deps = set([model])
GridReport.dependent_models(model, deps)
models.append( (ws_name, model, contenttype_id, deps) )
# Sort the list of models, based on dependencies between models
models = GridReport.sort_models(models)
print('197----', models)
# Process all rows in each worksheet
for ws_name, model, contenttype_id, dependencies in models:
print(force_text(_("Processing data in worksheet: %s") % ws_name))
# yield '<strong>' + force_text(_("Processing data in worksheet: %s") % ws_name) + '</strong><br>'
# yield ('<div class="table-responsive">'
# '<table class="table table-condensed" style="white-space: nowrap;"><tbody>')
numerrors = 0
numwarnings = 0
firsterror = True
ws = wb[ws_name]
for error in parseExcelWorksheet(model, ws, user=self.user, database=self.database, ping=True):
if error[0] == DEBUG:
# Yield some result so we can detect disconnect clients and interrupt the upload
# yield ' '
continue
if firsterror and error[0] in (ERROR, WARNING):
print('%s %s %s %s %s%s%s' % (
capfirst(_("worksheet")), capfirst(_("row")),
capfirst(_("field")), capfirst(_("value")),
capfirst(_("error")), " / ", capfirst(_("warning"))
))
# yield '<tr><th class="sr-only">%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s%s%s</th></tr>' % (
# capfirst(_("worksheet")), capfirst(_("row")),
# capfirst(_("field")), capfirst(_("value")),
# capfirst(_("error")), " / ", capfirst(_("warning"))
# )
firsterror = False
if error[0] == ERROR:
print('%s %s %s %s %s: %s' % (
ws_name,
error[1] if error[1] else '',
error[2] if error[2] else '',
#.........这里部分代码省略.........
示例10: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
if 'database' in options:
self.database = options['database'] or DEFAULT_DB_ALIAS
else:
self.database = DEFAULT_DB_ALIAS
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if 'user' in options and options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
now = datetime.now()
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'load from folder':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='load from folder', submitted=now, started=now, status='0%', user=self.user)
task.arguments = ' '.join(['"%s"' % i for i in args])
task.save(using=self.database)
# Choose the right self.delimiter and language
self.delimiter = get_format('DECIMAL_SEPARATOR', settings.LANGUAGE_CODE, True) == ',' and ';' or ','
translation.activate(settings.LANGUAGE_CODE)
# Execute
filestoupload = list()
if os.path.isdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
thisfolder = settings.DATABASES[self.database]['FILEUPLOADFOLDER']
for fileindir in os.listdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
if fileindir.endswith('.csv'):
filestoupload.append(fileindir)
#filestoupload.append([file,strftime("%Y-%m-%d %H:%M:%S",localtime(os.stat(os.path.join(thisfolder, file)).st_mtime)),sizeof_fmt(os.stat(os.path.join(thisfolder, file)).st_size, 'B')])
all_models = [ (ct.model_class(), ct.pk) for ct in ContentType.objects.all() if ct.model_class() ]
models = []
for ifile in filestoupload:
filename0 = ifile.split('.')[0]
model = None
contenttype_id = None
for m, ct in all_models:
if filename0.lower() in (m._meta.model_name.lower(), m._meta.verbose_name.lower(), m._meta.verbose_name_plural.lower()):
model = m
contenttype_id = ct
break
if not model or model in EXCLUDE_FROM_BULK_OPERATIONS:
print("Ignoring data in file: %s" % ifile)
elif not self.user==None and not self.user.has_perm('%s.%s' % (model._meta.app_label, get_permission_codename('add', model._meta))):
# Check permissions
print("You don't permissions to add: %s" % ifile)
else:
deps = set([model])
GridReport.dependent_models(model, deps)
models.append( (ifile, model, contenttype_id, deps) )
# Sort the list of models, based on dependencies between models
cnt = len(models)
ok = False
while not ok:
ok = True
for i in range(cnt):
for j in range(i + 1, cnt):
if models[i][1] in models[j][3]:
# A subsequent model i depends on model i. The list ordering is
# thus not ok yet. We move this element to the end of the list.
models.append(models.pop(i))
ok = False
for ifile, model, contenttype_id, dependencies in models:
print("Processing data in file: %s" % ifile)
rownum = 0
has_pk_field = False
headers = []
uploadform = None
changed = 0
added = 0
numerrors = 0
#Will the permissions have to be checked table by table?
permname = get_permission_codename('add', model._meta)
if not self.user == None and not self.user.has_perm('%s.%s' % (model._meta.app_label, permname)):
print('Permission denied')
#.........这里部分代码省略.........
示例11: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **options):
# Pick up the options
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
task = None
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_loadxml', 'loadxml'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='loadxml', submitted=now, started=now, status='0%', user=user)
task.arguments = ' '.join(options['file'])
task.processid = os.getpid()
task.save(using=database)
# Execute
# TODO: if frePPLe is available as a module, we don't really need to spawn another process.
os.environ['FREPPLE_HOME'] = settings.FREPPLE_HOME.replace('\\', '\\\\')
os.environ['FREPPLE_APP'] = settings.FREPPLE_APP
os.environ['FREPPLE_DATABASE'] = database
os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP
os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings'
if os.path.exists(os.path.join(os.environ['FREPPLE_HOME'], 'python36.zip')):
# For the py2exe executable
os.environ['PYTHONPATH'] = os.path.join(
os.environ['FREPPLE_HOME'],
'python%d%d.zip' % (sys.version_info[0], sys.version_info[1])
) + os.pathsep + os.path.normpath(os.environ['FREPPLE_APP'])
else:
# Other executables
os.environ['PYTHONPATH'] = os.path.normpath(os.environ['FREPPLE_APP'])
cmdline = [ '"%s"' % i for i in options['file'] ]
cmdline.insert(0, 'frepple')
cmdline.append( '"%s"' % os.path.join(settings.FREPPLE_APP, 'freppledb', 'execute', 'loadxml.py') )
proc = subprocess.run(' '.join(cmdline))
if proc.returncode:
raise Exception('Exit code of the batch run is %d' % proc.returncode)
# Task update
task.status = 'Done'
task.finished = datetime.now()
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
if task:
task.processid = None
task.save(using=database)
示例12: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
if 'database' in options:
self.database = options['database'] or DEFAULT_DB_ALIAS
else:
self.database = DEFAULT_DB_ALIAS
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if 'user' in options and options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
now = datetime.now()
task = None
self.logfile = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'load from folder':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='load from folder', submitted=now, started=now, status='0%', user=self.user)
task.arguments = ' '.join(['"%s"' % i for i in args])
task.save(using=self.database)
# Choose the right self.delimiter and language
self.delimiter = get_format('DECIMAL_SEPARATOR', settings.LANGUAGE_CODE, True) == ',' and ';' or ','
translation.activate(settings.LANGUAGE_CODE)
# Execute
if os.path.isdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
# Open the logfile
self.logfile = open(os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], 'loadfromfolder.log'), "a")
print("%s Started upload from folder\n" % datetime.now(), file=self.logfile)
all_models = [ (ct.model_class(), ct.pk) for ct in ContentType.objects.all() if ct.model_class() ]
models = []
for ifile in os.listdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
if not ifile.endswith('.csv'):
continue
filename0 = ifile.split('.')[0]
model = None
contenttype_id = None
for m, ct in all_models:
if filename0.lower() in (m._meta.model_name.lower(), m._meta.verbose_name.lower(), m._meta.verbose_name_plural.lower()):
model = m
contenttype_id = ct
print("%s Matched a model to file: %s" % (datetime.now(),ifile), file=self.logfile)
break
if not model or model in EXCLUDE_FROM_BULK_OPERATIONS:
print("%s Ignoring data in file: %s" % (datetime.now(),ifile), file=self.logfile)
elif self.user and not self.user.has_perm('%s.%s' % (model._meta.app_label, get_permission_codename('add', model._meta))):
# Check permissions
print("%s You don't have permissions to add: %s" % (datetime.now(),ifile), file=self.logfile)
else:
deps = set([model])
GridReport.dependent_models(model, deps)
models.append( (ifile, model, contenttype_id, deps) )
# Sort the list of models, based on dependencies between models
cnt = len(models)
ok = False
while not ok:
ok = True
for i in range(cnt):
for j in range(i + 1, cnt):
if models[i][1] != models[j][1] and models[i][1] in models[j][3]:
# A subsequent model i depends on model i. The list ordering is
# thus not ok yet. We move this element to the end of the list.
models.append(models.pop(i))
ok = False
task.status = '10%'
task.save(using=self.database)
i=0
errors = 0
for ifile, model, contenttype_id, dependencies in models:
i += 1
print("%s Started processing data in file: %s" % (datetime.now(),ifile), file=self.logfile)
filetoparse=os.path.join(os.path.abspath(settings.DATABASES[self.database]['FILEUPLOADFOLDER']), ifile)
errors += self.parseCSVloadfromfolder(model, filetoparse)
print("%s Finished processing data in file: %s\n" % (datetime.now(),ifile), file=self.logfile)
task.status = str(int(10+i/cnt*80))+'%'
task.save(using=self.database)
#.........这里部分代码省略.........
示例13: wrapTask
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def wrapTask(request, action):
# Allow only post
if request.method != 'POST':
raise Exception('Only post requests allowed')
# Parse the posted parameters as arguments for an asynchronous task to add to the queue. TODO MAKE MODULAR WITH SEPERATE TASK CLASS
worker_database = request.database
now = datetime.now()
task = None
args = request.POST or request.GET
# A
if action in ('frepple_run', 'runplan'):
if not request.user.has_perm('auth.generate_plan'):
raise Exception('Missing execution privileges')
constraint = 0
for value in args.getlist('constraint'):
try:
constraint += int(value)
except:
pass
task = Task(name='runplan', submitted=now, status='Waiting', user=request.user)
task.arguments = "--constraint=%s --plantype=%s" % (constraint, args.get('plantype', 1))
env = []
for value in args.getlist('env'):
env.append(value)
if env:
task.arguments = "%s --env=%s" % (task.arguments, ','.join(env))
task.save(using=request.database)
# C
elif action in ('frepple_flush', 'empty'):
if not request.user.has_perm('auth.run_db'):
raise Exception('Missing execution privileges')
task = Task(name='empty', submitted=now, status='Waiting', user=request.user)
models = ','.join(args.getlist('models'))
if models:
task.arguments = "--models=%s" % (models)
task.save(using=request.database)
# D
elif action == 'loaddata':
if not request.user.has_perm('auth.run_db'):
raise Exception('Missing execution privileges')
task = Task(name='loaddata', submitted=now, status='Waiting', user=request.user, arguments=args['fixture'])
task.save(using=request.database)
# Also run the workflow upon loading of manufacturing_demo or distribution_demo
if (args['regenerateplan'] == 'true'):
active_modules = 'supply'
task = Task(name='runplan', submitted=now, status='Waiting', user=request.user)
task.arguments = "--constraint=15 --plantype=1 --env=%s --background" % (active_modules,)
task.save(using=request.database)
# E
elif action in ('frepple_copy', 'scenario_copy'):
worker_database = DEFAULT_DB_ALIAS
if 'copy' in args:
if not request.user.has_perm('auth.copy_scenario'):
raise Exception('Missing execution privileges')
source = args.get('source', DEFAULT_DB_ALIAS)
worker_database = source
destination = args.getlist('destination')
force = args.get('force', False)
for sc in Scenario.objects.using(DEFAULT_DB_ALIAS):
arguments = "%s %s" % (source, sc.name)
if force:
arguments += ' --force'
if args.get(sc.name, 'off') == 'on' or sc.name in destination:
task = Task(name='scenario_copy', submitted=now, status='Waiting', user=request.user, arguments=arguments)
task.save(using=source)
elif 'release' in args:
# Note: release is immediate and synchronous.
if not request.user.has_perm('auth.release_scenario'):
raise Exception('Missing execution privileges')
for sc in Scenario.objects.using(DEFAULT_DB_ALIAS):
if args.get(sc.name, 'off') == 'on' and sc.status != 'Free':
sc.status = 'Free'
sc.lastrefresh = now
sc.save(using=DEFAULT_DB_ALIAS)
if request.database == sc.name:
# Erasing the database that is currently selected.
request.prefix = ''
elif 'update' in args:
# Note: update is immediate and synchronous.
if not request.user.has_perm('auth.release_scenario'):
raise Exception('Missing execution privileges')
for sc in Scenario.objects.using(DEFAULT_DB_ALIAS):
if args.get(sc.name, 'off') == 'on':
sc.description = args.get('description', None)
sc.save(using=DEFAULT_DB_ALIAS)
else:
raise Exception('Invalid scenario task')
# G
elif action in ('frepple_createbuckets', 'createbuckets'):
if not request.user.has_perm('auth.run_db'):
raise Exception('Missing execution privileges')
task = Task(name='createbuckets', submitted=now, status='Waiting', user=request.user)
arguments = []
start = args.get('start', None)
if start:
arguments.append("--start=%s" % start)
end = args.get('end', None)
if end:
#.........这里部分代码省略.........
示例14: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, **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 the options
if 'verbosity' in options:
verbosity = int(options['verbosity'])
else:
verbosity = 1
if 'cluster' in options:
cluster = int(options['cluster'])
else:
cluster = 100
if 'demand' in options:
demand = int(options['demand'])
else:
demand = 30
if 'forecast_per_item' in options:
forecast_per_item = int(options['forecast_per_item'])
else:
forecast_per_item = 50
if 'level' in options:
level = int(options['level'])
else:
level = 5
if 'resource' in options:
resource = int(options['resource'])
else:
resource = 60
if 'resource_size' in options:
resource_size = int(options['resource_size'])
else:
resource_size = 5
if 'components' in options:
components = int(options['components'])
else:
components = 200
if 'components_per' in options:
components_per = int(options['components_per'])
else:
components_per = 5
if components == 0:
components_per = 0
if 'deliver_lt' in options:
deliver_lt = int(options['deliver_lt'])
else:
deliver_lt = 30
if 'procure_lt' in options:
procure_lt = int(options['procure_lt'])
else:
procure_lt = 40
if 'currentdate' in options:
currentdate = options['currentdate'] or datetime.strftime(date.today(), '%Y-%m-%d')
else:
currentdate = datetime.strftime(date.today(), '%Y-%m-%d')
if 'database' in options:
database = options['database'] or DEFAULT_DB_ALIAS
else:
database = DEFAULT_DB_ALIAS
if not database in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if 'user' in options and options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
random.seed(100) # Initialize random seed to get reproducible results
now = datetime.now()
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'generate model':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='generate model', submitted=now, started=now, status='0%', user=user)
task.arguments = "--cluster=%s --demand=%s --forecast_per_item=%s --level=%s --resource=%s " \
"--resource_size=%s --components=%s --components_per=%s --deliver_lt=%s --procure_lt=%s" % (
cluster, demand, forecast_per_item, level, resource,
resource_size, components, components_per, deliver_lt, procure_lt
)
task.save(using=database)
transaction.commit(using=database)
# Pick up the startdate
try:
#.........这里部分代码省略.........
示例15: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import arguments [as 别名]
def handle(self, *args, **options):
# Pick up the options
now = datetime.now()
self.database = options['database']
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
timestamp = now.strftime("%Y%m%d%H%M%S")
if self.database == DEFAULT_DB_ALIAS:
logfile = 'exporttofolder-%s.log' % timestamp
else:
logfile = 'exporttofolder_%s-%s.log' % (self.database, timestamp)
try:
handler = logging.FileHandler(os.path.join(settings.FREPPLE_LOGDIR, logfile), encoding='utf-8')
# handler.setFormatter(logging.Formatter(settings.LOGGING['formatters']['simple']['format']))
logger.addHandler(handler)
logger.propagate = False
except Exception as e:
print("%s Failed to open logfile %s: %s" % (datetime.now(), logfile, e))
task = None
errors = 0
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_exporttofolder', 'exporttofolder'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.logfile = logfile
else:
task = Task(name='exporttofolder', submitted=now, started=now, status='0%', user=self.user, logfile=logfile)
task.arguments = ' '.join(['"%s"' % i for i in args])
task.processid = os.getpid()
task.save(using=self.database)
# Execute
if os.path.isdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
if not os.path.isdir(os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], 'export')):
try:
os.makedirs(os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], 'export'))
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
logger.info("%s Started export to folder\n" % datetime.now())
cursor = connections[self.database].cursor()
task.status = '0%'
task.save(using=self.database)
i = 0
cnt = len(self.statements)
# Calling all the pre-sql statements
for stmt in self.pre_sql_statements:
try:
logger.info("Executing pre-statement '%s'" % stmt)
cursor.execute(stmt)
logger.info("%s record(s) modified" % cursor.rowcount)
except:
errors += 1
logger.error("An error occurred when executing statement '%s'" % stmt)
for cfg in self.statements:
# Validate filename
filename = cfg.get('filename', None)
if not filename:
raise Exception("Missing filename in export configuration")
folder = cfg.get('folder', None)
if not folder:
raise Exception("Missing folder in export configuration for %s" % filename)
logger.info("%s Started export of %s" % (datetime.now(), filename))
# Make sure export folder exists
exportFolder = os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], folder)
if not os.path.isdir(exportFolder):
os.makedirs(exportFolder)
try:
reportclass = cfg.get('report', None)
sql = cfg.get('sql', None)
if reportclass:
# Export from report class
# Create a dummy request
#.........这里部分代码省略.........