From 90d653aa6a309f8c953e7c8573b9976a4e6fba4e Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 22 Jan 2018 14:16:04 -0600 Subject: [PATCH] Revamp install --- docs/source/manual.rst | 20 ++++++++++---------- examples/install/fbuildroot.py | 4 ++-- lib/fbuild/context.py | 12 ++++-------- lib/fbuild/main.py | 23 ++++++++++++++--------- lib/fbuild/path.py | 4 ++-- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/source/manual.rst b/docs/source/manual.rst index b64fbd3..89b83c4 100644 --- a/docs/source/manual.rst +++ b/docs/source/manual.rst @@ -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 ^^^^^^^^^ diff --git a/examples/install/fbuildroot.py b/examples/install/fbuildroot.py index 63176f4..45ef3b2 100644 --- a/examples/install/fbuildroot.py +++ b/examples/install/fbuildroot.py @@ -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') diff --git a/lib/fbuild/context.py b/lib/fbuild/context.py index 39f4e5e..941bdbf 100644 --- a/lib/fbuild/context.py +++ b/lib/fbuild/context.py @@ -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) @@ -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)) # ------------------------------------------------------------------------------ diff --git a/lib/fbuild/main.py b/lib/fbuild/main.py index e1abc0f..b2ed34a 100644 --- a/lib/fbuild/main.py +++ b/lib/fbuild/main.py @@ -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) # ------------------------------------------------------------------------------ diff --git a/lib/fbuild/path.py b/lib/fbuild/path.py index 1e1abc2..ae13d82 100644 --- a/lib/fbuild/path.py +++ b/lib/fbuild/path.py @@ -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)