Skip to content

Commit

Permalink
Revamp install
Browse files Browse the repository at this point in the history
  • Loading branch information
refi64 committed Jan 22, 2018
1 parent 01bc5e0 commit 90d653a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
20 changes: 10 additions & 10 deletions docs/source/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -750,19 +750,19 @@ this:

.. code-block:: python
def install(self, path, category, addroot=''):
def install(self, path, target, *, rename=None, perms=None):
The ``path`` is the path to install. If it's inside of ``ctx.buildroot``, then
that will be stripped off; for instance, if you have ``build/myfile.exe``, then
the ``build/`` portion will be removed to result in ``myfile.exe``.
The ``path`` is the path to install of the file to install, and ``target`` is a
subdirectory of the installation prefix to install into. For instance,
``ctx.install('somehwere/my-file', 'share/my-app')`` will copy ``somewhere/my-file``
to ``$PREFIX/share/my-app/my-file``.

``category`` is the subdirectory of ``ctx.installroot`` to install the file into.
If you want to change the file name (e.g. ``my-new-file`` instead), you can pass that
as the ``rename`` parameter, e.g.
``ctx.install('somewhere/my-file', 'share/my-app', 'my-new-file')``.

``addroot`` is a *further* subdirectory of ``ctx.installroot`` that the files will
be put into.

For instance, ``ctx.install('x', 'y', 'z')`` will install ``x`` into
``ctx.installroot / 'y' / 'z'``.
``perms`` can be used to assign custom permissions to the target file. By default, it
will use the same permissions as the original file.

Platforms
^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions examples/install/fbuildroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
def build(ctx):
c = guess.static(ctx)
exe = c.build_exe('x', ['x.c'])
ctx.install(exe, 'bin')
ctx.install('doc.txt', 'share', 'some_subdir_of_usr_share')
# ctx.install(exe, 'bin')
ctx.install('doc.txt', 'share/fbuild-demo', rename='documentation')
12 changes: 4 additions & 8 deletions lib/fbuild/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, options):
self.options = options

self.install_prefix = Path('/usr/local')
self.to_install = {'bin': [], 'lib': [], 'share': [], 'include': []}
self.to_install = []

self.tmpdir = self.buildroot / '.tmp'
fbuild.temp.set_default_tempdir(self.tmpdir)
Expand Down Expand Up @@ -251,13 +251,9 @@ def timeout_function(p):

return stdout, stderr

def install(self, path, category, addroot=''):
try:
self.to_install[category].append((Path(path).abspath(), addroot))
except AttributeError:
pass
except KeyError:
raise fbuild.Error('invalid install category: {}'.format(category))
def install(self, path, target, *, rename=None, perms=None):
"""Set the given file to be installed after the build completes."""
self.to_install.append((Path(path).abspath(), target, rename, perms))

# ------------------------------------------------------------------------------

Expand Down
23 changes: 14 additions & 9 deletions lib/fbuild/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,22 @@ def parse_args(argv):
# ------------------------------------------------------------------------------

def install_files(ctx):
for subdir, files in ctx.to_install.items():
for file, subdir, rename, perms in ctx.to_install:
# Generate the full subdirectory.
target_root = fbuild.path.Path(subdir).addroot(ctx.install_prefix)
for file, froot in files:
file = file.relpath(file.getcwd())
# Generate the target path.
target = file.basename().addroot(target_root / froot)
# Copy the file.
ctx.logger.check(' * install', '%s -> %s' % (file, target),
color='yellow')
file.copy(target)
target_root.makedirs(exist_ok=True)

# Generate the target path.
target = target_root / (rename or file.basename())
file = file.relpath(file.getcwd())

# Copy the file.
ctx.logger.check(' * install', '%s -> %s' % (file, target), color='yellow')
file.copy(target)

# Set permissions.
if perms is not None:
file.chmod(perms)

# ------------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions lib/fbuild/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def basename(self):
"""
return Path(os.path.basename(self))

def chmod(self):
return os.chmod(self)
def chmod(self, *args, **kw):
return os.chmod(self, *args, **kw)

def commonprefix(self):
return os.path.commonprefix(self)
Expand Down

0 comments on commit 90d653a

Please sign in to comment.