~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/reltool/bin/reltool.escript

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env escript
 
2
%% -*- erlang -*-
 
3
%%
 
4
%% %CopyrightBegin%
 
5
%%
 
6
%% Copyright Ericsson AB 2010. All Rights Reserved.
 
7
%%
 
8
%% The contents of this file are subject to the Erlang Public License,
 
9
%% Version 1.1, (the "License"); you may not use this file except in
 
10
%% compliance with the License. You should have received a copy of the
 
11
%% Erlang Public License along with this software. If not, it can be
 
12
%% retrieved online at http://www.erlang.org/.
 
13
%%
 
14
%% Software distributed under the License is distributed on an "AS IS"
 
15
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
16
%% the License for the specific language governing rights and limitations
 
17
%% under the License.
 
18
%%
 
19
%% %CopyrightEnd%
 
20
 
 
21
-include_lib("reltool/src/reltool.hrl").
 
22
 
 
23
main(Args) ->
 
24
    process_flag(trap_exit, true),
 
25
    try
 
26
        Tokens = scan_args(Args, [], []),
 
27
        {Options, Actions} = parse_args(Tokens, []),
 
28
        case invoke(Options, Actions) of
 
29
            ok ->
 
30
                safe_stop(0);
 
31
            {error, ReasonString} ->
 
32
                fatal_error(ReasonString, 2)
 
33
        end
 
34
    catch
 
35
        throw:usage ->
 
36
            usage(),
 
37
            safe_stop(1);
 
38
        exit:Reason ->
 
39
            String = lists:flatten(io_lib:format("EXIT: ~p", [Reason])),
 
40
            fatal_error(String, 3)
 
41
    end.
 
42
 
 
43
usage() ->
 
44
    Usage =
 
45
        [
 
46
         "[Config] [--window]",
 
47
         "[Config] --create_config [-defaults] [-derived] [ConfigFile]",
 
48
         "[Config] --create_rel RelName [RelFile]",
 
49
         "[Config] --create_script RelName [ScriptFile]",
 
50
         "[Config] --create_target TargetDir",
 
51
         "[Config] --create_target_spec [SpecFile]",
 
52
         "[Config] --eval_target_spec Spec TargetDir RootDir"
 
53
        ],
 
54
    Script = script_name(),
 
55
    String = lists:flatten([[Script, " ", U, "\n"] || U <- Usage]),
 
56
    io:format("Erlang/OTP release management tool\n\n"
 
57
              "~s\nConfig = ConfigFile | '{sys, [sys()]}'\n"
 
58
              "Spec   = SpecFile   | '{spec, [target_spec()}']\n\n"
 
59
              "See User's guide and Reference manual for more info.\n",
 
60
              [String]).
 
61
 
 
62
safe_stop(Code) ->
 
63
    init:stop(Code),
 
64
    timer:sleep(infinity).
 
65
 
 
66
invoke(Options, Actions) ->
 
67
    case Actions of
 
68
        [] ->
 
69
            invoke(Options, [["--window"]]);
 
70
        [["--window"]] ->
 
71
            start_window(Options);
 
72
        [["--create_config" | OptArgs]] ->
 
73
            DefArg = "-defaults",
 
74
            DerivArg = "-derived",
 
75
            InclDef = lists:member(DefArg, OptArgs),
 
76
            InclDeriv = lists:member(DerivArg, OptArgs),
 
77
            case reltool:get_config(Options, InclDef, InclDeriv) of
 
78
                {ok, Config} ->
 
79
                    String = pretty("config", Config),
 
80
                    case OptArgs -- [DefArg, DerivArg] of
 
81
                        [] ->
 
82
                            format("~s", [String]);
 
83
                        [ConfigFile] ->
 
84
                            write_file(ConfigFile, String);
 
85
                        _ ->
 
86
                            throw(usage)
 
87
                    end;
 
88
                {error, Reason} ->
 
89
                    {error, Reason}
 
90
            end;
 
91
        [["--create_rel", RelName | OptArgs]] ->
 
92
            case reltool:get_rel(Options, RelName) of
 
93
                {ok, Rel} ->
 
94
                    String = pretty("rel", Rel),
 
95
                    case OptArgs of
 
96
                        [] ->
 
97
                            format("~s", [String]);
 
98
                        [RelFile] ->
 
99
                            write_file(RelFile, String);
 
100
                        _ ->
 
101
                            throw(usage)
 
102
                    end;
 
103
                {error, Reason} ->
 
104
                    {error, Reason}
 
105
            end;
 
106
        [["--create_script", RelName | OptArgs]] ->
 
107
            case reltool:get_script(Options, RelName) of
 
108
                {ok, Script} ->
 
109
                    String = pretty("script", Script),
 
110
                    case OptArgs of
 
111
                        [] ->
 
112
                            format("~s", [String]);
 
113
                        [ScriptFile] ->
 
114
                            write_file(ScriptFile, String);
 
115
                        _ ->
 
116
                            throw(usage)
 
117
                    end;
 
118
                {error, Reason} ->
 
119
                    {error, Reason}
 
120
            end;
 
121
        [["--create_target", TargetDir]] ->
 
122
            reltool:create_target(Options, TargetDir);
 
123
        [["--create_target_spec" | OptArgs]] ->
 
124
            case reltool:get_target_spec(Options) of
 
125
                {ok, Script} ->
 
126
                    String = pretty("target_spec", Script),
 
127
                    case OptArgs of
 
128
                        [] ->
 
129
                            format("~s", [String]);
 
130
                        [SpecFile] ->
 
131
                            write_file(SpecFile, String);
 
132
                        _ ->
 
133
                            throw(usage)
 
134
                    end;
 
135
                {error, Reason} ->
 
136
                    {error, Reason}
 
137
            end;
 
138
        [["--eval_target_spec", TargetSpec, TargetDir, RootDir]] ->
 
139
            try
 
140
                {ok, Tokens, _} = erl_scan:string(TargetSpec ++ ". "),
 
141
                {ok, {spec, Spec}} = erl_parse:parse_term(Tokens),
 
142
                reltool:eval_target_spec(Spec, TargetDir, RootDir)
 
143
            catch
 
144
                error:{badmatch, _} ->
 
145
                    case file:consult(TargetSpec) of
 
146
                        {ok, Spec2} ->
 
147
                            reltool:eval_target_spec(Spec2, TargetDir, RootDir);
 
148
                        {error, Reason} ->
 
149
                            Text = file:format_error(Reason),
 
150
                            {error, TargetSpec ++ ": " ++ Text}
 
151
                    end
 
152
            end;
 
153
        _ ->
 
154
            throw(usage)
 
155
    end.
 
156
 
 
157
start_window(Options) ->
 
158
    case reltool:start_link(Options) of
 
159
        {ok, WinPid} ->
 
160
            receive
 
161
                {'EXIT', WinPid, shutdown} ->
 
162
                    ok;
 
163
                {'EXIT', WinPid, normal} ->
 
164
                    ok;
 
165
                {'EXIT', WinPid, Reason} ->
 
166
                    exit(Reason)
 
167
            end;
 
168
        {error, Reason} ->
 
169
            {error, Reason}
 
170
    end.
 
171
 
 
172
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
173
%% Helpers
 
174
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
175
 
 
176
script_name() ->
 
177
    filename:basename(escript:script_name(), ".escript").
 
178
 
 
179
fatal_error(String, Code) ->
 
180
    io:format(standard_error, "~s: ~s\n", [script_name(), String]),
 
181
    safe_stop(Code).
 
182
 
 
183
write_file(File, IoList) ->
 
184
    case file:write_file(File, IoList) of
 
185
        ok ->
 
186
            ok;
 
187
        {error, Reason} ->
 
188
            {error, file:format_error(Reason)}
 
189
    end.
 
190
 
 
191
format(Format, Args) ->
 
192
    io:format(Format, Args),
 
193
    %% Wait a while for the I/O to be processed
 
194
    timer:sleep(timer:seconds(1)).
 
195
 
 
196
pretty(Tag, Term) ->
 
197
    lists:flatten(io_lib:format("%% ~s generated at ~w ~w\n~p.\n\n",
 
198
                                [Tag, date(), time(), Term])).
 
199
 
 
200
scan_args([H | T], Single, Multi) ->
 
201
    case H of
 
202
        "--" ++ _ when Single =:= [] ->
 
203
            scan_args(T, [H], Multi);
 
204
        "--" ++ _ ->
 
205
            scan_args(T, [H], [lists:reverse(Single) | Multi]);
 
206
        _ ->
 
207
            scan_args(T, [H | Single], Multi)
 
208
    end;
 
209
scan_args([], [], Multi) ->
 
210
    lists:reverse(Multi);
 
211
scan_args([], Single, Multi) ->
 
212
    lists:reverse([lists:reverse(Single) | Multi]).
 
213
 
 
214
parse_args([H | T] = Args, Options) ->
 
215
    case H of
 
216
        ["--wx_debug" | Levels] ->
 
217
            Dbg =
 
218
                fun(L) ->
 
219
                        case catch list_to_integer(L) of
 
220
                            {'EXIT', _} ->
 
221
                                case catch list_to_atom(L) of
 
222
                                    {'EXIT', _} ->
 
223
                                        exit("Illegal wx debug level: " ++ L);
 
224
                                    Atom ->
 
225
                                                Atom
 
226
                                end;
 
227
                            Int ->
 
228
                                Int
 
229
                        end
 
230
                end,
 
231
            Levels2 = lists:map(Dbg, Levels),
 
232
            parse_args(T, [{wx_debug, Levels2} | Options]);
 
233
        ["--" ++ _ | _] ->
 
234
            %% No more options
 
235
            {lists:reverse(Options), Args};
 
236
        [Config] ->
 
237
            try
 
238
                {ok, Tokens, _} = erl_scan:string(Config ++ ". "),
 
239
                {ok, {sys, _} = Sys} = erl_parse:parse_term(Tokens),
 
240
                parse_args(T, [{config, Sys} | Options])
 
241
            catch
 
242
                error:{badmatch, _} ->
 
243
                    parse_args(T, [{config, Config} | Options]);
 
244
                X:Y ->
 
245
                    io:format("\n\n~p\n\n", [{X, Y}])
 
246
            end
 
247
    end;
 
248
parse_args([], Options) ->
 
249
    {lists:reverse(Options), []}.