Skip to content

Commit

Permalink
Fix PEXBuilder.clone.
Browse files Browse the repository at this point in the history
Previously neither shebang nor copy were cloned leading to inability to
set these attributes differently from their defaults using `bdist_pex`
which leverages `clone`.
  • Loading branch information
jsirois committed Apr 19, 2016
1 parent cfcfe70 commit 2fc3f73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
9 changes: 6 additions & 3 deletions pex/pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def clone(self, into=None):
clone = self.__class__(
chroot=chroot_clone,
interpreter=self._interpreter,
pex_info=self._pex_info.copy())
pex_info=self._pex_info.copy(),
preamble=self._preamble,
copy=self._copy)
clone.set_shebang(self._shebang)
for dist in self._distributions:
clone.add_distribution(dist)
return clone
Expand Down Expand Up @@ -236,10 +239,10 @@ def set_shebang(self, shebang):
used to override the default behavior which is to have a #!/usr/bin/env line referencing an
interpreter compatible with the one used to build the PEX.
:param shebang: The shebang line minus the #!.
:param shebang: The shebang line. If it does not include the leading '#!' it will be added.
:type shebang: str
"""
self._shebang = '#!%s' % shebang
self._shebang = '#!%s' % shebang if not shebang.startswith('#!') else shebang

def _add_dist_dir(self, path, dist_name):
for root, _, files in os.walk(path):
Expand Down
38 changes: 23 additions & 15 deletions tests/test_pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ def test_pex_builder():


def test_pex_builder_shebang():
pb = PEXBuilder()
pb.set_shebang('foobar')
def builder(shebang):
pb = PEXBuilder()
pb.set_shebang(shebang)
return pb

with temporary_dir() as td:
target = os.path.join(td, 'foo.pex')
pb.build(target)
expected_preamble = b'#!foobar\n'
with open(target, 'rb') as fp:
assert fp.read(len(expected_preamble)) == expected_preamble
for pb in builder('foobar'), builder('#!foobar'):
for b in pb, pb.clone():
with temporary_dir() as td:
target = os.path.join(td, 'foo.pex')
b.build(target)
expected_preamble = b'#!foobar\n'
with open(target, 'rb') as fp:
assert fp.read(len(expected_preamble)) == expected_preamble


def test_pex_builder_compilation():
Expand Down Expand Up @@ -114,13 +118,17 @@ def build_and_check(path, copy):
pb = PEXBuilder(path, copy=copy)
pb.add_source(src, 'exe.py')

s1 = os.stat(src)
s2 = os.stat(os.path.join(path, 'exe.py'))
is_link = (s1[stat.ST_INO], s1[stat.ST_DEV]) == (s2[stat.ST_INO], s2[stat.ST_DEV])
if copy:
assert not is_link
else:
assert is_link
path_clone = os.path.join(path, '__clone')
pb.clone(into=path_clone)

for root in path, path_clone:
s1 = os.stat(src)
s2 = os.stat(os.path.join(root, 'exe.py'))
is_link = (s1[stat.ST_INO], s1[stat.ST_DEV]) == (s2[stat.ST_INO], s2[stat.ST_DEV])
if copy:
assert not is_link
else:
assert is_link

build_and_check(td2, False)
build_and_check(td3, True)

0 comments on commit 2fc3f73

Please sign in to comment.