1# Copyright (c) 2010, Willow Garage, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7#     * Redistributions of source code must retain the above copyright
8#       notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above copyright
10#       notice, this list of conditions and the following disclaimer in the
11#       documentation and/or other materials provided with the distribution.
12#     * Neither the name of the Willow Garage, Inc. nor the names of its
13#       contributors may be used to endorse or promote products derived from
14#       this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28# Original from cygwin.py by Tingfan Wu tingfan@gmail.com
29# Modified for FreeBSD by Rene Ladan rene@freebsd.org
30# Updated for FreeBSD with pkg by Trenton Schulz trentonw@ifi.uio.no
31
32from rospkg.os_detect import OS_FREEBSD
33
34from .pip import PIP_INSTALLER
35from ..installers import PackageManagerInstaller
36from ..shell_utils import read_stdout
37
38PKG_INSTALLER = 'pkg'
39
40
41def register_installers(context):
42    context.set_installer(PKG_INSTALLER, PkgInstaller())
43
44
45def register_platforms(context):
46    context.add_os_installer_key(OS_FREEBSD, PKG_INSTALLER)
47    context.add_os_installer_key(OS_FREEBSD, PIP_INSTALLER)
48    context.set_default_os_installer_key(OS_FREEBSD, lambda self: PKG_INSTALLER)
49
50
51def pkg_detect_single(p, exec_fn):
52    if p == "builtin":
53        return True
54
55    cmd = ['/usr/sbin/pkg', 'query', '%n', p]
56    std_out = exec_fn(cmd)
57    return std_out.split() != []
58
59
60def pkg_detect(packages, exec_fn=None):
61    if exec_fn is None:
62        exec_fn = read_stdout
63    return [p for p in packages if pkg_detect_single(p, exec_fn)]
64
65
66class PkgInstaller(PackageManagerInstaller):
67    """
68    An implementation of the Installer for use on FreeBSD-style
69    systems.
70    """
71
72    def __init__(self):
73        super(PkgInstaller, self).__init__(pkg_detect)
74
75    def get_install_command(self, resolved, interactive=True, reinstall=False, quiet=False):
76        packages = self.get_packages_to_install(resolved, reinstall=reinstall)
77        if not packages:
78            return []
79        else:
80            return [self.elevate_priv(['/usr/sbin/pkg', 'install', '-y']) + packages]
81