Skip to content

Commit 7ab5ec6

Browse files
committed
use context manager node for ReplicationServer
1 parent 4128575 commit 7ab5ec6

File tree

3 files changed

+101
-101
lines changed

3 files changed

+101
-101
lines changed

src/osmium/replication/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def __init__(self, url, diff_type='osc.gz'):
3535
self.diff_type = diff_type
3636
self.session = None
3737

38-
def close():
38+
def close(self):
3939
""" Close any open connection to the replication server.
4040
"""
41-
if self.session:
42-
session.close()
41+
if self.session is not None:
42+
self.session.close()
4343
self.session = None
4444

4545
def __enter__(self):

tools/pyosmium-get-changes

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,32 @@ def main(args):
215215

216216
socket.setdefaulttimeout(options.socket_timeout)
217217

218-
svr = rserv.ReplicationServer(url)
219-
if options.cookie is not None:
220-
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
221-
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
222-
cookie_jar.load(options.cookie)
223-
opener = urlrequest.build_opener(urlrequest.HTTPCookieProcessor(cookie_jar))
224-
svr.open_url = opener.open
225-
226-
startseq = options.start.get_sequence(svr)
227-
if startseq is None:
228-
log.error("Cannot read state file from server. Is the URL correct?")
229-
return 1
218+
with rserv.ReplicationServer(url) as svr:
219+
if options.cookie is not None:
220+
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
221+
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
222+
cookie_jar.load(options.cookie)
223+
opener = urlrequest.build_opener(urlrequest.HTTPCookieProcessor(cookie_jar))
224+
svr.open_url = opener.open
225+
226+
startseq = options.start.get_sequence(svr)
227+
if startseq is None:
228+
log.error("Cannot read state file from server. Is the URL correct?")
229+
return 1
230230

231-
if options.outfile is None:
232-
write_end_sequence(options.seq_file, startseq)
233-
return 0
231+
if options.outfile is None:
232+
write_end_sequence(options.seq_file, startseq)
233+
return 0
234234

235-
log.debug("Starting download at ID %d (max %d MB)" % (startseq, options.outsize))
236-
if options.outformat is not None:
237-
outhandler = WriteHandler(options.outfile, 4096*1024, options.outformat)
238-
else:
239-
outhandler = WriteHandler(options.outfile)
235+
log.debug("Starting download at ID %d (max %d MB)" % (startseq, options.outsize))
236+
if options.outformat is not None:
237+
outhandler = WriteHandler(options.outfile, 4096*1024, options.outformat)
238+
else:
239+
outhandler = WriteHandler(options.outfile)
240240

241-
endseq = svr.apply_diffs(outhandler, startseq, max_size=options.outsize*1024,
242-
simplify=options.simplify)
243-
outhandler.close()
241+
endseq = svr.apply_diffs(outhandler, startseq, max_size=options.outsize*1024,
242+
simplify=options.simplify)
243+
outhandler.close()
244244

245245
# save cookies
246246
if options.cookie:

tools/pyosmium-up-to-date

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -70,84 +70,84 @@ def update_from_osm_server(ts, options):
7070

7171
def update_from_custom_server(url, seq, ts, options):
7272
"""Update from a custom URL, simply using the diff sequence as is."""
73-
svr = rserv.ReplicationServer(url, "osc.gz")
74-
if options.cookie is not None:
75-
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
76-
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
77-
cookie_jar.load(options.cookie)
78-
opener = urlrequest.build_opener(urlrequest.HTTPCookieProcessor(cookie_jar))
79-
svr.open_url = opener.open
80-
81-
log.info("Using replication service at %s", url)
82-
83-
current = svr.get_state_info()
84-
if current is None:
85-
log.error("Cannot download state information. Is the replication URL correct?")
86-
return 3
87-
log.debug("Server is at sequence %d (%s).", current.sequence, current.timestamp)
88-
89-
if seq is None:
90-
log.info("Using timestamp %s as starting point." % ts)
91-
startseq = svr.timestamp_to_sequence(ts)
92-
if startseq is None:
93-
log.error("No starting point found for time %s on server %s"
94-
% (str(ts), url))
95-
return 3
96-
else:
97-
if seq >= current.sequence:
98-
log.info("File is already up to date.")
99-
return 0
100-
101-
log.debug("Using given sequence ID %d" % seq)
102-
startseq = seq + 1
103-
ts = svr.get_state_info(seq=startseq)
104-
if ts is None:
105-
log.error("Cannot download state information for ID %d. Is the URL correct?" % seq)
106-
return 3
107-
ts = ts.timestamp
108-
109-
if not options.force_update:
110-
cmpdate = dt.datetime.utcnow() - dt.timedelta(days=90)
111-
cmpdate = cmpdate.replace(tzinfo=dt.timezone.utc)
112-
if ts < cmpdate:
113-
log.error(
114-
"""The OSM file is more than 3 months old. You should download a
115-
more recent file instead of updating. If you really want to
116-
update the file, use --force-update-of-old-planet.""")
117-
return 3
118-
119-
log.info("Starting download at ID %d (max %d MB)" % (startseq, options.outsize))
120-
121-
outfile = options.outfile
122-
infile = options.infile
123-
124-
if outfile is None:
125-
fdir, fname = os.path.split(infile)
126-
if options.tmpdir is not None:
127-
fdir = options.tmpdir
128-
ofname = mktemp(suffix='-' + fname, dir=fdir)
129-
else:
130-
ofname = outfile
131-
132-
try:
133-
extra_headers = { 'generator' : 'pyosmium-up-to-date/' + pyosmium_release }
134-
outseqs = svr.apply_diffs_to_file(infile, ofname, startseq,
135-
max_size=options.outsize*1024,
136-
extra_headers=extra_headers,
137-
outformat=options.outformat)
138-
139-
if outseqs is None:
140-
log.info("No new updates found.")
73+
with rserv.ReplicationServer(url, "osc.gz") as svr:
74+
if options.cookie is not None:
75+
# According to the documentation, the cookie jar loads the file only if FileCookieJar.load is called.
76+
cookie_jar = cookiejarlib.MozillaCookieJar(options.cookie)
77+
cookie_jar.load(options.cookie)
78+
opener = urlrequest.build_opener(urlrequest.HTTPCookieProcessor(cookie_jar))
79+
svr.open_url = opener.open
80+
81+
log.info("Using replication service at %s", url)
82+
83+
current = svr.get_state_info()
84+
if current is None:
85+
log.error("Cannot download state information. Is the replication URL correct?")
14186
return 3
87+
log.debug("Server is at sequence %d (%s).", current.sequence, current.timestamp)
88+
89+
if seq is None:
90+
log.info("Using timestamp %s as starting point." % ts)
91+
startseq = svr.timestamp_to_sequence(ts)
92+
if startseq is None:
93+
log.error("No starting point found for time %s on server %s"
94+
% (str(ts), url))
95+
return 3
96+
else:
97+
if seq >= current.sequence:
98+
log.info("File is already up to date.")
99+
return 0
100+
101+
log.debug("Using given sequence ID %d" % seq)
102+
startseq = seq + 1
103+
ts = svr.get_state_info(seq=startseq)
104+
if ts is None:
105+
log.error("Cannot download state information for ID %d. Is the URL correct?" % seq)
106+
return 3
107+
ts = ts.timestamp
108+
109+
if not options.force_update:
110+
cmpdate = dt.datetime.utcnow() - dt.timedelta(days=90)
111+
cmpdate = cmpdate.replace(tzinfo=dt.timezone.utc)
112+
if ts < cmpdate:
113+
log.error(
114+
"""The OSM file is more than 3 months old. You should download a
115+
more recent file instead of updating. If you really want to
116+
update the file, use --force-update-of-old-planet.""")
117+
return 3
118+
119+
log.info("Starting download at ID %d (max %d MB)" % (startseq, options.outsize))
120+
121+
outfile = options.outfile
122+
infile = options.infile
142123

143124
if outfile is None:
144-
os.rename(ofname, infile)
145-
finally:
146-
if outfile is None:
147-
try:
148-
os.remove(ofname)
149-
except FileNotFoundError:
150-
pass
125+
fdir, fname = os.path.split(infile)
126+
if options.tmpdir is not None:
127+
fdir = options.tmpdir
128+
ofname = mktemp(suffix='-' + fname, dir=fdir)
129+
else:
130+
ofname = outfile
131+
132+
try:
133+
extra_headers = { 'generator' : 'pyosmium-up-to-date/' + pyosmium_release }
134+
outseqs = svr.apply_diffs_to_file(infile, ofname, startseq,
135+
max_size=options.outsize*1024,
136+
extra_headers=extra_headers,
137+
outformat=options.outformat)
138+
139+
if outseqs is None:
140+
log.info("No new updates found.")
141+
return 3
142+
143+
if outfile is None:
144+
os.rename(ofname, infile)
145+
finally:
146+
if outfile is None:
147+
try:
148+
os.remove(ofname)
149+
except FileNotFoundError:
150+
pass
151151

152152
log.info("Downloaded until %d. Server has data available until %d." % outseqs)
153153

0 commit comments

Comments
 (0)