aboutsummaryrefslogtreecommitdiff
path: root/paludis/environments/paludis/bashable_conf.cc
blob: 3a3f408e10de07a3ff6d1a4ed56e6bebbb6c8d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007, 2008, 2009, 2010 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "bashable_conf.hh"
#include <paludis/util/config_file.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/log.hh>
#include <paludis/util/system.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <functional>

using namespace paludis;
using namespace paludis::paludis_environment;

std::string defined_vars_to_kv_func(
        const std::shared_ptr<const Map<std::string, std::string> > v,
        const KeyValueConfigFile &,
        const std::string & k)
{
    Map<std::string, std::string>::ConstIterator i(v->find(k));
    if (i != v->end())
        return i->second;
    return "";
}

std::shared_ptr<LineConfigFile>
paludis::paludis_environment::make_bashable_conf(const FSEntry & f, const LineConfigFileOptions & o)
{
    Context context("When making a config file out of '" + stringify(f) + "':");

    std::shared_ptr<LineConfigFile> result;

    if (is_file_with_extension(f, ".bash", { }))
    {
        std::stringstream s;
        Command cmd(Command("bash '" + stringify(f) + "'")
                .with_setenv("PALUDIS_LOG_LEVEL", stringify(Log::get_instance()->log_level()))
                .with_setenv("PALUDIS_EBUILD_DIR", getenv_with_default("PALUDIS_EBUILD_DIR", LIBEXECDIR "/paludis"))
                .with_stderr_prefix(f.basename() + "> ")
                .with_captured_stdout_stream(&s));
        int exit_status(run_command(cmd));
        result = std::make_shared<LineConfigFile>(s, o);

        if (exit_status != 0)
        {
            Log::get_instance()->message("paludis_environment.bash_conf.failure", ll_warning, lc_context)
                << "Script '" << f <<"' returned non-zero exit status '" << exit_status << "'";
            result.reset();
        }
    }
    else
        result = std::make_shared<LineConfigFile>(f, o);

    return result;
}

std::shared_ptr<KeyValueConfigFile>
paludis::paludis_environment::make_bashable_kv_conf(const FSEntry & f,
        const std::shared_ptr<const Map<std::string, std::string> > & predefined_variables,
        const KeyValueConfigFileOptions & o)
{
    Context context("When making a key=value config file out of '" + stringify(f) + "':");

    std::shared_ptr<KeyValueConfigFile> result;

    if (is_file_with_extension(f, ".bash", { }))
    {
        std::stringstream s;
        Command cmd(Command("bash '" + stringify(f) + "'")
                .with_setenv("PALUDIS_LOG_LEVEL", stringify(Log::get_instance()->log_level()))
                .with_setenv("PALUDIS_EBUILD_DIR", getenv_with_default("PALUDIS_EBUILD_DIR", LIBEXECDIR "/paludis"))
                .with_stderr_prefix(f.basename() + "> ")
                .with_captured_stdout_stream(&s));
        for (Map<std::string, std::string>::ConstIterator i(predefined_variables->begin()),
                end(predefined_variables->end()); i != end; ++i)
            cmd.with_setenv(i->first, i->second);
        int exit_status(run_command(cmd));
        result = std::make_shared<KeyValueConfigFile>(s, o, &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);

        if (exit_status != 0)
        {
            Log::get_instance()->message("paludis_environment.bash_conf.failure", ll_warning, lc_context)
                << "Script '" << f <<"' returned non-zero exit status '" << exit_status << "'";
            result.reset();
        }
    }
    else
    {
        result = std::make_shared<KeyValueConfigFile>(f, o,
                std::bind(&defined_vars_to_kv_func, predefined_variables, std::placeholders::_1, std::placeholders::_2),
                &KeyValueConfigFile::no_transformation);
    }

    return result;
}