Ticket #415: ruby-to-python.patch

File ruby-to-python.patch, 3.8 KB (added by roozbeh@…, 12 years ago)

Patch to remove the requirement on ruby to connect to thedatabase

  • render_from_list.py

     
    5858load_map(m,mapfile)
    5959prj = Projection("+proj=merc +datum=WGS84")
    6060
    61 def dotile(x,y,z):
     61def dotile(x,y,z,insert_tile_func):
    6262  x_str = "%s" % x
    6363  y_str = "%s" % y
    6464  z_str = "%s" % z
     
    9292  p = Popen(command, shell=True)
    9393  sts = os.waitpid(p.pid, 0)
    9494
    95   command = "/home/steve/insert_tile.rb %s %s %s %s" % (x_str, y_str, z_str, tile_uri)
    96   p = Popen(command, shell=True)
    97   sts = os.waitpid(p.pid, 0)
    98 
    99 for line in fileinput.input():
    100   tile_data = line.rstrip('\n').split(' ')
    101 #  if(eval(tile_data[0]) > 11):
    102   dotile(eval(tile_data[0]), eval(tile_data[1]), eval(tile_data[2]))
    103 
    104 
     95  insert_tile_func(x_str, y_str, z_str, tile_uri)
  • render_some_tiles.py

     
     1#!/usr/bin/python
     2import sys
     3import os
     4
     5import MySQLdb
     6
     7from render_from_list import dotile
     8
     9# Acquries a lock file to make sure no other version
     10# of the same script is running simultaneously
     11def acquire_lock(lockfilename):
     12  if os.path.isfile(lockfilename):
     13    #exit if the lockfile is there
     14    print >> sys.stderr, 'exiting, lock file present'
     15    sys.exit(1)
     16  else:
     17    #otherwise make one
     18    try:
     19      lockfile = open(lockfilename, 'w')
     20      print >> lockfile, 'eh-oh'
     21      lockfile.close()
     22    except IOError:
     23      print >> sys.stderr, 'can not open lock file for writing'
     24      sys.exit(1)
     25
     26
     27def release_lock(lockfilename):
     28  os.remove(lockfilename)
     29
     30# Gets a MySQL connection to localhost
     31def get_local_connection():
     32  try:
     33    return MySQLdb.connect(
     34      host  ='localhost',
     35      user  ='tile',
     36      passwd='tile',
     37      db    ='tile')
     38  except MySQLdb.MySQLError, e:
     39    print >> sys.stderr, e
     40    sys.exit(1)
     41
     42# do a SQL query and return the result as a list
     43def call_local_sql(sql):
     44  dbh = None
     45  print sql
     46  try:
     47    try:
     48      dbh = get_local_connection()
     49      cursor = dbh.cursor()
     50      cursor.execute(sql)
     51      res = cursor.fetchall()
     52      if res is None:
     53        return []
     54      else:
     55        return res
     56    except MySQLdb.MySQLError, ex:
     57      print >> sys.stderr, ex
     58  finally:
     59    if dbh is not None:
     60      dbh.commit()
     61      dbh.close()
     62
     63# Insert a tile from a file to the database
     64def insert_tile(x, y, z, filename):
     65  filesize = os.path.getsize(filename)
     66  print "tile %s,%s,%s size is %s" % (x, y, x, filesize)
     67
     68  if filesize == 158: # it's blank
     69    call_local_sql("delete from tiles where x=%s and y=%s and z=%s;" % (x, y, z))
     70  else:
     71    print 'inserting tile'
     72 
     73    tilefile = open(filename, "r")
     74    data = MySQLdb.escape_string(tilefile.read())
     75    call_local_sql("update tiles set data='%s', created_at=NOW(), dirty_t='false' where x=%s and y=%s and z=%s;" \
     76                    % (data, x, y, z))
     77    os.system('ls -l %s' % filename)
     78
     79  os.remove(filename)
     80
     81# Test version of dotile for debugging purposes.
     82# Uncomment this and comment the import from render_from_list
     83# for debugging mode.
     84#def dotile(x, y, z, insert_tile_func):
     85#  x = str(x); y = str(y); z = str(z)
     86#  filename = '%s-%s-%s.png' % (x, y, z)
     87#  os.system('echo \\"%s %s %s > %s' % (x, y, z, filename))
     88#  insert_tile_func(x, y, z, filename)
     89
     90
     91lockfilename = '/tmp/tilegen_running'
     92acquire_lock(lockfilename)
     93
     94res = call_local_sql("select x,y,z from tiles where dirty_t='true' limit 10000;")
     95for x, y, z in res:
     96  dotile(x, y, z, insert_tile)
     97
     98release_lock(lockfilename)