source: subversion/applications/utils/import/bulkupload/split.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.1 KB
Line 
1#! /usr/bin/python3
2# vim: fileencoding=utf-8 encoding=utf-8 et sw=4
3
4# Copyright (C) 2009 Jacek Konieczny <jajcus@jajcus.net>
5# Copyright (C) 2009 Andrzej Zaborowski <balrogg@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21"""
22Split large osmChange files.
23"""
24
25__version__ = "$Revision: 21 $"
26
27import os
28import sys
29import traceback
30import codecs
31import locale
32import subprocess
33
34import http
35
36import xml.etree.cElementTree as ElementTree
37
38#locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
39#encoding = locale.getlocale()[1]
40#sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors = "replace")
41#sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors = "replace")
42
43try:
44    this_dir = os.path.dirname(__file__)
45    version = subprocess.Popen(["svnversion", this_dir], stdout = subprocess.PIPE).communicate()[0].strip()
46    if len(sys.argv) not in (2, 3):
47        sys.stderr.write("Synopsis:\n")
48        sys.stderr.write("    %s <file_name> [<num_of_pieces>\n]" % (sys.argv[0],))
49        sys.exit(1)
50
51    filename = sys.argv[1]
52    if len(sys.argv) > 2:
53        num_parts = int(sys.argv[2])
54    else:
55        num_parts = 2
56    if not os.path.exists(filename):
57        sys.stderr.write("File %r doesn't exist!\n" % (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        sys.stderr.write("File %s is not a v0.3 osmChange file!\n" % (filename,))
69        sys.exit(1)
70
71    element_count = 0
72    for operation in root:
73        element_count += len(operation)
74
75    sys.stderr.write("Number of parts: %r\n" % (element_count,))
76    part_size = int((element_count + num_parts - 1) / num_parts)
77
78    part = 1
79    operation_iter = iter(root)
80    operation = next(operation_iter)
81    elements = list(operation)
82    while elements and operation:
83        filename = "%s-part%i.osc" % (filename_base, part)
84        part_root = ElementTree.Element(root.tag, root.attrib)
85        part_tree = ElementTree.ElementTree(part_root)
86        current_size = 0
87        while operation and current_size < part_size:
88            part_op = ElementTree.SubElement(part_root, operation.tag, operation.attrib)
89            this_part_elements = elements[:(part_size-current_size)]
90            elements = elements[(part_size-current_size):]
91            for element in this_part_elements:
92                part_op.append(element)
93                current_size += 1
94            if not elements:
95                try:
96                    while not elements:
97                        operation = next(operation_iter)
98                        elements = list(operation)
99                except StopIteration:
100                    operation = None
101                    elements = []
102        part_tree.write(filename, "utf-8")
103        part += 1
104    comment_fn = filename_base + ".comment"
105    if os.path.exists(comment_fn):
106        comment_file = codecs.open(comment_fn, "r", "utf-8")
107        comment = comment_file.read().strip()
108        comment_file.close()
109        for part in range(1, num_parts + 1):
110            comment_fn = "%s-part%i.comment" % (filename_base, part)
111            comment_file = codecs.open(comment_fn, "w", "utf-8")
112            comment_file.write("%s, part %i/%i" % (comment, part, num_parts))
113            comment_file.close()
114except Exception as err:
115    sys.stderr.write(repr(err))
116    traceback.print_exc(file=sys.stderr)
117    sys.exit(1)
Note: See TracBrowser for help on using the repository browser.