source: subversion/applications/utils/import/bulkupload/change2diff.py @ 34714

Last change on this file since 34714 was 19989, checked in by balrog-kun, 10 years ago

Set the executable bits on the executables. (this directory still needs a clean-up)

  • Property svn:executable set to *
File size: 4.0 KB
Line 
1#! /usr/bin/python
2# vim: fileencoding=utf-8 encoding=utf-8 et sw=4
3
4# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20"""
21Generate a .diff.xml file (the response from the server after a diff upload)
22from an uploaded changeset file (downloadable through
23http://www.openstreetmap.org/api/0.6/changeset/<id>/download) -- this is
24useful if the network connection broke after uploading the changeset but
25before receiving the server response.
26"""
27
28__version__ = "$Revision: 21 $"
29
30import os
31import sys
32import traceback
33import codecs
34import locale
35import subprocess
36
37import httplib
38
39import xml.etree.cElementTree as ElementTree
40
41import locale, codecs
42locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
43encoding = locale.getlocale()[1]
44sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors = "replace")
45sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors = "replace")
46
47try:
48    this_dir = os.path.dirname(__file__)
49    version = subprocess.Popen(["svnversion", this_dir], stdout = subprocess.PIPE).communicate()[0].strip()
50    if len(sys.argv) != 2:
51        print >>sys.stderr, u"Synopsis:"
52        print >>sys.stderr, u"    %s <file_name>"
53        sys.exit(1)
54
55    filename = sys.argv[1]
56    if not os.path.exists(filename):
57        print >>sys.stderr, u"File %r doesn't exist!" % (filename,)
58        sys.exit(1)
59    if filename.endswith(".osc"):
60        filename_base = filename[:-4]
61    else:
62        filename_base = filename
63
64    tree = ElementTree.parse(filename)
65    root = tree.getroot()
66    if root.tag != "osmChange" or (root.attrib.get("version") != "0.3" and
67            root.attrib.get("version") != "0.6"):
68        print >>sys.stderr, u"File %s is not a v0.3 osmChange file!" % (filename,)
69        sys.exit(1)
70
71    diff_attr = {"version": "0.6", "generator": root.attrib.get("generator")}
72    diff_root = ElementTree.Element("diffResult", diff_attr)
73    diff_tree = ElementTree.ElementTree(diff_root)
74
75    # Note this is broken, it assumes the nodes in the resulting osmChange
76    # are in the same order they were in the osmChange sent to the server
77    # and that the negative IDs there started at -1 and were increasing by
78    # -1 with each new element.
79    # A better idea (but still wrong) would be to parse the input osmChange
80    # xml at the same time and assume that the elements in input and output
81    # come in the same order, possibly with additional checks (lat/lon..)
82    old_id = -1
83    for operation in root:
84        for element in operation:
85            attr = {}
86            if operation.tag == "create":
87                attr["old_id"] = str(old_id)
88                attr["new_id"] = element.attrib.get("id")
89                attr["new_version"] = element.attrib.get("version")
90                old_id -= 1
91            elif operation.tag == "modify":
92                attr["old_id"] = element.attrib.get("id")
93                attr["new_id"] = element.attrib.get("id")
94                attr["new_version"] = element.attrib.get("version")
95            elif operation.tag == "delete":
96                attr["old_id"] = element.attrib.get("id")
97            else:
98                print "unknown operation", operation.tag
99                sys.exit(-1)
100            diff = ElementTree.SubElement(diff_root, element.tag, attr)
101
102    diff_tree.write(filename_base + ".diff.xml", "utf-8")
103
104except Exception,err:
105    print >>sys.stderr, repr(err)
106    traceback.print_exc(file=sys.stderr)
107    sys.exit(1)
Note: See TracBrowser for help on using the repository browser.