Пример #1
0
def source_diff_view_unsafe(request, submission1_id, submission2_id, source1,
                            source2, download_url1, download_url2,
                            reverse_diff_url):
    source1, decode_error1 = decode_str(source1)
    source2, decode_error2 = decode_str(source2)
    source1 = source1.splitlines()
    source2 = source2.splitlines()

    numwidth = len(str(max(len(source1), len(source2))))
    ndiff = difflib.ndiff(source1, source2)

    class DiffLine(object):
        def __init__(self, css_class, text, number):
            self.css_class = css_class
            self.text = text
            self.number = number

    def diffstrip(line):
        return line[2:]

    def numformat(num):
        return str(num).rjust(numwidth)

    diff1, diff2 = [], []
    count1, count2 = 1, 1

    for diffline in ndiff:
        line = diffstrip(diffline)
        line = line.expandtabs(4)
        maxlen = getattr(settings, 'CHARACTERS_IN_LINE', 80)
        parts = (len(line) + maxlen) / maxlen
        line = line.ljust(parts * maxlen)
        for i in xrange(parts):
            f, t = i * maxlen, ((i + 1) * maxlen)
            c1, c2 = numformat(count1), numformat(count2)
            if diffline.startswith('- '):
                diff1.append(DiffLine('left', line[f:t], '' if i else c1))
                diff2.append(DiffLine('empty', '', ''))
            elif diffline.startswith('+ '):
                diff1.append(DiffLine('empty', '', ''))
                diff2.append(DiffLine('right', line[f:t], '' if i else c2))
            elif diffline.startswith('  '):
                diff1.append(DiffLine('both', line[f:t], '' if i else c1))
                diff2.append(DiffLine('both', line[f:t], '' if i else c2))
        if diffline.startswith('- ') or diffline.startswith('  '):
            count1 += 1
        if diffline.startswith('+ ') or diffline.startswith('  '):
            count2 += 1

    return TemplateResponse(request, 'programs/source_diff.html',
            {'source1': diff1, 'decode_error1': decode_error1,
             'download_url1': download_url1,
             'source2': diff2, 'decode_error2': decode_error2,
             'download_url2': download_url2,
             'submission1_id': submission1_id,
             'submission2_id': submission2_id,
             'reverse_diff_url': reverse_diff_url})
Пример #2
0
def show_submission_source_view(request, submission_id):
    source_file = get_submission_source_file_or_error(request, submission_id)
    raw_source, decode_error = decode_str(source_file.read())
    filename = source_file.file.name
    is_source_safe = False
    try:
        lexer = guess_lexer_for_filename(filename, raw_source)
        formatter = HtmlFormatter(linenos=True,
                                  line_number_chars=3,
                                  cssclass='syntax-highlight')
        formatted_source = highlight(raw_source, lexer, formatter)
        formatted_source_css = HtmlFormatter() \
                .get_style_defs('.syntax-highlight')
        is_source_safe = True
    except ClassNotFound:
        formatted_source = raw_source
        formatted_source_css = ''
    download_url = reverse('download_submission_source',
                           kwargs={'submission_id': submission_id})
    return TemplateResponse(
        request, 'programs/source.html', {
            'source': formatted_source,
            'css': formatted_source_css,
            'is_source_safe': is_source_safe,
            'download_url': download_url,
            'decode_error': decode_error,
            'submission_id': submission_id
        })
Пример #3
0
def show_submission_source_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id, ProgramSubmission)
    raw_source = submission.source_file.read()
    raw_source, decode_error = decode_str(raw_source)
    filename = submission.source_file.file.name
    is_source_safe = False
    try:
        lexer = guess_lexer_for_filename(filename, raw_source)
        formatter = HtmlFormatter(linenos=True, line_number_chars=3, cssclass="syntax-highlight")
        formatted_source = highlight(raw_source, lexer, formatter)
        formatted_source_css = HtmlFormatter().get_style_defs(".syntax-highlight")
        is_source_safe = True
    except ClassNotFound:
        formatted_source = raw_source
        formatted_source_css = ""
    download_url = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission_id}
    )
    return TemplateResponse(
        request,
        "programs/source.html",
        {
            "source": formatted_source,
            "css": formatted_source_css,
            "is_source_safe": is_source_safe,
            "download_url": download_url,
            "decode_error": decode_error,
        },
    )
Пример #4
0
def show_output_file_view(request, submission_id, testrun_report_id=None):
    submission = get_submission_or_error(request, submission_id,
                                         TestRunProgramSubmission)
    result = get_testrun_report_or_404(request, submission, testrun_report_id)
    data = result.output_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = result.output_file.size
    download_url = reverse(
        'download_testrun_output',
        kwargs={
            'contest_id': request.contest.id,
            'submission_id': submission_id
        },
    )
    return TemplateResponse(
        request,
        'testrun/data.html',
        {
            'header': _("Output"),
            'data': data,
            'left': size - get_preview_size_limit(),
            'decode_error': decode_error,
            'download_url': download_url,
        },
    )
Пример #5
0
def show_submission_source_view_unsafe(request, submission_id, source_file,
                                       download_url):
    raw_source, decode_error = decode_str(source_file.read())
    filename = source_file.file.name
    is_source_safe = False
    try:
        lexer = guess_lexer_for_filename(
            filename,
            raw_source
        )
        formatter = HtmlFormatter(linenos=True, line_number_chars=3,
                            cssclass='syntax-highlight')
        formatted_source = highlight(raw_source, lexer, formatter)
        formatted_source_css = HtmlFormatter() \
                .get_style_defs('.syntax-highlight')
        is_source_safe = True
    except ClassNotFound:
        formatted_source = raw_source
        formatted_source_css = ''

    return TemplateResponse(request, 'programs/source.html', {
        'source': formatted_source,
        'css': formatted_source_css,
        'is_source_safe': is_source_safe,
        'download_url': download_url,
        'decode_error': decode_error,
        'submission_id': submission_id
    })
Пример #6
0
 def clean_file(self):
     cleaned_data = self.cleaned_data
     orig, _decode_error = decode_str(cleaned_data['file'].file.read())
     try:
         cleaned_data['file'] = generator(
             source=orig.expandtabs(4),
             header=unicode(
                 '%s (%s)' % (self.user.get_full_name(), self.user)))
     except PageLimitExceeded:
         raise ValidationError(_("The page limit exceeded."))
     return cleaned_data['file']
Пример #7
0
 def clean_file(self):
     cleaned_data = self.cleaned_data
     orig, _decode_error = decode_str(cleaned_data['file'].file.read())
     try:
         cleaned_data['file'] = generator(
             source=orig.expandtabs(4),
             header=unicode('%s (%s)' %
                            (self.user.get_full_name(), self.user)))
     except PageLimitExceeded:
         raise ValidationError(_("The page limit exceeded."))
     return cleaned_data['file']
Пример #8
0
def show_input_file_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)
    data = submission.input_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = submission.input_file.size
    download_url = reverse('download_testrun_input',
            kwargs={'contest_id': request.contest.id,
                    'submission_id': submission_id})
    return TemplateResponse(request, 'testrun/data.html', {
        'header': _("Input"),
        'data': data,
        'left': size - get_preview_size_limit(),
        'decode_error': decode_error,
        'download_url': download_url
    })
Пример #9
0
def show_input_file_view(request, submission_id):
    submission = get_submission_or_error(request, submission_id,
                                         TestRunProgramSubmission)
    data = submission.input_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = submission.input_file.size
    download_url = reverse('download_testrun_input',
                           kwargs={
                               'contest_id': request.contest.id,
                               'submission_id': submission_id
                           })
    return TemplateResponse(
        request, 'testrun/data.html', {
            'header': _("Input"),
            'data': data,
            'left': size - get_preview_size_limit(),
            'decode_error': decode_error,
            'download_url': download_url
        })
Пример #10
0
def show_output_file_view(request, submission_id,
        testrun_report_id=None):
    submission = get_submission_or_error(request, submission_id,
            ZeusTestRunProgramSubmission)
    result = get_testrun_report_or_404(request, submission, testrun_report_id,
            ZeusTestRunReport)
    data = result.output_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = result.full_out_size
    download_url = reverse('zeus_download_testrun_output',
        kwargs={'contest_id': request.contest.id,
                'submission_id': submission_id})
    return TemplateResponse(request, 'testrun/data.html', {
        'header': _("Output"),
        'data': data,
        'left': size - len(data),
        'decode_error': decode_error,
        'download_url': download_url
    })
Пример #11
0
def source_diff_view(request, submission1_id, submission2_id):
    if request.session.get('saved_diff_id'):
        request.session.pop('saved_diff_id')
    source1 = get_submission_source_file_or_error(request,
                                                  submission1_id).read()
    source2 = get_submission_source_file_or_error(request,
                                                  submission2_id).read()

    source1, decode_error1 = decode_str(source1)
    source2, decode_error2 = decode_str(source2)
    source1 = source1.splitlines()
    source2 = source2.splitlines()

    numwidth = len(str(max(len(source1), len(source2))))
    ndiff = difflib.ndiff(source1, source2)

    class DiffLine(object):
        def __init__(self, css_class, text, number):
            self.css_class = css_class
            self.text = text
            self.number = number

    def diffstrip(line):
        return line[2:]

    def numformat(num):
        return str(num).rjust(numwidth)

    diff1, diff2 = [], []
    count1, count2 = 1, 1

    for diffline in ndiff:
        line = diffstrip(diffline)
        line = line.expandtabs(4)
        maxlen = getattr(settings, 'CHARACTERS_IN_LINE', 80)
        parts = (len(line) + maxlen) / maxlen
        line = line.ljust(parts * maxlen)
        for i in range(parts):
            f, t = i * maxlen, ((i + 1) * maxlen)
            c1, c2 = numformat(count1), numformat(count2)
            if diffline.startswith('- '):
                diff1.append(DiffLine('left', line[f:t], '' if i else c1))
                diff2.append(DiffLine('empty', '', ''))
            elif diffline.startswith('+ '):
                diff1.append(DiffLine('empty', '', ''))
                diff2.append(DiffLine('right', line[f:t], '' if i else c2))
            elif diffline.startswith('  '):
                diff1.append(DiffLine('both', line[f:t], '' if i else c1))
                diff2.append(DiffLine('both', line[f:t], '' if i else c2))
        if diffline.startswith('- ') or diffline.startswith('  '):
            count1 += 1
        if diffline.startswith('+ ') or diffline.startswith('  '):
            count2 += 1

    download_url1 = reverse('download_submission_source',
                            kwargs={'submission_id': submission1_id})
    download_url2 = reverse('download_submission_source',
                            kwargs={'submission_id': submission2_id})

    return TemplateResponse(
        request, 'programs/source_diff.html', {
            'source1':
            diff1,
            'decode_error1':
            decode_error1,
            'download_url1':
            download_url1,
            'source2':
            diff2,
            'decode_error2':
            decode_error2,
            'download_url2':
            download_url2,
            'submission1_id':
            submission1_id,
            'submission2_id':
            submission2_id,
            'reverse_diff_url':
            reverse('source_diff',
                    kwargs={
                        'submission1_id': submission2_id,
                        'submission2_id': submission1_id
                    })
        })
Пример #12
0
def source_diff_view(request, contest_id, submission1_id, submission2_id):
    if request.session.get("saved_diff_id"):
        request.session.pop("saved_diff_id")
    submission1 = get_submission_or_404(request, contest_id, submission1_id, ProgramSubmission)
    submission2 = get_submission_or_404(request, contest_id, submission2_id, ProgramSubmission)
    source1 = submission1.source_file.read()
    source1, decode_error1 = decode_str(source1)
    source2 = submission2.source_file.read()
    source2, decode_error2 = decode_str(source2)
    source1 = source1.splitlines()
    source2 = source2.splitlines()

    numwidth = len(str(max(len(source1), len(source2))))
    ndiff = difflib.ndiff(source1, source2)

    class DiffLine(object):
        def __init__(self, css_class, text, number):
            self.css_class = css_class
            self.text = text
            self.number = number

    def diffstrip(line):
        return line[2:]

    def numformat(num):
        return str(num).rjust(numwidth)

    diff1, diff2 = [], []
    count1, count2 = 1, 1

    for diffline in ndiff:
        line = diffstrip(diffline)
        line = line.expandtabs(4)
        maxlen = getattr(settings, "CHARACTERS_IN_LINE", 80)
        parts = (len(line) + maxlen) / maxlen
        line = line.ljust(parts * maxlen)
        for i in xrange(parts):
            f, t = i * maxlen, ((i + 1) * maxlen)
            c1, c2 = numformat(count1), numformat(count2)
            if diffline.startswith("- "):
                diff1.append(DiffLine("left", line[f:t], "" if i else c1))
                diff2.append(DiffLine("empty", "", ""))
            elif diffline.startswith("+ "):
                diff1.append(DiffLine("empty", "", ""))
                diff2.append(DiffLine("right", line[f:t], "" if i else c2))
            elif diffline.startswith("  "):
                diff1.append(DiffLine("both", line[f:t], "" if i else c1))
                diff2.append(DiffLine("both", line[f:t], "" if i else c2))
        if diffline.startswith("- ") or diffline.startswith("  "):
            count1 += 1
        if diffline.startswith("+ ") or diffline.startswith("  "):
            count2 += 1

    download_url1 = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission1_id}
    )
    download_url2 = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission2_id}
    )

    return TemplateResponse(
        request,
        "programs/source_diff.html",
        {
            "source1": diff1,
            "decode_error1": decode_error1,
            "download_url1": download_url1,
            "source2": diff2,
            "decode_error2": decode_error2,
            "download_url2": download_url2,
            "reverse_diff_url": reverse(
                "source_diff",
                kwargs={"contest_id": contest_id, "submission1_id": submission2_id, "submission2_id": submission1_id},
            ),
        },
    )