pycl.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import optparse
  25. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  26. _default_port = 9292
  27. _default_editor = "emacsclient"
  28. temp_has_delete=True
  29. processes = {}
  30. class Handler(BaseHTTPRequestHandler):
  31. global temp_has_delete
  32. def do_GET(self):
  33. if self.path == '/status':
  34. self.send_response(200)
  35. self.send_header('Content-Type', 'text/plain; charset=utf-8')
  36. self.end_headers()
  37. self.wfile.write('edit-server is running.\n')
  38. return
  39. self.send_error(404, "GET Not Found: %s" % self.path)
  40. def do_POST(self):
  41. global processes
  42. try:
  43. (content, params) = cgi.parse_header(self.headers.
  44. getheader('content-type'))
  45. clength = 0
  46. cl = self.headers.getheader('content-length')
  47. if cl != None:
  48. clength = int(cl)
  49. else:
  50. self.send_response(411)
  51. self.end_headers()
  52. return
  53. body = self.rfile.read(clength)
  54. print body
  55. l = [s for s in self.path.split('/') if s]
  56. print l
  57. existing_file = self.headers.getheader('x-file')
  58. # write text into file
  59. if not existing_file or existing_file == "undefined":
  60. existing = False
  61. url = self.headers.getheader('x-url')
  62. print "url:", url
  63. prefix = "chrome_"
  64. if url:
  65. prefix += re.sub("[^.\w]", "_", re.sub("^.*?//","",url))
  66. prefix += "_"
  67. if temp_has_delete==True:
  68. f = tempfile.NamedTemporaryFile(
  69. delete=False, prefix=prefix, suffix='.txt')
  70. fname = f.name
  71. else:
  72. tf = tempfile.mkstemp(prefix=prefix, suffix='.txt')
  73. f = os.fdopen(tf[0],"w")
  74. fname = tf[1]
  75. print "Opening new file ", fname
  76. else:
  77. existing = True
  78. p = processes[existing_file]
  79. print "Opening existing file ", existing_file
  80. f = open(existing_file, "w")
  81. fname = existing_file
  82. f.write(body)
  83. f.close()
  84. last_mod_time = os.stat(fname)[stat.ST_MTIME]
  85. if not existing:
  86. # spawn editor...
  87. print "Spawning editor... ", fname
  88. cmd = self.editor.split(",")
  89. cmd.append(fname)
  90. p = subprocess.Popen(cmd, close_fds=True)
  91. processes[fname] = p
  92. saved = False
  93. rc = None
  94. while (True):
  95. time.sleep(1)
  96. rc = p.poll()
  97. if rc != None: break
  98. mod_time = os.stat(fname)[stat.ST_MTIME]
  99. if mod_time != last_mod_time:
  100. print "new mod time:", mod_time, " last:", last_mod_time
  101. last_mod_time = mod_time
  102. saved = True
  103. if saved: break
  104. if saved or not rc:
  105. self.send_response(200)
  106. f = file(fname, 'r')
  107. s = f.read()
  108. f.close()
  109. else:
  110. if rc > 0:
  111. msg = 'text editor returned %d' % rc
  112. elif rc < 0:
  113. msg = 'text editor died on signal %d' % -rc
  114. self.send_error(404, msg)
  115. if saved:
  116. self.send_header('x-open', "true")
  117. else:
  118. try:
  119. os.unlink(fname)
  120. except :
  121. print "Unable to unlink:", fname
  122. pass
  123. self.send_header('x-file', fname)
  124. self.end_headers()
  125. self.wfile.write(s)
  126. except :
  127. print "Error: ", sys.exc_info()[0]
  128. self.send_error(404, "Not Found: %s" % self.path)
  129. def parse_options():
  130. parser = optparse.OptionParser()
  131. parser.add_option(
  132. "-p", "--port", type="int", dest="port", default=_default_port,
  133. help="port number to listen on (default: " + str(_default_port) + ")")
  134. parser.add_option(
  135. "-e", "--editor", dest="editor", default=_default_editor,
  136. help='text editor to spawn (default: "' + _default_editor + '")')
  137. return parser.parse_args()[0]
  138. def main():
  139. global temp_has_delete
  140. import platform
  141. t = platform.python_version_tuple()
  142. if int(t[0]) == 2 and int(t[1]) < 6:
  143. temp_has_delete = False;
  144. print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
  145. options = parse_options()
  146. Handler.editor = options.editor
  147. try:
  148. httpserv = HTTPServer(('localhost', options.port), Handler)
  149. httpserv.table = {}
  150. httpserv.serve_forever()
  151. except KeyboardInterrupt:
  152. httpserv.socket.close()
  153. if __name__ == '__main__':
  154. main()