<commit_before>from __future__ import print_function
from .node import Node
import numpy as np
from lxml import etree as ET
import os
import subprocess
import sys
from .rappture import RapXML


class Tool(RapXML):
    def __init__(self, tool):
        """
        tool can be any of the following:

        - Path to a tool.xml file.
        - Name of a published tool.  The current version will be run.

        """
        dirname, xml = os.path.split(tool)
        if dirname == "":
            if xml != "tool.xml":
                # must be tool name
                dirname = "/apps/%s/current" % xml
                xml = dirname + "/rappture/tool.xml"
            else:
                dirname = os.getcwd()
        else:
            xml = tool
            dirname = os.path.abspath(os.path.join(dirname, '..'))

        xml = os.path.abspath(xml)
        if not os.path.isfile(xml):
            raise ValueError("tool must be a toolname or path to a tool.xml file.")

        invoke_file = os.path.join(dirname, 'middleware', 'invoke')
        if os.path.isfile(invoke_file):
            self.invoke_file = invoke_file

        sessdir = os.environ['SESSIONDIR']
        self.tmp_name = os.path.join(sessdir, 'tool_driver_%s.xml' % os.getpid())
        self.run_name = ""
        self.toolparameters_name = os.path.join(sessdir, 'driver_%s.hz' % os.getpid())
        self.rappturestatus_name = os.path.join(sessdir, 'rappture.status')
        self.fname = xml
        self.tree = ET.parse(xml)
        self.path = ''

    def run(self, verbose=True):
        # print("Writing", self.tmp_name)
        with open(self.tmp_name, 'w') as f:
            f.write(str(self.xml(pretty=False, header=True)))

        with open(self.toolparameters_name, 'w') as f:
            f.write("file(execute):%s" % (self.tmp_name))

        cmd = "TOOL_PARAMETERS=%s %s" % (self.toolparameters_name,self.invoke_file)
<commit_msg>make Tool work without invoke scripts again<commit_after>from __future__ import print_function
from .node import Node
import numpy as np
from lxml import etree as ET
import os
import subprocess
import sys
from .rappture import RapXML


class Tool(RapXML):
    def __init__(self, tool):
        """
        tool can be any of the following:

        - Path to a tool.xml file.
        - Name of a published tool.  The current version will be run.

        """
        dirname, xml = os.path.split(tool)
        if dirname == "":
            if xml != "tool.xml":
                # must be tool name
                dirname = "/apps/%s/current" % xml
                xml = dirname + "/rappture/tool.xml"
            else:
                dirname = os.getcwd()
        else:
            xml = tool
            dirname = os.path.abspath(dirname)

        xml = os.path.abspath(xml)
        if not os.path.isfile(xml):
            raise ValueError("tool must be a toolname or path to a tool.xml file.")

        sessdir = os.environ['SESSIONDIR']
        invoke_file = os.path.join(dirname, 'middleware', 'invoke')
        if os.path.isfile(invoke_file):
            self.invoke_file = invoke_file
        else:
            self.invoke_file = os.path.join(sessdir, 'invoke_%s' % os.getpid())
            with open(self.invoke_file, 'w') as f:
                print('#!/bin/sh', file=f)
                print('/usr/bin/invoke_app -T %s -C rappture' % dirname, file=f)
            subprocess.call('chmod +x %s' % self.invoke_file, shell=True)

        self.tmp_name = os.path.join(sessdir, 'tool_driver_%s.xml' % os.getpid())
        self.run_name = ""
        self.toolparameters_name = os.path.join(sessdir, 'driver_%s.hz' % os.getpid())
        self.rappturestatus_name = os.path.join(sessdir, 'rappture.status')
        self.dirname = dirname
        self.tool = xml
        RapXML.__init__(self, xml)

    def run(self, verbose=True):
        # print("Writing", self.tmp_name)
        with open(self.tmp_name, 'w') as f:
            f.write(str(self.xml(pretty=False, header=True)))

        with open(self.toolparameters_name, 'w') as f:
            f.write("file(execute):%s" % (self.tmp_name))
        cmd = "TOOL_PARAMETERS=%s %s" % (self.toolparameters_name, self.invoke_file)

{'fraction_non_alphanumeric': 0.12288640152417242, 'fraction_numerical': 0.0, 'mean_word_length': 2.0927835051546393, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, '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': '6851570', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1329, 'n_words': 330}