<s> from optparse import make_option <EOL> import datetime , os , sys <EOL> import urllib , urllib2 , urlparse <EOL> import re <EOL> from django . db import connection , models <EOL> from django . core . management import call_command , sql , color <EOL> from django . core . management . base import NoArgsCommand <EOL> from django . conf import settings <EOL> from compress_geonames import GEONAMES_DATA <EOL> GEONAMES_DUMPS_URL = '<STR_LIT>' <EOL> def download ( url , filepath = False ) : <EOL> """<STR_LIT>""" <EOL> filepath = filepath or url . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> web_file = urllib . urlopen ( url ) <EOL> dir = '<STR_LIT:/>' . join ( filepath . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] ) <EOL> if not os . path . isdir ( dir ) : <EOL> os . makedirs ( dir ) <EOL> web_file_size = web_file . info ( ) . getheaders ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if os . path . isfile ( filepath ) : <EOL> existing_file = open ( filepath , "<STR_LIT:r>" ) <EOL> existing_file_size = len ( existing_file . read ( ) ) <EOL> print existing_file_size , web_file_size <EOL> if existing_file_size == web_file_size : <EOL> existing_file_size . close ( ) <EOL> return <EOL> local_file = open ( filepath , '<STR_LIT:w>' ) <EOL> local_file . write ( web_file . read ( ) ) <EOL> web_file . close ( ) <EOL> local_file . close ( ) <EOL> class Command ( NoArgsCommand ) : <EOL> help = '<STR_LIT>' <EOL> option_list = NoArgsCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT:time>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle_noargs ( self , ** options ) : <EOL> if options [ '<STR_LIT:time>' ] : <EOL> start_time = datetime . datetime . now ( ) <EOL> response = urllib2 . urlopen ( urllib2 . Request ( url = GEONAMES_DUMPS_URL ) ) <EOL> files = re . findall ( r'<STR_LIT>' , response . read ( ) ) <EOL> for file in files : <EOL> if options [ '<STR_LIT>' ] and file != '<STR_LIT>' % options [ '<STR_LIT>' ] or options [ '<STR_LIT>' ] and file == '<STR_LIT>' or options [ '<STR_LIT>' ] and file == '<STR_LIT>' or not options [ '<STR_LIT>' ] and len ( file ) == <NUM_LIT:6> : <EOL> continue <EOL> print '<STR_LIT>' % file <EOL> download ( urlparse . urljoin ( GEONAMES_DUMPS_URL , file ) , os . path . join ( GEONAMES_DATA , file ) ) <EOL> if options [ '<STR_LIT:time>' ] : <EOL> print '<STR_LIT>' % ( datetime . datetime . now ( ) - start_time ) </s>
