Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:elfring:splitting
python-split0
split0.py
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File split0.py of Package python-split0
#!/usr/bin/python # Copyright (C) 2015 Markus Elfring # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License (version 2.1) # as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, see <http://www.gnu.org/licenses>. import sys def split(number, dir): """Try to distribute input data over some output files.""" import io input = io.FileIO(sys.stdin.fileno()).readall() if number == 1: import os with open("/".join([os.path.abspath(dir), "x1.part"]), "wb") as output: output.write(input) else: if input == "": sys.exit("The standard input was empty.") else: import os abs = os.path.abspath(dir) name = ["x", "", ".part"] delimiter = b'\0' parts = input.split(delimiter) count = len(parts) if count < number: map = {} for key in range(0, number): map[key] = [] key = 0 for part in parts: map[key].append(part) key += 1 if key == number: key = 0 for item in map.items(): name[1] = str(item[0]) with open("/".join([abs, ''.join(name)]), "wb") as output: if item[1]: output.write(delimiter.join(item[1])) else: output.write(b"") else: dm = divmod(count, number) portion = dm[0] end = portion + dm[1] # remainder handling with open("/".join([abs, "x0.part"]), "wb") as output: output.write(delimiter.join(parts[0:end])) for x in range(1, number): start = end end += portion name[1] = str(x) with open("/".join([abs, ''.join(name)]), "wb") as output: output.write(delimiter.join(parts[start:end])) def main(): if sys.__stdin__ == None: sys.exit("The standard input channel was not usable.") else: import getopt try: options, args = getopt.gnu_getopt(sys.argv[1:], "g:o:", ["groups=", "output-dir="]) except getopt.GetoptError as ex: sys.exit(ex) od = "" g = 2 for parameter, value in options: if parameter in ["-g", "--groups"]: g = int(value) if g < 1: sys.exit("invalid parameter value") elif parameter in ["-o", "--output-dir"]: if value: od = value else: sys.exit("invalid specification for an output directory") else: sys.exit(": ".join(["unhandled option", parameter])) split(g, od) if __name__ == "__main__": main()
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor