Beispiel #1
0
def generateHighlight(repository_path, sha1, language, output_file=None):
    highlighter = createHighlighter(language)
    if not highlighter: return False

    source = gitutils.Repository.readObject(repository_path, "blob", sha1)
    source = textutils.decode(source)

    if output_file:
        highlighter(source, output_file, None)
    else:
        output_path = syntaxhighlight.generateHighlightPath(sha1, language)

        try: os.makedirs(os.path.dirname(output_path), 0750)
        except OSError, error:
            if error.errno == errno.EEXIST: pass
            else: raise

        output_file = open(output_path + ".tmp", "w")
        contexts_path = output_path + ".ctx"

        highlighter(source, output_file, contexts_path)

        output_file.close()

        os.chmod(output_path + ".tmp", 0660)
        os.rename(output_path + ".tmp", output_path)
Beispiel #2
0
def importCodeContexts(db, sha1, language):
    codecontexts_path = syntaxhighlight.generateHighlightPath(
        sha1, language) + ".ctx"

    if os.path.isfile(codecontexts_path):
        contexts_values = []

        for line in open(codecontexts_path):
            line = line.strip()

            first_line, last_line, context = line.split(" ", 2)
            if len(context
                   ) > configuration.services.HIGHLIGHT["max_context_length"]:
                context = context[:configuration.services.
                                  HIGHLIGHT["max_context_length"] - 3] + "..."
            contexts_values.append(
                (sha1, context, int(first_line), int(last_line)))

        cursor = db.cursor()
        cursor.execute("DELETE FROM codecontexts WHERE sha1=%s", [sha1])
        cursor.executemany(
            "INSERT INTO codecontexts (sha1, context, first_line, last_line) VALUES (%s, %s, %s, %s)",
            contexts_values)
        db.commit()

        os.unlink(codecontexts_path)

        return len(contexts_values)
    else:
        return 0
Beispiel #3
0
def generateHighlight(repository_path, sha1, language, mode, output_file=None):
    highlighter = createHighlighter(language)
    if not highlighter: return False

    source = gitutils.Repository.readObject(repository_path, "blob", sha1)
    source = textutils.decode(source)

    if output_file:
        highlighter(source, output_file, None)
    else:
        output_path = syntaxhighlight.generateHighlightPath(
            sha1, language, mode)

        try:
            os.makedirs(os.path.dirname(output_path), 0750)
        except OSError as error:
            if error.errno == errno.EEXIST: pass
            else: raise

        output_file = open(output_path + ".tmp", "w")
        contexts_path = output_path + ".ctx"

        if mode == "json":
            outputter = JSONOutputter(output_file)
        else:
            outputter = HTMLOutputter(output_file)

        highlighter(source, outputter, contexts_path)

        output_file.close()

        os.chmod(output_path + ".tmp", 0660)
        os.rename(output_path + ".tmp", output_path)

    return True
Beispiel #4
0
def importCodeContexts(db, sha1, language):
    codecontexts_path = syntaxhighlight.generateHighlightPath(sha1, language) + ".ctx"

    if os.path.isfile(codecontexts_path):
        contexts_values = []

        for line in open(codecontexts_path):
            line = line.strip()

            first_line, last_line, context = line.split(" ", 2)
            if len(context) > configuration.services.HIGHLIGHT["max_context_length"]:
                context = context[:configuration.services.HIGHLIGHT["max_context_length"] - 3] + "..."
            contexts_values.append((sha1, context, int(first_line), int(last_line)))

        cursor = db.cursor()
        cursor.execute("DELETE FROM codecontexts WHERE sha1=%s", [sha1])
        cursor.executemany("INSERT INTO codecontexts (sha1, context, first_line, last_line) VALUES (%s, %s, %s, %s)", contexts_values)
        db.commit()

        os.unlink(codecontexts_path)

        return len(contexts_values)
    else:
        return 0