pycl.py 5.3 KB

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