diff options
Diffstat (limited to 'mediagoblin/gmg_commands')
-rw-r--r-- | mediagoblin/gmg_commands/__init__.py | 5 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/import_export.py | 36 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/migrate.py | 14 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/users.py | 36 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/util.py | 13 |
5 files changed, 60 insertions, 44 deletions
diff --git a/mediagoblin/gmg_commands/__init__.py b/mediagoblin/gmg_commands/__init__.py index 10c30385..04187ff2 100644 --- a/mediagoblin/gmg_commands/__init__.py +++ b/mediagoblin/gmg_commands/__init__.py @@ -29,7 +29,7 @@ SUBCOMMAND_MAP = { 'setup': 'mediagoblin.gmg_commands.migrate:migrate_parser_setup', 'func': 'mediagoblin.gmg_commands.migrate:migrate', 'help': 'Apply all unapplied bulk migrations to the database'}, - 'adduser':{ + 'adduser': { 'setup': 'mediagoblin.gmg_commands.users:adduser_parser_setup', 'func': 'mediagoblin.gmg_commands.users:adduser', 'help': 'Creates an user'}, @@ -68,7 +68,7 @@ def main_cli(): subparsers = parser.add_subparsers(help='sub-command help') for command_name, command_struct in SUBCOMMAND_MAP.iteritems(): - if command_struct.has_key('help'): + if 'help' in command_struct: subparser = subparsers.add_parser( command_name, help=command_struct['help']) else: @@ -94,4 +94,3 @@ def main_cli(): if __name__ == '__main__': main_cli() - diff --git a/mediagoblin/gmg_commands/import_export.py b/mediagoblin/gmg_commands/import_export.py index 78d30713..7f699429 100644 --- a/mediagoblin/gmg_commands/import_export.py +++ b/mediagoblin/gmg_commands/import_export.py @@ -16,7 +16,6 @@ from mediagoblin import mg_globals from mediagoblin.db.open import setup_connection_and_db_from_config -from mediagoblin.init.config import read_mediagoblin_config from mediagoblin.storage.filestorage import BasicFileStorage from mediagoblin.init import setup_storage, setup_global_and_app_config @@ -65,10 +64,10 @@ def _import_media(db, args): queue_cache = BasicFileStorage( args._cache_path['queue']) - for entry in db.media_entries.find(): - for name, path in entry['media_files'].items(): + for entry in db.MediaEntry.find(): + for name, path in entry.media_files.items(): _log.info('Importing: {0} - {1}'.format( - entry['title'], + entry.title, name)) media_file = mg_globals.public_store.get_file(path, mode='wb') @@ -88,7 +87,7 @@ def _import_database(db, args): args.mongorestore_path, '-d', db.name, os.path.join(args._cache_path['database'], db.name)]) - + p.wait() _log.info('...Database imported') @@ -108,7 +107,7 @@ def env_import(args): global_config, app_config = setup_global_and_app_config(args.conf_file) connection, db = setup_connection_and_db_from_config( - app_config, use_pymongo=True) + app_config) tf = tarfile.open( args.tar_file, @@ -207,15 +206,17 @@ def _export_media(db, args): queue_cache = BasicFileStorage( args._cache_path['queue']) - for entry in db.media_entries.find(): - for name, path in entry['media_files'].items(): - _log.info('Exporting {0} - {1}'.format( - entry['title'], + for entry in db.MediaEntry.find(): + for name, path in entry.media_files.items(): + _log.info(u'Exporting {0} - {1}'.format( + entry.title, name)) - - mc_file = media_cache.get_file(path, mode='wb') - mc_file.write( - mg_globals.public_store.get_file(path, mode='rb').read()) + try: + mc_file = media_cache.get_file(path, mode='wb') + mc_file.write( + mg_globals.public_store.get_file(path, mode='rb').read()) + except Exception as e: + _log.error('Failed: {0}'.format(e)) _log.info('...Media exported') @@ -226,7 +227,8 @@ def env_export(args): ''' if args.cache_path: if os.path.exists(args.cache_path): - _log.error('The cache directory must not exist before you run this script') + _log.error('The cache directory must not exist ' + 'before you run this script') _log.error('Cache directory: {0}'.format(args.cache_path)) return False @@ -242,9 +244,9 @@ def env_export(args): globa_config, app_config = setup_global_and_app_config(args.conf_file) setup_storage() - + connection, db = setup_connection_and_db_from_config( - app_config, use_pymongo=True) + app_config) _export_database(db, args) diff --git a/mediagoblin/gmg_commands/migrate.py b/mediagoblin/gmg_commands/migrate.py index fad9b363..0a8ee7dc 100644 --- a/mediagoblin/gmg_commands/migrate.py +++ b/mediagoblin/gmg_commands/migrate.py @@ -16,12 +16,12 @@ import sys -from mediagoblin.db import util as db_util +from mediagoblin.db.mongo import util as db_util from mediagoblin.db.open import setup_connection_and_db_from_config -from mediagoblin.init.config import read_mediagoblin_config +from mediagoblin.init import setup_global_and_app_config # This MUST be imported so as to set up the appropriate migrations! -from mediagoblin.db import migrations +from mediagoblin.db.mongo import migrations def migrate_parser_setup(subparser): @@ -41,9 +41,9 @@ def _print_finished_migration(migration_number, migration_func): def migrate(args): - config, validation_result = read_mediagoblin_config(args.conf_file) + global_config, app_config = setup_global_and_app_config(args.conf_file) connection, db = setup_connection_and_db_from_config( - config['mediagoblin'], use_pymongo=True) + app_config, use_pymongo=True) migration_manager = db_util.MigrationManager(db) # Clear old indexes @@ -53,13 +53,13 @@ def migrate(args): for collection, index_name in removed_indexes: print "Removed index '%s' in collection '%s'" % ( index_name, collection) - + # Migrate print "\n== Applying migrations... ==" migration_manager.migrate_new( pre_callback=_print_started_migration, post_callback=_print_finished_migration) - + # Add new indexes print "\n== Adding new indexes... ==" new_indexes = db_util.add_new_indexes(db) diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py index 345c3e5c..4bfe30a5 100644 --- a/mediagoblin/gmg_commands/users.py +++ b/mediagoblin/gmg_commands/users.py @@ -18,27 +18,30 @@ from mediagoblin.gmg_commands import util as commands_util from mediagoblin.auth import lib as auth_lib from mediagoblin import mg_globals - def adduser_parser_setup(subparser): subparser.add_argument( - 'username', + '--username','-u', help="Username used to login") subparser.add_argument( - 'password', - help="Your supersecret word to login") + '--password','-p', + help="Your supersecret word to login, beware of storing it in bash history") subparser.add_argument( - 'email', - help="Email to recieve notifications") + '--email','-e', + help="Email to receive notifications") def adduser(args): #TODO: Lets trust admins this do not validate Emails :) commands_util.setup_app(args) + args.username = commands_util.prompt_if_not_set(args.username, "Username:") + args.password = commands_util.prompt_if_not_set(args.password, "Password:",True) + args.email = commands_util.prompt_if_not_set(args.email, "Email:") + db = mg_globals.database users_with_username = \ db.User.find({ - 'username': args.username.lower() + 'username': args.username.lower(), }).count() if users_with_username: @@ -47,11 +50,11 @@ def adduser(args): else: # Create the user entry = db.User() - entry['username'] = unicode(args.username.lower()) - entry['email'] = unicode(args.email) - entry['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password) - entry['status'] = u'active' - entry['email_verified'] = True + entry.username = unicode(args.username.lower()) + entry.email = unicode(args.email) + entry.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password) + entry.status = u'active' + entry.email_verified = True entry.save(validate=True) print "User created (and email marked as verified)" @@ -68,9 +71,9 @@ def makeadmin(args): db = mg_globals.database - user = db.User.one({'username':unicode(args.username.lower())}) + user = db.User.one({'username': unicode(args.username.lower())}) if user: - user['is_admin'] = True + user.is_admin = True user.save() print 'The user is now Admin' else: @@ -91,11 +94,10 @@ def changepw(args): db = mg_globals.database - user = db.User.one({'username':unicode(args.username.lower())}) + user = db.User.one({'username': unicode(args.username.lower())}) if user: - user['pw_hash'] = auth_lib.bcrypt_gen_password_hash(args.password) + user.pw_hash = auth_lib.bcrypt_gen_password_hash(args.password) user.save() print 'Password successfully changed' else: print 'The user doesn\'t exist' - diff --git a/mediagoblin/gmg_commands/util.py b/mediagoblin/gmg_commands/util.py index 168a0760..3e26c53f 100644 --- a/mediagoblin/gmg_commands/util.py +++ b/mediagoblin/gmg_commands/util.py @@ -16,6 +16,7 @@ from mediagoblin import app +import getpass def setup_app(args): @@ -25,3 +26,15 @@ def setup_app(args): mgoblin_app = app.MediaGoblinApp(args.conf_file) return mgoblin_app + +def prompt_if_not_set(variable, text, password=False): + """ + Checks if the variable is None and prompt for a value if it is + """ + if variable is None: + if not password: + variable=raw_input(text + u' ') + else: + variable=getpass.getpass(text + u' ') + + return variable |