Aug-30-2017, 01:27 AM
I have this function here to read python and php from a single script and create a file for each and paste their code into them accordingly. Execution is a seperate function.
The tested mixed source looks like this: (the php echo statements are equivelent to pythons print and not relevent to this issue.)
The tested mixed source looks like this: (the php echo statements are equivelent to pythons print and not relevent to this issue.)
?> print("python check 1") <?php echo "php check 1"; ?> print("python check 2") <?php echo "php check 2"; ?>The python function to "compile" this:
from sys import argv from subprocess import call import os def split(): code, ext = os.path.splitext(argv[1]) source = code + ext phpexec = code + ".php" pyexec = code + ".py" with open(source, "r") as source: if ext != ".pyp": print("Source file has an invalid file extension! It must end with .pyp.") else: with open(phpexec, "a+") as phpexec: if ext == ".pyp": for line in source: if line.strip() == "<?php": copy = True elif line.strip() == "?>": copy = False elif copy: start = line.find("<?php") + 1 end = line.find("?>", start) lines = line[start:end] print(lines, file=phpexec) with open(pyexec, "a+") as pyfile: for line in source: if line.strip() == "?>": copy = True elif line.strip() == "<?php": copy = False elif copy: print("#!/usr/bin/env python", file=pyfile) print(lines, file=pyfile)The python isn't written to it's file and the php is returning the entire line which leads me to believe it's not running and python or my terminal is printing the entire lines:
echo "php check 1"; echo "php check 2";The function above is meant to check for the "<?php" and "?>" tags to locate the php and seperate it from python. Where is my problem? Should I be openning the files differently or maybe how I'm writing to them?