]> git.mxchange.org Git - flightgear.git/blob - utils/TerraSync/terrasync.py
8f01be5aaebd151e2516fa24d40201ce49e11479
[flightgear.git] / utils / TerraSync / terrasync.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2016  Torsten Dreyer
5 #
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.
10 #
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.
15 #
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.
19 #
20 # terrasync.py - synchronize terrascenery data to your local disk
21 #
22 # this is a terrible stub, please improve
23
24 import os 
25 import hashlib
26 import urllib.request
27
28 dirindex = ".dirindex"
29 DIRINDEXVERSION = 1
30
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"
38
39 ########################################################################
40
41 def fn_hash_of_file(fname):
42     hash = hashlib.sha1()
43     try:
44         with open(fname, "rb") as f:
45             for chunk in iter(lambda: f.read(4096), b""):
46                 hash.update(chunk)
47     except:
48         pass
49
50     return hash.hexdigest()
51
52 ########################################################################
53 def do_download_file( _url, _path, _localfile, _hash ):
54   if os.path.exists( _localfile ):
55     h = fn_hash_of_file(_localfile)
56     if h == _hash:
57       print("hash match for ", _localfile)
58       return
59
60   r = urllib.request.urlopen( _url + _path )
61   f = open(_localfile, 'wb')
62   f.write( r.read() )
63   f.close()
64   print("downloaded ", _localfile, " from ", _url + _path )
65
66 ########################################################################
67 def do_terrasync( _url, _path, _localdir ):
68   url = _url + _path
69   print("syncing ",url)
70
71   if not os.path.exists( _localdir ):
72     os.makedirs( _localdir )
73
74   for line in urllib.request.urlopen(url + "/.dirindex").readlines():
75     tokens = line.decode("utf-8").rstrip().split(':')
76     if( len(tokens) == 0 ):
77       continue
78
79     # TODO: check version number, should be equal to DIRINDEXVERSION
80     #       otherwise complain and terminate
81     if( tokens[0] == "version" ):
82       continue
83
84     if( tokens[0] == "path" ):
85       continue
86
87     if( tokens[0] == "d" ):
88       do_terrasync( url,  "/" + tokens[1], os.path.join(_localdir,tokens[1] ) )
89
90     if( tokens[0] == "f" ):
91       do_download_file( url, "/" + tokens[1], os.path.join(_localdir,tokens[1]), tokens[2] )
92
93 ########################################################################
94
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
101
102 do_terrasync( URL, "", "." )
103
104 ########################################################################