pycl.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (C) 2009 David Hilley <davidhi@cc.gatech.edu>
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. import cgi, urlparse
  18. import subprocess
  19. import tempfile, time
  20. import os, sys, re
  21. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  22. temp_has_delete=True
  23. class Handler(BaseHTTPRequestHandler):
  24. global temp_has_delete
  25. def do_GET(self):
  26. if self.path == '/status':
  27. self.send_response(200)
  28. self.send_header('Content-Type', 'text/plain; charset=utf-8')
  29. self.end_headers()
  30. self.wfile.write('edit-server is running.\n')
  31. return
  32. self.send_error(404, "GET Not Found: %s" % self.path)
  33. def do_POST(self):
  34. try:
  35. (content, params) = cgi.parse_header(self.headers.
  36. getheader('content-type'))
  37. clength = 0
  38. cl = self.headers.getheader('content-length')
  39. if cl != None:
  40. clength = int(cl)
  41. else:
  42. self.send_response(411)
  43. self.end_headers()
  44. return
  45. body = self.rfile.read(clength)
  46. print body
  47. l = [s for s in self.path.split('/') if s]
  48. print l
  49. # write text into file
  50. url = self.headers.getheader('x-url')
  51. print "url:", url
  52. prefix = "chrome_"
  53. if url:
  54. prefix += re.sub("[^.\w]", "_", re.sub("^.*?//","",url))
  55. prefix += "_"
  56. if temp_has_delete==True:
  57. f = tempfile.NamedTemporaryFile(
  58. delete=False, prefix=prefix, suffix='.txt')
  59. fname = f.name
  60. else:
  61. tf = tempfile.mkstemp(prefix=prefix, suffix='.txt')
  62. f = os.fdopen(tf[0],"w")
  63. fname = tf[1]
  64. f.write(body)
  65. f.close()
  66. # spawn editor...
  67. print "Spawning editor... ", fname
  68. p = subprocess.Popen(["/usr/bin/emacsclient", "-c", fname], close_fds=True)
  69. #p = subprocess.Popen(["/usr/local/bin/mvim", "--remote-wait", fname], close_fds=True)
  70. # hold connection open until editor finishes
  71. rc = p.wait()
  72. if not rc:
  73. self.send_response(200)
  74. self.end_headers()
  75. f = file(fname, 'r')
  76. s = f.read()
  77. f.close()
  78. else:
  79. if rc > 0:
  80. msg = 'text editor returned %d' % rc
  81. elif rc < 0:
  82. msg = 'text editor died on signal %d' % -rc
  83. self.send_error(404, msg)
  84. try:
  85. os.unlink(fname)
  86. except :
  87. print "Unable to unlink:", fname
  88. pass
  89. self.wfile.write(s)
  90. except :
  91. print "Error: ", sys.exc_info()[0]
  92. self.send_error(404, "Not Found: %s" % self.path)
  93. def main():
  94. global temp_has_delete
  95. import platform
  96. t = platform.python_version_tuple()
  97. if int(t[0]) == 2 and int(t[1]) < 6:
  98. temp_has_delete = False;
  99. print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
  100. try:
  101. httpserv = HTTPServer(('localhost', 9292), Handler)
  102. httpserv.table = {}
  103. httpserv.serve_forever()
  104. except KeyboardInterrupt:
  105. httpserv.socket.close()
  106. if __name__ == '__main__':
  107. main()