@@ -0,0 +1,2811 @@
+#!/usr/bin/env python%0A%22%22%22Add all users into the instances given on the command line.%0A%22%22%22%0Aimport os%0Aimport sys%0Afrom argparse import ArgumentParser%0A%0Afrom paste.deploy import appconfig%0Afrom adhocracy.config.environment import load_environment%0Afrom adhocracy import model%0A%0A%0Adef load_config(filename):%0A    conf = appconfig('config:' + os.path.abspath(filename) + '#content')%0A    load_environment(conf.global_conf, conf.local_conf)%0A%0A%0Adef parse_args():%0A    parser = ArgumentParser(description=__doc__)%0A    parser.add_argument(%22conf_file%22, help=%22configuration to use%22)%0A    parser.add_argument(%22instance_keys%22, metavar='instance', nargs='+',%0A                        help=(%22Instance key(s). If ALL is given, the %22%0A                              %22users will be added to all visible instances.%22))%0A    return parser.parse_args()%0A%0A%0Adef main():%0A    args = parse_args()%0A    load_config(args.conf_file)%0A    session = model.meta.Session%0A%0A    num_added = %7B%7D%0A    keys_added = %7B%7D%0A%0A    def increment(dict_, key):%0A        num = dict_.get(key, 0)%0A        num += 1%0A        dict_%5Bkey%5D = num%0A%0A    # filter the instances we have to add%0A    instance_keys = args.instance_keys%0A    instances = model.Instance.all()%0A    if not 'ALL' in instance_keys:%0A        instances = %5Binstance for instance in instances if instance.key%0A                     in instance_keys%5D%0A%0A    num_instances = len(instances)%0A    users = model.User.all()%0A    num_users = len(users)%0A    total_added = 0%0A%0A    print %22%5Cn***Adding users***%5Cn%22%0A    for user in users:%0A        user_status = user.user_name + ': '%0A        added = 0%0A        for instance in set(instances) - set(user.instances):%0A            increment(keys_added, instance.key)%0A            user_status += instance.key + ' '%0A            added += 1%0A            total_added += 1%0A            membership = model.Membership(user, instance,%0A                                          instance.default_group)%0A            session.add(membership)%0A            if (total_added %25 2) == 0:%0A                session.commit()%0A        if added:%0A            print %22%25s (%25s)%22 %25 (user_status, added)%0A        increment(num_added, added)%0A    session.commit()%0A%0A    print %22%5Cn***Statistics***%22%0A    print %22Instances:%22, num_instances%0A    print %22Users:%22, num_users%0A%0A    print %22%5Cn**Instances added per user**%5Cn%22%0A    added = 0%0A    for (key, count) in sorted(num_added.items()):%0A        added += key * count%0A        print %22%25s membership added for %25s users%22 %25 (key, count)%0A    print ''%0A    print %22Mean number of memberships added per user: %25s%22 %25 (float(added) /%0A                                                             float(num_users))%0A%0A    print %22%5Cn**Users added per group**%5Cn%22%0A    for key in sorted(keys_added.keys()):%0A        print %22%25s: %25s%22 %25 (key, (float(keys_added%5Bkey%5D) / float(num_users)))%0A%0A%0Aif __name__ == '__main__':%0A    sys.exit(main())%0A

Add a command line script to ad all users to instances