annotate jb2_webserver.py @ 7:b04fd993b31e draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
author fubar
date Wed, 17 Jan 2024 07:50:52 +0000
parents
children 4c201a3d4755
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
1 #!/usr/bin/env python3# spec: simplest python web server with range support and multithreading that takes root path,
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
2 # port and bind address as command line arguments; by default uses the current dir as webroot,
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
3 # port 8000 and bind address of 0.0.0.0
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
4 # borrowed from https://github.com/danvk/RangeHTTPServer
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
5 # and reborrowed from https://gist.github.com/glowinthedark/b99900abe935e4ab4857314d647a9068
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
6 #
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
7 # The Apache 2.0 license copy in this repository is distributed with this code in accordance with that licence.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
8 # https://www.apache.org/licenses/LICENSE-2.0.txt
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
9 # This part is not MIT licenced like the other components.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
10
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
11 # APPENDIX: How to apply the Apache License to your work.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
12
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
13 # To apply the Apache License to your work, attach the following
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
14 # boilerplate notice, with the fields enclosed by brackets "[]"
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
15 # replaced with your own identifying information. (Don't include
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
16 # the brackets!) The text should be enclosed in the appropriate
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
17 # comment syntax for the file format. We also recommend that a
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
18 # file or class name and description of purpose be included on the
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
19 # same "printed page" as the copyright notice for easier
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
20 # identification within third-party archives.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
21
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
22 # Licensed under the Apache License, Version 2.0 (the "License");
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
23 # you may not use this file except in compliance with the License.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
24 # You may obtain a copy of the License at
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
25
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
26 # http://www.apache.org/licenses/LICENSE-2.0
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
27
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
28 # Unless required by applicable law or agreed to in writing, software
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
29 # distributed under the License is distributed on an "AS IS" BASIS,
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
30 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
31 # See the License for the specific language governing permissions and
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
32 # limitations under the License.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
33
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
34
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
35 import argparse
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
36 import functools
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
37 import os
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
38 import re
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
39 import socketserver
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
40 import webbrowser
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
41 from http.server import SimpleHTTPRequestHandler
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
42
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
43
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
44 DEFAULT_PORT = 8080
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
45
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
46
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
47 def copy_byte_range(infile, outfile, start=None, stop=None, bufsize=16 * 1024):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
48 """Like shutil.copyfileobj, but only copy a range of the streams.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
49
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
50 Both start and stop are inclusive.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
51 """
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
52 if start is not None:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
53 infile.seek(start)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
54 while 1:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
55 to_read = min(bufsize, stop + 1 - infile.tell() if stop else bufsize)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
56 buf = infile.read(to_read)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
57 if not buf:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
58 break
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
59 outfile.write(buf)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
60
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
61
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
62 BYTE_RANGE_RE = re.compile(r"bytes=(\d+)-(\d+)?$")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
63
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
64
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
65 def parse_byte_range(byte_range):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
66 """Returns the two numbers in 'bytes=123-456' or throws ValueError.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
67
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
68 The last number or both numbers may be None.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
69 """
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
70 if byte_range.strip() == "":
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
71 return None, None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
72
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
73 m = BYTE_RANGE_RE.match(byte_range)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
74 if not m:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
75 raise ValueError("Invalid byte range %s" % byte_range)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
76
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
77 first, last = [x and int(x) for x in m.groups()]
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
78 if last and last < first:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
79 raise ValueError("Invalid byte range %s" % byte_range)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
80 return first, last
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
81
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
82
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
83 class RangeRequestHandler(SimpleHTTPRequestHandler):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
84 """Adds support for HTTP 'Range' requests to SimpleHTTPRequestHandler
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
85
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
86 The approach is to:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
87 - Override send_head to look for 'Range' and respond appropriately.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
88 - Override copyfile to only transmit a range when requested.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
89 """
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
90
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
91 def handle(self):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
92 try:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
93 SimpleHTTPRequestHandler.handle(self)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
94 except Exception:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
95 # ignored, thrown whenever the client aborts streaming (broken pipe)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
96 pass
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
97
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
98 def send_head(self):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
99 if "Range" not in self.headers:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
100 self.range = None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
101 return SimpleHTTPRequestHandler.send_head(self)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
102 try:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
103 self.range = parse_byte_range(self.headers["Range"])
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
104 except ValueError:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
105 self.send_error(400, "Invalid byte range")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
106 return None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
107 first, last = self.range
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
108
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
109 # Mirroring SimpleHTTPServer.py here
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
110 path = self.translate_path(self.path)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
111 f = None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
112 ctype = self.guess_type(path)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
113 try:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
114 f = open(path, "rb")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
115 except IOError:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
116 self.send_error(404, "File not found")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
117 return None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
118
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
119 fs = os.fstat(f.fileno())
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
120 file_len = fs[6]
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
121 if first >= file_len:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
122 self.send_error(416, "Requested Range Not Satisfiable")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
123 return None
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
124
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
125 self.send_response(206)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
126 self.send_header("Content-type", ctype)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
127
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
128 if last is None or last >= file_len:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
129 last = file_len - 1
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
130 response_length = last - first + 1
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
131
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
132 self.send_header("Content-Range", "bytes %s-%s/%s" % (first, last, file_len))
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
133 self.send_header("Content-Length", str(response_length))
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
134 self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
135 self.end_headers()
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
136 return f
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
137
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
138 def end_headers(self):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
139 self.send_header("Accept-Ranges", "bytes")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
140 return SimpleHTTPRequestHandler.end_headers(self)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
141
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
142 def copyfile(self, source, outputfile):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
143 if not self.range:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
144 return SimpleHTTPRequestHandler.copyfile(self, source, outputfile)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
145
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
146 # SimpleHTTPRequestHandler uses shutil.copyfileobj, which doesn't let
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
147 # you stop the copying before the end of the file.
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
148 start, stop = self.range # set in send_head()
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
149 copy_byte_range(source, outputfile, start, stop)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
150
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
151
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
152 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
153 allow_reuse_address = True
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
154
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
155
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
156 if __name__ == "__main__":
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
157 parser = argparse.ArgumentParser(
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
158 description="Simple Python Web Server with Range Support"
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
159 )
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
160 parser.add_argument(
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
161 "--root",
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
162 default=os.getcwd(),
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
163 help="Root path to serve files from (default: current working directory)",
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
164 )
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
165 parser.add_argument(
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
166 "--port",
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
167 type=int,
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
168 default=DEFAULT_PORT,
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
169 help=f"Port to listen on (default: {DEFAULT_PORT})",
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
170 )
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
171 parser.add_argument(
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
172 "--bind", default="0.0.0.0", help="IP address to bind to (default: 0.0.0.0)"
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
173 )
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
174 args = parser.parse_args()
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
175
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
176 handler = functools.partial(RangeRequestHandler, directory=args.root)
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
177
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
178 webbrowser.open(f"http://{args.bind}:{args.port}")
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
179
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
180 with ThreadedTCPServer((args.bind, args.port), handler) as httpd:
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
181 print(
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
182 f"Serving HTTP on {args.bind} port {args.port} (http://{args.bind}:{args.port}/)"
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
183 )
b04fd993b31e planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/jbrowse2 commit 53a108d8153c955044ae7eb8cb06bdcfd0036717
fubar
parents:
diff changeset
184 httpd.serve_forever()