<commit_before>#!/usr/bin/env python

"""
Convert PGF files to an image file using pdflatex, pdfcrop, (from
texlive) and convert (from ImageMagick).
"""

import os
import sys
import tempfile
import time
import subprocess

def __find_exec(executable):
    '''Try to find an executable in the system path.'''

    if os.path.isfile(executable):
        return executable
    else:
        paths = os.environ['PATH'].split(os.pathsep)
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return ''

def __check_for_exec(executable, msg):
    '''Exit on error if the specified executable cannot be
    found. Otherwise, return the path to the executable.'''

    path = __find_exec(executable)
    if path == '':
        print msg
        sys.exit()
    else:
        return path
    
def __run_cmd(cmd, msg, cwd=None, wait=30):
    '''Run a system command; display an error message if it returns a
    nonzero exit code or it stalls for more than the specified number
    of seconds.'''

    dev_null = open('/dev/null', 'w')
    p = subprocess.Popen(cmd, stdout=dev_null, stderr=dev_null, shell=True, cwd=cwd)
    tic = time.time()
    while p.returncode == None and time.time() < tic+wait:
        try:
            p.poll()
        except KeyboardInterrupt:
            print 'manually killing command %s' % cmd
            p.kill()
            sys.exit(1)
    if p.returncode == None:
        print 'killing stalled command %s ' % cmd
        p.kill()
    if p.returncode < 0:
        print msg
        sys.exit(1)

# Check for required executables:
PDFLATEX = __check_for_exec('pdflatex', 'cannot find pdflatex')
PDFCROP = __check_for_exec('pdfcrop', 'cannot find pdfcrop')
CONVERT = __check_for_exec('convert', 'cannot find convert')
RM = __check_for_exec('rm', 'cannot find rm')

# Used to redirect program output to /dev/null:
redirect_output = ' 1>/dev/null 2>&1'

# Defaults:
default_template = """\\documentclass[10pt]{article}
\\usepackage{amsmath,amssymb,amsbsy,amsfonts,amsthm}
\\usepackage{cmbright}
\\usepackage{tikz}
\\pagestyle{empty}
\\begin{document}
<>
\\end{document}
"""
default_density = 200

def pgf2img(input_filename, output_filename,
            template=default_template, density=default_density):
    """Convert a PGF/TikZ file to an image file.<commit_msg>Update pgf2img.py to properly convert very wide diagrams.<commit_after>#!/usr/bin/env python

"""
Convert PGF files to an image file using pdflatex, pdfcrop, (from
texlive) and convert (from ImageMagick).
"""

import os
import sys
import tempfile
import time
import subprocess

def __find_exec(executable):
    '''Try to find an executable in the system path.'''

    if os.path.isfile(executable):
        return executable
    else:
        paths = os.environ['PATH'].split(os.pathsep)
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return ''

def __check_for_exec(executable, msg):
    '''Exit on error if the specified executable cannot be
    found. Otherwise, return the path to the executable.'''

    path = __find_exec(executable)
    if path == '':
        print msg
        sys.exit()
    else:
        return path
    
def __run_cmd(cmd, msg, cwd=None, wait=30):
    '''Run a system command; display an error message if it returns a
    nonzero exit code or it stalls for more than the specified number
    of seconds.'''

    dev_null = open('/dev/null', 'w')
    p = subprocess.Popen(cmd, stdout=dev_null, stderr=dev_null, shell=True, cwd=cwd)
    tic = time.time()
    while p.returncode == None and time.time() < tic+wait:
        try:
            p.poll()
        except KeyboardInterrupt:
            print 'manually killing command %s' % cmd
            p.kill()
            sys.exit(1)
    if p.returncode == None:
        print 'killing stalled command %s ' % cmd
        p.kill()
    if p.returncode < 0:
        print msg
        sys.exit(1)

# Check for required executables:
PDFLATEX = __check_for_exec('pdflatex', 'cannot find pdflatex')
PDFCROP = __check_for_exec('pdfcrop', 'cannot find pdfcrop')
CONVERT = __check_for_exec('convert', 'cannot find convert')
RM = __check_for_exec('rm', 'cannot find rm')

# Used to redirect program output to /dev/null:
redirect_output = ' 1>/dev/null 2>&1'

# Defaults:
default_template = """\\documentclass[10pt]{article}
\\usepackage{amsmath,amssymb,amsbsy,amsfonts,amsthm}
\\usepackage[landscape]{geometry}
\\usepackage{cmbright}
\\usepackage{tikz}
\\pagestyle{empty}
\\begin{document}
<>
\\end{document}
"""
default_density = 200

def pgf2img(input_filename, output_filename,
            template=default_template, density=default_density):
    """Convert a PGF/TikZ file to an image file.
{'fraction_non_alphanumeric': 0.11564059900166389, 'fraction_numerical': 0.006031613976705491, 'mean_word_length': 2.997506234413965, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5071256', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1494, 'n_words': 461}