<commit_before>import io
import logging
import os
import platform
import shlex
import shutil
import subprocess
import sys
import tempfile
import tkinter as tk
from tkinter import messagebox

from porcupine import utils


log = logging.getLogger(__name__)
_scriptdir = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'scripts')


if platform.system() == 'Windows':
    if os.path.basename(sys.executable).lower() == 'pythonw.exe':
        # pythonw.exe is like python.exe but hides stdout and stderr
        _running_pythonw = True
        _python = sys.executable[:-5] + sys.executable[-4:]  # get rid of 'w'
    else:
        # porcupine was started from command prompt or powershell
        _running_pythonw = False
        _python = sys.executable

    def run(path):
        command = [_python, os.path.join(_scriptdir, 'windows_run.py'), path]
        if not _running_pythonw:
            # windows wants to run python in the same terminal that
            # Porcupine was started from, this is the only way to open a
            # new command prompt i found and it works :^) we need cmd
            # because start is built in to cmd (lol)
            command = ['cmd', '/c', 'start'] + command
        subprocess.Popen(command)

else:
    # we can't assume X11 or Aqua yet because someone might be running
    # X11 on OSX and we can't check that with tkinter yet

    def run(path):
        dirname, basename = os.path.split(os.path.abspath(path))
        command = [os.path.join(_scriptdir, 'sh_run.sh'),
                   sys.executable, dirname, basename]

        if utils.windowingsystem() == 'aqua':
            # these things are wrong with this:
            #  - i needed to cheat and use stackoverflow because i don't
            #    have a mac :( http://stackoverflow.com/a/989357
            #  - new OSX versions seem to keep the terminal open by
            #    default but older versions don't, so people using a new
            #    OSX need to change their terminal settings
            # big thanks to go|dfish for testing this code!
            quoted_command = ' '.join(map(shlex.quote, command))
            file = tempfile.NamedTemporaryFile(
                prefix='porcupine-', suffix='.command', delete=False)

            with io.TextIOWrapper(file.file) as textfile:
                print('#!/bin/sh', file=textfile)
                print(quoted_command, file=textfile)
                print('rm', shlex.quote(file.name), file=textfile)  # see below

            os.chmod(file.name, 0o755)
            subprocess.Popen(['open', file.name])
            # the terminal might be still opening when we get here,
            # that's why the file deletes itself

        else:
            terminal = shutil.which('x-terminal-emulator')
            if terminal is None:
                log.error("shutil.which('x-terminal-emulator') returned None")
                messagebox.showerror(
                    "Terminal not found",
                    "Cannot find x-terminal-emulator in $PATH. Make sure " +
                    "that you have a terminal installed and try again.")
                return

            while os.path.islink(terminal):
                terminal = os.readlink(terminal)
            log.debug("x-terminal-emulator points to '%s'", terminal)

            if os.path.basename(terminal) == 'mate-terminal.wrapper':
                # it's a python script that changes some command line
                # options and runs mate-terminal, but it breaks the -e
                # option for some reason
                log.info("using mate-terminal instead of "
                         "mate-terminal.wrapper")
                terminal = 'mate-terminal'

            quoted_command = ' '.join(map(shlex.quote, command))
            subprocess.Popen([terminal, '-e', quoted_command])


if __name__ == '__main__':
    # simple test
    with tempfile.TemporaryDirectory() as tempdir:
        script = os.path.join(tempdir, 'hello.py')
        with open(script, 'w') as f:
            print("print('hello world')", file=f)

        root = tk.Tk()
        button = tk.Button(root, text="run", command=lambda: run(script))
        button.pack()
        root.mainloop()
<commit_msg>__Myst__'s suggestions<commit_after>import logging
import os
import platform
import shlex
import shutil
import subprocess
import sys
import tempfile
import tkinter as tk
from tkinter import messagebox

from porcupine import utils


log = logging.getLogger(__name__)
_scriptdir = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'scripts')


if platform.system() == 'Windows':
    if os.path.basename(sys.executable).lower() == 'pythonw.exe':
        # pythonw.exe is like python.exe but hides stdout and stderr
        _running_pythonw = True
        _python = sys.executable[:-5] + sys.executable[-4:]  # get rid of 'w'
    else:
        # porcupine was started from command prompt or powershell
        _running_pythonw = False
        _python = sys.executable

    def run(path):
        command = [_python, os.path.join(_scriptdir, 'windows_run.py'), path]
        if not _running_pythonw:
            # windows wants to run python in the same terminal that
            # Porcupine was started from, this is the only way to open a
            # new command prompt i found and it works :^) we need cmd
            # because start is built in to cmd (lol)
            command = ['cmd', '/c', 'start'] + command
        subprocess.Popen(command)

else:
    # we can't assume X11 or Aqua yet because someone might be running
    # X11 on OSX and we can't check that with tkinter yet

    def run(path):
        dirname, basename = os.path.split(os.path.abspath(path))
        command = [os.path.join(_scriptdir, 'sh_run.sh'),
                   sys.executable, dirname, basename]

        if utils.windowingsystem() == 'aqua' and 'TERMINAL' not in os.environ:
            # use OSX's default Terminal.app
            # these things are wrong with this:
            #  - i needed to cheat and use stackoverflow because i don't
            #    have a mac :( http://stackoverflow.com/a/989357
            #  - new OSX versions seem to keep the terminal open by
            #    default but older versions don't, so people using a new
            #    OSX need to change their terminal settings
            # big thanks to go|dfish for testing this code!
            quoted_command = ' '.join(map(shlex.quote, command))
            with tempfile.NamedTemporaryFile('w', delete=False,
                                             prefix='porcupine-') as file:
                print('#!/bin/sh', file=file)
                print(quoted_command, file=file)
                print('rm', shlex.quote(file.name), file=file)  # see below

            os.chmod(file.name, 0o755)
            subprocess.Popen(['open', '-a', 'Terminal.app', file.name])
            # the terminal might be still opening when we get here,
            # that's why the file deletes itself

        else:
            env_terminal = os.environ.get('TERMINAL', 'x-terminal-emulator')
            terminal = shutil.which(env_terminal)
            if terminal is None:
                log.error("shutil.which(%r) returned None" % env_terminal)
                messagebox.showerror(
                    "Terminal not found",
                    ("Cannot find %s in $PATH. Make sure that you have " +
                     "a terminal installed and try again.") % env_terminal)
                return

            if env_terminal == 'x-terminal-emulator':
                while os.path.islink(terminal):
                    terminal = os.readlink(terminal)
                log.debug("x-terminal-emulator points to '%s'", terminal)

                if os.path.basename(terminal) == 'mate-terminal.wrapper':
                    # it's a python script that changes some command line
                    # options and runs mate-terminal, but it breaks the -e
                    # option for some reason
                    log.info("using mate-terminal instead of "
                             "mate-terminal.wrapper")
                    terminal = 'mate-terminal'

            quoted_command = ' '.join(map(shlex.quote, command))
            subprocess.Popen([terminal, '-e', quoted_command])


if __name__ == '__main__':
    # simple test
    with tempfile.TemporaryDirectory() as tempdir:
        script = os.path.join(tempdir, 'hello.py')
        with open(script, 'w') as f:
            print("print('hello world')", file=f)

        root = tk.Tk()
        button = tk.Button(root, text="run", command=lambda: run(script))
        button.pack()
        root.mainloop()

{'fraction_non_alphanumeric': 0.09894033632803502, 'fraction_numerical': 0.0036857866850956, 'mean_word_length': 2.019123783031989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5792720', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2354, 'n_words': 759}