Commit 8c4cbb98 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

Fix stdout/stderr write in astakos/quota commands

Write actual command output and user prompts to stdout; error and progress
messages to stderr.
parent 6d0d27d4
...@@ -111,6 +111,6 @@ class Command(BaseCommand): ...@@ -111,6 +111,6 @@ class Command(BaseCommand):
profile.save() profile.save()
if update: if update:
print "Profile updated" self.stderr.write("Profile updated\n")
else: else:
print "Profile stored" self.stderr.write("Profile stored\n")
...@@ -44,10 +44,10 @@ class Command(NoArgsCommand): ...@@ -44,10 +44,10 @@ class Command(NoArgsCommand):
help = "Cleanup sessions and session catalog" help = "Cleanup sessions and session catalog"
def handle_noargs(self, **options): def handle_noargs(self, **options):
self.stdout.write('Cleanup sessions ...\n') self.stderr.write('Cleanup sessions ...\n')
call_command('cleanup') call_command('cleanup')
self.stdout.write('Cleanup session catalog ...\n') self.stderr.write('Cleanup session catalog ...\n')
engine = import_module(settings.SESSION_ENGINE) engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore() store = engine.SessionStore()
tbd = (entry for entry in SessionCatalog.objects.all() tbd = (entry for entry in SessionCatalog.objects.all()
......
...@@ -49,8 +49,8 @@ class Command(NoArgsCommand): ...@@ -49,8 +49,8 @@ class Command(NoArgsCommand):
fixed = fix_superusers() fixed = fix_superusers()
count = len(fixed) count = len(fixed)
if count != 0: if count != 0:
self.stdout.write("Fixed %s superuser(s).\n" % count) self.stderr.write("Fixed %s superuser(s).\n" % count)
else: else:
self.stdout.write("No superuser needed a fix.\n") self.stderr.write("No superuser needed a fix.\n")
except BaseException, e: except BaseException, e:
raise CommandError(e) raise CommandError(e)
...@@ -47,4 +47,4 @@ class Command(BaseCommand): ...@@ -47,4 +47,4 @@ class Command(BaseCommand):
raise CommandError("Invalid number of arguments") raise CommandError("Invalid number of arguments")
Group.objects.get_or_create(name=args[0].strip()) Group.objects.get_or_create(name=args[0].strip())
print "Group created" self.stderr.write("Group created\n")
...@@ -124,23 +124,23 @@ class Command(BaseCommand): ...@@ -124,23 +124,23 @@ class Command(BaseCommand):
s = '1 expired project:\n' s = '1 expired project:\n'
else: else:
s = '%d expired projects:\n' % (length,) s = '%d expired projects:\n' % (length,)
self.stdout.write(s) self.stderr.write(s)
if length > 0: if length > 0:
labels = ('Project', 'Name', 'Status', 'Expiration date') labels = ('Project', 'Name', 'Status', 'Expiration date')
columns = (10, 30, 14, 30) columns = (10, 30, 14, 30)
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns)) line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n') self.stderr.write(line + '\n')
sep = '-' * len(line) sep = '-' * len(line)
self.stdout.write(sep + '\n') self.stderr.write(sep + '\n')
for project in projects: for project in projects:
line = ' '.join(f.rjust(w) for f, w in zip(project, columns)) line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
self.stdout.write(line + '\n') self.stderr.write(line + '\n')
if execute: if execute:
self.stdout.write('%d projects have been terminated.\n' % ( self.stderr.write('%d projects have been terminated.\n' % (
length,)) length,))
def expire(self, execute=False): def expire(self, execute=False):
......
...@@ -79,20 +79,20 @@ class Command(SynnefoCommand): ...@@ -79,20 +79,20 @@ class Command(SynnefoCommand):
def print_sync(self, diff_quotas): def print_sync(self, diff_quotas):
size = len(diff_quotas) size = len(diff_quotas)
if size == 0: if size == 0:
self.stdout.write("No sync needed.\n") self.stderr.write("No sync needed.\n")
else: else:
self.stdout.write("Synced %s users:\n" % size) self.stderr.write("Synced %s users:\n" % size)
uuids = diff_quotas.keys() uuids = diff_quotas.keys()
users = AstakosUser.objects.filter(uuid__in=uuids) users = AstakosUser.objects.filter(uuid__in=uuids)
for user in users: for user in users:
self.stdout.write("%s (%s)\n" % (user.uuid, user.username)) self.stderr.write("%s (%s)\n" % (user.uuid, user.username))
def print_verify(self, qh_limits, diff_quotas): def print_verify(self, qh_limits, diff_quotas):
for holder, local in diff_quotas.iteritems(): for holder, local in diff_quotas.iteritems():
registered = qh_limits.pop(holder, None) registered = qh_limits.pop(holder, None)
user = get_user_by_uuid(holder) user = get_user_by_uuid(holder)
if registered is None: if registered is None:
self.stdout.write( self.stderr.write(
"No quota for %s (%s) in quotaholder.\n" % "No quota for %s (%s) in quotaholder.\n" %
(holder, user.username)) (holder, user.username))
else: else:
...@@ -105,4 +105,4 @@ class Command(SynnefoCommand): ...@@ -105,4 +105,4 @@ class Command(SynnefoCommand):
diffs = len(diff_quotas) diffs = len(diff_quotas)
if diffs: if diffs:
self.stdout.write("Quota differ for %d users.\n" % (diffs)) self.stderr.write("Quota differ for %d users.\n" % (diffs))
...@@ -69,7 +69,7 @@ class Command(BaseCommand): ...@@ -69,7 +69,7 @@ class Command(BaseCommand):
@transaction.commit_on_success @transaction.commit_on_success
def handle(self, *args, **options): def handle(self, *args, **options):
write = self.stdout.write write = self.stderr.write
force = options['force'] force = options['force']
userid = options['userid'] userid = options['userid']
...@@ -130,7 +130,7 @@ class Command(BaseCommand): ...@@ -130,7 +130,7 @@ class Command(BaseCommand):
headers = ("User", "Resource", "Astakos", "Quotaholder") headers = ("User", "Resource", "Astakos", "Quotaholder")
if unsynced: if unsynced:
pprint_table(self.stderr, unsynced, headers) pprint_table(self.stdout, unsynced, headers)
if options["fix"]: if options["fix"]:
provisions = map(create_provision, unsynced) provisions = map(create_provision, unsynced)
try: try:
......
...@@ -111,7 +111,7 @@ class Command(BaseCommand): ...@@ -111,7 +111,7 @@ class Command(BaseCommand):
resource = self.get_resource(resource_name) resource = self.get_resource(resource_name)
resource.api_visible = allow resource.api_visible = allow
if not allow and resource.ui_visible: if not allow and resource.ui_visible:
self.stdout.write("Also resetting 'ui_visible' for consistency.\n") self.stderr.write("Also resetting 'ui_visible' for consistency.\n")
resource.ui_visible = False resource.ui_visible = False
resource.save() resource.save()
...@@ -127,7 +127,7 @@ class Command(BaseCommand): ...@@ -127,7 +127,7 @@ class Command(BaseCommand):
resource.ui_visible = allow resource.ui_visible = allow
if allow and not resource.api_visible: if allow and not resource.api_visible:
self.stdout.write("Also setting 'api_visible' for consistency.\n") self.stderr.write("Also setting 'api_visible' for consistency.\n")
resource.api_visible = True resource.api_visible = True
resource.save() resource.save()
...@@ -196,7 +196,7 @@ class Command(BaseCommand): ...@@ -196,7 +196,7 @@ class Command(BaseCommand):
updates.append((resource, value)) updates.append((resource, value))
break break
if updates: if updates:
self.stdout.write("Updating...\n") self.stderr.write("Updating...\n")
update_resources(updates) update_resources(updates)
def parse_limit(self, limit): def parse_limit(self, limit):
......
...@@ -73,7 +73,7 @@ class Command(BaseCommand): ...@@ -73,7 +73,7 @@ class Command(BaseCommand):
self.add_services(data) self.add_services(data)
def add_services(self, data): def add_services(self, data):
write = self.stdout.write write = self.stderr.write
output = [] output = []
for name, service_dict in data.iteritems(): for name, service_dict in data.iteritems():
try: try:
...@@ -91,7 +91,7 @@ class Command(BaseCommand): ...@@ -91,7 +91,7 @@ class Command(BaseCommand):
try: try:
existed = add_service(component, name, service_type, endpoints, existed = add_service(component, name, service_type, endpoints,
out=self.stdout) out=self.stderr)
except RegisterException as e: except RegisterException as e:
raise CommandError(e.message) raise CommandError(e.message)
......
...@@ -59,4 +59,4 @@ class Command(BaseCommand): ...@@ -59,4 +59,4 @@ class Command(BaseCommand):
activation_backend.send_user_verification_email(user) activation_backend.send_user_verification_email(user)
self.stdout.write("Activation sent to '%s'\n" % (user.email,)) self.stderr.write("Activation sent to '%s'\n" % (user.email,))
...@@ -257,7 +257,7 @@ class Command(BaseCommand): ...@@ -257,7 +257,7 @@ class Command(BaseCommand):
group = Group.objects.get(name=groupname) group = Group.objects.get(name=groupname)
user.groups.add(group) user.groups.add(group)
except Group.DoesNotExist, e: except Group.DoesNotExist, e:
self.stdout.write( self.stderr.write(
"Group named %s does not exist\n" % groupname) "Group named %s does not exist\n" % groupname)
groupname = options.get('delete-group') groupname = options.get('delete-group')
...@@ -266,7 +266,7 @@ class Command(BaseCommand): ...@@ -266,7 +266,7 @@ class Command(BaseCommand):
group = Group.objects.get(name=groupname) group = Group.objects.get(name=groupname)
user.groups.remove(group) user.groups.remove(group)
except Group.DoesNotExist, e: except Group.DoesNotExist, e:
self.stdout.write( self.stderr.write(
"Group named %s does not exist\n" % groupname) "Group named %s does not exist\n" % groupname)
pname = options.get('add-permission') pname = options.get('add-permission')
...@@ -274,13 +274,13 @@ class Command(BaseCommand): ...@@ -274,13 +274,13 @@ class Command(BaseCommand):
try: try:
r, created = add_user_permission(user, pname) r, created = add_user_permission(user, pname)
if created: if created:
self.stdout.write( self.stderr.write(
'Permission: %s created successfully\n' % pname) 'Permission: %s created successfully\n' % pname)
if r > 0: if r > 0:
self.stdout.write( self.stderr.write(
'Permission: %s added successfully\n' % pname) 'Permission: %s added successfully\n' % pname)
elif r == 0: elif r == 0:
self.stdout.write( self.stderr.write(
'User has already permission: %s\n' % pname) 'User has already permission: %s\n' % pname)
except Exception, e: except Exception, e:
raise CommandError(e) raise CommandError(e)
...@@ -290,12 +290,12 @@ class Command(BaseCommand): ...@@ -290,12 +290,12 @@ class Command(BaseCommand):
try: try:
r = remove_user_permission(user, pname) r = remove_user_permission(user, pname)
if r < 0: if r < 0:
self.stdout.write( self.stderr.write(
'Invalid permission codename: %s\n' % pname) 'Invalid permission codename: %s\n' % pname)
elif r == 0: elif r == 0:
self.stdout.write('User has not permission: %s\n' % pname) self.stderr.write('User has not permission: %s\n' % pname)
elif r > 0: elif r > 0:
self.stdout.write( self.stderr.write(
'Permission: %s removed successfully\n' % pname) 'Permission: %s removed successfully\n' % pname)
except Exception, e: except Exception, e:
raise CommandError(e) raise CommandError(e)
...@@ -374,7 +374,7 @@ class Command(BaseCommand): ...@@ -374,7 +374,7 @@ class Command(BaseCommand):
self.stdout.write("Confirm? [y/N] ") self.stdout.write("Confirm? [y/N] ")
response = raw_input() response = raw_input()
if string.lower(response) not in ['y', 'yes']: if string.lower(response) not in ['y', 'yes']:
self.stdout.write("Aborted.\n") self.stderr.write("Aborted.\n")
exit() exit()
def handle_limits_user(self, user, res, capacity, style): def handle_limits_user(self, user, res, capacity, style):
......
import os import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
import sys
from optparse import OptionParser from optparse import OptionParser
from synnefo.lib.services import fill_endpoints, filter_public from synnefo.lib.services import fill_endpoints, filter_public
from django.utils import simplejson as json from django.utils import simplejson as json
...@@ -303,7 +304,7 @@ def main(): ...@@ -303,7 +304,7 @@ def main():
try: try:
services = definitions[component] services = definitions[component]
except KeyError: except KeyError:
print "Unrecognized component %s" % component print >> sys.stderr, "Unrecognized component %s" % component
exit(1) exit(1)
base_url = args[1] base_url = args[1]
print_definitions(services, base_url) print_definitions(services, base_url)
......
...@@ -77,10 +77,10 @@ class Command(SynnefoCommand): ...@@ -77,10 +77,10 @@ class Command(SynnefoCommand):
) )
def confirm(self): def confirm(self):
self.stderr.write("Confirm? [y/N] ") self.stdout.write("Confirm? [y/N] ")
response = raw_input() response = raw_input()
if string.lower(response) not in ['y', 'yes']: if string.lower(response) not in ['y', 'yes']:
self.stdout.write("Aborted.\n") self.stderr.write("Aborted.\n")
exit() exit()
def get_handlers(self, resources): def get_handlers(self, resources):
...@@ -180,11 +180,11 @@ class Command(SynnefoCommand): ...@@ -180,11 +180,11 @@ class Command(SynnefoCommand):
return return
headers = ("#", "User", "Source", "Resource", "Limit", "Usage") headers = ("#", "User", "Source", "Resource", "Limit", "Usage")
pprint_table(self.stderr, overlimit, headers, pprint_table(self.stdout, overlimit, headers,
options["output_format"], title="Violations") options["output_format"], title="Violations")
if any(actions.values()): if any(actions.values()):
write("\n") self.stdout.write("\n")
if fix: if fix:
if dangerous and not force: if dangerous and not force:
write("You are enforcing resources that may permanently " write("You are enforcing resources that may permanently "
...@@ -197,5 +197,5 @@ class Command(SynnefoCommand): ...@@ -197,5 +197,5 @@ class Command(SynnefoCommand):
headers = ("Type", "ID", "State", "Backend", "Action", "Violation") headers = ("Type", "ID", "State", "Backend", "Action", "Violation")
if fix: if fix:
headers += ("Result",) headers += ("Result",)
pprint_table(self.stderr, log, headers, pprint_table(self.stdout, log, headers,
options["output_format"], title=title) options["output_format"], title=title)
...@@ -65,7 +65,7 @@ class Command(BaseCommand): ...@@ -65,7 +65,7 @@ class Command(BaseCommand):
) )
def handle(self, *args, **options): def handle(self, *args, **options):
write = self.stdout.write write = self.stderr.write
userid = options['userid'] userid = options['userid']
# Get holdings from Cyclades DB # Get holdings from Cyclades DB
...@@ -117,7 +117,7 @@ class Command(BaseCommand): ...@@ -117,7 +117,7 @@ class Command(BaseCommand):
headers = ("User", "Resource", "Database", "Quotaholder") headers = ("User", "Resource", "Database", "Quotaholder")
if unsynced: if unsynced:
pprint_table(self.stderr, unsynced, headers) pprint_table(self.stdout, unsynced, headers)
if options["fix"]: if options["fix"]:
qh = quotas.Quotaholder.get() qh = quotas.Quotaholder.get()
request = {} request = {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment