예제 #1
0
def engine2_run():
    #~ botsglobal.ini.set('directories','data',botslib.join(data_storage))
    print datetime.datetime.now()
    botslib.dirshouldbethere(data_storage)
    run = get_control_information()
    read_incoming(run)
    translate(run)
    mergemessages(run)
    write_outgoing(run)
    trace(run)
    report(run)
    cleanup(run)
    print datetime.datetime.now()
    return run.errorinrun
예제 #2
0
def engine2_run():
    #~ botsglobal.ini.set('directories','data',botslib.join(data_storage))
    print datetime.datetime.now()
    botslib.dirshouldbethere(data_storage)
    run = get_control_information()
    read_incoming(run)
    translate(run)
    mergemessages(run)
    write_outgoing(run)
    trace(run)
    report(run)
    cleanup(run)
    print datetime.datetime.now()
    return run.errorinrun
예제 #3
0
def write_outgoing(run):
    outputdir = botslib.join(run.outpath)
    botslib.dirshouldbethere(outputdir)
    for outgoing in run.outgoing:
        if not outgoing['error']:
            try:
                unique_filename = filename_formatter(run.outfilename,outgoing)
                tofilepath = botslib.join(outputdir,unique_filename)
                fromfilepath = botslib.abspathdata(outgoing['filename'])
                shutil.move(fromfilepath,tofilepath)
            except:
                txt = botslib.txtexc()
                outgoing.update({'error':txt})
            else:
                outgoing.update({'outfilename':tofilepath})
예제 #4
0
def write_outgoing(run):
    outputdir = botslib.join(run.outpath)
    botslib.dirshouldbethere(outputdir)
    for outgoing in run.outgoing:
        if not outgoing['error']:
            try:
                unique_filename = filename_formatter(run.outfilename, outgoing)
                tofilepath = botslib.join(outputdir, unique_filename)
                fromfilepath = botslib.abspathdata(outgoing['filename'])
                shutil.move(fromfilepath, tofilepath)
            except:
                txt = botslib.txtexc()
                outgoing.update({'error': txt})
            else:
                outgoing.update({'outfilename': tofilepath})
예제 #5
0
def delete(request,*kw,**kwargs):
    if request.method == 'GET':
        form = forms.DeleteForm()
        return  django.shortcuts.render_to_response('bots/delete.html', {'form': form},context_instance=django.template.RequestContext(request))
    else:
        if 'submit' in request.POST:
            form = forms.DeleteForm(request.POST)
            if form.is_valid():
                botsglobal.logger.info(_(u'Start deleting in configuration.'))
                if form.cleaned_data['deltransactions']:
                    #while testing with very big loads, deleting transaction when wrong. Using raw SQL solved this.
                    from django.db import connection, transaction
                    cursor = connection.cursor()
                    cursor.execute("DELETE FROM ta")
                    cursor.execute("DELETE FROM filereport")
                    cursor.execute("DELETE FROM report")
                    transaction.commit_unless_managed()
                    request.user.message_set.create(message=_(u'Transactions are deleted.'))
                    botsglobal.logger.info(_(u'    Transactions are deleted.'))
                    #clean data files
                    deletefrompath = botsglobal.ini.get('directories','data','botssys/data')
                    shutil.rmtree(deletefrompath,ignore_errors=True)
                    botslib.dirshouldbethere(deletefrompath)
                    request.user.message_set.create(message=_(u'Data files are deleted.'))
                    botsglobal.logger.info(_(u'    Data files are deleted.'))
                if form.cleaned_data['delconfiguration']:
                    models.confirmrule.objects.all().delete()
                    models.channel.objects.all().delete()
                    models.chanpar.objects.all().delete()
                    models.partner.objects.all().delete()
                    models.translate.objects.all().delete()
                    models.routes.objects.all().delete()
                    request.user.message_set.create(message=_(u'Database configuration is deleted.'))
                    botsglobal.logger.info(_(u'    Database configuration is deleted.'))
                if form.cleaned_data['delcodelists']:
                    models.ccode.objects.all().delete()
                    models.ccodetrigger.objects.all().delete()
                    request.user.message_set.create(message=_(u'User code lists are deleted.'))
                    botsglobal.logger.info(_(u'    User code lists are deleted.'))
                if form.cleaned_data['delinfile']:
                    deletefrompath = botslib.join(botsglobal.ini.get('directories','botssys','botssys'),'infile')
                    shutil.rmtree('bots/botssys/infile',ignore_errors=True)
                    request.user.message_set.create(message=_(u'Files in botssys/infile are deleted.'))
                    botsglobal.logger.info(_(u'    Files in botssys/infile are deleted.'))
                if form.cleaned_data['deloutfile']:
                    deletefrompath = botslib.join(botsglobal.ini.get('directories','botssys','botssys'),'outfile')
                    shutil.rmtree('bots/botssys/outfile',ignore_errors=True)
                    request.user.message_set.create(message=_(u'Files in botssys/outfile are deleted.'))
                    botsglobal.logger.info(_(u'    Files in botssys/outfile are deleted.'))
                if form.cleaned_data['deluserscripts']:
                    deletefrompath = botsglobal.ini.get('directories','usersysabs')
                    for root, dirs, files in os.walk(deletefrompath):
                        head, tail = os.path.split(root)
                        if tail == 'charsets':
                            del dirs[:]
                            continue
                        for bestand in files:
                            if bestand != '__init__.py':
                                os.remove(os.path.join(root,bestand))
                    request.user.message_set.create(message=_(u'User scripts are deleted (in usersys).'))
                    botsglobal.logger.info(_(u'    User scripts are deleted (in usersys).'))
                elif form.cleaned_data['delbackup']:
                    deletefrompath = botsglobal.ini.get('directories','usersysabs')
                    for root, dirs, files in os.walk(deletefrompath):
                        head, tail = os.path.split(root)
                        if tail == 'charsets':
                            del dirs[:]
                            continue
                        for bestand in files:
                            name,ext = os.path.splitext(bestand)
                            if ext and len(ext) == 15 and ext[1:].isdigit() :
                                os.remove(os.path.join(root,bestand))
                    request.user.message_set.create(message=_(u'Backupped user scripts are deleted/purged (in usersys).'))
                    botsglobal.logger.info(_(u'    Backupped user scripts are deleted/purged (in usersys).'))
                botsglobal.logger.info(_(u'Finished deleting in configuration.'))
    return django.shortcuts.redirect('/home')
예제 #6
0
def delete(request, *kw, **kwargs):
    if request.method == 'GET':
        form = forms.DeleteForm()
        return django.shortcuts.render(request, 'bots/delete.html',
                                       {'form': form})
    else:
        if 'submit' in request.POST:
            form = forms.DeleteForm(request.POST)
            if form.is_valid():
                from django.db import connection, transaction
                if form.cleaned_data['delconfiguration'] or form.cleaned_data[
                        'delcodelists'] or form.cleaned_data['deluserscripts']:
                    #write backup plugin first
                    plugout_backup_core(request, *kw, **kwargs)
                botsglobal.logger.info(_(u'Start deleting in configuration.'))
                if form.cleaned_data['deltransactions']:
                    #while testing with very big loads, deleting gave error. Using raw SQL solved this.
                    cursor = connection.cursor()
                    cursor.execute("DELETE FROM ta")
                    cursor.execute("DELETE FROM filereport")
                    cursor.execute("DELETE FROM report")
                    transaction.commit_unless_managed()
                    messages.add_message(request, messages.INFO,
                                         _(u'Transactions are deleted.'))
                    botsglobal.logger.info(_(u'Transactions are deleted.'))
                    #clean data files
                    deletefrompath = botsglobal.ini.get(
                        'directories', 'data', 'botssys/data')
                    shutil.rmtree(deletefrompath, ignore_errors=True)
                    botslib.dirshouldbethere(deletefrompath)
                    notification = _(u'Data files are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                elif form.cleaned_data['delacceptance']:
                    from django.db.models import Min
                    list_file = [
                    ]  #list of files for deletion in data-directory
                    report_idta_lowest = 0
                    for acc_report in models.report.objects.filter(
                            acceptance=1
                    ):  #for each acceptance report. is not very efficient.
                        if not report_idta_lowest:
                            report_idta_lowest = acc_report.idta
                        max_report_idta = models.report.objects.filter(
                            idta__gt=acc_report.idta).aggregate(Min('idta'))[
                                'idta__min']  #select 'next' report...
                        if not max_report_idta:  #if report is report of last run, there is no next report
                            max_report_idta = sys.maxint
                        #we have a idta-range now: from (and including) acc_report.idta till (and excluding) max_report_idta
                        list_file += models.ta.objects.filter(
                            idta__gte=acc_report.idta,
                            idta__lt=max_report_idta).exclude(
                                status=1).values_list('filename',
                                                      flat=True).distinct()
                        models.ta.objects.filter(
                            idta__gte=acc_report.idta,
                            idta__lt=max_report_idta).delete(
                            )  #delete ta in range
                        models.filereport.objects.filter(
                            idta__gte=acc_report.idta,
                            idta__lt=max_report_idta).delete(
                            )  #delete filereports in range
                    if report_idta_lowest:
                        models.report.objects.filter(
                            idta__gte=report_idta_lowest, acceptance=1).delete(
                            )  #delete all acceptance reports
                        for filename in list_file:  #delete all files in data directory geenrated during acceptance testing
                            if filename.isdigit():
                                botslib.deldata(filename)
                    notification = _(
                        u'Transactions from acceptance-testing deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delconfiguration']:
                    models.confirmrule.objects.all().delete()
                    models.routes.objects.all().delete()
                    models.channel.objects.all().delete()
                    models.chanpar.objects.all().delete()
                    models.translate.objects.all().delete()
                    models.partner.objects.all().delete()
                    notification = _(u'Database configuration is deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delcodelists']:
                    #while testing with very big loads, deleting gave error. Using raw SQL solved this.
                    cursor = connection.cursor()
                    cursor.execute("DELETE FROM ccode")
                    cursor.execute("DELETE FROM ccodetrigger")
                    transaction.commit_unless_managed()
                    notification = _(u'User code lists are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delpersist']:
                    cursor = connection.cursor()
                    cursor.execute("DELETE FROM persist")
                    transaction.commit_unless_managed()
                    notification = _(u'Persist data is deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delinfile']:
                    deletefrompath = botslib.join(
                        botsglobal.ini.get('directories', 'botssys',
                                           'botssys'), 'infile')
                    shutil.rmtree(deletefrompath, ignore_errors=True)
                    notification = _(u'Files in botssys/infile are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['deloutfile']:
                    deletefrompath = botslib.join(
                        botsglobal.ini.get('directories', 'botssys',
                                           'botssys'), 'outfile')
                    shutil.rmtree(deletefrompath, ignore_errors=True)
                    notification = _(u'Files in botssys/outfile are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['deluserscripts']:
                    deletefrompath = botsglobal.ini.get(
                        'directories', 'usersysabs')
                    for root, dirs, files in os.walk(deletefrompath):
                        head, tail = os.path.split(root)
                        if tail == 'charsets':
                            del dirs[:]
                            continue
                        for bestand in files:
                            if bestand != '__init__.py':
                                os.remove(os.path.join(root, bestand))
                    notification = _(u'User scripts are deleted (in usersys).')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                botsglobal.logger.info(
                    _(u'Finished deleting in configuration.'))
    return django.shortcuts.redirect('/home')
예제 #7
0
파일: botsinit.py 프로젝트: peterklijn/bots
def generalinit(configdir):
    ##########################################################################
    #Configdir: settings.py & bots.ini#########################################
    #Configdir MUST be importable. So configdir is relative to PYTHONPATH. Try several options for this import.
    try:  #first check if is configdir outside bots-directory: import configdir.settings.py
        importnameforsettings = os.path.normpath(
            os.path.join(configdir, 'settings')).replace(os.sep, '.')
        settings = botslib.botsbaseimport(importnameforsettings)
    except ImportError:  #normal: configdir is in bots directory: import bots.configdir.settings.py
        try:
            importnameforsettings = os.path.normpath(
                os.path.join('bots', configdir,
                             'settings')).replace(os.sep, '.')
            settings = botslib.botsbaseimport(importnameforsettings)
        except ImportError:  #set pythonpath to config directory first
            if not os.path.exists(configdir):  #check if configdir exists.
                raise botslib.PanicError(
                    u'In initilisation: path to configuration does not exists: "%(configdir)s".',
                    {'configdir': configdir})
            addtopythonpath = os.path.abspath(os.path.dirname(configdir))
            moduletoimport = os.path.basename(configdir)
            sys.path.append(addtopythonpath)
            importnameforsettings = os.path.normpath(
                os.path.join(moduletoimport, 'settings')).replace(os.sep, '.')
            settings = botslib.botsbaseimport(importnameforsettings)
    #settings is imported, so now we know where to find settings.py: importnameforsettings
    #note: the imported settings.py itself is NOT used, this is doen via django.conf.settings
    configdirectory = os.path.abspath(os.path.dirname(settings.__file__))
    #Read configuration-file bots.ini.
    botsglobal.ini = BotsConfig()
    botsglobal.ini.read(os.path.join(configdirectory, 'bots.ini'))
    # 'directories','botspath': absolute path for bots directory
    botsglobal.ini.set('directories', 'botspath',
                       os.path.abspath(os.path.dirname(__file__)))
    # 'directories','config': absolute path for config directory
    botsglobal.ini.set('directories', 'config', configdirectory)
    #set config as originally received; used in starting engine via bots-monitor
    botsglobal.ini.set('directories', 'config_org', configdir)

    ############################################################################
    #Usersys####################################################################
    #usersys MUST be importable. So usersys is relative to PYTHONPATH. Try several options for this import.
    usersys = botsglobal.ini.get('directories', 'usersys', 'usersys')
    try:  #usersys outside bots-directory: import usersys
        importnameforusersys = os.path.normpath(usersys).replace(os.sep, '.')
        importedusersys = botslib.botsbaseimport(importnameforusersys)
    except ImportError:  #usersys is in bots directory: import bots.usersys
        try:
            importnameforusersys = os.path.normpath(
                os.path.join('bots', usersys)).replace(os.sep, '.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
        except ImportError:  #set pythonpath to usersys directory first
            if not os.path.exists(usersys):  #check if configdir exists.
                raise botslib.PanicError(
                    u'In initilisation: path to configuration does not exists: "%(usersys)s".',
                    {'usersys': usersys})
            addtopythonpath = os.path.abspath(os.path.dirname(usersys))  #????
            moduletoimport = os.path.basename(usersys)
            sys.path.append(addtopythonpath)
            importnameforusersys = os.path.normpath(usersys).replace(
                os.sep, '.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
    # 'directories','usersysabs': absolute path for config usersysabs
    botsglobal.ini.set('directories', 'usersysabs',
                       os.path.abspath(
                           os.path.dirname(importedusersys.__file__))
                       )  #???Find pathname usersys using imported usersys
    # botsglobal.usersysimportpath: used for imports from usersys
    botsglobal.usersysimportpath = importnameforusersys
    botsglobal.ini.set(
        'directories', 'templatehtml',
        botslib.join(botsglobal.ini.get('directories', 'usersysabs'),
                     'grammars/templatehtml/templates'))
    ############################################################################
    #Botssys####################################################################
    # 'directories','botssys': absolute path for config botssys
    botssys = botsglobal.ini.get('directories', 'botssys', 'botssys')
    botsglobal.ini.set('directories', 'botssys_org',
                       botssys)  #store original botssys setting
    botsglobal.ini.set('directories', 'botssys',
                       botslib.join(botssys))  #use absolute path
    botsglobal.ini.set('directories', 'data', botslib.join(botssys, 'data'))
    botsglobal.ini.set('directories', 'logging',
                       botslib.join(botssys, 'logging'))
    ############################################################################
    #other inits##############################################################
    if botsglobal.ini.get(
            'webserver', 'environment', 'development'
    ) != 'development':  #values in bots.ini are also used in setting up cherrypy
        logging.raiseExceptions = 0  # during production: if errors occurs in writing to log: ignore error. (leads to a missing log line, better than error;-).
    botslib.dirshouldbethere(botsglobal.ini.get('directories', 'data'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories', 'logging'))
    initbotscharsets()  #initialise bots charsets
    node.Node.checklevel = botsglobal.ini.getint('settings', 'get_checklevel',
                                                 1)
    botslib.settimeout(botsglobal.ini.getint('settings', 'globaltimeout', 10))
    ############################################################################
    #Init django#################################################################################
    os.environ['DJANGO_SETTINGS_MODULE'] = importnameforsettings
    import django
    if hasattr(django, 'setup'):
        django.setup()
    from django.conf import settings
    botsglobal.settings = settings  #settings are accessed using botsglobal
예제 #8
0
def load(pathzipfile):
    ''' process uploaded plugin. '''
    #test is valid zipfile
    if not zipfile.is_zipfile(pathzipfile):
        raise botslib.PluginError(_(u'Plugin is not a valid file.'))

    #read index file
    try:
        Zipimporter = zipimport.zipimporter(pathzipfile)
        importedbotsindex = Zipimporter.load_module('botsindex')
        pluglist = importedbotsindex.plugins[:]
        if 'botsindex' in sys.modules:
            del sys.modules['botsindex']
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(
            _(u'Error in plugin. Nothing is written. Error: "%s"') % (txt))
    else:
        botsglobal.logger.info(_(u'Plugin is OK.\nStart writing to database.'))

    #write content of index file to the bots database
    try:
        writetodatabase(pluglist)
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(
            'Error writing plugin to database. Nothing is written. Error: "%s"'
            % (txt))
    else:
        botsglobal.logger.info(
            u'Writing to database is OK.\nStart writing to files')

    #write files to the file system.
    try:
        warnrenamed = False  #to report in GUI files have been overwritten.
        z = zipfile.ZipFile(pathzipfile, mode="r")
        orgtargetpath = botsglobal.ini.get('directories', 'botspath')
        if (orgtargetpath[-1:] in (os.path.sep, os.path.altsep)
                and len(os.path.splitdrive(orgtargetpath)[1]) > 1):
            orgtargetpath = orgtargetpath[:-1]
        for f in z.infolist():
            if f.filename not in [
                    'botsindex.py', 'README', 'botssys/sqlitedb/botsdb',
                    'config/bots.ini'
            ] and os.path.splitext(f.filename)[1] not in ['.pyo', '.pyc']:
                #~ botsglobal.logger.info(u'filename in zip "%s".',f.filename)
                if f.filename[0] == '/':
                    targetpath = f.filename[1:]
                else:
                    targetpath = f.filename
                targetpath = targetpath.replace(
                    'usersys', botsglobal.ini.get('directories', 'usersysabs'),
                    1)
                targetpath = targetpath.replace(
                    'botssys', botsglobal.ini.get('directories', 'botssys'), 1)
                targetpath = botslib.join(orgtargetpath, targetpath)
                #targetpath is OK now.
                botsglobal.logger.info(_(u'    Start writing file: "%s".'),
                                       targetpath)

                if botslib.dirshouldbethere(os.path.dirname(targetpath)):
                    botsglobal.logger.info(
                        _(u'        Create directory "%s".'),
                        os.path.dirname(targetpath))
                if f.filename[
                        -1] == '/':  #check if this is a dir; if so continue
                    continue
                if os.path.isfile(targetpath):  #check if file already exists
                    try:  #this ***sometimes*** fails. (python25, for static/help/home.html...only there...)
                        os.rename(
                            targetpath,
                            targetpath + '.' + time.strftime('%Y%m%d%H%M%S'))
                        warnrenamed = True
                        botsglobal.logger.info(
                            _(u'        Renamed existing file "%(from)s" to "%(to)s".'
                              ), {
                                  'from': targetpath,
                                  'to':
                                  targetpath + time.strftime('%Y%m%d%H%M%S')
                              })
                    except:
                        pass
                source = z.read(f.filename)
                target = open(targetpath, "wb")
                target.write(source)
                target.close()
                botsglobal.logger.info(_(u'        File written: "%s".'),
                                       targetpath)
    except:
        txt = botslib.txtexc()
        z.close()
        raise botslib.PluginError(
            _(u'Error writing files to system. Nothing is written to database. Error: "%s"'
              ) % (txt))
    else:
        z.close()
        botsglobal.logger.info(_(u'Writing files to filesystem is OK.'))
        return warnrenamed
예제 #9
0
파일: views.py 프로젝트: divadrei/bots
def delete(request,*kw,**kwargs):
    if request.method == 'GET':
        form = forms.DeleteForm()
        return  django.shortcuts.render_to_response('bots/delete.html', {'form': form},context_instance=django.template.RequestContext(request))
    else:
        if 'submit' in request.POST:
            form = forms.DeleteForm(request.POST)
            if form.is_valid():
                botsglobal.logger.info(_(u'Start deleting in configuration.'))
                if form.cleaned_data['deltransactions']:
                    from django.db import connection, transaction
                    #while testing with very big loads, deleting transaction when wrong. Using raw SQL solved this.
                    cursor = connection.cursor()
                    cursor.execute("DELETE FROM ta")
                    cursor.execute("DELETE FROM filereport")
                    cursor.execute("DELETE FROM report")
                    transaction.commit_unless_managed()
                    messages.add_message(request, messages.INFO, _(u'Transactions are deleted.'))
                    botsglobal.logger.info(_(u'Transactions are deleted.'))
                    #clean data files
                    deletefrompath = botsglobal.ini.get('directories','data','botssys/data')
                    shutil.rmtree(deletefrompath,ignore_errors=True)
                    botslib.dirshouldbethere(deletefrompath)
                    notification = _(u'Data files are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                elif form.cleaned_data['delacceptance']:
                    from django.db.models import Min
                    list_file = []  #list of files for deletion in data-directory
                    report_idta_lowest = 0
                    for acc_report in models.report.objects.filter(acceptance=1): #for each acceptance report. is not very efficient.
                        if not report_idta_lowest:
                            report_idta_lowest = acc_report.idta
                        max_report_idta = models.report.objects.filter(idta__gt=acc_report.idta).aggregate(Min('idta'))['idta__min'] #select 'next' report...
                        if not max_report_idta: #if report is report of last run, there is no next report
                            max_report_idta = sys.maxint
                        #we have a idta-range now: from (and including) acc_report.idta till (and excluding) max_report_idta
                        list_file += models.ta.objects.filter(idta__gte=acc_report.idta,idta__lt=max_report_idta).exclude(status=1).values_list('filename', flat=True).distinct()
                        models.ta.objects.filter(idta__gte=acc_report.idta,idta__lt=max_report_idta).delete()   #delete ta in range 
                        models.filereport.objects.filter(idta__gte=acc_report.idta,idta__lt=max_report_idta).delete()   #delete filereports in range
                    if report_idta_lowest:
                        models.report.objects.filter(idta__gte=report_idta_lowest,acceptance=1).delete()     #delete all acceptance reports
                        for filename in list_file:      #delete all files in data directory geenrated during acceptance testing
                            if filename.isdigit():
                                botslib.deldata(filename)
                    notification = _(u'Transactions from acceptance-testing deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delconfiguration']:
                    models.confirmrule.objects.all().delete()
                    models.routes.objects.all().delete()
                    models.channel.objects.all().delete()
                    models.chanpar.objects.all().delete()
                    models.translate.objects.all().delete()
                    models.partner.objects.all().delete()
                    notification = _(u'Database configuration is deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delcodelists']:
                    models.ccode.objects.all().delete()
                    models.ccodetrigger.objects.all().delete()
                    notification = _(u'User code lists are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['delinfile']:
                    deletefrompath = botslib.join(botsglobal.ini.get('directories','botssys','botssys'),'infile')
                    shutil.rmtree(deletefrompath,ignore_errors=True)
                    notification = _(u'Files in botssys/infile are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['deloutfile']:
                    deletefrompath = botslib.join(botsglobal.ini.get('directories','botssys','botssys'),'outfile')
                    shutil.rmtree(deletefrompath,ignore_errors=True)
                    notification = _(u'Files in botssys/outfile are deleted.')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                if form.cleaned_data['deluserscripts']:
                    deletefrompath = botsglobal.ini.get('directories','usersysabs')
                    for root, dirs, files in os.walk(deletefrompath):
                        head, tail = os.path.split(root)
                        if tail == 'charsets':
                            del dirs[:]
                            continue
                        for bestand in files:
                            if bestand != '__init__.py':
                                os.remove(os.path.join(root,bestand))
                    notification = _(u'User scripts are deleted (in usersys).')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                elif form.cleaned_data['delbackup']:
                    deletefrompath = botsglobal.ini.get('directories','usersysabs')
                    for root, dirs, files in os.walk(deletefrompath):
                        head, tail = os.path.split(root)
                        if tail == 'charsets':
                            del dirs[:]
                            continue
                        for bestand in files:
                            name,ext = os.path.splitext(bestand)
                            if ext and len(ext) == 15 and ext[1:].isdigit() :
                                os.remove(os.path.join(root,bestand))
                    notification = _(u'Backupped user scripts are deleted/purged (in usersys).')
                    messages.add_message(request, messages.INFO, notification)
                    botsglobal.logger.info(notification)
                botsglobal.logger.info(_(u'Finished deleting in configuration.'))
    return django.shortcuts.redirect('/home')
예제 #10
0
def load(pathzipfile):
    ''' process uploaded plugin. '''
    #test is valid zipfile
    if not zipfile.is_zipfile(pathzipfile):
        raise botslib.PluginError(_(u'Plugin is not a valid file.'))

    #read index file
    try:
        myzipimport = zipimport.zipimporter(pathzipfile)
        importedbotsindex = myzipimport.load_module('botsindex')
        pluglist = importedbotsindex.plugins[:]
        if 'botsindex' in sys.modules:
            del sys.modules['botsindex']
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(_(u'Error in plugin. Nothing is written. Error:\n%s')%(txt))
    else:
        botsglobal.logger.info(_(u'Plugin is OK.'))
        botsglobal.logger.info(_(u'Start writing to database.'))

    #write content of index file to the bots database
    try:
        writetodatabase(pluglist)
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(_(u'Error writing plugin to database. Nothing is written. Error:\n%s'%(txt)))
    else:
        botsglobal.logger.info(u'Writing to database is OK.')
        botsglobal.logger.info(u'Start writing to files')

    #write files to the file system.
    try:
        warnrenamed = False     #to report in GUI files have been overwritten.
        myzip = zipfile.ZipFile(pathzipfile, mode="r")
        orgtargetpath = botsglobal.ini.get('directories','botspath')
        if (orgtargetpath[-1:] in (os.path.sep, os.path.altsep) and len(os.path.splitdrive(orgtargetpath)[1]) > 1):
            orgtargetpath = orgtargetpath[:-1]
        for zipfileobject in myzip.infolist():
            if zipfileobject.filename not in ['botsindex.py','README','botssys/sqlitedb/botsdb','config/bots.ini'] and os.path.splitext(zipfileobject.filename)[1] not in ['.pyo','.pyc']:
                #~ botsglobal.logger.info(u'filename in zip "%s".',zipfileobject.filename)
                if zipfileobject.filename[0] == '/':
                    targetpath = zipfileobject.filename[1:]
                else:
                    targetpath = zipfileobject.filename
                targetpath = targetpath.replace('usersys',botsglobal.ini.get('directories','usersysabs'),1)
                targetpath = targetpath.replace('botssys',botsglobal.ini.get('directories','botssys'),1)
                targetpath = botslib.join(orgtargetpath, targetpath)
                #targetpath is OK now.
                botsglobal.logger.info(_(u'    Start writing file: "%s".'),targetpath)

                if botslib.dirshouldbethere(os.path.dirname(targetpath)):
                    botsglobal.logger.info(_(u'        Create directory "%s".'),os.path.dirname(targetpath))
                if zipfileobject.filename[-1] == '/':    #check if this is a dir; if so continue
                    continue
                if os.path.isfile(targetpath):  #check if file already exists
                    try:    #this ***sometimes*** fails. (python25, for static/help/home.html...only there...)
                        os.rename(targetpath,targetpath+'.'+time.strftime('%Y%m%d%H%M%S'))
                        warnrenamed = True
                        botsglobal.logger.info(_(u'        Renamed existing file "%(from)s" to "%(to)s".'),{'from':targetpath,'to':targetpath+time.strftime('%Y%m%d%H%M%S')})
                    except:
                        pass
                source = myzip.read(zipfileobject.filename)
                target = open(targetpath, "wb")
                target.write(source)
                target.close()
                botsglobal.logger.info(_(u'        File written: "%s".'),targetpath)
    except:
        txt = botslib.txtexc()
        myzip.close()
        raise botslib.PluginError(_(u'Error writing files to system. Nothing is written to database. Error:\n%s')%(txt))
    else:
        myzip.close()
        botsglobal.logger.info(_(u'Writing files to filesystem is OK.'))
        return warnrenamed
예제 #11
0
def generalinit(configdir):
    #Set Configdir 
    #Configdir MUST be importable. So configdir is relative to PYTHONPATH. Try several options for this import.
    try:                        #configdir outside bots-directory: import configdir.settings.py
        importnameforsettings = os.path.normpath(os.path.join(configdir,'settings')).replace(os.sep,'.')
        settings = botslib.botsbaseimport(importnameforsettings)
    except ImportError:         #configdir is in bots directory: import bots.configdir.settings.py
        try:
            importnameforsettings = os.path.normpath(os.path.join('bots',configdir,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
        except ImportError:     #set pythonpath to config directory first
            if not os.path.exists(configdir):    #check if configdir exists.
                raise botslib.BotsError(_(u'In initilisation: path to configuration does not exists: "$path".'),path=configdir)
            addtopythonpath = os.path.abspath(os.path.dirname(configdir))
            #~ print 'add pythonpath for usersys',addtopythonpath
            moduletoimport = os.path.basename(configdir)
            sys.path.append(addtopythonpath)
            importnameforsettings = os.path.normpath(os.path.join(moduletoimport,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
    #settings are accessed using botsglobal
    botsglobal.settings = settings  
    #Find pathname configdir using imported settings.py.
    configdirectory = os.path.abspath(os.path.dirname(settings.__file__))
            
    #Read configuration-file bots.ini.
    botsglobal.ini = BotsConfig()
    cfgfile = open(os.path.join(configdirectory,'bots.ini'), 'r')
    botsglobal.ini.readfp(cfgfile)
    cfgfile.close()
    
    #Set usersys. 
    #usersys MUST be importable. So usersys is relative to PYTHONPATH. Try several options for this import.
    usersys = botsglobal.ini.get('directories','usersys','usersys')
    try:                        #usersys outside bots-directory: import usersys
        importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
        importedusersys = botslib.botsbaseimport(importnameforusersys)
    except ImportError:         #usersys is in bots directory: import bots.usersys
        try:
            importnameforusersys = os.path.normpath(os.path.join('bots',usersys)).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
        except ImportError:     #set pythonpath to usersys directory first
            if not os.path.exists(usersys):    #check if configdir exists.
                raise botslib.BotsError(_(u'In initilisation: path to configuration does not exists: "$path".'),path=usersys)
            addtopythonpath = os.path.abspath(os.path.dirname(usersys))     #????
            moduletoimport = os.path.basename(usersys)
            #~ print 'add pythonpath for usersys',addtopythonpath
            sys.path.append(addtopythonpath)
            importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
            
    #set directory settings in bots.ini************************************************************
    botsglobal.ini.set('directories','botspath',botsglobal.settings.PROJECT_PATH)
    botsglobal.ini.set('directories','config',configdirectory)
    botsglobal.ini.set('directories','usersysabs',os.path.abspath(os.path.dirname(importedusersys.__file__)))    #???Find pathname usersys using imported usersys
    
    botsglobal.usersysimportpath = importnameforusersys
    botssys = botsglobal.ini.get('directories','botssys','botssys')
    botsglobal.ini.set('directories','botssys',botslib.join(botssys))
    
    botsglobal.ini.set('directories','data',botslib.join(botssys,'data'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories','data'))
    
    botsglobal.ini.set('directories','logging',botslib.join(botssys,'logging'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories','logging'))
    botsglobal.ini.set('directories','templates',botslib.join(botsglobal.ini.get('directories','usersysabs'),'grammars/template/templates'))
    botsglobal.ini.set('directories','templateshtml',botslib.join(botsglobal.ini.get('directories','usersysabs'),'grammars/templatehtml/templates'))

    #set values in setting.py**********************************************************************
    if botsglobal.ini.get('webserver','environment','development') == 'development':   #values in bots.ini are also used in setting up cherrypy
        settings.DEBUG = True
    else:
        settings.DEBUG = False
    settings.TEMPLATE_DEBUG = settings.DEBUG
    #set paths in settings.py:
    #~ settings.FILE_UPLOAD_TEMP_DIR = os.path.join(settings.PROJECT_PATH, 'botssys/pluginsuploaded')

    #start initializing bots charsets
    initbotscharsets()
    #set environment for django to start***************************************************************************************************
    os.environ['DJANGO_SETTINGS_MODULE'] = importnameforsettings
    initbotscharsets()
    botslib.settimeout(botsglobal.ini.getint('settings','globaltimeout',10))    #
예제 #12
0
def read_plugin(pathzipfile):
    ''' process uploaded plugin. '''
    #test if valid zipfile
    if not zipfile.is_zipfile(pathzipfile):
        raise botslib.PluginError(_(u'Plugin is not a valid file.'))

    #read index file
    try:
        myzipimport = zipimport.zipimporter(pathzipfile)
        importedbotsindex = myzipimport.load_module('botsindex')
        pluglist = importedbotsindex.plugins[:]
        if 'botsindex' in sys.modules:
            del sys.modules['botsindex']
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(_(u'Error in plugin. Nothing is written. Error:\n%(txt)s'),{'txt':txt})
    else:
        botsglobal.logger.info(_(u'Plugin is OK.'))
        botsglobal.logger.info(_(u'Start writing to database.'))

    #write content of index file to the bots database
    try:
        read_index2database(pluglist)
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(_(u'Error writing plugin to database. Nothing is written. Error:\n%(txt)s'),{'txt':txt})
    else:
        botsglobal.logger.info(_(u'Writing to database is OK.'))

    #write files to the file system.
    botsglobal.logger.info(_(u'Start writing to files'))
    try:
        warnrenamed = False     #to report in GUI files have been overwritten.
        myzip = zipfile.ZipFile(pathzipfile, mode="r")
        orgtargetpath = botsglobal.ini.get('directories','botspath')
        if (orgtargetpath[-1:] in (os.path.sep, os.path.altsep) and len(os.path.splitdrive(orgtargetpath)[1]) > 1):
            orgtargetpath = orgtargetpath[:-1]
        for zipfileobject in myzip.infolist():
            if zipfileobject.filename not in ['botsindex.py','README','botssys/sqlitedb/botsdb','config/bots.ini'] and os.path.splitext(zipfileobject.filename)[1] not in ['.pyo','.pyc']:
                #~ botsglobal.logger.info(u'Filename in zip "%s".',zipfileobject.filename)
                if zipfileobject.filename[0] == '/':
                    targetpath = zipfileobject.filename[1:]
                else:
                    targetpath = zipfileobject.filename
                #convert for correct environment: repacle botssys, config, usersys in filenames
                if targetpath.startswith('usersys'):
                    targetpath = targetpath.replace('usersys',botsglobal.ini.get('directories','usersysabs'),1)
                elif targetpath.startswith('botssys'):
                    targetpath = targetpath.replace('botssys',botsglobal.ini.get('directories','botssys'),1)
                elif targetpath.startswith('config'):
                    targetpath = targetpath.replace('config',botsglobal.ini.get('directories','config'),1)
                targetpath = botslib.join(orgtargetpath, targetpath)
                #targetpath is OK now.
                botsglobal.logger.info(_(u'    Start writing file: "%(targetpath)s".'),{'targetpath':targetpath})

                if botslib.dirshouldbethere(os.path.dirname(targetpath)):
                    botsglobal.logger.info(_(u'        Create directory "%(directory)s".'),{'directory':os.path.dirname(targetpath)})
                if zipfileobject.filename[-1] == '/':    #check if this is a dir; if so continue
                    continue
                if os.path.isfile(targetpath):  #check if file already exists
                    try:    #this ***sometimes*** fails. (python25, for static/help/home.html...only there...)
                        warnrenamed = True
                    except:
                        pass
                source = myzip.read(zipfileobject.filename)
                target = open(targetpath, "wb")
                target.write(source)
                target.close()
                botsglobal.logger.info(_(u'        File written: "%(targetpath)s".'),{'targetpath':targetpath})
    except:
        txt = botslib.txtexc()
        myzip.close()
        raise botslib.PluginError(_(u'Error writing files to system. Nothing is written to database. Error:\n%(txt)s'),{'txt':txt})
    else:
        myzip.close()
        botsglobal.logger.info(_(u'Writing files to filesystem is OK.'))
        return warnrenamed
예제 #13
0
def generalinit(configdir):
    #Set Configdir
    #Configdir MUST be importable. So configdir is relative to PYTHONPATH. Try several options for this import.
    try:  #configdir outside bots-directory: import configdir.settings.py
        importnameforsettings = os.path.normpath(
            os.path.join(configdir, 'settings')).replace(os.sep, '.')
        settings = botslib.botsbaseimport(importnameforsettings)
    except ImportError:  #configdir is in bots directory: import bots.configdir.settings.py
        try:
            importnameforsettings = os.path.normpath(
                os.path.join('bots', configdir,
                             'settings')).replace(os.sep, '.')
            settings = botslib.botsbaseimport(importnameforsettings)
        except ImportError:  #set pythonpath to config directory first
            if not os.path.exists(configdir):  #check if configdir exists.
                raise botslib.BotsError(_(
                    u'In initilisation: path to configuration does not exists: "$path".'
                ),
                                        path=configdir)
            addtopythonpath = os.path.abspath(os.path.dirname(configdir))
            #~ print 'add pythonpath for usersys',addtopythonpath
            moduletoimport = os.path.basename(configdir)
            sys.path.append(addtopythonpath)
            importnameforsettings = os.path.normpath(
                os.path.join(moduletoimport, 'settings')).replace(os.sep, '.')
            settings = botslib.botsbaseimport(importnameforsettings)
    #settings are accessed using botsglobal
    botsglobal.settings = settings
    #Find pathname configdir using imported settings.py.
    configdirectory = os.path.abspath(os.path.dirname(settings.__file__))

    #Read configuration-file bots.ini.
    botsglobal.ini = BotsConfig()
    cfgfile = open(os.path.join(configdirectory, 'bots.ini'), 'r')
    botsglobal.ini.readfp(cfgfile)
    cfgfile.close()

    #Set usersys.
    #usersys MUST be importable. So usersys is relative to PYTHONPATH. Try several options for this import.
    usersys = botsglobal.ini.get('directories', 'usersys', 'usersys')
    try:  #usersys outside bots-directory: import usersys
        importnameforusersys = os.path.normpath(usersys).replace(os.sep, '.')
        importedusersys = botslib.botsbaseimport(importnameforusersys)
    except ImportError:  #usersys is in bots directory: import bots.usersys
        try:
            importnameforusersys = os.path.normpath(
                os.path.join('bots', usersys)).replace(os.sep, '.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
        except ImportError:  #set pythonpath to usersys directory first
            if not os.path.exists(usersys):  #check if configdir exists.
                raise botslib.BotsError(_(
                    u'In initilisation: path to configuration does not exists: "$path".'
                ),
                                        path=usersys)
            addtopythonpath = os.path.abspath(os.path.dirname(usersys))  #????
            moduletoimport = os.path.basename(usersys)
            #~ print 'add pythonpath for usersys',addtopythonpath
            sys.path.append(addtopythonpath)
            importnameforusersys = os.path.normpath(usersys).replace(
                os.sep, '.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)

    #set directory settings in bots.ini************************************************************
    botsglobal.ini.set('directories', 'botspath',
                       botsglobal.settings.PROJECT_PATH)
    botsglobal.ini.set('directories', 'config', configdirectory)
    botsglobal.ini.set('directories', 'usersysabs',
                       os.path.abspath(
                           os.path.dirname(importedusersys.__file__))
                       )  #???Find pathname usersys using imported usersys

    botsglobal.usersysimportpath = importnameforusersys
    botssys = botsglobal.ini.get('directories', 'botssys', 'botssys')
    botsglobal.ini.set('directories', 'botssys', botslib.join(botssys))

    botsglobal.ini.set('directories', 'data', botslib.join(botssys, 'data'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories', 'data'))

    botsglobal.ini.set('directories', 'logging',
                       botslib.join(botssys, 'logging'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories', 'logging'))
    botsglobal.ini.set(
        'directories', 'templates',
        botslib.join(botsglobal.ini.get('directories', 'usersysabs'),
                     'grammars/template/templates'))
    botsglobal.ini.set(
        'directories', 'templateshtml',
        botslib.join(botsglobal.ini.get('directories', 'usersysabs'),
                     'grammars/templatehtml/templates'))

    #set values in setting.py**********************************************************************
    if botsglobal.ini.get(
            'webserver', 'environment', 'development'
    ) == 'development':  #values in bots.ini are also used in setting up cherrypy
        settings.DEBUG = True
    else:
        settings.DEBUG = False
    settings.TEMPLATE_DEBUG = settings.DEBUG
    #set paths in settings.py:
    #~ settings.FILE_UPLOAD_TEMP_DIR = os.path.join(settings.PROJECT_PATH, 'botssys/pluginsuploaded')

    #start initializing bots charsets
    initbotscharsets()
    #set environment for django to start***************************************************************************************************
    os.environ['DJANGO_SETTINGS_MODULE'] = importnameforsettings
    initbotscharsets()
    botslib.settimeout(botsglobal.ini.getint('settings', 'globaltimeout',
                                             10))  #
예제 #14
0
def generalinit(configdir):
    ##########################################################################
    #Configdir: settings.py & bots.ini#########################################
    #Configdir MUST be importable. So configdir is relative to PYTHONPATH. Try several options for this import.
    try:                        #first check if is configdir outside bots-directory: import configdir.settings.py
        importnameforsettings = os.path.normpath(os.path.join(configdir,'settings')).replace(os.sep,'.')
        settings = botslib.botsbaseimport(importnameforsettings)
    except ImportError:         #normal: configdir is in bots directory: import bots.configdir.settings.py
        try:
            importnameforsettings = os.path.normpath(os.path.join('bots',configdir,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
        except ImportError:     #set pythonpath to config directory first
            if not os.path.exists(configdir):    #check if configdir exists.
                raise botslib.PanicError(u'In initilisation: path to configuration does not exists: "%(configdir)s".',{'configdir':configdir})
            addtopythonpath = os.path.abspath(os.path.dirname(configdir))
            moduletoimport = os.path.basename(configdir)
            sys.path.append(addtopythonpath)
            importnameforsettings = os.path.normpath(os.path.join(moduletoimport,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
    #settings is imported, so now we know where to find settings.py: importnameforsettings
    #note: the imported settings.py itself is NOT used, this is doen via django.conf.settings
    configdirectory = os.path.abspath(os.path.dirname(settings.__file__))
    #Read configuration-file bots.ini.
    botsglobal.ini = BotsConfig()
    botsglobal.ini.read(os.path.join(configdirectory,'bots.ini'))
    # 'directories','botspath': absolute path for bots directory
    botsglobal.ini.set('directories','botspath',os.path.abspath(os.path.dirname(__file__)))
    # 'directories','config': absolute path for config directory
    botsglobal.ini.set('directories','config',configdirectory)
    #set config as originally received; used in starting engine via bots-monitor
    botsglobal.ini.set('directories','config_org',configdir)
    
    ############################################################################
    #Usersys####################################################################
    #usersys MUST be importable. So usersys is relative to PYTHONPATH. Try several options for this import.
    usersys = botsglobal.ini.get('directories','usersys','usersys')
    try:                        #usersys outside bots-directory: import usersys
        importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
        importedusersys = botslib.botsbaseimport(importnameforusersys)
    except ImportError:         #usersys is in bots directory: import bots.usersys
        try:
            importnameforusersys = os.path.normpath(os.path.join('bots',usersys)).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
        except ImportError:     #set pythonpath to usersys directory first
            if not os.path.exists(usersys):    #check if configdir exists.
                raise botslib.PanicError(u'In initilisation: path to configuration does not exists: "%(usersys)s".',{'usersys':usersys})
            addtopythonpath = os.path.abspath(os.path.dirname(usersys))     #????
            moduletoimport = os.path.basename(usersys)
            sys.path.append(addtopythonpath)
            importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
    # 'directories','usersysabs': absolute path for config usersysabs
    botsglobal.ini.set('directories','usersysabs',os.path.abspath(os.path.dirname(importedusersys.__file__)))    #???Find pathname usersys using imported usersys
    # botsglobal.usersysimportpath: used for imports from usersys
    botsglobal.usersysimportpath = importnameforusersys
    botsglobal.ini.set('directories','templatehtml',botslib.join(botsglobal.ini.get('directories','usersysabs'),'grammars/templatehtml/templates'))
    ############################################################################
    #Botssys####################################################################
    # 'directories','botssys': absolute path for config botssys
    botssys = botsglobal.ini.get('directories','botssys','botssys')
    botsglobal.ini.set('directories','botssys_org',botssys)             #store original botssys setting
    botsglobal.ini.set('directories','botssys',botslib.join(botssys))   #use absolute path
    botsglobal.ini.set('directories','data',botslib.join(botssys,'data'))
    botsglobal.ini.set('directories','logging',botslib.join(botssys,'logging'))
    ############################################################################
    #other inits##############################################################
    if botsglobal.ini.get('webserver','environment','development') != 'development':   #values in bots.ini are also used in setting up cherrypy
        logging.raiseExceptions = 0     # during production: if errors occurs in writing to log: ignore error. (leads to a missing log line, better than error;-).
    botslib.dirshouldbethere(botsglobal.ini.get('directories','data'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories','logging'))
    initbotscharsets()  #initialise bots charsets
    node.Node.checklevel = botsglobal.ini.getint('settings','get_checklevel',1)
    botslib.settimeout(botsglobal.ini.getint('settings','globaltimeout',10))
    ############################################################################
    #Init django#################################################################################
    os.environ['DJANGO_SETTINGS_MODULE'] = importnameforsettings
    import django
    if hasattr(django,'setup'):
        django.setup()
    from django.conf import settings
    botsglobal.settings = settings      #settings are accessed using botsglobal 
예제 #15
0
def read_plugin(pathzipfile):
    """ process uploaded plugin. """
    # test if valid zipfile
    if not zipfile.is_zipfile(pathzipfile):
        raise botslib.PluginError(_(u"Plugin is not a valid file."))

    # read index file
    try:
        myzipimport = zipimport.zipimporter(pathzipfile)
        importedbotsindex = myzipimport.load_module("botsindex")
        pluglist = importedbotsindex.plugins[:]
        if "botsindex" in sys.modules:
            del sys.modules["botsindex"]
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(_(u"Error in plugin. Nothing is written. Error:\n%(txt)s"), {"txt": txt})
    else:
        botsglobal.logger.info(_(u"Plugin is OK."))
        botsglobal.logger.info(_(u"Start writing to database."))

    # write content of index file to the bots database
    try:
        read_index2database(pluglist)
    except:
        txt = botslib.txtexc()
        raise botslib.PluginError(
            _(u"Error writing plugin to database. Nothing is written. Error:\n%(txt)s"), {"txt": txt}
        )
    else:
        botsglobal.logger.info(_(u"Writing to database is OK."))

    # write files to the file system.
    botsglobal.logger.info(_(u"Start writing to files"))
    try:
        warnrenamed = False  # to report in GUI files have been overwritten.
        myzip = zipfile.ZipFile(pathzipfile, mode="r")
        orgtargetpath = botsglobal.ini.get("directories", "botspath")
        if orgtargetpath[-1:] in (os.path.sep, os.path.altsep) and len(os.path.splitdrive(orgtargetpath)[1]) > 1:
            orgtargetpath = orgtargetpath[:-1]
        for zipfileobject in myzip.infolist():
            if zipfileobject.filename not in [
                "botsindex.py",
                "README",
                "botssys/sqlitedb/botsdb",
                "config/bots.ini",
            ] and os.path.splitext(zipfileobject.filename)[1] not in [".pyo", ".pyc"]:
                # ~ botsglobal.logger.info(u'Filename in zip "%s".',zipfileobject.filename)
                if zipfileobject.filename[0] == "/":
                    targetpath = zipfileobject.filename[1:]
                else:
                    targetpath = zipfileobject.filename
                # convert for correct environment: repacle botssys, config, usersys in filenames
                if targetpath.startswith("usersys"):
                    targetpath = targetpath.replace("usersys", botsglobal.ini.get("directories", "usersysabs"), 1)
                elif targetpath.startswith("botssys"):
                    targetpath = targetpath.replace("botssys", botsglobal.ini.get("directories", "botssys"), 1)
                elif targetpath.startswith("config"):
                    targetpath = targetpath.replace("config", botsglobal.ini.get("directories", "config"), 1)
                targetpath = botslib.join(orgtargetpath, targetpath)
                # targetpath is OK now.
                botsglobal.logger.info(_(u'    Start writing file: "%(targetpath)s".'), {"targetpath": targetpath})

                if botslib.dirshouldbethere(os.path.dirname(targetpath)):
                    botsglobal.logger.info(
                        _(u'        Create directory "%(directory)s".'), {"directory": os.path.dirname(targetpath)}
                    )
                if zipfileobject.filename[-1] == "/":  # check if this is a dir; if so continue
                    continue
                if os.path.isfile(targetpath):  # check if file already exists
                    try:  # this ***sometimes*** fails. (python25, for static/help/home.html...only there...)
                        warnrenamed = True
                    except:
                        pass
                source = myzip.read(zipfileobject.filename)
                target = open(targetpath, "wb")
                target.write(source)
                target.close()
                botsglobal.logger.info(_(u'        File written: "%(targetpath)s".'), {"targetpath": targetpath})
    except:
        txt = botslib.txtexc()
        myzip.close()
        raise botslib.PluginError(
            _(u"Error writing files to system. Nothing is written to database. Error:\n%(txt)s"), {"txt": txt}
        )
    else:
        myzip.close()
        botsglobal.logger.info(_(u"Writing files to filesystem is OK."))
        return warnrenamed
예제 #16
0
파일: botsinit.py 프로젝트: divadrei/bots
def generalinit(configdir):
    #Set Configdir
    #Configdir MUST be importable. So configdir is relative to PYTHONPATH. Try several options for this import.
    try:                        #configdir outside bots-directory: import configdir.settings.py
        importnameforsettings = os.path.normpath(os.path.join(configdir,'settings')).replace(os.sep,'.')
        settings = botslib.botsbaseimport(importnameforsettings)
    except ImportError:         #configdir is in bots directory: import bots.configdir.settings.py
        try:
            importnameforsettings = os.path.normpath(os.path.join('bots',configdir,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
        except ImportError:     #set pythonpath to config directory first
            if not os.path.exists(configdir):    #check if configdir exists.
                raise botslib.BotsError(u'In initilisation: path to configuration does not exists: "%(configdir)s".',{'configdir':configdir})
            addtopythonpath = os.path.abspath(os.path.dirname(configdir))
            #~ print 'add pythonpath for usersys',addtopythonpath
            moduletoimport = os.path.basename(configdir)
            sys.path.append(addtopythonpath)
            importnameforsettings = os.path.normpath(os.path.join(moduletoimport,'settings')).replace(os.sep,'.')
            settings = botslib.botsbaseimport(importnameforsettings)
    #settings are accessed using botsglobal
    botsglobal.settings = settings
    if hasattr(settings,'DATABASE_ENGINE'):      #check for old django settings.py
        print u'You use an old settings.py. Please change settings.py first. See migration instructions in wiki: http://code.google.com/p/bots/wiki/Migrate'
        sys.exit(0)
    #Find pathname configdir using imported settings.py.
    configdirectory = os.path.abspath(os.path.dirname(settings.__file__))

    #Read configuration-file bots.ini.
    botsglobal.ini = BotsConfig()
    botsglobal.ini.read(os.path.join(configdirectory,'bots.ini'))

    #Set usersys.
    #usersys MUST be importable. So usersys is relative to PYTHONPATH. Try several options for this import.
    usersys = botsglobal.ini.get('directories','usersys','usersys')
    try:                        #usersys outside bots-directory: import usersys
        importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
        importedusersys = botslib.botsbaseimport(importnameforusersys)
    except ImportError:         #usersys is in bots directory: import bots.usersys
        try:
            importnameforusersys = os.path.normpath(os.path.join('bots',usersys)).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)
        except ImportError:     #set pythonpath to usersys directory first
            if not os.path.exists(usersys):    #check if configdir exists.
                raise botslib.BotsError(u'In initilisation: path to configuration does not exists: "%(usersys)s".',{'usersys':usersys})
            addtopythonpath = os.path.abspath(os.path.dirname(usersys))     #????
            moduletoimport = os.path.basename(usersys)
            #~ print 'add pythonpath for usersys',addtopythonpath
            sys.path.append(addtopythonpath)
            importnameforusersys = os.path.normpath(usersys).replace(os.sep,'.')
            importedusersys = botslib.botsbaseimport(importnameforusersys)

    #set directory settings in bots.ini************************************************************
    # 'directories','botspath': absolute path for bots directory
    botsglobal.ini.set('directories','botspath',settings.PROJECT_PATH)
    # 'directories','config': absolute path for config directory
    botsglobal.ini.set('directories','config',configdirectory)
    botsglobal.ini.set('directories','config_org',configdir)            #set config as originally received.
    # 'directories','usersysabs': absolute path for config usersysabs
    botsglobal.ini.set('directories','usersysabs',os.path.abspath(os.path.dirname(importedusersys.__file__)))    #???Find pathname usersys using imported usersys
    # botsglobal.usersysimportpath: used for imports from usersys
    botsglobal.usersysimportpath = importnameforusersys
    # 'directories','botssys': absolute path for config botssys
    botssys = botsglobal.ini.get('directories','botssys','botssys')
    botsglobal.ini.set('directories','botssys_org',botssys)             #store original botssys setting
    botsglobal.ini.set('directories','botssys',botslib.join(botssys))

    botsglobal.ini.set('directories','data',botslib.join(botssys,'data'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories','data'))

    botsglobal.ini.set('directories','logging',botslib.join(botssys,'logging'))
    botslib.dirshouldbethere(botsglobal.ini.get('directories','logging'))
    botsglobal.ini.set('directories','templates',botslib.join(botsglobal.ini.get('directories','usersysabs'),'grammars/template/templates'))
    botsglobal.ini.set('directories','templateshtml',botslib.join(botsglobal.ini.get('directories','usersysabs'),'grammars/templatehtml/templates'))

    if botsglobal.ini.get('webserver','environment','development') != 'development':   #values in bots.ini are also used in setting up cherrypy
        logging.raiseExceptions = 0     # during production: if errors occurs in writing to log: ignore error. (leads to a missing log line, better than error;-).

    #initialise bots charsets
    initbotscharsets()
    #set environment for django to start***************************************************************************************************
    os.environ['DJANGO_SETTINGS_MODULE'] = importnameforsettings
    botslib.settimeout(botsglobal.ini.getint('settings','globaltimeout',10))