Skip to content

Commit

Permalink
Merge pull request pyinstaller#1990 from linusg-github/develop
Browse files Browse the repository at this point in the history
Added command-line options --add-data and --add-binary.

Closes pyinstaller#703.
  • Loading branch information
htgoebel committed Jun 5, 2016
2 parents 582599c + d601cf5 commit 3de588a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
33 changes: 32 additions & 1 deletion PyInstaller/building/makespec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import sys
import argparse

from .. import HOMEPATH, DEFAULT_SPECPATH
from .. import log as logging
Expand All @@ -22,6 +23,7 @@
cipher_init_template, bundleexetmplt, bundletmplt

logger = logging.getLogger(__name__)
add_command_sep = os.pathsep


def quote_win_filepath(path):
Expand Down Expand Up @@ -53,6 +55,19 @@ def make_path_spec_relative(filename, spec_dir):
)


def add_data_or_binary(string):
try:
src, dest = string.split(add_command_sep)
except ValueError:
# Split into SRC and DEST failed, wrong syntax
raise argparse.ArgumentError("Wrong syntax, should be SRC{}DEST".format(add_command_sep))
if not src or not dest:
# Syntax was correct, but one or both of SRC and DEST was not given
raise argparse.ArgumentError("You have to specify both SRC and DEST")
# Return tuple containing SRC and SRC
return (src, dest)


def make_variable_path(filename, conversions=path_conversions):
for (from_path, to_name) in conversions:
assert os.path.abspath(from_path) == from_path, (
Expand Down Expand Up @@ -100,6 +115,20 @@ def __add_options(parser):
"(default: first script's basename)")

g = parser.add_argument_group('What to bundle, where to search')
g.add_argument('--add-data',
action='append', default=[], type=add_data_or_binary,
metavar='SRC{}DEST'.format(add_command_sep), dest='datas',
help='Additional non-binary files or folders to be added '
'to the executable. The path separator is '
'platform specific, `os.pathsep` (`;` on Windows, `:` on most '
'UNIX systems) is used. This option can be used '
'multiple times.')
g.add_argument('--add-binary',
action='append', default=[], type=add_data_or_binary,
metavar='SRC{}DEST'.format(add_command_sep), dest='binaries',
help='Additional binary files to be added to the executable. '
'See `--add-data` option for more details. '
'This option can be used multiple times.')
g.add_argument("-p", "--paths", dest="pathex",
metavar="DIR", action="append", default=[],
help="A path to search for imports (like using PYTHONPATH). "
Expand Down Expand Up @@ -215,7 +244,7 @@ def __add_options(parser):
def main(scripts, name=None, onefile=None,
console=True, debug=False, strip=False, noupx=False,
pathex=None, version_file=None, specpath=None,
icon_file=None, manifest=None, resources=None, bundle_identifier=None,
datas=None, binaries=None, icon_file=None, manifest=None, resources=None, bundle_identifier=None,
hiddenimports=None, hookspath=None, key=None, runtime_hooks=None,
excludes=None, uac_admin=False, uac_uiaccess=False,
win_no_prefer_redirects=False, win_private_assemblies=False,
Expand Down Expand Up @@ -312,6 +341,8 @@ def main(scripts, name=None, onefile=None,
d = {
'scripts': scripts,
'pathex': pathex,
'binaries': binaries,
'datas': datas,
'hiddenimports': hiddenimports,
'name': name,
'debug': debug,
Expand Down
8 changes: 4 additions & 4 deletions PyInstaller/building/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
a = Analysis(%(scripts)s,
pathex=%(pathex)s,
binaries=None,
datas=None,
binaries=%(binaries)s,
datas=%(datas)s,
hiddenimports=%(hiddenimports)r,
hookspath=%(hookspath)r,
runtime_hooks=%(runtime_hooks)r,
Expand All @@ -45,8 +45,8 @@
a = Analysis(%(scripts)s,
pathex=%(pathex)s,
binaries=None,
datas=None,
binaries=%(binaries)s,
datas=%(datas)s,
hiddenimports=%(hiddenimports)r,
hookspath=%(hookspath)r,
runtime_hooks=%(runtime_hooks)r,
Expand Down
24 changes: 20 additions & 4 deletions doc/spec-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ The statements in a spec file create instances of four classes,

- ``scripts``: the python scripts named on the command line;
- ``pure``: pure python modules needed by the scripts;
- ``binaries``: non-python modules needed by the scripts;
- ``datas``: non-binary files included in the app.
- ``binaries``: non-python modules needed by the scripts, including names
given by the ``--add-binary`` option;
- ``datas``: non-binary files included in the app, including names given
by the ``--add-data`` option.

* An instance of class ``PYZ`` is a ``.pyz`` archive (described
under :ref:`Inspecting Archives` below), which contains all the
Expand Down Expand Up @@ -129,7 +131,10 @@ To find the data files at run-time, see :ref:`Run-time Information`.
Adding Data Files
------------------

To have data files included in the bundle, provide a list that
You can add data files to the bundle by using the ``--add-data`` command option, or by
adding them as a list to the spec file.

When using the spec file, provide a list that
describes the files as the value of the ``datas=`` argument to ``Analysis``.
The list of data files is a list of tuples.
Each tuple has two values, both of which must be strings:
Expand All @@ -147,6 +152,10 @@ you could modify the spec file as follows::
...
)

And the command line equivalent::

pyinstaller --add-data 'src/README.txt:.' myscript.py

You have made the ``datas=`` argument a one-item list.
The item is a tuple in which the first string says the existing file
is ``src/README.txt``.
Expand Down Expand Up @@ -237,7 +246,10 @@ If it is actually characters, you must decode it::
Adding Binary Files
--------------------

To add binary files, make a list of tuples that describe the files needed.
You can add binary files to the bundle by using the ``--add-binary`` command option,
or by adding them as a list to the spec file.

In the spec file, make a list of tuples that describe the files needed.
Assign the list of tuples to the ``binaries=`` argument of Analysis.

Normally |PyInstaller| learns about ``.so`` and ``.dll`` libraries by
Expand All @@ -258,6 +270,10 @@ You could add it to the bundle this way::
binaries=[ ( '/usr/lib/libiodbc.2.dylib', 'libiodbc.dylib' ) ],
...

Or via the command line::

pyinstaller --add-binary '/usr/lib/libiodbc.2.dylib:libiodbc.dylib' myscript.py

As with data files, if you have multiple binary files to add,
create the list in a separate statement and pass the list by name.

Expand Down
6 changes: 6 additions & 0 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ For example, in Linux::

pyinstaller --noconfirm --log-level=WARN \
--onefile --nowindow \
--add-data="README:." \
--add-data="image1.png:img" \
--add-binary="libfoo.so:lib" \
--hidden-import=secret1 \
--hidden-import=secret2 \
--upx-dir=/usr/local/share/ \
Expand All @@ -77,6 +80,9 @@ Or in Windows, use the little-known BAT file line continuation::

pyinstaller --noconfirm --log-level=WARN ^
--onefile --nowindow ^
--add-data="README;." ^
--add-data="image1.png;img" ^
--add-binary="libfoo.so;lib" ^
--hidden-import=secret1 ^
--hidden-import=secret2 ^
--icon=..\MLNMFLCN.ICO ^
Expand Down

0 comments on commit 3de588a

Please sign in to comment.