pycl.py 4.8 KB

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