]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Split/Split.py
e223a72a94e14dcedd61b1ff925226d79930f304
2 # Split a file into two pieces at the request offset.
4 # Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
20 parser
= argparse
.ArgumentParser(description
='''
21 SplitFile creates two Binary files either in the same directory as the current working directory or in the specified directory.
23 parser
.add_argument("-f", "--filename", dest
="inputfile",
24 required
=True, help="The input file to split tool.")
25 parser
.add_argument("-s", "--split", dest
="position",
26 required
=True, help="The number of bytes in the first file. The valid format are HEX, Decimal and Decimal[KMG].")
27 parser
.add_argument("-p", "--prefix", dest
="output",
28 help="The output folder.")
29 parser
.add_argument("-o", "--firstfile", help="The first file name")
30 parser
.add_argument("-t", "--secondfile", help="The second file name")
31 parser
.add_argument("--version", action
="version", version
='%(prog)s Version 2.0',
32 help="Print debug information.")
34 group
= parser
.add_mutually_exclusive_group()
35 group
.add_argument("-v", "--verbose", action
="store_true",
36 help="Print debug information.")
37 group
.add_argument("-q", "--quiet", action
="store_true",
38 help="Disable all messages except fatal errors")
47 def GetPositionValue(position
):
49 Parse the string of the argument position and return a decimal number.
50 The valid position formats are
56 e.g. 100K or 100M or 100G or 100k or 100m or 100g
58 logger
= logging
.getLogger('Split')
60 header
= position
[:2].upper()
61 tailer
= position
[-1].upper()
64 if tailer
in SizeDict
:
65 PosVal
= int(position
[:-1]) * SizeDict
[tailer
]
68 PosVal
= int(position
, 16)
70 PosVal
= int(position
)
71 except Exception as e
:
73 "The parameter %s format is incorrect. The valid format is HEX, Decimal and Decimal[KMG]." % position
)
79 def getFileSize(filename
):
81 Read the input file and return the file size.
83 logger
= logging
.getLogger('Split')
86 with
open(filename
, "rb") as fin
:
87 fin
.seek(0, io
.SEEK_END
)
89 except Exception as e
:
90 logger
.error("Access file failed: %s", filename
)
95 def getoutputfileabs(inputfile
, prefix
, outputfile
,index
):
96 inputfile
= os
.path
.abspath(inputfile
)
97 if outputfile
is None:
99 outputfileabs
= os
.path
.join(os
.path
.dirname(inputfile
), "{}{}".format(os
.path
.basename(inputfile
),index
))
101 if os
.path
.isabs(prefix
):
102 outputfileabs
= os
.path
.join(prefix
, "{}{}".format(os
.path
.basename(inputfile
),index
))
104 outputfileabs
= os
.path
.join(os
.getcwd(), prefix
, "{}{}".format(os
.path
.basename(inputfile
),index
))
105 elif not os
.path
.isabs(outputfile
):
107 outputfileabs
= os
.path
.join(os
.getcwd(), outputfile
)
109 if os
.path
.isabs(prefix
):
110 outputfileabs
= os
.path
.join(prefix
, outputfile
)
112 outputfileabs
= os
.path
.join(os
.getcwd(), prefix
, outputfile
)
114 outputfileabs
= outputfile
117 def splitFile(inputfile
, position
, outputdir
=None, outputfile1
=None, outputfile2
=None):
119 Split the inputfile into outputfile1 and outputfile2 from the position.
121 logger
= logging
.getLogger('Split')
123 if not os
.path
.exists(inputfile
):
124 logger
.error("File Not Found: %s" % inputfile
)
127 if outputfile1
and outputfile2
and outputfile1
== outputfile2
:
129 "The firstfile and the secondfile can't be the same: %s" % outputfile1
)
132 # Create dir for the output files
135 outputfile1
= getoutputfileabs(inputfile
, outputdir
, outputfile1
,1)
136 outputfolder
= os
.path
.dirname(outputfile1
)
137 if not os
.path
.exists(outputfolder
):
138 os
.makedirs(outputfolder
)
140 outputfile2
= getoutputfileabs(inputfile
, outputdir
, outputfile2
,2)
141 outputfolder
= os
.path
.dirname(outputfile2
)
142 if not os
.path
.exists(outputfolder
):
143 os
.makedirs(outputfolder
)
145 except Exception as e
:
146 logger
.error("Can't make dir: %s" % outputfolder
)
150 if outputfile2
!= os
.path
.abspath(inputfile
):
151 shutil
.copy2(os
.path
.abspath(inputfile
), outputfile2
)
152 with
open(outputfile1
, "wb") as fout
:
155 inputfilesize
= getFileSize(inputfile
)
156 if position
>= inputfilesize
:
157 if outputfile1
!= os
.path
.abspath(inputfile
):
158 shutil
.copy2(os
.path
.abspath(inputfile
), outputfile1
)
159 with
open(outputfile2
, "wb") as fout
:
163 tempdir
= tempfile
.mkdtemp()
164 tempfile1
= os
.path
.join(tempdir
, "file1.bin")
165 tempfile2
= os
.path
.join(tempdir
, "file2.bin")
166 with
open(inputfile
, "rb") as fin
:
167 content1
= fin
.read(position
)
168 with
open(tempfile1
, "wb") as fout1
:
169 fout1
.write(content1
)
171 content2
= fin
.read(inputfilesize
- position
)
172 with
open(tempfile2
, "wb") as fout2
:
173 fout2
.write(content2
)
174 shutil
.copy2(tempfile1
, outputfile1
)
175 shutil
.copy2(tempfile2
, outputfile2
)
176 except Exception as e
:
177 logger
.error("Split file failed")
180 if os
.path
.exists(tempdir
):
181 shutil
.rmtree(tempdir
)
185 args
= parser
.parse_args()
188 logger
= logging
.getLogger('Split')
190 logger
.setLevel(logging
.CRITICAL
)
192 logger
.setLevel(logging
.DEBUG
)
194 lh
= logging
.StreamHandler(sys
.stdout
)
195 lf
= logging
.Formatter("%(levelname)-8s: %(message)s")
197 logger
.addHandler(lh
)
200 position
= GetPositionValue(args
.position
)
201 splitFile(args
.inputfile
, position
, args
.output
,
202 args
.firstfile
, args
.secondfile
)
203 except Exception as e
:
209 if __name__
== "__main__":