You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.9 KiB

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% -------------------------------------------------------------------
  8. -module(rebar_fetch).
  9. -export([lock_source/3,
  10. download_source/3,
  11. needs_update/3]).
  12. -export([format_error/1]).
  13. -include("rebar.hrl").
  14. -include_lib("providers/include/providers.hrl").
  15. -spec lock_source(file:filename_all(), rebar_resource:resource(), rebar_state:t()) ->
  16. rebar_resource:resource() | {error, string()}.
  17. lock_source(AppDir, Source, State) ->
  18. Resources = rebar_state:resources(State),
  19. Module = get_resource_type(Source, Resources),
  20. Module:lock(AppDir, Source).
  21. -spec download_source(file:filename_all(), rebar_resource:resource(), rebar_state:t()) ->
  22. true | {error, any()}.
  23. download_source(AppDir, Source, State) ->
  24. try download_source_(AppDir, Source, State) of
  25. true ->
  26. true;
  27. Error ->
  28. throw(?PRV_ERROR(Error))
  29. catch
  30. C:T ->
  31. ?DEBUG("rebar_fetch exception ~p ~p ~p", [C, T, erlang:get_stacktrace()]),
  32. throw(?PRV_ERROR({fetch_fail, Source}))
  33. end.
  34. download_source_(AppDir, Source, State) ->
  35. Resources = rebar_state:resources(State),
  36. Module = get_resource_type(Source, Resources),
  37. TmpDir = ec_file:insecure_mkdtemp(),
  38. AppDir1 = ec_cnv:to_list(AppDir),
  39. case Module:download(TmpDir, Source, State) of
  40. {ok, _} ->
  41. ec_file:mkdir_p(AppDir1),
  42. code:del_path(filename:absname(filename:join(AppDir1, "ebin"))),
  43. ec_file:remove(filename:absname(AppDir1), [recursive]),
  44. ?DEBUG("Moving checkout ~p to ~p", [TmpDir, filename:absname(AppDir1)]),
  45. ok = rebar_file_utils:mv(TmpDir, filename:absname(AppDir1)),
  46. true;
  47. Error ->
  48. Error
  49. end.
  50. -spec needs_update(file:filename_all(), rebar_resource:resource(), rebar_state:t()) -> boolean() | {error, string()}.
  51. needs_update(AppDir, Source, State) ->
  52. Resources = rebar_state:resources(State),
  53. Module = get_resource_type(Source, Resources),
  54. try
  55. Module:needs_update(AppDir, Source)
  56. catch
  57. _:_ ->
  58. true
  59. end.
  60. format_error({bad_download, CachePath}) ->
  61. io_lib:format("Download of package does not match md5sum from server: ~s", [CachePath]);
  62. format_error({failed_extract, CachePath}) ->
  63. io_lib:format("Failed to extract package: ~s", [CachePath]);
  64. format_error({bad_etag, Source}) ->
  65. io_lib:format("MD5 Checksum comparison failed for: ~s", [Source]);
  66. format_error({fetch_fail, Name, Vsn}) ->
  67. io_lib:format("Failed to fetch and copy dep: ~s-~s", [Name, Vsn]);
  68. format_error({fetch_fail, Source}) ->
  69. io_lib:format("Failed to fetch and copy dep: ~p", [Source]);
  70. format_error({bad_checksum, File}) ->
  71. io_lib:format("Checksum mismatch against tarball in ~s", [File]);
  72. format_error({bad_registry_checksum, File}) ->
  73. io_lib:format("Checksum mismatch against registry in ~s", [File]).
  74. get_resource_type({Type, Location}, Resources) ->
  75. find_resource_module(Type, Location, Resources);
  76. get_resource_type({Type, Location, _}, Resources) ->
  77. find_resource_module(Type, Location, Resources);
  78. get_resource_type({Type, _, _, Location}, Resources) ->
  79. find_resource_module(Type, Location, Resources);
  80. get_resource_type(_, _) ->
  81. rebar_pkg_resource.
  82. find_resource_module(Type, Location, Resources) ->
  83. case lists:keyfind(Type, 1, Resources) of
  84. false ->
  85. case code:which(Type) of
  86. non_existing ->
  87. {error, io_lib:format("Cannot handle dependency ~s.~n"
  88. " No module for resource type ~p", [Location, Type])};
  89. _ ->
  90. Type
  91. end;
  92. {Type, Module} ->
  93. Module
  94. end.