Skip to content

Commit ee9b625

Browse files
committed
Update pyfoxfile.py
1 parent 4a4c26a commit ee9b625

1 file changed

Lines changed: 43 additions & 78 deletions

File tree

pyfoxfile.py

Lines changed: 43 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@
115115
bytes_type = bytes
116116
text_type = str
117117

118+
# Text streams (as provided by Python)
119+
PY_STDIN_TEXT = sys.stdin
120+
PY_STDOUT_TEXT = sys.stdout
121+
PY_STDERR_TEXT = sys.stderr
122+
123+
# Binary-friendly streams (use .buffer on Py3, fall back on Py2)
124+
PY_STDIN_BUF = getattr(sys.stdin, "buffer", sys.stdin)
125+
PY_STDOUT_BUF = getattr(sys.stdout, "buffer", sys.stdout)
126+
PY_STDERR_BUF = getattr(sys.stderr, "buffer", sys.stderr)
127+
118128
# Text vs bytes tuples you can use with isinstance()
119129
TEXT_TYPES = (basestring,) # "str or unicode" on Py2, "str" on Py3
120130
BINARY_TYPES = (bytes,) if not PY2 else (str,) # bytes on Py3, str on Py2
@@ -2389,7 +2399,7 @@ def GetTotalSize(file_list):
23892399
try:
23902400
total_size += os.path.getsize(item)
23912401
except OSError:
2392-
sys.stderr.write("Error accessing file {}: {}\n".format(item, e))
2402+
PY_STDERR_TEXT.write("Error accessing file {}: {}\n".format(item, e))
23932403
return total_size
23942404

23952405

@@ -5524,10 +5534,7 @@ def ReadInFileWithContentToArray(infile, fmttype="auto", filestart=0, seekstart=
55245534
currentfilepos = fp.tell()
55255535
elif(infile == "-"):
55265536
fp = MkTempFile()
5527-
if(hasattr(sys.stdin, "buffer")):
5528-
shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5529-
else:
5530-
shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5537+
shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
55315538
try:
55325539
fp.seek(0, 2)
55335540
except OSError:
@@ -5678,10 +5685,7 @@ def ReadInFileWithContentToList(infile, fmttype="auto", filestart=0, seekstart=0
56785685
currentfilepos = fp.tell()
56795686
elif(infile == "-"):
56805687
fp = MkTempFile()
5681-
if(hasattr(sys.stdin, "buffer")):
5682-
shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5683-
else:
5684-
shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5688+
shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
56855689
try:
56865690
fp.seek(0, 2)
56875691
except OSError:
@@ -6055,10 +6059,7 @@ def MakeEmptyFile(outfile, fmttype="auto", compression="auto", compresswholefile
60556059
pass
60566060
if(outfile == "-"):
60576061
fp.seek(0, 0)
6058-
if(hasattr(sys.stdout, "buffer")):
6059-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
6060-
else:
6061-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
6062+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
60626063
elif(outfile is None):
60636064
fp.seek(0, 0)
60646065
outvar = fp.read()
@@ -6176,10 +6177,10 @@ def AppendFilesWithContent(infiles, fp, dirlistfromtxt=False, extradata=[], json
61766177
altinode = formatspecs['use_alt_inode']
61776178
if(verbose):
61786179
logging.basicConfig(format="%(message)s",
6179-
stream=sys.stdout, level=logging.DEBUG)
6180+
stream=PY_STDOUT_TEXT, level=logging.DEBUG)
61806181
infilelist = []
61816182
if(infiles == "-"):
6182-
for line in sys.stdin:
6183+
for line in PY_STDIN_TEXT:
61836184
infilelist.append(line.strip())
61846185
infilelist = list(filter(None, infilelist))
61856186
elif(infiles != "-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles == os.devnull)):
@@ -6497,7 +6498,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
64976498
return False
64986499
if(verbose):
64996500
logging.basicConfig(format="%(message)s",
6500-
stream=sys.stdout, level=logging.DEBUG)
6501+
stream=PY_STDOUT_TEXT, level=logging.DEBUG)
65016502
curinode = 0
65026503
curfid = 0
65036504
inodelist = []
@@ -6506,10 +6507,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
65066507
inodetoforminode = {}
65076508
if(infile == "-"):
65086509
infile = MkTempFile()
6509-
if(hasattr(sys.stdin, "buffer")):
6510-
shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6511-
else:
6512-
shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6510+
shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
65136511
infile.seek(0, 0)
65146512
if(not infile):
65156513
return False
@@ -6731,7 +6729,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67316729
return False
67326730
if(verbose):
67336731
logging.basicConfig(format="%(message)s",
6734-
stream=sys.stdout, level=logging.DEBUG)
6732+
stream=PY_STDOUT_TEXT, level=logging.DEBUG)
67356733
curinode = 0
67366734
curfid = 0
67376735
inodelist = []
@@ -6740,10 +6738,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67406738
inodetoforminode = {}
67416739
if(infile == "-"):
67426740
infile = MkTempFile()
6743-
if(hasattr(sys.stdin, "buffer")):
6744-
shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6745-
else:
6746-
shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6741+
shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
67476742
infile.seek(0, 0)
67486743
if(not infile):
67496744
return False
@@ -6969,7 +6964,7 @@ def AppendFilesWithContentFromRarFile(infile, fp, extradata=[], jsondata={}, com
69696964
return False
69706965
if(verbose):
69716966
logging.basicConfig(format="%(message)s",
6972-
stream=sys.stdout, level=logging.DEBUG)
6967+
stream=PY_STDOUT_TEXT, level=logging.DEBUG)
69736968
curinode = 0
69746969
curfid = 0
69756970
inodelist = []
@@ -7223,7 +7218,7 @@ def AppendFilesWithContentFromSevenZipFile(infile, fp, extradata=[], jsondata={}
72237218
return False
72247219
if(verbose):
72257220
logging.basicConfig(format="%(message)s",
7226-
stream=sys.stdout, level=logging.DEBUG)
7221+
stream=PY_STDOUT_TEXT, level=logging.DEBUG)
72277222
formver = formatspecs['format_ver']
72287223
fileheaderver = str(int(formver.replace(".", "")))
72297224
curinode = 0
@@ -7407,7 +7402,7 @@ def AppendListsWithContent(inlist, fp, dirlistfromtxt=False, extradata=[], jsond
74077402
if(not hasattr(fp, "write")):
74087403
return False
74097404
if(verbose):
7410-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
7405+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
74117406
GetDirList = inlist
74127407
if(not GetDirList):
74137408
return False
@@ -7531,10 +7526,7 @@ def AppendFilesWithContentToOutFile(infiles, outfile, dirlistfromtxt=False, fmtt
75317526
pass
75327527
if(outfile == "-"):
75337528
fp.seek(0, 0)
7534-
if(hasattr(sys.stdout, "buffer")):
7535-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7536-
else:
7537-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7529+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
75387530
elif(outfile is None):
75397531
fp.seek(0, 0)
75407532
outvar = fp.read()
@@ -7624,10 +7616,7 @@ def AppendListsWithContentToOutFile(inlist, outfile, dirlistfromtxt=False, fmtty
76247616
pass
76257617
if(outfile == "-"):
76267618
fp.seek(0, 0)
7627-
if(hasattr(sys.stdout, "buffer")):
7628-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7629-
else:
7630-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7619+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
76317620
elif(outfile is None):
76327621
fp.seek(0, 0)
76337622
outvar = fp.read()
@@ -7704,10 +7693,7 @@ def AppendFilesWithContentFromTarFileToOutFile(infiles, outfile, fmttype="auto",
77047693
pass
77057694
if(outfile == "-"):
77067695
fp.seek(0, 0)
7707-
if(hasattr(sys.stdout, "buffer")):
7708-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7709-
else:
7710-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7696+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
77117697
elif(outfile is None):
77127698
fp.seek(0, 0)
77137699
outvar = fp.read()
@@ -7799,10 +7785,7 @@ def AppendFilesWithContentFromZipFileToOutFile(infiles, outfile, fmttype="auto",
77997785
pass
78007786
if(outfile == "-"):
78017787
fp.seek(0, 0)
7802-
if(hasattr(sys.stdout, "buffer")):
7803-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7804-
else:
7805-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7788+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
78067789
elif(outfile is None):
78077790
fp.seek(0, 0)
78087791
outvar = fp.read()
@@ -7899,10 +7882,7 @@ def AppendFilesWithContentFromRarFileToOutFile(infiles, outfile, fmttype="auto",
78997882
pass
79007883
if(outfile == "-"):
79017884
fp.seek(0, 0)
7902-
if(hasattr(sys.stdout, "buffer")):
7903-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7904-
else:
7905-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7885+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
79067886
elif(outfile is None):
79077887
fp.seek(0, 0)
79087888
outvar = fp.read()
@@ -7999,10 +7979,7 @@ def AppendFilesWithContentFromSevenZipFileToOutFile(infiles, outfile, fmttype="a
79997979
pass
80007980
if(outfile == "-"):
80017981
fp.seek(0, 0)
8002-
if(hasattr(sys.stdout, "buffer")):
8003-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
8004-
else:
8005-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7982+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
80067983
elif(outfile is None):
80077984
fp.seek(0, 0)
80087985
outvar = fp.read()
@@ -9844,7 +9821,7 @@ def PackFoxFileFromInFile(infile, outfile, fmttype="auto", compression="auto", c
98449821
if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
98459822
formatspecs = formatspecs[checkcompressfile]
98469823
if(verbose):
9847-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
9824+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
98489825
if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
98499826
return PackFoxFileFromTarFile(infile, outfile, fmttype, compression, compresswholefile, compressionlevel, compressionuselist, checksumtype, extradata, jsondata, formatspecs, verbose, returnfp)
98509827
elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
@@ -9927,7 +9904,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
99279904
formatspecs=__file_format_multi_dict__, # keep default like original
99289905
seektoend=False, verbose=False, returnfp=False):
99299906
if(verbose):
9930-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
9907+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
99319908

99329909
if(IsNestedDict(formatspecs) and fmttype!="auto" and fmttype in formatspecs):
99339910
formatspecs = formatspecs[fmttype]
@@ -9954,10 +9931,7 @@ def FoxFileValidate(infile, fmttype="auto", filestart=0,
99549931

99559932
elif(infile == "-"):
99569933
fp = MkTempFile()
9957-
if(hasattr(sys.stdin, "buffer")):
9958-
shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
9959-
else:
9960-
shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
9934+
shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
99619935
fp.seek(filestart, 0)
99629936
fp = UncompressFileAlt(fp, formatspecs, filestart)
99639937
checkcompressfile = CheckCompressionSubType(fp, formatspecs, filestart, True)
@@ -10677,7 +10651,7 @@ def RePackFoxFile(infile, outfile, fmttype="auto", compression="auto", compressw
1067710651
compression = "auto"
1067810652

1067910653
if verbose:
10680-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
10654+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1068110655

1068210656
# No files?
1068310657
if not listarrayfiles.get('ffilelist'):
@@ -10911,10 +10885,7 @@ def RePackFoxFile(infile, outfile, fmttype="auto", compression="auto", compressw
1091110885

1091210886
if outfile == "-":
1091310887
fp.seek(0, 0)
10914-
if hasattr(sys.stdout, "buffer"):
10915-
shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
10916-
else:
10917-
shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
10888+
shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
1091810889
elif outfile is None:
1091910890
fp.seek(0, 0)
1092010891
outvar = fp.read()
@@ -10973,7 +10944,7 @@ def UnPackFoxFile(infile, outdir=None, followlink=False, filestart=0, seekstart=
1097310944
if(outdir is not None):
1097410945
outdir = RemoveWindowsPath(outdir)
1097510946
if(verbose):
10976-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
10947+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1097710948
if(isinstance(infile, dict)):
1097810949
listarrayfiles = infile
1097910950
else:
@@ -11249,7 +11220,7 @@ def ftype_to_str(ftype):
1124911220

1125011221
def FoxFileListFiles(infile, fmttype="auto", filestart=0, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_multi_dict__, seektoend=False, verbose=False, newstyle=False, returnfp=False):
1125111222
if(verbose):
11252-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11223+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1125311224
if(isinstance(infile, dict)):
1125411225
listarrayfileslist = [infile]
1125511226
if(isinstance(infile, list)):
@@ -11362,13 +11333,10 @@ def FoxFileStringListFiles(instr, filestart=0, seekstart=0, seekend=0, skipcheck
1136211333

1136311334
def TarFileListFiles(infile, verbose=False, returnfp=False):
1136411335
if(verbose):
11365-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11336+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1136611337
if(infile == "-"):
1136711338
infile = MkTempFile()
11368-
if(hasattr(sys.stdin, "buffer")):
11369-
shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11370-
else:
11371-
shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11339+
shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
1137211340
infile.seek(0, 0)
1137311341
if(not infile):
1137411342
return False
@@ -11487,13 +11455,10 @@ def TarFileListFiles(infile, verbose=False, returnfp=False):
1148711455

1148811456
def ZipFileListFiles(infile, verbose=False, returnfp=False):
1148911457
if(verbose):
11490-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11458+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1149111459
if(infile == "-"):
1149211460
infile = MkTempFile()
11493-
if(hasattr(sys.stdin, "buffer")):
11494-
shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
11495-
else:
11496-
shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
11461+
shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
1149711462
infile.seek(0, 0)
1149811463
if(not infile):
1149911464
return False
@@ -11625,7 +11590,7 @@ def RarFileListFiles(infile, verbose=False, returnfp=False):
1162511590
if(rarfile_support):
1162611591
def RarFileListFiles(infile, verbose=False, returnfp=False):
1162711592
if(verbose):
11628-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11593+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1162911594
if(not os.path.exists(infile) or not os.path.isfile(infile)):
1163011595
return False
1163111596
if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
@@ -11762,7 +11727,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1176211727
if(py7zr_support):
1176311728
def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1176411729
if(verbose):
11765-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11730+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1176611731
if(not os.path.exists(infile) or not os.path.isfile(infile)):
1176711732
return False
1176811733
lcfi = 0
@@ -11865,7 +11830,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1186511830

1186611831
def InFileListFiles(infile, verbose=False, formatspecs=__file_format_multi_dict__, seektoend=False, newstyle=False, returnfp=False):
1186711832
if(verbose):
11868-
logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG)
11833+
logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT, level=logging.DEBUG)
1186911834
checkcompressfile = CheckCompressionSubType(infile, formatspecs, filestart, True)
1187011835
if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
1187111836
formatspecs = formatspecs[checkcompressfile]

0 commit comments

Comments
 (0)