@@ -70,84 +70,84 @@ def update_from_osm_server(ts, options):
7070
7171def 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