예제 #1
0
def run():
    if '--help' in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
    except getopt.GetoptError as e:
        usage(e.msg)
    if len(sys.argv) != 1:
        usage()
    # Disable pytis logging and notification thread (may cause troubles when
    # creating data objects for profile validation).
    config.dblisten = False
    # Disable pytis logging of data operations etc.
    config.log_exclude = [
        pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG,
        pytis.util.OPERATIONAL
    ]
    while True:
        try:
            updater = pytis.help.HelpUpdater()
        except pytis.data.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " %
                                           login)
                config.dbconnection.update_login_data(user=login,
                                                      password=password)
            else:
                sys.stderr.write(e.message())
                sys.exit(1)
        else:
            break
    updater.update()
예제 #2
0
def run():
    if '--help' in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
    except getopt.GetoptError as e:
        usage(e.msg)
    if len(sys.argv) != 1:
        usage()
    # Disable pytis logging and notification thread (may cause troubles when
    # creating data objects for profile validation).
    config.dblisten = False
    # Disable pytis logging of data operations etc.
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
    while True:
        try:
            updater = pytis.help.HelpUpdater()
        except pytis.data.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
            else:
                sys.stderr.write(e.message())
                sys.exit(1)
        else:
            break
    updater.update()
예제 #3
0
def run():
    if "--help" in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
        if len(sys.argv) > 1:
            usage()
    except getopt.GetoptError as e:
        usage(e.msg)
    wiking.cfg.user_config_file = config.config_file
    wiking.cms.cfg.user_config_file = config.config_file
    config.dblisten = False
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
    while True:
        try:
            data = pd.dbtable("users", ("uid", "login", "password"), config.dbconnection)
        except pd.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass

                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
        else:
            break
    storage = wiking.Pbkdf2PasswordStorage()
    transaction = pd.DBTransactionDefault(config.dbconnection)
    data.select(transaction=transaction)
    n = 0
    plain = 0
    try:
        while True:
            row = data.fetchone()
            if row is None:
                break
            orig_prefix, orig_password = row["password"].value().split(":", 1)
            if orig_prefix == "md5u":
                prefix = "pbkdf2/md5"
            elif orig_prefix == "plain":
                prefix = "pbkdf2"
                plain += 1
            else:
                continue
            password = prefix + ":" + storage.stored_password(orig_password)
            data.update(row["uid"], pd.Row([("password", pd.sval(password))]), transaction=transaction)
            n += 1
    except:
        try:
            transaction.rollback()
        except:
            pass
        sys.stderr.write("Transaction rolled back.\n")
        raise
    else:
        print "Total %d passwords updated (%d from plain text, %d from md5)." % (n, plain, n - plain)
        transaction.commit()
    transaction.close()
예제 #4
0
def run():
    # Process command line options and init configuration.
    try:
        config.add_command_line_options(sys.argv)
        command, username = sys.argv[1:3]
        option = len(sys.argv) > 3 and sys.argv[3] or None
        value = len(sys.argv) > 4 and sys.argv[4] or None
    except getopt.GetoptError as e:
        usage(e.msg)
    except ValueError as e:
        usage()
    if command not in ('get', 'set', 'unset'):
        usage("Unknown command '%s'" % command)
    if command != 'get' and option is None:
        usage("Option name is mandatory for command '%s'" % command)
    if command == 'set' and value is None:
        usage("Value must be specified for command '%s'" % command)
    if command != 'set' and value is not None:
        usage("Value makes no sense for command '%s'" % command)
    storage = DBConfigurationStorage(config.dbconnection, username=username)
    cfg = dict(storage.read())
    if command == 'get':
        if option is not None:
            if option not in cfg:
                die("Option '%s' not set for user '%s'." % (option, username))
            options = (option,)
        else:
            if not cfg.keys():
                die("No options set for user '%s'." % username)
            options = sorted(cfg.keys())
        for option in options:
            print "%s = %s" % (option, cfg[option])
    else:
        if command == 'unset':
            if option not in cfg:
                die("Option '%s' not set for user '%s'." % (option, username))
            else:
                del cfg[option]
        else: # command == 'set'
            cfg[option] = value
        # Avoid pytis logging during the update.
        config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
        # Update the saved configuration.
        storage.write(cfg.items())
예제 #5
0
def run():
    if '--help' in sys.argv:
        usage()
    if '--no-act' in sys.argv:
        del sys.argv[sys.argv.index('--no-act')]
        no_act = True
    else:
        no_act = False
    try:
        config.add_command_line_options(sys.argv)
        if len(sys.argv) > 1:
            usage()
    except getopt.GetoptError as e:
        usage(e.msg)
    wiking.cfg.user_config_file = config.config_file
    wiking.cms.cfg.user_config_file = config.config_file
    config.dblisten = False
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG,
                          pytis.util.OPERATIONAL]
    while True:
        try:
            data = pd.dbtable('cms_page_attachments', ('attachment_id', 'filename'),
                              config.dbconnection)
        except pd.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
        else:
            break
    attachments = []
    directory = os.path.join(wiking.cms.cfg.storage, config.dbname, 'attachments')
    ok = stray = 0

    data.select()
    while True:
        row = data.fetchone()
        if row is None:
            break
        ext = os.path.splitext(row['filename'].value())[1].lower()
        fname = row['attachment_id'].export() + (ext or '.')
        path = os.path.join(directory, fname)
        if not os.path.exists(path):
            sys.stderr.write("Missing file: %s\n" % path)
        attachments.append(fname)

    for fname in os.listdir(directory):
        path = os.path.join(directory, fname)
        if os.path.isfile(path):
            if fname in attachments:
                ok += 1
            else:
                stray += 1
                if no_act:
                    sys.stderr.write("Stray file: %s\n" % path)
                else:
                    sys.stderr.write("Removing file: %s\n" % path)
                    os.unlink(path)
    sys.stderr.write("Total %d of %d files ok, %d %s.\n" %
                     (ok, len(attachments), stray, 'stray' if no_act else 'removed'))
예제 #6
0
def run():
    parser = argparse.ArgumentParser(
        description="Validate and optionally fix saved form profiles",
        epilog=("Additionally, you can pass other valid Pytis command line arguments, "
                "such as --dbhost or --dbname to override certain configuration file "
                "options."),
    )
    parser.add_argument('-d', '--delete-column', nargs='+', metavar='SPEC_NAME:COLUMN',
                        default=(), help="Delete COLUMN of SPEC_NAME from all existing profiles")
    parser.add_argument('-r', '--rename-column', nargs='+', metavar='SPEC_NAME:OLD:NEW',
                        default=(), help="Rename column OLD of SPEC_NAME to NEW in all existing profiles")
    parser.add_argument('--config', required=True, metavar='PATH',
                        help="Configuration file path")
    args, argv = parser.parse_known_args()
    import config
    try:
        config.add_command_line_options([sys.argv[0], '--config', args.config] + argv)
        delete_columns = {}
        for spec_name, column in [x.split(':') for x in args.delete_column]:
            delete_columns.setdefault(spec_name, []).append(column)
        rename_columns = {}
        for spec_name, old, new in [x.split(':') for x in args.rename_column]:
            rename_columns.setdefault(spec_name, {})[old] = new
    except (getopt.GetoptError, ValueError):
        parser.print_help()
        sys.exit(1)
    # Disable pytis logging and notification thread (may cause troubles when
    # creating data objects for profile validation).
    config.dblisten = False
    # Disable pytis logging of data operations etc.
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
    while True:
        try:
            data = pytis.data.dbtable('e_pytis_form_profile_base', ('id', 'username'),
                                      config.dbconnection)
        except pytis.data.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
            else:
                sys.stderr.write(e.message())
                sys.exit(1)
        else:
            break
    total_processed = 0
    total_valid = 0
    total_invalid = 0
    skipped = 0
    usernames = [v.value() for v in data.distinct('username')]
    resolver = pytis.util.resolver()
    for username in usernames:
        manager = FormProfileManager(config.dbconnection, username=username)
        for spec_name in manager.list_spec_names():
            try:
                view_spec, data_object, error = cache[spec_name]
            except KeyError:
                try:
                    view_spec = resolver.get(spec_name, 'view_spec')
                    data_spec = resolver.get(spec_name, 'data_spec')
                    data_object = data_spec.create(dbconnection_spec=config.dbconnection)
                except Exception as e:
                    print "%s: %s" % (spec_name, e)
                    skipped += 1
                    view_spec, data_object,  error = None, None, e
                else:
                    error = None
                cache[spec_name] = view_spec, data_object, error
            if not error:
                for form_name in manager.list_form_names(spec_name):
                    for profile in manager.load_profiles(
                            spec_name, form_name, view_spec, data_object,
                            Profile('__default_profile__', '-'),
                            delete_columns=delete_columns.get(spec_name, ()),
                            rename_columns=rename_columns.get(spec_name, {}),
                    ):
                        # Update the 'errors' column in the database table.
                        manager.save_profile(spec_name, form_name, profile)
                        for param, error in profile.errors():
                            print ':'.join((username, spec_name, form_name, profile.id(), ' %s: %s' % (param, error)))
                        if profile.errors():
                            total_invalid += 1
                        else:
                            total_valid += 1
                        total_processed += 1
    print (u"Total %d profiles processed:\n"
           u"  %d valid\n"
           u"  %d invalid") % (total_processed, total_valid, total_invalid)
    if skipped == 1:
        print u"  1 specification skipped due to errors"
    elif skipped > 1:
        print u"  %d specifications skipped due to errors" % skipped
예제 #7
0
def run():
    parser = argparse.ArgumentParser(
        description="Dump Pytis application specifications to STDOUT",
        epilog=(
            "Additionally, you can pass other valid Pytis command line arguments, "
            "such as --dbhost or --dbname to override certain configuration file "
            "options."
        ),
    )
    parser.add_argument(
        "-w", "--wiking", action="store_true", help="Dump a wiking application (Pytis wx application is the default)"
    )
    parser.add_argument(
        "-e",
        "--exit-on-error",
        action="store_true",
        help=(
            "Print traceback and exit when exception occurs. "
            "By default, the error message is printed to the "
            "dump output and the program continues."
        ),
    )
    parser.add_argument(
        "-x", "--exclude", nargs="+", metavar="NAME", default=(), help="Name(s) of specification(s) to skip"
    )
    parser.add_argument(
        "-i", "--include", nargs="+", metavar="NAME", default=(), help="Dump only given specification name(s)"
    )
    parser.add_argument("--config", required=True, metavar="PATH", help="Configuration file path")

    args, argv = parser.parse_known_args()
    import config

    try:
        config.add_command_line_options([sys.argv[0], "--config", args.config] + argv)
    except getopt.GetoptError as e:
        parser.print_help()
        sys.exit(1)
    config.log_logger = (NullLogger, (), {})
    import lcg

    lcg.log = lambda *args, **kwargs: None
    if args.wiking:
        import wiking

        wiking.cfg.user_config_file = config.config_file
        config.resolver = wiking.cfg.resolver = wiking.WikingResolver(wiking.cfg.modules)
        config.dbconnections = wiking.cfg.connections
        config.dbconnection = config.option("dbconnection").default()
    resolver = config.resolver
    processed, errors = 0, 0
    if args.include:
        names = args.include
    else:
        if args.wiking:
            base_class = wiking.Module
        else:
            base_class = pytis.presentation.Specification
        names = sorted(name for name, cls in resolver.walk(cls=base_class))
    for name in names:
        if name not in args.exclude:
            try:
                if args.wiking:
                    cls = resolver.wiking_module_cls(name)
                    if not hasattr(cls, "Spec"):
                        continue
                    module = resolver.wiking_module(name)
                    spec = module.Spec(cls)
                    data = module._data
                else:
                    spec = resolver.specification(name)
                    if not spec.public:
                        continue
                    data_spec = spec.data_spec()
                    data = data_spec.create(dbconnection_spec=config.dbconnection)
                # print ' ', spec.table
                view_spec = spec.view_spec()
                record = pytis.presentation.PresentedRow(view_spec.fields(), data, None)
                for fid in sorted(record.keys()):
                    t = record.type(fid)
                    f = view_spec.field(fid)
                    attr = (
                        # ('label', f.label()),
                        ("not_null", t.not_null()),
                        # We care only about the resulting not_null value which
                        # is printed above, not about type's not_null argument.
                        ("type", (str(t).replace(" not_null=True", "").replace(" not_null=False", ""))),
                        ("editable", f.editable()),
                        ("computer", f.computer()),
                    )
                    attributes = " ".join("%s=%s" % (k, v) for k, v in attr if v is not None)
                    print OBJID_REGEX.sub("", "%s.%s %s" % (name, fid, attributes)).encode("utf-8")
                print
            except Exception as e:
                if args.exit_on_error:
                    try:
                        import cgitb

                        tb = cgitb.text(sys.exc_info())
                    except:
                        import traceback

                        tb = "".join(traceback.format_exception(*sys.exc_info())) + "\n"
                    sys.stderr.write(tb)
                    sys.stderr.write("Failed on specification: %s\n\n" % name)
                    sys.exit(1)
                else:
                    print "ERROR: %s: %s: %r" % (name, e.__class__.__name__, e)
                    errors += 1
            processed += 1
    sys.stderr.write(
        "Processed %d specifications with %d errors%s.\n"
        % (processed, errors, " (grep output for '^ERROR:' or use --exit-on-error)" if errors else "")
    )
예제 #8
0
def run():
    if '--help' in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
        if len(sys.argv) > 1:
            usage()
    except getopt.GetoptError as e:
        usage(e.msg)
    wiking.cfg.user_config_file = config.config_file
    wiking.cms.cfg.user_config_file = config.config_file
    config.dblisten = False
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
    while True:
        try:
            data = pd.dbtable('cms_page_attachments',
                              ('attachment_id', 'filename', 'image', 'thumbnail', 
                               'thumbnail_size', 'thumbnail_width', 'thumbnail_height'),
                              config.dbconnection)
        except pd.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
        else:
            break
    image_screen_size = wiking.cms.cfg.image_screen_size
    image_thumbnail_sizes = wiking.cms.cfg.image_thumbnail_sizes
    transaction = pd.DBTransactionDefault(config.dbconnection)
    data.select(transaction=transaction)
    try:
        while True:
            row = data.fetchone()
            if row is None:
                break
            ext = os.path.splitext(row['filename'].value())[1].lower()
            path = os.path.join(wiking.cms.cfg.storage, config.dbname, 'attachments',
                                row['attachment_id'].export() + (ext or '.'))
            attachment = file(path)
            try:
                image = PIL.Image.open(attachment)
            except IOError, e:
                continue
            sys.stderr.write("Resizing %s (%dx%d): " % 
                             (row['filename'].value(), image.size[0], image.size[1]))
            thumbnail_size = row['thumbnail_size'].value()
            if thumbnail_size is None:
                thumbnail_value, real_thumbnail_size = None, (None, None)
            else:
                if thumbnail_size == 'small':
                    size = image_thumbnail_sizes[0]
                elif thumbnail_size == 'medium':
                    size = image_thumbnail_sizes[1]
                else:
                    size = image_thumbnail_sizes[2]
                thumbnail_value, real_thumbnail_size = resize(image, (size, size))
                sys.stderr.write("%dx%d, " % real_thumbnail_size)
            resized_image_value, resized_image_size = resize(image, image_screen_size)
            sys.stderr.write("%dx%d\n" % resized_image_size)
            values = dict(thumbnail=thumbnail_value,
                          thumbnail_width=real_thumbnail_size[0],
                          thumbnail_height=real_thumbnail_size[1],
                          image=resized_image_value)
            r = pd.Row([(key, pd.Value(row[key].type(), value)) for key, value in values.items()])
            #data.update(row['attachment_id'], r, transaction=transaction)
    except:
        try:
            transaction.rollback()
        except:
            pass
        sys.stderr.write("Transaction rolled back.\n")
        raise
    else:
        sys.stderr.write("Transaction commited.\n")
        transaction.commit()
    transaction.close()
예제 #9
0
def run():
    if "--help" in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
        if len(sys.argv) > 1:
            usage()
    except getopt.GetoptError as e:
        usage(e.msg)
    # Disable pytis logging and notification thread (may cause troubles when
    # creating data objects for profile validation).
    config.dblisten = False
    # Disable pytis logging of data operations etc.
    config.log_exclude = [pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG, pytis.util.OPERATIONAL]
    while True:
        try:
            data = pytis.data.dbtable("e_pytis_form_profile_base", ("id", "username"), config.dbconnection)
        except pytis.data.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass

                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " % login)
                config.dbconnection.update_login_data(user=login, password=password)
            else:
                sys.stderr.write(e.message())
                sys.exit(1)
        else:
            break
    total_processed = 0
    total_valid = 0
    total_invalid = 0
    skipped = 0
    usernames = [v.value() for v in data.distinct("username")]
    resolver = pytis.util.resolver()
    for username in usernames:
        manager = FormProfileManager(config.dbconnection, username=username)
        for spec_name in manager.list_spec_names():
            try:
                view_spec, data_object, error = cache[spec_name]
            except KeyError:
                try:
                    view_spec = resolver.get(spec_name, "view_spec")
                    data_spec = resolver.get(spec_name, "data_spec")
                    data_object = data_spec.create(dbconnection_spec=config.dbconnection)
                except Exception as e:
                    print "%s: %s" % (spec_name, e)
                    skipped += 1
                    view_spec, data_object, error = None, None, e
                else:
                    error = None
                cache[spec_name] = view_spec, data_object, error
            if not error:
                for form_name in manager.list_form_names(spec_name):
                    for profile in manager.load_profiles(
                        spec_name, form_name, view_spec, data_object, Profile("__default_profile__", "-")
                    ):
                        # Update the 'errors' column in the database table.
                        manager.save_profile(spec_name, form_name, profile)
                        for param, error in profile.errors():
                            print ":".join((username, spec_name, form_name, profile.id(), " %s: %s" % (param, error)))
                        if profile.errors():
                            total_invalid += 1
                        else:
                            total_valid += 1
                        total_processed += 1
    print (u"Total %d profiles processed:\n" u"  %d valid\n" u"  %d invalid") % (
        total_processed,
        total_valid,
        total_invalid,
    )
    if skipped == 1:
        print u"  1 specification skipped due to errors"
    elif skipped > 1:
        print u"  %d specifications skipped due to errors" % skipped
예제 #10
0
def run():
    if '--help' in sys.argv:
        usage()
    try:
        config.add_command_line_options(sys.argv)
        if len(sys.argv) > 1:
            usage()
    except getopt.GetoptError as e:
        usage(e.msg)
    # Disable pytis logging and notification thread (may cause troubles when
    # creating data objects for profile validation).
    config.dblisten = False
    # Disable pytis logging of data operations etc.
    config.log_exclude = [
        pytis.util.ACTION, pytis.util.EVENT, pytis.util.DEBUG,
        pytis.util.OPERATIONAL
    ]
    while True:
        try:
            data = pytis.data.dbtable('e_pytis_form_profile_base',
                                      ('id', 'username'), config.dbconnection)
        except pytis.data.DBLoginException as e:
            if config.dbconnection.password() is None:
                import getpass
                login = config.dbuser
                password = getpass.getpass("Enter database password for %s: " %
                                           login)
                config.dbconnection.update_login_data(user=login,
                                                      password=password)
            else:
                sys.stderr.write(e.message())
                sys.exit(1)
        else:
            break
    total_processed = 0
    total_valid = 0
    total_invalid = 0
    skipped = 0
    usernames = [v.value() for v in data.distinct('username')]
    resolver = pytis.util.resolver()
    for username in usernames:
        manager = FormProfileManager(config.dbconnection, username=username)
        for spec_name in manager.list_spec_names():
            try:
                view_spec, data_object, error = cache[spec_name]
            except KeyError:
                try:
                    view_spec = resolver.get(spec_name, 'view_spec')
                    data_spec = resolver.get(spec_name, 'data_spec')
                    data_object = data_spec.create(
                        dbconnection_spec=config.dbconnection)
                except Exception as e:
                    print "%s: %s" % (spec_name, e)
                    skipped += 1
                    view_spec, data_object, error = None, None, e
                else:
                    error = None
                cache[spec_name] = view_spec, data_object, error
            if not error:
                for form_name in manager.list_form_names(spec_name):
                    for profile in manager.load_profiles(
                            spec_name, form_name, view_spec, data_object,
                            Profile('__default_profile__', '-')):
                        # Update the 'errors' column in the database table.
                        manager.save_profile(spec_name, form_name, profile)
                        for param, error in profile.errors():
                            print ':'.join(
                                (username, spec_name, form_name, profile.id(),
                                 ' %s: %s' % (param, error)))
                        if profile.errors():
                            total_invalid += 1
                        else:
                            total_valid += 1
                        total_processed += 1
    print(u"Total %d profiles processed:\n"
          u"  %d valid\n"
          u"  %d invalid") % (total_processed, total_valid, total_invalid)
    if skipped == 1:
        print u"  1 specification skipped due to errors"
    elif skipped > 1:
        print u"  %d specifications skipped due to errors" % skipped