コード例 #1
0
ファイル: tests.py プロジェクト: transformaps/mapstory
 def setUp(self):
     self.journalEntry = JournalEntry()
     self.journalEntry.title = "Test title"
     self.journalEntry.author = testUser
     self.journalEntry.show_on_main = False
コード例 #2
0
def webpay_run(name, payment_pk, *args, **kwargs):
    """Runs a Webpay binary within a preconfigured environment.
    Before running the binary, this context manager recreates the
    directories and files in /tmp that the binary needs to run correctly,
    as if it were sitting in an standalone web server.
    Yields a subprocess.Popen instance with an open pipe to stdin and stdout.
    After running the binary, log and journal entries are captured and
    then temporary files are removed.
    Args:
        name: A string, basename of the binary as is found in assets_dir.
        payment_pk: An integer, primary key for the related payment. This
            is needed to associate the logs with their payment.
        *args: Extra positional arguments are appended to the command line
            before execution.
    Example:
        >>> webpay_run('tbk_bp_resultado.cgi', 3958) as cgi:
                output, _ = cgi.communicate("TBK_MONTO=384800&...\n")
                do_something_with(output)
    WARNING: Always use Popen with communicate() method when using PIPE
        or there will be deadlocks.
    """

    from pprint import pprint

    # prepare the configuration files
    assets_dir = PaymentProcessor.get_backend_setting('WEBPAY_DOCS')
    tbk_config = PaymentProcessor.get_tbk_config(kwargs.get('request'),
                                                 payment_pk, 'CLP')  # FIXME
    tbk_param = PaymentProcessor.get_tbk_param()
    tbk_trace = PaymentProcessor.get_tbk_trace()

    # temp_dir = mkdtemp()
    cgi_path = os.path.join(assets_dir, name)
    # temp_cgi_path = os.path.join(assets_dir, name)
    datos_path = os.path.join(assets_dir, 'datos')

    # os.mkdir(datos_path)
    # with open(os.path.join(datos_path, 'tbk_config.dat'), 'w') as f:
    #     pprint("TBK_CONFIG: %s" % tbk_config)
    #     pprint('------------------------------------------')
    #     f.write(tbk_config)
    # with open(os.path.join(datos_path, 'tbk_param.txt'), 'w') as f:
    #     f.write(tbk_param)
    # with open(os.path.join(datos_path, 'tbk_trace.dat'), 'w') as f:
    #     f.write(tbk_trace)

    # prepare the public and private keys
    maestros_path = os.path.join(assets_dir, 'maestros')
    public_key, private_key = PaymentProcessor.get_keys()

    # os.mkdir(maestros_path)
    # with open(os.path.join(maestros_path, 'tbk_public_key.pem'), 'w') as f:
    #     f.write(public_key)
    # with open(os.path.join(maestros_path, 'privada.pem'), 'w') as f:
    #     f.write(private_key)

    # prepare the log directory
    log_path = os.path.join(assets_dir, 'log')
    # os.mkdir(log_path)

    # copy the binary to the temp dir and make it executable
    # copyfile(cgi_path, temp_cgi_path)
    # os.chmod(cgi_path, S_IEXEC)

    yield Popen([sys.executable, cgi_path] + list(args),
                stdin=PIPE,
                stdout=PIPE)

    # capture the logs
    try:
        from getpaid.models import Payment

        payment = Payment.objects.get(pk=payment_pk)

        for event_log in glob.glob(os.path.join(log_path, 'TBK_EVN*')):
            with open(event_log, 'r') as f:
                for line in map(str.strip, f.readlines()):
                    pprint("TBK_ENV: %s" % line)
                    from models import LogEntry

                    entry = LogEntry.from_line(line=line, payment=payment)
                    entry.save()
                pprint('------------------------------------------')

        for journal_log in glob.glob(
                os.path.join(log_path, 'tbk_bitacora_TR_NORMAL*')):
            st = os.stat(journal_log)
            date = datetime.date.fromtimestamp(st.st_mtime)
            with open(journal_log, 'r') as f:
                for line in map(str.strip, f.readlines()):
                    pprint("TBK_BITACORA: %s" % line)
                    from models import JournalEntry
                    entry = JournalEntry(date=date, body=line, payment=payment)
                    entry.save()
                pprint('------------------------------------------')
    except Payment.DoesNotExist:
        pass