Skip to content

Commit 873e544

Browse files
committed
fix(io): BREAKING, wrap more out-stream usages
+ chrore(deps): depend on *contextlib2* for `ExitStack` in PY2. + refact(util): BREAKING API move consts out of utils. + style(pep8): fixe all sources.
1 parent a566e11 commit 873e544

18 files changed

+196
-193
lines changed

Diff for: .travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ git:
1313
install:
1414
- pip install coveralls
1515
- pip install -I git+https://github.com/ankostis/[email protected]
16+
- pip install -r requirements.txt
1617
script:
1718
- ulimit -n 48
1819
- ulimit -n

Diff for: gitdb/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515

1616

1717
# default imports
18-
from gitdb.base import *
19-
from gitdb.db import *
20-
from gitdb.stream import *
18+
from gitdb.base import * # @IgnorePep8
19+
from gitdb.db import * # @IgnorePep8
20+
from gitdb.stream import * # @IgnorePep8

Diff for: gitdb/db/loose.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ def info(self, sha):
166166

167167
def stream(self, sha):
168168
m = self._map_loose_object(sha)
169-
type, size, stream = DecompressMemMapReader.new(m, close_on_deletion=True)
170-
return OStream(sha, type, size, stream)
169+
typ, size, stream = DecompressMemMapReader.new(m, close_on_deletion=True)
170+
return OStream(sha, typ, size, stream)
171171

172172
def has_object(self, sha):
173173
try:
@@ -247,7 +247,7 @@ def store(self, istream):
247247

248248
def sha_iter(self):
249249
# find all files which look like an object, extract sha from there
250-
for root, dirs, files in os.walk(self.root_path()):
250+
for root, dirs, files in os.walk(self.root_path()): # @UnusedVariable
251251
root_base = basename(root)
252252
if len(root_base) != 2:
253253
continue

Diff for: gitdb/pack.py

+48-45
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@
33
# This module is part of GitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55
"""Contains PackIndexFile and PackFile implementations"""
6+
import array
7+
from binascii import crc32
8+
import os
9+
from struct import pack
10+
import sys
11+
import tempfile
612
import zlib
713

14+
from gitdb.base import (
15+
OInfo,
16+
OStream,
17+
OPackInfo,
18+
OPackStream,
19+
ODeltaStream,
20+
ODeltaPackInfo,
21+
ODeltaPackStream,
22+
)
23+
from gitdb.const import NULL_BYTE
824
from gitdb.exc import (
925
BadObject,
1026
AmbiguousObjectName,
1127
UnsupportedOperation,
1228
ParseError
1329
)
14-
15-
from gitdb.util import (
16-
mman,
17-
LazyMixin,
18-
unpack_from,
19-
bin_to_hex,
20-
byte_ord,
21-
)
22-
2330
from gitdb.fun import (
2431
create_pack_object_header,
2532
pack_object_header_info,
@@ -33,46 +40,34 @@
3340
REF_DELTA,
3441
msb_size
3542
)
36-
37-
try:
38-
from gitdb_speedups._perf import PackIndexFile_sha_to_index
39-
except ImportError:
40-
pass
41-
# END try c module
42-
43-
from gitdb.base import (
44-
OInfo,
45-
OStream,
46-
OPackInfo,
47-
OPackStream,
48-
ODeltaStream,
49-
ODeltaPackInfo,
50-
ODeltaPackStream,
51-
)
52-
5343
from gitdb.stream import (
5444
DecompressMemMapReader,
5545
DeltaApplyReader,
5646
Sha1Writer,
5747
NullStream,
5848
FlexibleSha1Writer
5949
)
60-
61-
from struct import pack
62-
from binascii import crc32
63-
64-
from gitdb.const import NULL_BYTE
50+
from gitdb.util import (
51+
mman,
52+
LazyMixin,
53+
bin_to_hex,
54+
byte_ord,
55+
)
6556
from gitdb.utils.compat import (
6657
izip,
6758
buffer,
6859
xrange,
69-
to_bytes
60+
to_bytes,
61+
unpack_from,
7062
)
7163

72-
import tempfile
73-
import array
74-
import os
75-
import sys
64+
65+
try:
66+
from gitdb_speedups._perf import PackIndexFile_sha_to_index
67+
except ImportError:
68+
pass
69+
# END try c module
70+
7671

7772
__all__ = ('PackIndexFile', 'PackFile', 'PackEntity')
7873

@@ -290,8 +285,9 @@ def _make_cursor(self):
290285

291286
# We will assume that the index will always fully fit into memory !
292287
if mman.window_size() > 0 and cursor.file_size() > mman.window_size():
293-
raise AssertionError("The index file at %s is too large to fit into a mapped window (%i > %i). This is a limitation of the implementation" % (
294-
self._indexpath, cursor.file_size(), mman.window_size()))
288+
raise AssertionError("The index file at %s is too large to fit into a mapped window (%i > %i). "
289+
"This is a limitation of the hardware." % (
290+
self._indexpath, cursor.file_size(), mman.window_size()))
295291

296292
return cursor
297293

@@ -528,7 +524,7 @@ class PackFile(LazyMixin):
528524
'_size',
529525
'_version',
530526
'_entered',
531-
)
527+
)
532528
pack_signature = 0x5041434b # 'PACK'
533529
pack_version_default = 2
534530

@@ -603,7 +599,11 @@ def data(self):
603599
"""
604600
:return: read-only data of this pack. It provides random access and usually
605601
is a memory map.
606-
:note: This method is unsafe as it returns a window into a file which might be larger than than the actual window size"""
602+
603+
.. note::
604+
This method is unsafe as it returns a window into a file which might be larger
605+
than than the actual window size
606+
"""
607607
# can use map as we are starting at offset 0. Otherwise we would have to use buffer()
608608
return self._cursor.use_region().map()
609609

@@ -761,7 +761,8 @@ def _object(self, sha, as_stream, index=-1):
761761
sha = self._index.sha(index)
762762
# END assure sha is present ( in output )
763763
offset = self._index.offset(index)
764-
type_id, uncomp_size, data_rela_offset = pack_object_header_info(self._pack._cursor.use_region(offset).buffer())
764+
type_id, uncomp_size, _ = pack_object_header_info(
765+
self._pack._cursor.use_region(offset).buffer())
765766
if as_stream:
766767
if type_id not in delta_types:
767768
packstream = self._pack.stream(offset)
@@ -784,7 +785,7 @@ def _object(self, sha, as_stream, index=-1):
784785
# the actual target size, as opposed to the size of the delta data
785786
streams = self.collect_streams_at_offset(offset)
786787
buf = streams[0].read(512)
787-
offset, src_size = msb_size(buf)
788+
offset, src_size = msb_size(buf) # @UnusedVariable
788789
offset, target_size = msb_size(buf, offset)
789790

790791
# collect the streams to obtain the actual object type
@@ -1019,8 +1020,9 @@ def write_pack(cls, object_iter, pack_write, index_write=None,
10191020
# END for each object
10201021

10211022
if actual_count != object_count:
1022-
raise ValueError(
1023-
"Expected to write %i objects into pack, but received only %i from iterators" % (object_count, actual_count))
1023+
raise ValueError("Expected to write %i objects into pack, "
1024+
"but received only %i from iterators" %
1025+
(object_count, actual_count))
10241026
# END count assertion
10251027

10261028
# write footer
@@ -1049,7 +1051,8 @@ def create(cls, object_iter, base_dir, object_count=None, zlib_compression=zlib.
10491051
pack_write = lambda d: os.write(pack_fd, d)
10501052
index_write = lambda d: os.write(index_fd, d)
10511053

1052-
pack_binsha, index_binsha = cls.write_pack(object_iter, pack_write, index_write, object_count, zlib_compression)
1054+
pack_binsha, _ = cls.write_pack(
1055+
object_iter, pack_write, index_write, object_count, zlib_compression)
10531056
os.close(pack_fd)
10541057
os.close(index_fd)
10551058

Diff for: gitdb/stream.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55

66
from io import BytesIO
7-
87
import mmap
98
import os
109
import sys
1110
import zlib
1211

12+
from gitdb.const import NULL_BYTE, BYTE_SPACE
1313
from gitdb.fun import (
1414
msb_size,
1515
stream_copy,
1616
apply_delta_data,
1717
connect_deltas,
1818
delta_types
1919
)
20-
2120
from gitdb.util import (
2221
allocate_memory,
2322
LazyMixin,
@@ -27,10 +26,9 @@
2726
suppress,
2827
is_darwin,
2928
)
30-
31-
from gitdb.const import NULL_BYTE, BYTE_SPACE
3229
from gitdb.utils.compat import buffer
3330

31+
3432
has_perf_mod = False
3533
PY26 = sys.version_info[:2] < (2, 7)
3634
try:
@@ -157,7 +155,8 @@ def data(self):
157155
def close(self):
158156
"""Close our underlying stream of compressed bytes if this was allowed during initialization
159157
:return: True if we closed the underlying stream
160-
:note: can be called safely
158+
159+
.. note:: can be called safely
161160
"""
162161
if self._close:
163162
if hasattr(self._m, 'close'):
@@ -196,7 +195,7 @@ def compressed_bytes_read(self):
196195
# but keep the window at its current position
197196
self._br = 0
198197
if hasattr(self._zip, 'status'):
199-
while self._zip.status == zlib.Z_OK:
198+
while self._zip.status == zlib.Z_OK: # @UndefinedVariable
200199
self.read(mmap.PAGESIZE)
201200
# END scrub-loop custom zlib
202201
else:
@@ -762,7 +761,7 @@ class FDStream(object):
762761
__slots__ = ('_fd',
763762
'_pos',
764763
'_entered',
765-
)
764+
)
766765

767766
def __init__(self, fd):
768767
self._fd = fd
@@ -794,9 +793,9 @@ def read(self, count=0):
794793
count = os.path.getsize(self._filepath)
795794
# END handle read everything
796795

797-
bytes = os.read(self._fd, count)
798-
self._pos += len(bytes)
799-
return bytes
796+
bs = os.read(self._fd, count)
797+
self._pos += len(bs)
798+
return bs
800799

801800
def fileno(self):
802801
return self._fd

Diff for: gitdb/test/db/lib.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,28 @@
33
# This module is part of GitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55
"""Base classes for object db testing"""
6-
from gitdb.test.lib import (
7-
with_rw_directory,
8-
with_packs_rw,
9-
fixture_path,
10-
TestBase
11-
)
12-
13-
from gitdb.stream import (
14-
Sha1Writer,
15-
ZippedStoreShaWriter
16-
)
6+
from io import BytesIO
7+
from struct import pack
178

189
from gitdb.base import (
1910
IStream,
2011
OStream,
2112
OInfo
2213
)
23-
2414
from gitdb.exc import BadObject
15+
from gitdb.stream import (
16+
Sha1Writer,
17+
ZippedStoreShaWriter
18+
)
19+
from gitdb.test.lib import (
20+
with_rw_directory, # @UnusedImport
21+
with_packs_rw, # @UnusedImport
22+
fixture_path, # @UnusedImport
23+
TestBase
24+
)
2525
from gitdb.typ import str_blob_type
2626
from gitdb.utils.compat import xrange
2727

28-
from io import BytesIO
29-
30-
from struct import pack
31-
3228

3329
__all__ = ('TestDBBase', 'with_rw_directory', 'with_packs_rw', 'fixture_path')
3430

@@ -98,10 +94,10 @@ def _assert_object_writing(self, db):
9894
assert str_blob_type == info.type
9995
assert info.size == len(data)
10096

101-
ostream = db.stream(sha)
102-
assert ostream.read() == data
103-
assert ostream.type == str_blob_type
104-
assert ostream.size == len(data)
97+
with db.stream(sha) as ostream:
98+
assert ostream.read() == data
99+
assert ostream.type == str_blob_type
100+
assert ostream.size == len(data)
105101
else:
106102
self.failUnlessRaises(BadObject, db.info, sha)
107103
self.failUnlessRaises(BadObject, db.stream, sha)
@@ -120,10 +116,9 @@ def _assert_object_writing(self, db):
120116
db.set_ostream(ZippedStoreShaWriter())
121117
db.store(istream)
122118
assert istream.binsha == prev_sha
123-
new_ostream = db.ostream()
124-
125-
# note: only works as long our store write uses the same compression
126-
# level, which is zip_best
127-
assert ostream.getvalue() == new_ostream.getvalue()
119+
with db.ostream() as new_ostream:
120+
# note: only works as long our store write uses the same compression
121+
# level, which is zip_best
122+
assert ostream.getvalue() == new_ostream.getvalue()
128123
# END for each data set
129124
# END for each dry_run mode

Diff for: gitdb/test/db/test_git.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
# This module is part of GitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55
import os
6+
7+
from gitdb.base import OStream, OInfo
8+
from gitdb.db import GitDB
9+
from gitdb.exc import BadObject
610
from gitdb.test.db.lib import (
711
TestDBBase,
812
with_rw_directory
913
)
10-
from gitdb.exc import BadObject
11-
from gitdb.db import GitDB
12-
from gitdb.base import OStream, OInfo
1314
from gitdb.util import bin_to_hex
1415

1516

@@ -25,7 +26,7 @@ def test_reading(self):
2526
gitdb_sha = next(gdb.sha_iter())
2627
assert isinstance(gdb.info(gitdb_sha), OInfo)
2728
with gdb.stream(gitdb_sha) as stream:
28-
assert isinstance(gdb.stream(gitdb_sha), OStream)
29+
assert isinstance(stream, OStream)
2930
ni = 50
3031
assert gdb.size() >= ni
3132
sha_list = list(gdb.sha_iter())

0 commit comments

Comments
 (0)