From 14dc28856e0f9f16a406cdf2a16a603276efe273 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 25 Jan 2013 16:55:48 -0800 Subject: move app discovery to a dedicated module --- src/rcl_app_discovery.erl | 223 ++++++++++++++++++++++++++++++++++++++++++++++ src/rcl_prv_discover.erl | 189 +++------------------------------------ 2 files changed, 234 insertions(+), 178 deletions(-) create mode 100644 src/rcl_app_discovery.erl diff --git a/src/rcl_app_discovery.erl b/src/rcl_app_discovery.erl new file mode 100644 index 0000000..20df1cf --- /dev/null +++ b/src/rcl_app_discovery.erl @@ -0,0 +1,223 @@ +%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*- +%%% Copyright 2012 Erlware, LLC. All Rights Reserved. +%%% +%%% This file is provided to you under the Apache License, +%%% Version 2.0 (the "License"); you may not use this file +%%% except in compliance with the License. You may obtain +%%% a copy of the License at +%%% +%%% http://www.apache.org/licenses/LICENSE-2.0 +%%% +%%% Unless required by applicable law or agreed to in writing, +%%% software distributed under the License is distributed on an +%%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%%% KIND, either express or implied. See the License for the +%%% specific language governing permissions and limitations +%%% under the License. +%%%--------------------------------------------------------------------------- +%%% @author Eric Merritt +%%% @copyright (C) 2012 Erlware, LLC. +%%% +%%% @doc This provider uses the lib_dir setting of the state. It searches the +%%% Lib Dirs looking for all OTP Applications that are available. When it finds +%%% those OTP Applications it loads the information about them and adds them to +%%% the state of available apps. This implements the rcl_provider behaviour. +-module(rcl_app_discovery). + +-export([do/2, + format_error/1]). + +-include_lib("relcool/include/relcool.hrl"). + +%%============================================================================ +%% API +%%============================================================================ + +%% @doc recursively dig down into the library directories specified in the state +%% looking for OTP Applications +-spec do(rcl_state:t(), [filename:name()]) -> {ok, [rcl_app_info:t()]} | relcool:error(). +do(State, LibDirs) -> + rcl_log:info(rcl_state:log(State), + fun() -> + ["Resolving OTP Applications from directories:\n", + [[rcl_util:indent(1), LibDir, "\n"] || LibDir <- LibDirs]] + end), + resolve_app_metadata(State, LibDirs). + +-spec format_error([ErrorDetail::term()]) -> iolist(). +format_error(ErrorDetails) + when erlang:is_list(ErrorDetails) -> + [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. + +%%%=================================================================== +%%% Internal Functions +%%%=================================================================== +resolve_app_metadata(State, LibDirs) -> + AppMeta0 = lists:flatten(ec_plists:map(fun(LibDir) -> + discover_dir([], LibDir) + end, LibDirs)), + AppMeta1 = setup_overrides(State, AppMeta0), + + Errors = [case El of + {error, Ret} -> Ret; + _ -> El + end + || El <- AppMeta1, + case El of + {error, _} -> + true; + _ -> + false + end], + + case Errors of + [] -> + AppMeta2 = lists:flatten(AppMeta1), + rcl_log:debug(rcl_state:log(State), + fun() -> + ["Resolved the following OTP Applications from the system: \n", + [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta2]] + end), + {ok, AppMeta2}; + _ -> + ?RCL_ERROR(Errors) + end. + +app_name({error, _}) -> + undefined; +app_name(AppMeta) -> + rcl_app_info:name(AppMeta). + +setup_overrides(State, AppMetas0) -> + Overrides = rcl_state:overrides(State), + AppMetas1 = [AppMeta || AppMeta <- AppMetas0, + not lists:keymember(app_name(AppMeta), 1, Overrides)], + [case is_valid_otp_app(filename:join([FileName, "ebin", + erlang:atom_to_list(AppName) ++ ".app"])) of + [] -> + {error, {invalid_override, AppName, FileName}}; + Error = {error, _} -> + Error; + App -> + rcl_app_info:link(App, true) + end || {AppName, FileName} <- Overrides] ++ AppMetas1. + + + +-spec format_detail(ErrorDetail::term()) -> iolist(). +format_detail({error, {invalid_override, AppName, FileName}}) -> + io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?", + [AppName, FileName]); +format_detail({accessing, File, eaccess}) -> + io_lib:format("permission denied accessing file ~s", [File]); +format_detail({accessing, File, Type}) -> + io_lib:format("error (~p) accessing file ~s", [Type, File]); +format_detail({no_beam_files, EbinDir}) -> + io_lib:format("no beam files found in directory ~s", [EbinDir]); +format_detail({not_a_directory, EbinDir}) -> + io_lib:format("~s is not a directory when it should be a directory", [EbinDir]); +format_detail({unable_to_load_app, AppDir, _}) -> + io_lib:format("Unable to load the application metadata from ~s", [AppDir]); +format_detail({invalid_app_file, File}) -> + io_lib:format("Application metadata file exists but is malformed: ~s", + [File]); +format_detail({unversioned_app, AppDir, _AppName}) -> + io_lib:format("Application metadata exists but version is not available: ~s", + [AppDir]); +format_detail({app_info_error, {Module, Detail}}) -> + Module:format_error(Detail). + +-spec discover_dir([file:name()], + file:name()) -> + [rcl_app_info:t() | {error, Reason::term()}] | + rcl_app_info:t() | {error, Reason::term()}. +discover_dir(IgnoreDirs, File) -> + case (not lists:member(File, IgnoreDirs)) + andalso filelib:is_dir(File) of + true -> + case file:list_dir(File) of + {error, Reason} -> + {error, {accessing, File, Reason}}; + {ok, List} -> + ec_plists:map(fun(LibDir) -> + discover_dir(IgnoreDirs, LibDir) + end, [filename:join([File, Dir]) || Dir <- List]) + end; + false -> + is_valid_otp_app(File) + end. + +-spec is_valid_otp_app(file:name()) -> rcl_app_info:t() | {error, Reason::term()} | []. +is_valid_otp_app(File) -> + %% Is this an ebin dir? + EbinDir = filename:dirname(File), + case filename:basename(EbinDir) of + "ebin" -> + case lists:suffix(".app", File) of + true -> + has_at_least_one_beam(EbinDir, File); + false -> + [] + end; + _ -> + [] + end. + +-spec has_at_least_one_beam(file:name(), file:filename()) -> + rcl_app_info:t() | {error, Reason::term()}. +has_at_least_one_beam(EbinDir, File) -> + case file:list_dir(EbinDir) of + {ok, List} -> + case lists:any(fun(NFile) -> lists:suffix(".beam", NFile) end, List) of + true -> + gather_application_info(EbinDir, File); + false -> + {error, {no_beam_files, EbinDir}} + end; + _ -> + {error, {not_a_directory, EbinDir}} + end. + +-spec gather_application_info(file:name(), file:filename()) -> + rcl_app_info:t() | {error, Reason::term()}. +gather_application_info(EbinDir, File) -> + AppDir = filename:dirname(EbinDir), + case file:consult(File) of + {ok, [{application, AppName, AppDetail}]} -> + get_vsn(AppDir, AppName, AppDetail); + {error, Reason} -> + {error, {unable_to_load_app, AppDir, Reason}}; + _ -> + {error, {invalid_app_file, File}} + end. + +-spec get_vsn(file:name(), atom(), proplists:proplist()) -> + rcl_app_info:t() | {error, Reason::term()}. +get_vsn(AppDir, AppName, AppDetail) -> + case proplists:get_value(vsn, AppDetail) of + undefined -> + {error, {unversioned_app, AppDir, AppName}}; + AppVsn -> + case get_deps(AppDir, AppName, AppVsn, AppDetail) of + {ok, App} -> + App; + {error, Detail} -> + {error, {app_info_error, Detail}} + end + end. + +-spec get_deps(file:name(), atom(), string(), proplists:proplist()) -> + {ok, rcl_app_info:t()} | {error, Reason::term()}. +get_deps(AppDir, AppName, AppVsn, AppDetail) -> + ActiveApps = proplists:get_value(applications, AppDetail, []), + LibraryApps = proplists:get_value(included_applications, AppDetail, []), + rcl_app_info:new(AppName, AppVsn, AppDir, ActiveApps, LibraryApps). + +%%%=================================================================== +%%% Test Functions +%%%=================================================================== + +-ifndef(NOTEST). +-include_lib("eunit/include/eunit.hrl"). + +-endif. diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl index 862bd6e..1ffbece 100644 --- a/src/rcl_prv_discover.erl +++ b/src/rcl_prv_discover.erl @@ -43,71 +43,22 @@ init(State) -> -spec do(rcl_state:t()) -> {ok, rcl_state:t()} | relcool:error(). do(State) -> LibDirs = get_lib_dirs(State), - rcl_log:info(rcl_state:log(State), - fun() -> - ["Resolving OTP Applications from directories:\n", - [[rcl_util:indent(1), LibDir, "\n"] || LibDir <- LibDirs]] - end), - resolve_app_metadata(State, LibDirs). + case rcl_app_discovery:do(State, LibDirs) of + {ok, AppMeta} -> + {ok, rcl_state:available_apps(State, AppMeta)}; + Error -> + Error + end. --spec format_error([ErrorDetail::term()]) -> iolist(). -format_error(ErrorDetails) - when erlang:is_list(ErrorDetails) -> - [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. +%% @doc this is here to comply with the signature. However, we do not actually +%% produce any errors and so simply return an empty string. +-spec format_error(any()) -> iolist(). +format_error(_) -> + "". %%%=================================================================== %%% Internal Functions %%%=================================================================== -resolve_app_metadata(State, LibDirs) -> - AppMeta0 = lists:flatten(ec_plists:map(fun(LibDir) -> - discover_dir([], LibDir) - end, LibDirs)), - AppMeta1 = setup_overrides(State, AppMeta0), - - Errors = [case El of - {error, Ret} -> Ret; - _ -> El - end - || El <- AppMeta1, - case El of - {error, _} -> - true; - _ -> - false - end], - - case Errors of - [] -> - AppMeta2 = lists:flatten(AppMeta1), - rcl_log:debug(rcl_state:log(State), - fun() -> - ["Resolved the following OTP Applications from the system: \n", - [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta2]] - end), - {ok, rcl_state:available_apps(State, AppMeta2)}; - _ -> - ?RCL_ERROR(Errors) - end. - -app_name({error, _}) -> - undefined; -app_name(AppMeta) -> - rcl_app_info:name(AppMeta). - -setup_overrides(State, AppMetas0) -> - Overrides = rcl_state:overrides(State), - AppMetas1 = [AppMeta || AppMeta <- AppMetas0, - not lists:keymember(app_name(AppMeta), 1, Overrides)], - [case is_valid_otp_app(filename:join([FileName, "ebin", - erlang:atom_to_list(AppName) ++ ".app"])) of - [] -> - {error, {invalid_override, AppName, FileName}}; - Error = {error, _} -> - Error; - App -> - rcl_app_info:link(App, true) - end || {AppName, FileName} <- Overrides] ++ AppMetas1. - get_lib_dirs(State) -> LibDirs0 = rcl_state:lib_dirs(State), case rcl_state:get(State, disable_default_libs, false) of @@ -140,121 +91,3 @@ add_system_lib_dir(State, LibDirs) -> LibDirs end end. - --spec format_detail(ErrorDetail::term()) -> iolist(). -format_detail({error, {invalid_override, AppName, FileName}}) -> - io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?", - [AppName, FileName]); -format_detail({accessing, File, eaccess}) -> - io_lib:format("permission denied accessing file ~s", [File]); -format_detail({accessing, File, Type}) -> - io_lib:format("error (~p) accessing file ~s", [Type, File]); -format_detail({no_beam_files, EbinDir}) -> - io_lib:format("no beam files found in directory ~s", [EbinDir]); -format_detail({not_a_directory, EbinDir}) -> - io_lib:format("~s is not a directory when it should be a directory", [EbinDir]); -format_detail({unable_to_load_app, AppDir, _}) -> - io_lib:format("Unable to load the application metadata from ~s", [AppDir]); -format_detail({invalid_app_file, File}) -> - io_lib:format("Application metadata file exists but is malformed: ~s", - [File]); -format_detail({unversioned_app, AppDir, _AppName}) -> - io_lib:format("Application metadata exists but version is not available: ~s", - [AppDir]); -format_detail({app_info_error, {Module, Detail}}) -> - Module:format_error(Detail). - --spec discover_dir([file:name()], - file:name()) -> - [rcl_app_info:t() | {error, Reason::term()}] | - rcl_app_info:t() | {error, Reason::term()}. -discover_dir(IgnoreDirs, File) -> - case (not lists:member(File, IgnoreDirs)) - andalso filelib:is_dir(File) of - true -> - case file:list_dir(File) of - {error, Reason} -> - {error, {accessing, File, Reason}}; - {ok, List} -> - ec_plists:map(fun(LibDir) -> - discover_dir(IgnoreDirs, LibDir) - end, [filename:join([File, Dir]) || Dir <- List]) - end; - false -> - is_valid_otp_app(File) - end. - --spec is_valid_otp_app(file:name()) -> rcl_app_info:t() | {error, Reason::term()} | []. -is_valid_otp_app(File) -> - %% Is this an ebin dir? - EbinDir = filename:dirname(File), - case filename:basename(EbinDir) of - "ebin" -> - case lists:suffix(".app", File) of - true -> - has_at_least_one_beam(EbinDir, File); - false -> - [] - end; - _ -> - [] - end. - --spec has_at_least_one_beam(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. -has_at_least_one_beam(EbinDir, File) -> - case file:list_dir(EbinDir) of - {ok, List} -> - case lists:any(fun(NFile) -> lists:suffix(".beam", NFile) end, List) of - true -> - gather_application_info(EbinDir, File); - false -> - {error, {no_beam_files, EbinDir}} - end; - _ -> - {error, {not_a_directory, EbinDir}} - end. - --spec gather_application_info(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. -gather_application_info(EbinDir, File) -> - AppDir = filename:dirname(EbinDir), - case file:consult(File) of - {ok, [{application, AppName, AppDetail}]} -> - get_vsn(AppDir, AppName, AppDetail); - {error, Reason} -> - {error, {unable_to_load_app, AppDir, Reason}}; - _ -> - {error, {invalid_app_file, File}} - end. - --spec get_vsn(file:name(), atom(), proplists:proplist()) -> - rcl_app_info:t() | {error, Reason::term()}. -get_vsn(AppDir, AppName, AppDetail) -> - case proplists:get_value(vsn, AppDetail) of - undefined -> - {error, {unversioned_app, AppDir, AppName}}; - AppVsn -> - case get_deps(AppDir, AppName, AppVsn, AppDetail) of - {ok, App} -> - App; - {error, Detail} -> - {error, {app_info_error, Detail}} - end - end. - --spec get_deps(file:name(), atom(), string(), proplists:proplist()) -> - {ok, rcl_app_info:t()} | {error, Reason::term()}. -get_deps(AppDir, AppName, AppVsn, AppDetail) -> - ActiveApps = proplists:get_value(applications, AppDetail, []), - LibraryApps = proplists:get_value(included_applications, AppDetail, []), - rcl_app_info:new(AppName, AppVsn, AppDir, ActiveApps, LibraryApps). - -%%%=================================================================== -%%% Test Functions -%%%=================================================================== - --ifndef(NOTEST). --include_lib("eunit/include/eunit.hrl"). - --endif. -- cgit v1.2.3 From a7e6f77f4450cb5bef8c7da594449ff053123876 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 31 Jan 2013 15:25:16 -0800 Subject: fix bugs in constraint parsing for rcl_release --- src/rcl_release.erl | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/rcl_release.erl b/src/rcl_release.erl index 560a555..8cd3033 100644 --- a/src/rcl_release.erl +++ b/src/rcl_release.erl @@ -72,7 +72,7 @@ {app_name(), app_vsn(), app_type() | incl_apps()} | {app_name(), app_vsn(), app_type(), incl_apps()}. --type application_constraint() :: rcl_depsolver:constraint() | string() | binary(). +-type application_constraint() :: rcl_depsolver:raw_constraint() | string() | binary(). -type application_goal() :: application_constraint() | {application_constraint(), app_type() | incl_apps()} | {application_constraint(), app_type(), incl_apps() | none}. @@ -189,7 +189,7 @@ format_error({topo_error, E}) -> rcl_topo:format_error(E); format_error({failed_to_parse, Con}) -> io_lib:format("Failed to parse constraint ~p", [Con]); -format_error({invalid_constraint, Con}) -> +format_error({invalid_constraint, _, Con}) -> io_lib:format("Invalid constraint specified ~p", [Con]); format_error({not_realized, Name, Vsn}) -> io_lib:format("Unable to produce metadata release: ~p-~s has not been realized", @@ -209,10 +209,10 @@ realize_erts(Rel) -> process_specs(Rel=#release_t{annotations=Annots, goals=Goals}, World) -> ActiveApps = lists:flatten([rcl_app_info:active_deps(El) || El <- World] ++ - [case get_app_name(Goal) of - {error, _} -> []; - G -> G - end || Goal <- Goals]), + [case get_app_name(Goal) of + {error, _} -> []; + G -> G + end || Goal <- Goals]), LibraryApps = lists:flatten([rcl_app_info:library_deps(El) || El <- World]), Specs = [create_app_spec(Annots, App, ActiveApps, LibraryApps) || App <- World], {ok, Rel#release_t{annotations=Annots, @@ -308,7 +308,7 @@ parse_goal0(Constraint0, {ok, Release}) -> parse_goal0(_, E = {error, _}) -> E; parse_goal0(Constraint, _) -> - ?RCL_ERROR({invalid_constraint, Constraint}). + ?RCL_ERROR({invalid_constraint, 1, Constraint}). parse_goal1(Release = #release_t{annotations=Annots, goals=Goals}, Constraint, NewAnnots) -> @@ -331,25 +331,45 @@ parse_constraint(Constraint0) {ok, Constraint1} -> {ok, Constraint1} end; -parse_constraint(Constraint) - when erlang:is_tuple(Constraint); - erlang:is_atom(Constraint) -> - case rcl_depsolver:is_valid_constraint(Constraint) of +parse_constraint(Constraint0) + when erlang:is_tuple(Constraint0); + erlang:is_atom(Constraint0) -> + Constraint1 = parse_version(Constraint0), + case rcl_depsolver:is_valid_constraint(Constraint1) of false -> - ?RCL_ERROR({invalid_constraint, Constraint}); + ?RCL_ERROR({invalid_constraint, 2, Constraint0}); true -> - {ok, Constraint} + {ok, Constraint1} end; parse_constraint(Constraint) -> - ?RCL_ERROR({invalid_constraint, Constraint}). + ?RCL_ERROR({invalid_constraint, 3, Constraint}). --spec get_app_name(rcl_depsolver:constraint()) -> +-spec get_app_name(rcl_depsolver:raw_constraint()) -> AppName::atom() | relcool:error(). get_app_name(AppName) when erlang:is_atom(AppName) -> AppName; +get_app_name({AppName, _}) when erlang:is_atom(AppName) -> + AppName; get_app_name({AppName, _, _}) when erlang:is_atom(AppName) -> AppName; get_app_name({AppName, _, _, _}) when erlang:is_atom(AppName) -> AppName; get_app_name(V) -> - ?RCL_ERROR({invalid_constraint, V}). + ?RCL_ERROR({invalid_constraint, 4, V}). + +-spec parse_version(rcl_depsolver:raw_constraint()) -> + rcl_depsolver:constraint(). +parse_version({AppName, Version}) + when erlang:is_binary(Version); + erlang:is_list(Version) -> + {AppName, rcl_depsolver:parse_version(Version)}; +parse_version({AppName, Version, Constraint}) + when erlang:is_binary(Version); + erlang:is_list(Version) -> + {AppName, rcl_depsolver:parse_version(Version), Constraint}; +parse_version({AppName, Version, Constraint0, Constraint1}) + when erlang:is_binary(Version); + erlang:is_list(Version) -> + {AppName, rcl_depsolver:parse_version(Version), Constraint1, Constraint0}; +parse_version(Constraint) -> + Constraint. -- cgit v1.2.3 From 367d9fdf1d577dd67990cdd4ad4480ba1130bc92 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 31 Jan 2013 15:25:59 -0800 Subject: fix release processing bugs in rcl_prv_release --- src/rcl_prv_release.erl | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/rcl_prv_release.erl b/src/rcl_prv_release.erl index a98048f..4574a0e 100644 --- a/src/rcl_prv_release.erl +++ b/src/rcl_prv_release.erl @@ -101,36 +101,25 @@ find_default_release(State, DepGraph) -> end. resolve_default_release(State0, DepGraph) -> - %% Here we will just get the lastest version and run that. + %% Here we will just get the highest versioned release and run that. case lists:sort(fun release_sort/2, ec_dictionary:to_list(rcl_state:releases(State0))) of - All = [{{RelName, RelVsn}, _} | _] -> + [{{RelName, RelVsn}, _} | _] -> State1 = rcl_state:default_release(State0, RelName, RelVsn), - lists:foldl(fun({{RN, RV}, _}, {ok, State2}) -> - solve_release(State2, - DepGraph, RN, RV); - (_, E) -> - E - end, {ok, State1}, All); + solve_release(State1, DepGraph, RelName, RelVsn); [] -> ?RCL_ERROR(no_releases_in_system) end. - resolve_default_version(State0, DepGraph, RelName) -> %% Here we will just get the lastest version and run that. AllReleases = ec_dictionary:to_list(rcl_state:releases(State0)), SpecificReleases = [Rel || Rel={{PossibleRelName, _}, _} <- AllReleases, PossibleRelName =:= RelName], case lists:sort(fun release_sort/2, SpecificReleases) of - All = [{{RelName, RelVsn}, _} | _] -> + [{{RelName, RelVsn}, _} | _] -> State1 = rcl_state:default_release(State0, RelName, RelVsn), - lists:foldl(fun({RN, RV}, {ok, State2}) -> - solve_release(State2, - DepGraph, RN, RV); - (_, E) -> - E - end, {ok, State1}, All); + solve_release(State1, DepGraph, RelName, RelVsn); [] -> ?RCL_ERROR({no_releases_for, RelName}) end. @@ -150,7 +139,11 @@ release_sort({{RelNameA, RelVsnA}, _}, {{RelNameB, RelVsnB}, _}) -> ec_semver:lte(RelVsnA, RelVsnB). solve_release(State0, DepGraph, RelName, RelVsn) -> + rcl_log:debug(rcl_state:log(State0), + "Solving Release ~p-~s~n", + [RelName, RelVsn]), try + io:format("Solving ~p ~p", [RelName, RelVsn]), Release = rcl_state:get_release(State0, RelName, RelVsn), Goals = rcl_release:goals(Release), case Goals of @@ -176,7 +169,7 @@ set_resolved(State, Release0, Pkgs) -> "Resolved ~p-~s~n", [rcl_release:name(Release1), rcl_release:vsn(Release1)]), - rcl_log:info(rcl_state:log(State), + rcl_log:debug(rcl_state:log(State), fun() -> rcl_release:format(1, Release1) end), -- cgit v1.2.3 From 8b43038c86a2b4d7329434dab85dd5aac17fc9fe Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 31 Jan 2013 15:26:30 -0800 Subject: support release discovery in relcool along with app discovery --- src/rcl_prv_discover.erl | 17 +++-- src/rcl_rel_discovery.erl | 166 ++++++++++++++++++++++++++++++++++++++++++++ src/rcl_release.erl | 11 ++- test/rclt_release_SUITE.erl | 74 ++++++++++++++++++-- 4 files changed, 257 insertions(+), 11 deletions(-) create mode 100644 src/rcl_rel_discovery.erl diff --git a/src/rcl_prv_discover.erl b/src/rcl_prv_discover.erl index 1ffbece..c5a625d 100644 --- a/src/rcl_prv_discover.erl +++ b/src/rcl_prv_discover.erl @@ -41,11 +41,20 @@ init(State) -> %% @doc recursively dig down into the library directories specified in the state %% looking for OTP Applications -spec do(rcl_state:t()) -> {ok, rcl_state:t()} | relcool:error(). -do(State) -> - LibDirs = get_lib_dirs(State), - case rcl_app_discovery:do(State, LibDirs) of +do(State0) -> + LibDirs = get_lib_dirs(State0), + case rcl_app_discovery:do(State0, LibDirs) of {ok, AppMeta} -> - {ok, rcl_state:available_apps(State, AppMeta)}; + case rcl_rel_discovery:do(State0, LibDirs, AppMeta) of + {ok, Releases} -> + State1 = rcl_state:available_apps(State0, AppMeta), + State3 = lists:foldl(fun(Rel, State2) -> + rcl_state:add_release(State2, Rel) + end, State1, Releases), + {ok, State3}; + Error -> + Error + end; Error -> Error end. diff --git a/src/rcl_rel_discovery.erl b/src/rcl_rel_discovery.erl new file mode 100644 index 0000000..0fbcf23 --- /dev/null +++ b/src/rcl_rel_discovery.erl @@ -0,0 +1,166 @@ +%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*- +%%% Copyright 2012 Erlware, LLC. All Rights Reserved. +%%% +%%% This file is provided to you under the Apache License, +%%% Version 2.0 (the "License"); you may not use this file +%%% except in compliance with the License. You may obtain +%%% a copy of the License at +%%% +%%% http://www.apache.org/licenses/LICENSE-2.0 +%%% +%%% Unless required by applicable law or agreed to in writing, +%%% software distributed under the License is distributed on an +%%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%%% KIND, either express or implied. See the License for the +%%% specific language governing permissions and limitations +%%% under the License. +%%%--------------------------------------------------------------------------- +%%% @author Eric Merritt +%%% @copyright (C) 2012 Erlware, LLC. +%%% +%%% @doc This provider uses the lib_dir setting of the state. It searches the +%%% Lib Dirs looking for all OTP Applications that are available. When it finds +%%% those OTP Applications it loads the information about them and adds them to +%%% the state of available apps. This implements the rcl_provider behaviour. +-module(rcl_rel_discovery). + +-export([do/3, + format_error/1]). + +-include_lib("relcool/include/relcool.hrl"). + +%%============================================================================ +%% API +%%============================================================================ + +%% @doc recursively dig down into the library directories specified in the state +%% looking for OTP Applications +-spec do(rcl_state:t(), [filename:name()], [rcl_app_info:t()]) -> + {ok, [rcl_release:t()]} | relcool:error(). +do(State, LibDirs, AppMeta) -> + rcl_log:info(rcl_state:log(State), + fun() -> + ["Resolving available releases from directories:\n", + [[rcl_util:indent(1), LibDir, "\n"] || LibDir <- LibDirs]] + end), + resolve_rel_metadata(State, LibDirs, AppMeta). + +-spec format_error([ErrorDetail::term()]) -> iolist(). +format_error(ErrorDetails) + when erlang:is_list(ErrorDetails) -> + [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. + +%%%=================================================================== +%%% Internal Functions +%%%=================================================================== +resolve_rel_metadata(State, LibDirs, AppMeta) -> + ReleaseMeta0 = + lists:flatten(ec_plists:map(fun(LibDir) -> + discover_dir([], LibDir, AppMeta) + end, LibDirs)), + Errors = [case El of + {error, Ret} -> Ret; + _ -> El + end + || El <- ReleaseMeta0, + case El of + {error, _} -> + true; + _ -> + false + end], + + case Errors of + [] -> + ReleaseMeta1 = lists:flatten(ReleaseMeta0), + rcl_log:debug(rcl_state:log(State), + fun() -> + ["Resolved the following OTP Releases from the system: \n", + [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta1]] + end), + {ok, ReleaseMeta1}; + _ -> + ?RCL_ERROR(Errors) + end. + +-spec format_detail(ErrorDetail::term()) -> iolist(). +format_detail({accessing, File, eaccess}) -> + io_lib:format("permission denied accessing file ~s", [File]); +format_detail({accessing, File, Type}) -> + io_lib:format("error (~p) accessing file ~s", [Type, File]). + +-spec discover_dir([file:name()], file:name(), [rcl_app_info:t()]) -> + [rcl_release:t() | {error, Reason::term()}] + | rcl_release:t() + | {error, Reason::term()}. +discover_dir(IgnoreDirs, File, AppMeta) -> + case (not lists:member(File, IgnoreDirs)) + andalso filelib:is_dir(File) of + true -> + case file:list_dir(File) of + {error, Reason} -> + {error, {accessing, File, Reason}}; + {ok, List} -> + ec_plists:map(fun(LibDir) -> + discover_dir(IgnoreDirs, LibDir, AppMeta) + end, + [filename:join([File, Dir]) || Dir <- List]) + end; + false -> + is_valid_release(File, AppMeta) + end. + +-spec is_valid_release(file:name(), + [rcl_app_info:t()]) -> + rcl_release:t() + | {error, Reason::term()} + | []. +is_valid_release(File, AppMeta) -> + case lists:suffix(".rel", File) of + true -> + resolve_release(File, AppMeta); + false -> + [] + end. + +resolve_release(RelFile, AppMeta) -> + case file:consult(RelFile) of + {ok, [{release, {RelName, RelVsn}, + {erts, ErtsVsn}, + Apps}]} -> + build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta); + {ok, InvalidRelease} -> + ?RCL_ERROR({invalid_release_information, InvalidRelease}); + {error, Reason} -> + ?RCL_ERROR({unable_to_read, RelFile, Reason}) + end. + +build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta) -> + Release = rcl_release:erts(rcl_release:new(RelName, RelVsn), + ErtsVsn), + resolve_apps(Apps, AppMeta, Release, []). + +resolve_apps([], _AppMeta, Release, Acc) -> + rcl_release:application_details(Release, Acc); +resolve_apps([AppInfo | Apps], AppMeta, Release, Acc) -> + AppName = erlang:element(1, AppInfo), + AppVsn = erlang:element(2, AppInfo), + case find_app(AppName, AppVsn, AppMeta) of + Error = {error, _} -> + Error; + ResolvedApp -> + resolve_apps(Apps, AppMeta, Release, [ResolvedApp | Acc]) + end. + +find_app(AppName, AppVsn, AppMeta) -> + case ec_lists:find(fun(App) -> + NAppName = rcl_app:name(App), + NAppVsn = rcl_app:version(App), + AppName == NAppName andalso + AppVsn == NAppVsn + end, AppMeta) of + {ok, Head} -> + Head; + error -> + ?RCL_ERROR({could_not_find, {AppName, AppVsn}}) + end. diff --git a/src/rcl_release.erl b/src/rcl_release.erl index 8cd3033..ea20f1f 100644 --- a/src/rcl_release.erl +++ b/src/rcl_release.erl @@ -32,6 +32,7 @@ realize/3, applications/1, application_details/1, + application_details/2, realized/1, metadata/1, format/1, @@ -134,12 +135,18 @@ applications(#release_t{applications=Apps}) -> Apps. %% @doc this gives the rcl_app_info objects representing the applications in -%% this release. These can only be populated by the 'realize' call in this -%% module. +%% this release. These should only be populated by the 'realize' call in this +%% module or by reading an existing rel file. -spec application_details(t()) -> [rcl_app_info:t()]. application_details(#release_t{app_detail=App}) -> App. +%% @doc this is only expected to be called by a process building a new release +%% from an existing rel file. +-spec application_details(t(), [rcl_app_info:t()]) -> t(). +application_details(Release, AppDetail) -> + Release#release_t{app_detail=AppDetail}. + -spec realized(t()) -> boolean(). realized(#release_t{realized=Realized}) -> Realized. diff --git a/test/rclt_release_SUITE.erl b/test/rclt_release_SUITE.erl index 95aa385..1104303 100644 --- a/test/rclt_release_SUITE.erl +++ b/test/rclt_release_SUITE.erl @@ -32,7 +32,8 @@ overlay_release/1, make_goalless_release/1, make_depfree_release/1, - make_invalid_config_release/1]). + make_invalid_config_release/1, + make_relup_release/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -60,7 +61,7 @@ all() -> [make_release, make_scriptless_release, make_overridden_release, make_implicit_config_release, make_rerun_overridden_release, overlay_release, make_goalless_release, make_depfree_release, - make_invalid_config_release]. + make_invalid_config_release, make_relup_release]. make_release(Config) -> LibDir1 = proplists:get_value(lib1, Config), @@ -178,7 +179,7 @@ make_overridden_release(Config) -> || _ <- lists:seq(1, 100)]], OverrideApp = create_random_name("override_app"), OverrideVsn = create_random_vsn(), - OverrideAppDir = filename:join(OverrideDir1, OverrideApp), + OverrideAppDir = filename:join(OverrideDir1, OverrideApp ++ "-" ++ OverrideVsn), OverrideAppName = erlang:list_to_atom(OverrideApp), create_app(LibDir1, "goal_app_1", "0.0.1", [stdlib,kernel,non_goal_1], []), @@ -268,7 +269,8 @@ make_rerun_overridden_release(Config) -> || _ <- lists:seq(1, 100)]], OverrideApp = create_random_name("override_app"), OverrideVsn = create_random_vsn(), - OverrideAppDir = filename:join(OverrideDir1, OverrideApp), + OverrideAppDir = filename:join(OverrideDir1, OverrideApp ++ "-" + ++ OverrideVsn), OverrideAppName = erlang:list_to_atom(OverrideApp), create_app(LibDir1, "goal_app_1", "0.0.1", [stdlib,kernel,non_goal_1], []), @@ -500,11 +502,73 @@ make_depfree_release(Config) -> ?assert(lists:keymember(stdlib, 1, AppSpecs)), ?assert(lists:keymember(kernel, 1, AppSpecs)). +make_relup_release(Config) -> + LibDir1 = proplists:get_value(lib1, Config), + [(fun({Name, Vsn}) -> + create_app(LibDir1, Name, Vsn, [kernel, stdlib], []) + end)(App) + || + App <- + [{create_random_name("lib_app1_"), create_random_vsn()} + || _ <- lists:seq(1, 100)]], + + create_app(LibDir1, "goal_app_1", "0.0.1", [stdlib,kernel,non_goal_1], []), + create_app(LibDir1, "goal_app_1", "0.0.2", [stdlib,kernel,non_goal_1], []), + create_app(LibDir1, "goal_app_1", "0.0.3", [stdlib,kernel,non_goal_1], []), + create_app(LibDir1, "lib_dep_1", "0.0.1", [stdlib,kernel], []), + create_app(LibDir1, "goal_app_2", "0.0.1", [stdlib,kernel,goal_app_1,non_goal_2], []), + create_app(LibDir1, "goal_app_2", "0.0.2", [stdlib,kernel,goal_app_1,non_goal_2], []), + create_app(LibDir1, "goal_app_2", "0.0.3", [stdlib,kernel,goal_app_1,non_goal_2], []), + create_app(LibDir1, "non_goal_1", "0.0.1", [stdlib,kernel], [lib_dep_1]), + create_app(LibDir1, "non_goal_2", "0.0.1", [stdlib,kernel], []), + + ConfigFile = filename:join([LibDir1, "relcool.config"]), + write_config(ConfigFile, + [{release, {foo, "0.0.1"}, + [{goal_app_1, "0.0.1"}, + {goal_app_2, "0.0.1"}]}, + {release, {foo, "0.0.2"}, + [{goal_app_1, "0.0.2"}, + {goal_app_2, "0.0.2"}]}, + {release, {foo, "0.0.3"}, + [{goal_app_1, "0.0.3"}, + {goal_app_2, "0.0.3"}]}]), + OutputDir = filename:join([proplists:get_value(data_dir, Config), + create_random_name("relcool-output")]), + {ok, _} = relcool:do(foo, "0.0.1", [], [LibDir1], 2, + OutputDir, ConfigFile), + {ok, _} = relcool:do(foo, "0.0.2", [], [LibDir1], 2, + OutputDir, ConfigFile), + {ok, State} = relcool:do(foo, "0.0.3", [], [LibDir1], 2, + OutputDir, ConfigFile), + + %% we should have one 'resolved' release and three discovered releases. + ?assertMatch([{foo, "0.0.1"}, + {foo, "0.0.2"}, + {foo, "0.0.3"}], + lists:sort(ec_dictionary:keys(rcl_state:releases(State)))), + Release = ec_dictionary:get({foo, "0.0.3"}, rcl_state:releases(State)), + ?assert(rcl_release:realized(Release)), + ?assert(not rcl_release:realized(ec_dictionary:get({foo, "0.0.2"}, + rcl_state:releases(State)))), + ?assert(not rcl_release:realized(ec_dictionary:get({foo, "0.0.1"}, + rcl_state:releases(State)))), + ?assertMatch(foo, rcl_release:name(Release)), + ?assertMatch("0.0.3", rcl_release:vsn(Release)), + AppSpecs = rcl_release:applications(Release), + ?assert(lists:keymember(stdlib, 1, AppSpecs)), + ?assert(lists:keymember(kernel, 1, AppSpecs)), + ?assert(lists:member({non_goal_1, "0.0.1"}, AppSpecs)), + ?assert(lists:member({non_goal_2, "0.0.1"}, AppSpecs)), + ?assert(lists:member({goal_app_1, "0.0.3"}, AppSpecs)), + ?assert(lists:member({goal_app_2, "0.0.3"}, AppSpecs)), + ?assert(lists:member({lib_dep_1, "0.0.1", load}, AppSpecs)). + %%%=================================================================== %%% Helper Functions %%%=================================================================== create_app(Dir, Name, Vsn, Deps, LibDeps) -> - AppDir = filename:join([Dir, Name]), + AppDir = filename:join([Dir, Name ++ "-" ++ Vsn]), write_app_file(AppDir, Name, Vsn, Deps, LibDeps), write_beam_file(AppDir, Name), rcl_app_info:new(erlang:list_to_atom(Name), Vsn, AppDir, -- cgit v1.2.3 From 882d4fbfe65dfc812d3de9d55647f9fb468be4b4 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 31 Jan 2013 15:26:40 -0800 Subject: fix formatting bigs in depsolver --- src/rcl_depsolver.erl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/rcl_depsolver.erl b/src/rcl_depsolver.erl index e74cf3b..0cf8c88 100644 --- a/src/rcl_depsolver.erl +++ b/src/rcl_depsolver.erl @@ -471,12 +471,13 @@ add_constraint(SrcPkg, SrcVsn, PkgsConstraints, PkgConstraint) -> {value, {PkgName, Constraints0}} -> Constraints0 end, - [{PkgName, [{PkgConstraint, {SrcPkg, SrcVsn}} | Constraints1]} | - lists:keydelete(PkgName, 1, PkgsConstraints)]. + [{PkgName, [{PkgConstraint, {SrcPkg, SrcVsn}} | Constraints1]} + | lists:keydelete(PkgName, 1, PkgsConstraints)]. %% @doc %% Extend the currently active constraints correctly for the given constraints. --spec extend_constraints(pkg_name(), vsn(), constraints(),constraints()) -> [{pkg_name(), constraints()}]. +-spec extend_constraints(pkg_name(), vsn(), constraints(),constraints()) -> + [{pkg_name(), constraints()}]. extend_constraints(SrcPkg, SrcVsn, ExistingConstraints0, NewConstraints) -> lists:foldl(fun (Constraint, ExistingConstraints1) -> add_constraint(SrcPkg, SrcVsn, ExistingConstraints1, Constraint) -- cgit v1.2.3