Python Forum
read python and php from a file and copy them to their own files to execute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read python and php from a file and copy them to their own files to execute
#1
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.)
?>
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?
Reply
#2
I think you want to print line to pyfile on line 34, not lines. I would also write the shebang to the file (line 33) before the for loop, not during it. You just want it at the top, not before every line.

Also, I would use pyfile.write(line). It's more pythonic. Using print makes your code rather confusing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
ok so i changed lines to line (i had a start=/end= for python too but edited it out trying to debug)
and moved the shebang just above the for loop and the "compiled" python file now has a shebang but thats it and the php is still the same

EDIT: after changing the prints to writes its not automatically going to the next line. how can i get that back while still using write?
Reply
#4
Adding \n to the end of the shebang literal should fix the problem with the next line.

For the php section, the elif copy block triggers on the line after '<?php'. Then you look for '<?php' and '?>', neither of which exist on that line. So it ends up writing an empty string to the phpexec file. You would want to make it more like the python section. If you want to keep the php tags in the php file, write them when you detect them and change the copy flag.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
ok so both open blocks look like this:
   with open(pyexec, "a+") as pyfile:
    pyfile.write("#!/usr/bin/env python")
    for line in source:
     if line.strip() == "?>":
      copy = True
     elif line.strip() == "<?php":
      copy = False
     elif copy:
      pyfile.write(line)
php is written to it's file properly but not executed like it should be and pythons file just get the shebang but if i write to it manually it does run. EDIT: the php block is now outputing:
php check 1
php check 2
like it should. The biggest issue is python not writing it lines
Reply
#6
Like I said, the shebang needs a \n at the end of it to write to a new line. The lines you are reading in from source should be read in with a terminal \n, so they should write out fine. They're writing out fine for me.

I don't know about executing php, I'm a Python programmer.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Aug-30-2017, 03:33 PM)ichabod801 Wrote: I don't know about executing php, I'm a Python programmer.

the php is fine now the python just isnt writing out to its file
Declare variables, not war. **hand** **biggrin**
Reply
#8
Can you repost your code and input as it is now?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
I have made a small modification that does nothing for the issue but allows me to use a browser for a gui. Basicaly the php block is now html wich contains php. Here's my entire source:
#!/bin/env python
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("./index.php", "a+") as phpexec:
    if ext == ".pyp":
     phpexec.write("<html>\n")
     for line in source:
      if line.strip() == "<html>":
       copy = True
      elif line.strip() == "</html>":
       copy = False
      elif copy:
       phpexec.write(line)
     phpexec.write("</html>")
   with open(pyexec, "a+") as pyfile:
    pyfile.write("#!/usr/bin/env python\n")
    for line in source:
     if line.strip() == "</html>":
      copy = True
     elif line.strip() == "<html>":
      copy = False
     elif copy:
      pyfile.write(line)
      
      
def exe():
 code, ext = os.path.splitext(argv[1])
 #i was calling the python script from here but it now needs to be in the php so it can get phps variables. php already gets pythons variable through the "requests" module.
 php = ["php","-S" , "127.0.0.1:1270"]
 call(php)
 

split()
exe()
and the test .pyp file:
</html>
print("python check 1")
<html>
 <?php
  echo "php check 1<br>";
  ?>
 </html>
print("python check 2")
<html>
 <?php
  echo "php check 2<br>"; #the <br> is an html line break. it's equivelant to a \n but works in a browser.
  ?>
</html>
Declare variables, not war. **hand** **biggrin**
Reply
#10
I'm not having any problems with the python, it's writing to the file fine.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 257 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 425 Feb-07-2024, 12:24 PM
Last Post: Viento
  Recommended way to read/create PDF file? Winfried 3 2,872 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,431 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,106 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,105 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,514 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,196 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Create new folders and copy files cocobolli 3 1,445 Mar-22-2023, 10:23 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020