Пример #1
0
    def handle(self, options, global_options, *args):
        from uliweb.core.SimpleFrame import get_app_dir
        from uliweb import orm

        engine = get_engine(options, global_options)

        if not args:
            apps_list = self.get_apps(global_options)
        else:
            apps_list = args

        for p in apps_list:
            if not is_pyfile_exist(get_app_dir(p), 'dbinit'):
                continue
            m = '%s.dbinit' % p
            try:
                if global_options.verbose:
                    print "[%s] Processing %s..." % (options.engine, m)
                orm.Begin()
                mod = __import__(m, fromlist=['*'])
                orm.Commit()
            except ImportError:
                orm.Rollback()
                log.exception(
                    "There are something wrong when importing module [%s]" % m)
Пример #2
0
    def handle(self, options, global_options, *args):
        from uliweb import orm

        if args:
            message = """This command will delete all data of [%s]-[%s] before loading, 
are you sure to load data""" % (options.engine, ','.join(args))
        else:
            print "Failed! You should pass one or more tables name."
            sys.exit(1)

        ans = get_answer(message, answers='Yn', quit='q')

        path = os.path.join(options.dir, options.engine)
        if not os.path.exists(path):
            os.makedirs(path)

        engine = get_engine(options, global_options)

        tables = get_sorted_tables(
            get_tables(global_options.apps_dir,
                       engine_name=options.engine,
                       settings_file=global_options.settings,
                       tables=args,
                       local_settings_file=global_options.local_settings))
        _len = len(tables)

        for i, (name, t) in enumerate(tables):
            if t.__mapping_only__:
                if global_options.verbose:
                    msg = 'SKIPPED(Mapping Table)'
                    print '[%s] Loading %s...%s' % (
                        options.engine, show_table(name, t, i, _len), msg)
                continue
            if global_options.verbose:
                print '[%s] Loading %s...' % (options.engine,
                                              show_table(name, t, i, _len)),
            try:
                orm.Begin()
                filename = os.path.join(path, name + '.txt')
                if options.text:
                    format = 'txt'
                else:
                    format = None
                t = load_table(t,
                               filename,
                               engine,
                               delimiter=options.delimiter,
                               format=format,
                               encoding=options.encoding,
                               delete=ans == 'Y',
                               bulk=int(options.bulk),
                               engine_name=engine.engine_name)
                orm.Commit()
                if global_options.verbose:
                    print t
            except:
                log.exception(
                    "There are something wrong when loading table [%s]" % name)
                orm.Rollback()
Пример #3
0
    def handle(self, options, global_options, *args):
        from uliweb import orm

        if len(args) != 2:
            print self.print_help(self.prog_name, 'loadtablefile')
            sys.exit(1)

        if args:
            message = """Do you want to delete all data of [%s]-[%s] before loading, if you choose N, the data will not be deleted""" % (
                options.engine, args[0])
        else:
            print "Failed! You should pass one or more tables name."
            sys.exit(1)

        ans = 'Y' if global_options.yes else get_answer(message, quit='q')

        engine = get_engine(options, global_options)

        name = args[0]
        tables = get_tables(global_options.apps_dir,
                            engine_name=options.engine,
                            settings_file=global_options.settings,
                            tables=[name],
                            local_settings_file=global_options.local_settings)
        t = tables[name]
        if t.__mapping_only__:
            if global_options.verbose:
                msg = 'SKIPPED(Mapping Table)'
                print '[%s] Loading %s...%s' % (
                    options.engine, show_table(name, t, i, _len), msg)
            return

        if global_options.verbose:
            print '[%s] Loading %s...' % (options.engine,
                                          show_table(name, t, 0, 1)),
        try:
            orm.Begin()
            if options.text:
                format = 'txt'
            else:
                format = None
            t = load_table(t,
                           args[1],
                           engine,
                           delimiter=options.delimiter,
                           format=format,
                           encoding=options.encoding,
                           delete=ans == 'Y',
                           bulk=int(options.bulk),
                           engine_name=engine.engine_name)
            orm.Commit()
            if global_options.verbose:
                print t
        except:
            log.exception("There are something wrong when loading table [%s]" %
                          name)
            orm.Rollback()
Пример #4
0
 def _f(table, filename, msg):
     session = orm.get_session()
     if session:
         session.close()
     orm.Begin()
     try:
         result = load_table(table, filename, engine, delimiter=options.delimiter,
             format=format, encoding=options.encoding, delete=ans=='Y',
             bulk=int(options.bulk), engine_name=engine.engine_name)
         if global_options.verbose:
             print(msg, result)
         orm.Commit()
     except:
         orm.Rollback()
Пример #5
0
    def handle(self, options, global_options, *args):
        from uliweb import orm

        if args:
            message = """This command will delete all data of [%s]-[%s] before loading, 
are you sure to load data""" % (options.engine, ','.join(args))
        else:
            print "Failed! You should pass one or more tables name."
            sys.exit(1)

        ans = get_answer(message, answers='Yn', quit='q')

        path = os.path.join(options.dir, options.engine)
        if not os.path.exists(path):
            os.makedirs(path)

        engine = get_engine(options, global_options)

        tables = get_tables(global_options.apps_dir,
                            engine=options.engine,
                            settings_file=global_options.settings,
                            tables=args,
                            local_settings_file=global_options.local_settings)
        for name, t in tables.items():
            if global_options.verbose:
                print '[%s] Loading %s...' % (options.engine, name)
            try:
                orm.Begin()
                filename = os.path.join(path, name + '.txt')
                if options.text:
                    format = 'txt'
                else:
                    format = None
                load_table(t,
                           filename,
                           engine,
                           delimiter=options.delimiter,
                           format=format,
                           encoding=options.encoding,
                           delete=ans == 'Y')
                orm.Commit()
            except:
                log.exception(
                    "There are something wrong when loading table [%s]" % name)
                orm.Rollback()
Пример #6
0
    def handle(self, options, global_options, *args):
        from uliweb import orm
        
        if args:
            message = """This command will delete all data of [%s]-[%s] before loading, 
are you sure to load data""" % (options.engine, ','.join(args))
        else:
            message = """This command will delete whole database [%s] before loading, 
are you sure to load data""" % options.engine

        get_answer(message)

        path = os.path.join(options.dir, options.engine)
        if not os.path.exists(path):
            os.makedirs(path)
        
        engine = get_engine(options, global_options)

        tables = get_sorted_tables(get_tables(global_options.apps_dir, args, 
            engine_name=options.engine, 
            settings_file=global_options.settings, 
            local_settings_file=global_options.local_settings))
        _len = len(tables)
        for i, (name, t) in enumerate(tables):
            if global_options.verbose:
                print '[%s] Loading %s...' % (options.engine, show_table(name, t, i, _len))
            try:
                orm.Begin()
                filename = os.path.join(path, name+'.txt')
                if options.text:
                    format = 'txt'
                else:
                    format = None
                load_table(t, filename, engine, delimiter=options.delimiter, 
                    format=format, encoding=options.encoding)
                orm.Commit()
            except:
                log.exception("There are something wrong when loading table [%s]" % name)
                orm.Rollback()
Пример #7
0
    def handle(self, options, global_options, *args):
        from uliweb import orm
        from zipfile import ZipFile
        import shutil

        if args:
            message = """This command will delete all data of [%s]-[%s] before loading, 
are you sure to load data""" % (options.engine, ','.join(args))
        else:
            message = """This command will delete whole database [%s] before loading, 
are you sure to load data""" % options.engine

        ans = 'Y' if global_options.yes else get_answer(message)

        if ans != 'Y':
            return

        # extract zip file to path
        if options.zipfile:
            if options.dir and not os.path.exists(options.dir):
                os.makedirs(options.dir)
            path = get_temppath(prefix='dump', dir=options.dir)
            if global_options.verbose:
                print "Extract path is %s" % path
            zipfile = None
            try:
                zipfile = ZipFile(options.zipfile, 'r')
                zipfile.extractall(path)
            except:
                log.exception(
                    "There are something wrong when extract zip file [%s]" %
                    options.zipfile)
                sys.exit(1)
            finally:
                if zipfile:
                    zipfile.close()
        else:
            path = os.path.join(options.dir, options.engine)
            if not os.path.exists(path):
                os.makedirs(path)

        engine = get_engine(options, global_options)

        tables = get_sorted_tables(
            get_tables(global_options.apps_dir,
                       args,
                       engine_name=options.engine,
                       settings_file=global_options.settings,
                       local_settings_file=global_options.local_settings,
                       all=options.all))
        _len = len(tables)
        for i, (name, t) in enumerate(tables):
            if hasattr(t, '__mapping_only__') and t.__mapping_only__:
                if global_options.verbose:
                    msg = 'SKIPPED(Mapping Table)'
                    print '[%s] Loading %s...%s' % (
                        options.engine, show_table(name, t, i, _len), msg)
                continue
            if global_options.verbose:
                print '[%s] Loading %s...' % (options.engine,
                                              show_table(name, t, i, _len)),
            try:
                orm.Begin()
                filename = os.path.join(path, name + '.txt')
                if options.text:
                    format = 'txt'
                else:
                    format = None
                t = load_table(t,
                               filename,
                               engine,
                               delimiter=options.delimiter,
                               format=format,
                               encoding=options.encoding,
                               bulk=int(options.bulk),
                               engine_name=engine.engine_name)
                orm.Commit()
                if global_options.verbose:
                    print t

            except:
                log.exception(
                    "There are something wrong when loading table [%s]" % name)
                orm.Rollback()

        if options.zipfile:
            shutil.rmtree(path)