Skip to content

Commit

Permalink
Merge branch 'filename-clashes'
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Oct 3, 2018
2 parents bf2408b + 7872ad2 commit 3ec8e08
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
56 changes: 25 additions & 31 deletions nsist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ def fetch_python_embeddable(self):

logger.info('Unpacking Python...')
python_dir = pjoin(self.build_dir, 'Python')
try:
shutil.rmtree(python_dir)
except OSError as e:
if e.errno != errno.ENOENT:
raise

with zipfile.ZipFile(str(cache_file)) as z:
z.extractall(python_dir)
Expand All @@ -223,12 +218,6 @@ def prepare_msvcrt(self):
dst = pjoin(self.build_dir, 'msvcrt')
self.msvcrt_files = sorted(os.listdir(src))

try:
shutil.rmtree(dst)
except OSError as e:
if e.errno != errno.ENOENT:
raise

shutil.copytree(src, dst)

SCRIPT_TEMPLATE = """#!python{qualifier}
Expand Down Expand Up @@ -341,8 +330,6 @@ def prepare_packages(self):
"""
logger.info("Copying packages into build directory...")
build_pkg_dir = pjoin(self.build_dir, 'pkgs')
if os.path.isdir(build_pkg_dir):
shutil.rmtree(build_pkg_dir)

# 1. Manually prepared packages
if os.path.isdir('pynsist_pkgs'):
Expand All @@ -363,8 +350,6 @@ def prepare_packages(self):

def prepare_commands(self):
command_dir = Path(self.build_dir) / 'bin'
if command_dir.is_dir():
shutil.rmtree(str(command_dir))
command_dir.mkdir()
prepare_bin_directory(command_dir, self.commands, bitness=self.py_bitness)
self.install_dirs.append((command_dir.name, '$INSTDIR'))
Expand Down Expand Up @@ -393,9 +378,23 @@ def copy_extra_files(self):
"""Copy a list of files into the build directory, and add them to
install_files or install_dirs as appropriate.
"""
# Create installer.nsi, so that a data file with the same name will
# automatically be renamed installer.1.nsi. All the other files needed
# in the build directory should already be in place.
Path(self.nsi_file).touch()

for file, destination in self.extra_files:
file = file.rstrip('/\\')
basename = os.path.basename(file)
in_build_dir = Path(self.build_dir, os.path.basename(file))

# Find an unused name in the build directory,
# similar to the source filename, e.g. foo.1.txt, foo.2.txt, ...
stem, suffix = in_build_dir.stem, in_build_dir.suffix
n = 1
while in_build_dir.exists():
name = '{}.{}{}'.format(stem, n, suffix)
in_build_dir = in_build_dir.with_name(name)
n += 1

if destination:
# Normalize destination paths to Windows-style
Expand All @@ -404,22 +403,17 @@ def copy_extra_files(self):
destination = '$INSTDIR'

if os.path.isdir(file):
target_name = pjoin(self.build_dir, basename)
if os.path.isdir(target_name):
shutil.rmtree(target_name)
elif os.path.exists(target_name):
os.unlink(target_name)
if self.exclude:
shutil.copytree(file, target_name,
shutil.copytree(file, str(in_build_dir),
ignore=self.copytree_ignore_callback)
else:
# Don't use our exclude callback if we don't need to,
# as it slows things down.
shutil.copytree(file, target_name)
self.install_dirs.append((basename, destination))
shutil.copytree(file, str(in_build_dir))
self.install_dirs.append((in_build_dir.name, destination))
else:
shutil.copy2(file, self.build_dir)
self.install_files.append((basename, destination))
shutil.copy2(file, str(in_build_dir))
self.install_files.append((in_build_dir.name, destination))

def write_nsi(self):
"""Write the NSI file to define the NSIS installer.
Expand Down Expand Up @@ -456,11 +450,11 @@ def run_nsis(self):
def run(self, makensis=True):
"""Run all the steps to build an installer.
"""
try:
os.makedirs(self.build_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise e
try: # Start with a clean build directory
shutil.rmtree(self.build_dir)
except FileNotFoundError:
pass
os.makedirs(self.build_dir)

self.fetch_python_embeddable()
if self.inc_msvcrt:
Expand Down
1 change: 1 addition & 0 deletions nsist/tests/data_files/dir1/eg-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example data file 1
1 change: 1 addition & 0 deletions nsist/tests/data_files/dir1/installer.nsi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;For testing name conflicts - the name installer.nsi is used by Pynsist
Empty file.
1 change: 1 addition & 0 deletions nsist/tests/data_files/dir2/eg-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Example data file 2
Empty file.
36 changes: 36 additions & 0 deletions nsist/tests/test_installerbuilder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import os
from os.path import join as pjoin
from testpath import assert_isfile

Expand Down Expand Up @@ -30,3 +31,38 @@ def test_prepare_shortcuts(tmpdir):
preamble_contents = f.read().strip()

assert preamble_contents in contents

def test_copy_extra_files(tmpdir):
files = [
(pjoin(test_dir, 'data_files', 'dir1', 'eg-data.txt'), '$INSTDIR'),
(pjoin(test_dir, 'data_files', 'dir2', 'eg-data.txt'), '$INSTDIR\\foo'),
(pjoin(test_dir, 'data_files', 'dir1', 'subdir'), '$INSTDIR'),
(pjoin(test_dir, 'data_files', 'dir2', 'subdir'), '$INSTDIR\\foo'),
]
ib = InstallerBuilder("Test App", "1.0", {}, extra_files=files,
build_dir=tmpdir)
ib.copy_extra_files()

build_dir_files = set(os.listdir(tmpdir))
for file in ['eg-data.txt', 'eg-data.1.txt', 'subdir', 'subdir.1']:
assert file in build_dir_files

assert ib.install_dirs == [
('subdir', '$INSTDIR'),
('subdir.1', '$INSTDIR\\foo'),
]
assert ib.install_files == [
('eg-data.txt', '$INSTDIR'),
('eg-data.1.txt', '$INSTDIR\\foo'),
]

def test_copy_installer_nsi(tmpdir):
files = [
(pjoin(test_dir, 'data_files', 'dir1', 'installer.nsi'), None),
]
ib = InstallerBuilder("Test App", "1.0", {}, extra_files=files,
build_dir=tmpdir)
ib.copy_extra_files()

assert_isfile(pjoin(tmpdir, 'installer.1.nsi'))
assert ib.install_files == [('installer.1.nsi', '$INSTDIR')]

0 comments on commit 3ec8e08

Please sign in to comment.