bpo-32713: Fix tarfile.itn for large/negative float values. (GH-5434)
[python-delta-tar] / filesplit.py
index 00060ec..1be0152 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 # Copyright (C) 2013 Intra2net AG
 #
@@ -28,19 +28,19 @@ def split_file(separator, prefix, input_file, new_file_func=None):
     '''
     i = 0
     pos = 0
-    buf = ""
+    buf = b""
     sep_len = len(separator)
     if sep_len == 0:
         raise Exception("empty separator")
 
     if new_file_func is None:
-        new_file_func = lambda prefix, i: open(prefix + str(i), 'w')
+        new_file_func = lambda prefix, i: open(prefix + str(i), 'wb')
 
     output = new_file_func(prefix, i)
 
     # buffered search. we try not to have the while input file in memory, as
     # it's not needed
-    with open(input_file, 'r') as f:
+    with open(input_file, 'rb') as f:
         while True:
             buf += f.read(BUFSIZE)
             if len(buf) == 0:
@@ -71,18 +71,33 @@ def split_file(separator, prefix, input_file, new_file_func=None):
 
             # else: continue writing to the current output and iterate
             output.write(buf)
-            buf = ""
+            buf = b""
 
     output.close()
 
+def chunk_file(input_file, output_file, from_pos, to_pos):
+    ifl = open(input_file, 'rb')
+    ofl = open(output_file, 'wb')
+
+    ifl.seek(from_pos)
+    ofl.write(ifl.read(to_pos-from_pos))
+    ifl.close()
+    ofl.close()
+
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
 
-    parser.add_argument("-s", "--separator", required=True,
-                        help="string for the separator")
-    parser.add_argument("-p", "--prefix", required=True,
-                        help="prefix for split files")
+    parser.add_argument("-s", "--separator", help="string for the separator")
+    parser.add_argument("-p", "--prefix", help="prefix for split files")
     parser.add_argument("input_file", help="input file")
+    parser.add_argument("-f", "--from-pos", type=int, default=-1)
+    parser.add_argument("-t", "--to-pos", type=int, default=-1)
+    parser.add_argument("-o", "--output", default=None)
 
     args = parser.parse_args()
-    split_file(separator=args.separator, prefix=args.prefix, input_file=args.input_file)
+    if args.from_pos > -1 and args.to_pos > -1:
+        chunk_file(input_file=args.input_file, output_file=args.output,
+                   from_pos=args.from_pos, to_pos=args.to_pos)
+    else:
+        split_file(separator=args.separator.encode('UTF-8', errors="surrogateescape"),
+                   prefix=args.prefix, input_file=args.input_file)