-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreposerver
executable file
·193 lines (146 loc) · 6.3 KB
/
reposerver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python
# Copyright (C) 2011 Javier Palacios
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License Version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
__version__ = "1.0"
__usage__ = """%prog [options]"""
from BaseHTTPServer import BaseHTTPRequestHandler , HTTPServer
import os
import repolib
import repolib.cache
class ServerConf ( dict ) :
def __init__ ( self ) :
dict.__init__ ( self )
def append ( self , reporoot , mirror_class=None , source=None ) :
key = os.path.basename( reporoot )
if mirror_class :
url = source
if key in self.keys() :
if url != self.source_url(key) :
repolib.logger.warning( "Directory %s : mismatching urls %s & %s" % ( key , self.source_url(key) , url ) )
return
# FIXME : this check is probably redundant
if not url.endswith('/') :
repolib.logger.warning( "Repo %s : Fix configuration, url should have a trailing '/'" % key )
url += "/"
else :
reporoot = source
url = None
self[key] = reporoot , url
def __getitem__ ( self , key ) :
return dict.__getitem__( self , key )[0]
def source_url ( self , key ) :
return dict.__getitem__( self , key )[1]
def read ( self ) :
for repoconf in repolib.config.get_all_build_configs() :
try :
if repoconf['type'] == "snapshot" :
repolib.logger.warning( "Repo %s : snapshot repositories not yet supported" % repoconf.name )
continue
repo = repolib.BuildRepository.new( repoconf.name )
self.append( repoconf.name , source=repo.repo_path() )
except Exception , ex :
repolib.logger.warning( "Repo %s : %s" % ( repoconf.name , ex ) )
for repoconf in repolib.config.get_all_mirror_configs( "class" , "passthru" ) :
try :
repo = repolib.MirrorRepository.new( repoconf.name )
self.append( repo.repo_path() , "passthru" )
except Exception , ex :
repolib.logger.warning( "Repo %s : %s" % ( repoconf.name , ex ) )
for repoconf in repolib.config.get_all_mirror_configs( "class" , "cache" ) :
try :
repo = repolib.MirrorRepository.new( repoconf.name )
self.append( repo.repo_path() , "cache" , repo.base_url() )
except Exception , ex :
repolib.logger.warning( "Repo %s : %s" % ( repoconf.name , ex ) )
def dump ( self , key ) :
out = [ '' ]
out.append( 'Alias %s/%s "%s"' % ( repolib.config.web['uri'] , key , self[key] ) )
out.append( '<Directory "%s">' % self[key] )
out.append( ' Options Indexes MultiViews' )
url = self.source_url( key )
if url :
out.append( ' SetHandler python-program' )
out.append( ' PythonOption source_url %s' % url )
out.append( ' PythonHandler repolib.cache' )
out.append( '</Directory>' )
out.append( '' )
return "\n".join( out )
server_conf = ServerConf()
class Handler ( BaseHTTPRequestHandler ) :
status = None
content_type = None
def sendfile ( self , path ) :
fd = open( path )
self.wfile.write( fd.read() )
fd.close()
def write ( self , msg ) :
self.wfile.write( msg )
def log_error ( self , msg , severity=repolib.cache.apache.APLOG_ERROR ) :
BaseHTTPRequestHandler.log_error( self , "%s : %s" % ( severity , msg ) )
def do_GET ( self ) :
repo , uri = os.path.normpath(self.path).strip('/').split('/',1)
if not server_conf.has_key( repo ) :
self.status = 404
self.log_error( "Unknown repo '%s'" % repo )
else :
local_path = os.path.join( server_conf[repo] , uri )
source_url = server_conf.source_url( repo )
if source_url :
remote_url = repolib.urljoin( source_url , uri )
retcode = repolib.cache.get_file( self , local_path , remote_url )
if retcode :
if self.status and self.status != retcode :
self.log_error( "Return code '%s' didn't match status '%s'" % ( retcode , self.status ) )
self.status = retcode
else :
if os.path.isfile( local_path ) :
self.status = 200
self.sendfile( local_path )
else :
self.status = 404
self.log_error( "%s not found" % self.path )
self.send_response( self.status )
if self.content_type :
self.send_header( "Content-Type" , self.content_type )
self.end_headers()
import optparse
def option_parser () :
version_string = "%%prog %s" % __version__
parser = optparse.OptionParser( usage=__usage__ , version=version_string )
parser.set_defaults( port = 8080 )
parser.set_defaults( outfile = os.path.join( repolib.config.web['conf'] , "conf.d" , "repomirror.conf" ) )
parser.add_option( "--port" , type=int ,
help="listen port for server (defaults 8080)" )
parser.add_option( "--apache" , action="store_true" ,
help="dump apache configuration" )
parser.add_option( "--outfile" , metavar="FILENAME" ,
help="file for configuration dump [defaults to ServerRoot+conf.d/repomirror.conf]" )
return parser
def main ( opts ) :
if opts.apache :
fd = open( opts.outfile , "w" )
for k in server_conf :
fd.write( server_conf.dump(k) )
fd.close()
else :
try :
server = HTTPServer( ('',opts.port) , Handler )
server.serve_forever()
except KeyboardInterrupt , ex :
server.socket.close()
if __name__ == "__main__" :
server_conf.read()
parser = option_parser()
opts , args = parser.parse_args()
if args :
parser.print_help()
sys.exit(1)
main( opts )