docker-osm/docker-osmupdate/download.py

151 wiersze
5.1 KiB
Python
Czysty Zwykły widok Historia

2015-07-30 09:38:13 +00:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
2015-07-30 13:45:34 +00:00
"""
/***************************************************************************
Docker-OSM
An ImpOSM database up-to-date.
-------------------
begin : 2015-07-15
email : etienne at kartoza dot com
contributor : Etienne Trimaille
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
2015-07-30 09:38:13 +00:00
2015-07-31 09:09:04 +00:00
from os.path import exists, join, isabs, abspath, isfile
2015-07-30 09:38:13 +00:00
from os import listdir, environ
from sys import exit
from subprocess import call
from datetime import datetime
from time import sleep
from sys import stderr
2015-07-30 13:45:34 +00:00
# In docker-compose, we should wait for the DB is ready.
sleep(45)
2015-07-30 09:38:13 +00:00
# Default values which can be overwritten.
default = {
'MAX_DAYS': '100',
'DIFF': 'sporadic',
'MAX_MERGE': '7',
'COMPRESSION_LEVEL': '1',
'BASE_URL': 'http://planet.openstreetmap.org/replication/',
'IMPORT_QUEUE': 'import_queue',
'IMPORT_DONE': 'import_done',
2015-07-31 08:43:29 +00:00
'SETTINGS': 'settings',
2015-07-30 09:38:13 +00:00
'TIME': 120,
}
for key in default.keys():
if key in environ:
default[key] = environ[key]
# Folders
2015-07-31 08:43:29 +00:00
folders = ['IMPORT_QUEUE', 'IMPORT_DONE', 'SETTINGS']
2015-07-30 09:38:13 +00:00
for folder in folders:
if not isabs(default[folder]):
# Get the absolute path.
default[folder] = abspath(default[folder])
# Test the folder
if not exists(default[folder]):
print >> stderr, 'The folder %s does not exist.' % default[folder]
exit()
# Test files
state_file = None
osm_file = None
poly_file = None
2015-07-31 08:43:29 +00:00
for f in listdir(default['SETTINGS']):
2015-07-30 09:38:13 +00:00
if f.endswith('.state.txt'):
2015-07-31 08:43:29 +00:00
state_file = join(default['SETTINGS'], f)
2015-07-30 09:38:13 +00:00
2015-07-30 14:08:18 +00:00
if f.endswith('.pbf'):
2015-07-31 08:43:29 +00:00
osm_file = join(default['SETTINGS'], f)
2015-07-30 09:38:13 +00:00
if f.endswith('.poly'):
2015-07-31 08:43:29 +00:00
poly_file = join(default['SETTINGS'], f)
2015-07-30 09:38:13 +00:00
2015-07-31 09:09:04 +00:00
"""
# Todo : need fix custom URL and sporadic diff : daily, hourly and minutely
if f == 'custom_url_diff.txt':
with open(join(default['SETTINGS'], f), 'r') as content_file:
default['BASE_URL'] = content_file.read()
"""
2015-07-30 09:38:13 +00:00
if not state_file:
2015-07-31 08:43:29 +00:00
print >> stderr, 'State file *.state.txt is missing in %s' % default['SETTINGS']
2015-07-30 09:38:13 +00:00
exit()
if not osm_file:
2015-07-31 08:43:29 +00:00
print >> stderr, 'OSM file *.osm.pbf is missing in %s' % default['SETTINGS']
2015-07-30 09:38:13 +00:00
exit()
if not poly_file:
2015-07-31 08:43:29 +00:00
print 'No *.poly detected in %s' % default['SETTINGS']
2015-07-30 09:38:13 +00:00
else:
print '%s detected for clipping.' % poly_file
while True:
# Check if diff to be imported is empty. If not, take the latest diff.
diff_to_be_imported = sorted(listdir(default['IMPORT_QUEUE']))
if len(diff_to_be_imported):
print "Timestamp from the lastest not imported diff."
timestamp = diff_to_be_imported[-1].split('.')[0]
else:
# Check if imported diff is empty. If not, take the latest diff.
imported_diff = sorted(listdir(default['IMPORT_DONE']))
if len(imported_diff):
print "Timestamp from the lastest imported diff."
timestamp = imported_diff[-1].split('.')[0]
else:
# Take the timestamp from original file.
print "Timestamp from the original state file."
state_file_settings = {}
with open(state_file) as a_file:
for line in a_file:
if '=' in line:
name, value = line.partition("=")[::2]
state_file_settings[name] = value
timestamp = state_file_settings['timestamp'].strip()
# Removing some \ in the timestamp.
timestamp = timestamp.replace('\\', '')
# Save time
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
print 'Old time : %s' % timestamp
print 'Current time : %s' % current_time
# Destination
file_name = '%s.osc.gz' % current_time
file_path = join(default['IMPORT_QUEUE'], file_name)
# Command
command = ['osmupdate', '-v']
if poly_file:
command.append('-B=%s' % poly_file)
command += ['--max-days=' + default['MAX_DAYS']]
command += [default['DIFF']]
command += ['--max-merge=' + default['MAX_MERGE']]
command += ['--compression-level=' + default['COMPRESSION_LEVEL']]
command += ['--base-url=' + default['BASE_URL']]
command.append(timestamp)
command.append(file_path)
if call(command) != 0:
print >> stderr, 'An error occured in osmupdate.'
# Sleep
print 'Sleeping for %s seconds.' % default['TIME']
2015-07-30 13:14:47 +00:00
sleep(float(default['TIME']))