Example #1
0
def get_log(request, content_type, content_id):    
    if not content_type in reports.all:
        raise ValueError("Invalid content type '%s'." % content_type)
    report = reports.all[content_type]
    obj = report.get(content_id)
    header_data = \
        [report.data[untitle(s)](obj, since='all') for s in report.headers]
    local_path = os.path.join(settings.NORC_LOG_DIR, obj.log_path)
    if os.path.isfile(local_path):
        f = open(local_path, 'r')
        log = ''.join(f.readlines())
        f.close()
    elif settings.BACKUP_SYSTEM == "AmazonS3":
        try:
            log = get_s3_key("norc_logs/" + obj.log_path)
        except:
            log = "Error retrieving log from S3."
    else:
        log = "Could not retrieve log file from local machine."
    return render_to_response('norc/log.html', {
        'key': content_type,
        'log': log,
        'headers': report.headers,
        'data': header_data,
    })
Example #2
0
def main():
    usage = "norc_log_viewer <class_name> <id> [-r]"

    def bad_args(message):
        print message
        print usage
        sys.exit(2)

    parser = OptionParser(usage)
    parser.add_option("-r",
                      "--remote",
                      action="store_true",
                      default=False,
                      help="Forces log retrieval from the remote source.")

    (options, args) = parser.parse_args()

    if len(args) != 2:
        bad_args("A class name and id are required.")

    model_name = args[0].lower()
    try:
        if args[1].startswith("#"):
            args[1] = args[1][1:]
        obj_id = int(args[1])
    except ValueError:
        bad_args("Invalid id '%s', must be an integer." % args[1])

    ctypes = ContentType.objects.filter(model=model_name)
    ct_count = ctypes.count()

    if ct_count == 0:
        print "No model found matching '%s'." % model_name
        return

    i = 0
    if ct_count > 1:
        i = -1
        while i < 0 or i >= ct_count:
            print
            for i, ct in enumerate(ctypes):
                print "%s: %s.%s" % \
                    (i, ct.app_label, ct.model_class().__name__)
            try:
                i = int(
                    raw_input(
                        "Please enter the number of the correct model: "))
            except ValueError:
                pass

    Model = ctypes[i].model_class()
    try:
        obj = Model.objects.get(id=obj_id)
    except Model.DoesNotExist:
        print "No %s found with id=%s." % (Model.__name__, obj_id)
        return

    if hasattr(obj, "log_path"):
        local_path = os.path.join(settings.NORC_LOG_DIR, obj.log_path)
        log = None
        if os.path.isfile(local_path) and not options.remote:
            f = open(local_path, 'r')
            log = ''.join(f.readlines())
            f.close()
        elif settings.BACKUP_SYSTEM == "AmazonS3":
            print "Retreiving log from S3..."
            try:
                key = 'norc_logs/' + obj.log_path
                log = get_s3_key(key)
            except:
                log = 'Could not retrieve log file from local machine or S3.'
        print log,
    else:
        print "Object does not support a log file."
Example #3
0
def main():
    usage = "norc_log_viewer <class_name> <id> [-r]"
    
    def bad_args(message):
        print message
        print usage
        sys.exit(2)
    
    parser = OptionParser(usage)
    parser.add_option("-r", "--remote", action="store_true", default=False,
        help="Forces log retrieval from the remote source.")
    
    (options, args) = parser.parse_args()
    
    if len(args) != 2:
        bad_args("A class name and id are required.")
    
    model_name = args[0].lower()  
    try:
        obj_id = int(args[1])
    except ValueError:
        bad_args("Invalid id '%s', must be an integer." % args[1])
    
    ctypes = ContentType.objects.filter(model=model_name)
    ct_count = ctypes.count()
    
    if ct_count == 0:
        print "No model found matching '%s'." % model_name
        return
    
    i = 0
    if ct_count > 1:
        i = -1
        while i < 0 or i >= ct_count:
            print 
            for i, ct in enumerate(ctypes):
                print "%s: %s.%s" % \
                    (i, ct.app_label, ct.model_class().__name__)
            try:
                i = int(raw_input(
                    "Please enter the number of the correct model: "))
            except ValueError:
                pass
    
    Model = ctypes[i].model_class()
    try:
        obj = Model.objects.get(id=obj_id)
    except Model.DoesNotExist:
        print "No %s found with id=%s." % (Model.__name__, obj_id)
        return
    
    if hasattr(obj, "log_path"):
        local_path = os.path.join(settings.NORC_LOG_DIR, obj.log_path)
        log = None
        if os.path.isfile(local_path) and not options.remote:
            f = open(local_path, 'r')
            log = ''.join(f.readlines())
            f.close()
        elif settings.BACKUP_SYSTEM == "AmazonS3":
            print "Retreiving log from S3..."
            try:
                key = 'norc_logs/' + obj.log_path
                log = get_s3_key(key)
            except:
                log = 'Could not retrieve log file from local machine or S3.'
        print log,
    else:
        print "Object does not support a log file."