2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2016 Torsten Dreyer
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # terrasync.py - synchronize terrascenery data to your local disk
22 # this is a terrible stub, please improve
28 dirindex = ".dirindex"
31 #TODO: use DNS to resolve list of mirrors
32 # - lookup terrasync.flightgear.org, type=NAPTR, service="ws20", flags="U"
33 # - sort by order,preference ascending
34 # - pick entries with lowest order and preference
35 # - randomly pick one of those
36 # - use regexp fields URL
37 URL="http://flightgear.sourceforge.net/scenery"
39 ########################################################################
41 def fn_hash_of_file(fname):
44 with open(fname, "rb") as f:
45 for chunk in iter(lambda: f.read(4096), b""):
50 return hash.hexdigest()
52 ########################################################################
53 def do_download_file( _url, _path, _localfile, _hash ):
54 if os.path.exists( _localfile ):
55 h = fn_hash_of_file(_localfile)
57 print("hash match for ", _localfile)
60 r = urllib.request.urlopen( _url + _path )
61 f = open(_localfile, 'wb')
64 print("downloaded ", _localfile, " from ", _url + _path )
66 ########################################################################
67 def do_terrasync( _url, _path, _localdir ):
71 if not os.path.exists( _localdir ):
72 os.makedirs( _localdir )
74 for line in urllib.request.urlopen(url + "/.dirindex").readlines():
75 tokens = line.decode("utf-8").rstrip().split(':')
76 if( len(tokens) == 0 ):
79 # TODO: check version number, should be equal to DIRINDEXVERSION
80 # otherwise complain and terminate
81 if( tokens[0] == "version" ):
84 if( tokens[0] == "path" ):
87 if( tokens[0] == "d" ):
88 do_terrasync( url, "/" + tokens[1], os.path.join(_localdir,tokens[1] ) )
90 if( tokens[0] == "f" ):
91 do_download_file( url, "/" + tokens[1], os.path.join(_localdir,tokens[1]), tokens[2] )
93 ########################################################################
95 # TODO: parse command line args
96 # --url=automatic for automatic detection of server URL
97 # --url=http://flightgear.sourceforge.net/scenery to use explicit server
98 # --path=/Models sync only the /Models path from the server
99 # --destination=/some/path write files to destination instead of pwd
100 # TODO: sanitize user provided data
102 do_terrasync( URL, "", "." )
104 ########################################################################