本文整理汇总了Python中freppledb.execute.models.Task.message方法的典型用法代码示例。如果您正苦于以下问题:Python Task.message方法的具体用法?Python Task.message怎么用?Python Task.message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freppledb.execute.models.Task
的用法示例。
在下文中一共展示了Task.message方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
def handle(self, **options):
# Pick up the options
if 'verbosity' in options: self.verbosity = int(options['verbosity'] or '1')
else: self.verbosity = 1
if 'user' in options: user = options['user']
else: user = ''
if 'database' in options: self.database = options['database'] or DEFAULT_DB_ALIAS
else: self.database = DEFAULT_DB_ALIAS
if not self.database in settings.DATABASES.keys():
raise CommandError("No database settings known for '%s'" % self.database )
# Pick up configuration parameters
self.openerp_user = Parameter.getValue("openerp.user", self.database)
self.openerp_password = Parameter.getValue("openerp.password", self.database)
self.openerp_db = Parameter.getValue("openerp.db", self.database)
self.openerp_url = Parameter.getValue("openerp.url", self.database)
# 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
now = datetime.now()
ac = transaction.get_autocommit(using=self.database)
transaction.set_autocommit(False, using=self.database)
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 != 'OpenERP export':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='OpenERP edport', submitted=now, started=now, status='0%', user=user)
task.save(using=self.database)
transaction.commit(using=self.database)
# Log in to the openerp server
sock_common = xmlrpclib.ServerProxy(self.openerp_url + 'xmlrpc/common')
self.uid = sock_common.login(self.openerp_db, self.openerp_user, self.openerp_password)
# Connect to openerp server
self.sock = xmlrpclib.ServerProxy(self.openerp_url + 'xmlrpc/object')
# Create a database connection to the frePPLe database
self.cursor = connections[self.database].cursor()
# Upload all data
self.export_procurement_order()
task.status = '50%'
task.save(using=self.database)
transaction.commit(using=self.database)
self.export_sales_order()
task.status = '100%'
task.save(using=self.database)
transaction.commit(using=self.database)
# Log success
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=self.database)
try: transaction.commit(using=self.database)
except: pass
settings.DEBUG = tmp_debug
transaction.set_autocommit(ac, using=self.database)
示例2: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
#.........这里部分代码省略.........
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
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
task.save(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")
# Prepare environment
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,
'python%d%d.zip' %(sys.version_info[0], sys.version_info[1])
) + os.pathsep + os.path.normpath(settings.FREPPLE_APP)
else:
# Other executables
os.environ['PYTHONPATH'] = os.path.normpath(settings.FREPPLE_APP)
if options['background']:
# Execute as background process on Windows
if os.name == 'nt':
subprocess.Popen(['frepple', cmd], creationflags=0x08000000)
else:
# Execute as background process on Linux
subprocess.Popen(['frepple', cmd]).pid
else:
# Execute in foreground
ret = subprocess.call(['frepple', cmd])
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)
示例3: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [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)
示例4: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [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_restore', 'restore'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='restore', submitted=now, started=now, status='0%', user=user)
task.arguments = options['dump']
task.processid = os.getpid()
task.save(using=database)
# Validate options
dumpfile = os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, options['dump']))
if not os.path.isfile(dumpfile):
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 = [ "pg_restore", "-n", "public", "-Fc", "-c", "--if-exists" ]
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("-d")
cmd.append(settings.DATABASES[database]['NAME'])
cmd.append('<%s' % dumpfile)
# Shell needs to be True in order to interpret the < character
with subprocess.Popen(cmd, shell=True) as p:
try:
task.processid = p.pid
task.save(using=database)
p.wait()
except:
p.kill()
p.wait()
raise Exception("Database restoration failed")
# Task update
# We need to recreate a new task record, since the previous one is lost during the restoration.
task = Task(
name='restore', 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.processid = None
task.save(using=database)
示例5: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
def handle(self, **options):
# Pick up the options
if 'verbosity' in options:
self.verbosity = int(options['verbosity'] or '1')
else:
self.verbosity = 1
if 'user' in options:
user = options['user']
else:
user = ''
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.keys():
raise CommandError("No database settings known for '%s'" % self.database )
# Pick up configuration parameters
self.openbravo_user = Parameter.getValue("openbravo.user", self.database)
# Passwords in djangosettings file are preferably used
if settings.OPENBRAVO_PASSWORDS.get(db) == '':
self.openbravo_password = Parameter.getValue("openbravo.password", self.database)
else:
self.openbravo_password = settings.OPENBRAVO_PASSWORDS.get(db)
self.openbravo_host = Parameter.getValue("openbravo.host", self.database)
self.openbravo_organization = Parameter.getValue("openbravo.organization", self.database)
if not self.openbravo_user:
raise CommandError("Missing or invalid parameter openbravo_user")
if not self.openbravo_password:
raise CommandError("Missing or invalid parameter openbravo_password")
if not self.openbravo_host:
raise CommandError("Missing or invalid parameter openbravo_host")
if not self.openbravo_organization:
raise CommandError("Missing or invalid parameter openbravo_organization")
# 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
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 != 'Openbravo export':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='Openbravo export', submitted=now, started=now, status='0%', user=user)
task.save(using=self.database)
# Create a database connection to the frePPLe database
cursor = connections[self.database].cursor()
# Look up the id of the Openbravo user
query = urllib.quote("name='%s'" % self.openbravo_user.encode('utf8'))
conn = self.get_data("/openbravo/ws/dal/ADUser?where=%s&includeChildren=false" % query)[0]
self.user_id = None
for event, elem in conn:
if event != 'end' or elem.tag != 'ADUser':
continue
self.user_id = elem.get('id')
if not self.user_id:
raise CommandError("Can't find user id in Openbravo")
# Look up the id of the Openbravo organization id
query = urllib.quote("name='%s'" % self.openbravo_organization.encode('utf8'))
conn = self.get_data("/openbravo/ws/dal/Organization?where=%s&includeChildren=false" % query)[0]
self.organization_id = None
for event, elem in conn:
if event != 'end' or elem.tag != 'Organization':
continue
self.organization_id = elem.get('id')
if not self.organization_id:
raise CommandError("Can't find organization id in Openbravo")
# Upload all data
self.export_procurement_order(cursor)
self.export_work_order(cursor)
self.export_sales_order(cursor)
# Log success
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
#.........这里部分代码省略.........
示例6: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
#.........这里部分代码省略.........
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)
# Task update
if errors:
task.status = 'Failed'
task.message = "Uploaded %s data files with %s errors" % (cnt, errors)
else:
task.status = 'Done'
task.message = "Uploaded %s data file" % cnt
task.finished = datetime.now()
except Exception as e:
print("%s Failed" % datetime.now(), file=self.logfile)
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
if task:
task.status = '100%'
task.save(using=self.database)
if self.logfile:
print('%s End of upload from folder\n' % datetime.now(), file=self.logfile)
self.logfile.close()
示例7: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
#.........这里部分代码省略.........
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 '',
error[3] if error[3] else '',
capfirst(_('error')),
error[4]
))
# yield '<tr><td class="sr-only">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s: %s</td></tr>' % (
# ws_name,
# error[1] if error[1] else '',
# error[2] if error[2] else '',
# error[3] if error[3] else '',
# capfirst(_('error')),
# error[4]
# )
numerrors += 1
elif error[1] == WARNING:
print('%s %s %s %s %s: %s' % (
ws_name,
error[1] if error[1] else '',
error[2] if error[2] else '',
error[3] if error[3] else '',
capfirst(_('warning')),
error[4]
))
# yield '<tr><td class="sr-only">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s: %s</td></tr>' % (
# ws_name,
# error[1] if error[1] else '',
# error[2] if error[2] else '',
# error[3] if error[3] else '',
# capfirst(_('warning')),
# error[4]
# )
numwarnings += 1
else:
print('%s %s %s %s %s %s' % (
"danger" if numerrors > 0 else 'success',
ws_name,
error[1] if error[1] else '',
error[2] if error[2] else '',
error[3] if error[3] else '',
error[4]
))
# yield '<tr class=%s><td class="sr-only">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % (
# "danger" if numerrors > 0 else 'success',
# ws_name,
# error[1] if error[1] else '',
# error[2] if error[2] else '',
# error[3] if error[3] else '',
# error[4]
# )
# yield '</tbody></table></div>'
print('%s' % _("Done"))
# yield '<div><strong>%s</strong></div>' % _("Done")
except GeneratorExit:
logger.warning('Connection Aborted')
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
setattr(_thread_locals, 'database', None)
if task:
task.save(using=self.database)
return _("Done")
示例8: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [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
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 != 'empty database':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='empty database', submitted=now, started=now, status='0%', user=user)
task.save(using=database)
transaction.commit(using=database)
# Create a database connection
cursor = connections[database].cursor()
# Get a list of all django tables in the database
tables = set(connections[database].introspection.django_table_names(only_existing=True))
# Some tables need to be handled a bit special
cursor.execute('update common_user set horizonbuckets = null')
tables.discard('auth_group_permissions')
tables.discard('auth_permission')
tables.discard('auth_group')
tables.discard('django_session')
tables.discard('common_user')
tables.discard('common_user_groups')
tables.discard('common_user_user_permissions')
tables.discard('django_content_type')
tables.discard('execute_log')
tables.discard('execute_scenario')
transaction.commit(using=database)
# Delete all records from the tables.
for stmt in connections[database].ops.sql_flush(no_style(), tables, []):
cursor.execute(stmt)
transaction.commit(using=database)
# SQLite specials
if settings.DATABASES[database]['ENGINE'] == 'django.db.backends.sqlite3':
cursor.execute('vacuum') # Shrink the database file
# 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
settings.DEBUG = tmp_debug
transaction.leave_transaction_management(using=database)
示例9: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
def handle(self, *fixture_labels, **options):
# get the database object
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
now = datetime.now()
task = None
try:
setattr(_thread_locals, 'database', database)
# 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 != 'loaddata':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.processid = os.getpid()
task.save(using=database, update_fields=['started', 'status', 'processid'])
else:
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
task = Task(
name='loaddata', submitted=now, started=now, status='0%',
user=user, arguments=' '.join(fixture_labels)
)
task.processid = os.getpid()
task.save(using=database)
# Excecute the standard django command
super(Command, self).handle(*fixture_labels, **options)
# if the fixture doesn't contain the 'demo' word, let's not apply loaddata post-treatments
for f in fixture_labels:
if '_demo' not in f.lower():
return
with transaction.atomic(using=database, savepoint=False):
print('updating fixture to current date')
cursor = connections[database].cursor()
cursor.execute('''
select to_timestamp(value,'YYYY-MM-DD hh24:mi:ss') from common_parameter where name = 'currentdate'
''')
currentDate = cursor.fetchone()[0]
now = datetime.now()
offset = (now - currentDate).days
#update currentdate to now
cursor.execute('''
update common_parameter set value = 'now' where name = 'currentdate'
''')
#update demand due dates
cursor.execute('''
update demand set due = due + %s * interval '1 day'
''', (offset,))
#update PO/DO/MO due dates
cursor.execute('''
update operationplan set startdate = startdate + %s * interval '1 day', enddate = enddate + %s * interval '1 day'
''', 2 * (offset,))
# Task update
task.status = 'Done'
task.finished = datetime.now()
task.processid = None
task.save(using=database, update_fields=['status', 'finished'])
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
task.processid = None
task.save(using=database, update_fields=['status', 'finished', 'message'])
raise CommandError('%s' % e)
finally:
setattr(_thread_locals, 'database', None)
示例10: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
#.........这里部分代码省略.........
# 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')
return
filetoparse=os.path.join(os.path.abspath(thisfolder), ifile)
self.parseCSVloadfromfolder(model, filetoparse)
# 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=self.database)
示例11: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [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 message [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(using=database)
transaction.managed(True, 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 != "backup database":
raise CommandError("Invalid task identifier")
task.status = "0%"
task.started = now
else:
task = Task(name="backup database", submitted=now, started=now, status="0%", user=user)
# Choose the backup file name
backupfile = now.strftime("database.%s.%%Y%%m%%d.%%H%%M%%S.dump" % database)
task.message = "Backup to file %s" % backupfile
task.save(using=database)
transaction.commit(using=database)
# Run the backup command
if settings.DATABASES[database]["ENGINE"] == "django.db.backends.sqlite3":
# SQLITE
shutil.copy2(
settings.DATABASES[database]["NAME"],
os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, backupfile)),
)
elif settings.DATABASES[database]["ENGINE"] == "django.db.backends.mysql":
# MYSQL
args = [
"mysqldump",
"--password=%s" % settings.DATABASES[database]["PASSWORD"],
"--user=%s" % settings.DATABASES[database]["USER"],
"--quick",
"--compress",
"--extended-insert",
"--add-drop-table",
"--result-file=%s" % os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, backupfile)),
]
if settings.DATABASES[database]["HOST"]:
args.append("--host=%s " % settings.DATABASES[database]["HOST"])
if settings.DATABASES[database]["PORT"]:
args.append("--port=%s " % settings.DATABASES[database]["PORT"])
args.append(settings.DATABASES[database]["NAME"])
ret = subprocess.call(args)
if ret:
raise Exception("Run of mysqldump 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"],
)
args = [
"expdp",
dsn,
"schemas=%s" % settings.DATABASES[database]["USER"],
"directory=frepple_logdir",
"nologfile=Y",
"dumpfile=%s" % backupfile,
]
ret = subprocess.call(args)
if ret:
raise Exception("Run of expdp failed")
elif settings.DATABASES[database]["ENGINE"] == "django.db.backends.postgresql_psycopg2":
# POSTGRESQL
args = [
#.........这里部分代码省略.........
示例13: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [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)
示例14: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
#.........这里部分代码省略.........
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'
destinationscenario.lastrefresh = datetime.today()
destinationscenario.save(using=DEFAULT_DB_ALIAS)
raise Exception("Database copy failed")
# Update the scenario table
destinationscenario.status = 'In use'
destinationscenario.lastrefresh = datetime.today()
if 'description' in options:
destinationscenario.description = options['description']
destinationscenario.save(using=DEFAULT_DB_ALIAS)
# Give access to the destination scenario to:
# a) the user doing the copy
# b) all superusers from the source schema
User.objects.using(destination).filter(is_superuser=True).update(is_active=True)
User.objects.using(destination).filter(is_superuser=False).update(is_active=False)
if user:
User.objects.using(destination).filter(username=user.username).update(is_active=True)
# Logging message
task.processid = None
task.status = 'Done'
task.finished = datetime.now()
# Update the task in the destination database
task.message = "Scenario copied from %s" % source
task.save(using=destination)
task.message = "Scenario copied to %s" % destination
# Delete any waiting tasks in the new copy.
# This is needed for situations where the same source is copied to
# multiple destinations at the same moment.
Task.objects.all().using(destination).filter(id__gt=task.id).delete()
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(using=DEFAULT_DB_ALIAS)
raise e
finally:
if task:
task.processid = None
task.save(using=source)
settings.DEBUG = tmp_debug
示例15: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import message [as 别名]
def handle(self, **options):
# Pick up the options
if 'verbosity' in options: self.verbosity = int(options['verbosity'] or '1')
else: self.verbosity = 1
if 'user' in options: user = options['user']
else: user = ''
if 'database' in options: self.database = options['database'] or DEFAULT_DB_ALIAS
else: self.database = DEFAULT_DB_ALIAS
if not self.database in settings.DATABASES.keys():
raise CommandError("No database settings known for '%s'" % self.database )
if 'delta' in options: self.delta = float(options['delta'] or '3650')
else: self.delta = 3650
self.date = datetime.now()
# 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
now = datetime.now()
ac = transaction.get_autocommit(using=self.database)
transaction.set_autocommit(False, using=self.database)
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 != 'Odoo import':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='Odoo import', submitted=now, started=now, status='0%', user=user,
arguments="--delta=%s" % self.delta)
task.save(using=self.database)
transaction.commit(using=self.database)
# Find the connector class
# We look for a module called "odoo_export" in each of the installed
# applications, and expect to find a class called connector in it
connector = None
for app in reversed(settings.INSTALLED_APPS):
try:
connector = getattr(import_module('%s.odoo_import' % app),'Connector')
except ImportError as e:
# Silently ignore if it's the module which isn't found in the app
if str(e) != 'No module named odoo_import': raise e
if not connector:
raise CommandError("No odoo_import connector found")
# Instantiate the connector and upload all data
connector(task, self.delta, self.database, self.verbosity).run()
# Log success
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=self.database)
try: transaction.commit(using=self.database)
except: pass
settings.DEBUG = tmp_debug
transaction.set_autocommit(ac, using=self.database)