[erlang-questions] Escript archives and the load path

Håkan Mattsson hm@REDACTED
Wed Jul 11 13:34:39 CEST 2012


The code server can handle one "primary" archive. It is usually used to
load code from a running (active) escript but if the system has been
started in the classic way (without an escript) the primary archive
mechanism could be used to load code from a passive escript instead.
See the example below.

The "primary archive" mechanism is however only intended to be used in
the context of one (active) escript. That's why code:set_primary_archive/3
is undocumented and its parameters may look somewhat strange.

But if loading of code from passive escripts is something that is regarded
as useful enough, it would not be too hard to generalize the mechanism
(in erl_prim_loader and code_server) with one single "primary archive" to
handle multiple "escript archives" instead.

It's just to contribute to the community...

/Håkan

Eshell V5.9.1  (abort with ^G)
1> Mod = rebar.
rebar
2> code:wh
where_is_file/1  where_is_file/2  which/1
2> code:which(Mod).
non_existing
3> Escript = "rebar".
"rebar"
4> {ok,Props} = escript:extract(Escript,[]).
{ok,[{shebang,default},
     {comment,[]},
     {emu_args,"-noshell -noinput"},
     {archive,<<80,75,3,4,20,0,0,0,8,0,129,102,235,64,180,
                80,127,93,93,54,0,...>>}]}
5> Bin = proplists:get_value(archive,Props).
<<80,75,3,4,20,0,0,0,8,0,129,102,235,64,180,80,127,93,93,
  54,0,0,228,65,0,0,11,0,0,...>>
6> {ok, Info} = file:read_file_info(Escript).
{ok,{file_info,283118,regular,read_write,
               {{2012,7,11},{12,52,47}},
               {{2012,7,11},{12,52,33}},
               {{2012,7,11},{12,52,33}},
               33277,1,2056,0,4853045,1000,1000}}
7> code:set_primary_archive(Escript, Bin, Info).
ok
8> code:which(Mod).
"/home/hm/repos/rebar/rebar/rebar.beam"
9>

On Wed, Jul 11, 2012 at 10:35 AM, Siri Hansen <erlangsiri@REDACTED> wrote:

> Hi Geoff!
>
> There is no good way to do this really. Since rebar is an escript, the
> code server can not read it as a general archive. The only option you have
> would be to extract the archive part from it, write it to a file with .ez
> extension and then add code path to it, e.g.
>
> 1> {ok,Stuff} = escript:extract("rebar",[]).
> {ok,[{shebang,default},
>      {comment,[]},
>      {emu_args,"-noshell -noinput"},
>      {archive,<<80,75,3,4,20,0,0,0,8,0,15,84,235,64,105,178,
>                 62,157,168,18,0,...>>}]}
> 2>
> 2> Bin = proplists:get_value(archive,Stuff).
> <<80,75,3,4,20,0,0,0,8,0,15,84,235,64,105,178,62,157,168,
>   18,0,0,164,30,0,0,11,0,0,...>>
> 3> file:write_file("rebar.ez",Bin).
> ok
> 4> erl_prim_loader:list_dir("rebar.ez").
> {ok,["priv","rebar_xref.beam","rebar_utils.beam",
>      "rebar_upgrade.beam","rebar_templater.beam",
>      "rebar_subdirs.beam","rebar_require_vsn.beam",
>      "rebar_reltool.beam","rebar_rel_utils.beam",
>      "rebar_protobuffs_compiler.beam","rebar_port_compiler.beam",
>      "rebar_otp_app.beam","rebar_neotoma_compiler.beam",
>      "rebar_log.beam","rebar_lfe_compiler.beam",
>      "rebar_file_utils.beam","rebar_eunit.beam",
>      "rebar_escripter.beam","rebar_erlydtl_compiler.beam",
>      "rebar_erlc_compiler.beam","rebar_edoc.beam",
>      "rebar_deps.beam","rebar_ct.beam","rebar_core.beam",
>      "rebar_config.beam","rebar_cleaner.beam",
>      [...]|...]}
> 5> code:which(rebar).
> non_existing
> 6> code:add_path("rebar.ez").
> true
> 7> code:which(rebar).
> "rebar.ez/rebar.beam"
> 8>
>
>
> Regards
> /siri
>
> 2012/7/10 Geoff Cant <nem@REDACTED>
>
>>
>> Hi all, after looking for a while, I couldn't figure out how to tell
>> ERTS or the code server that /some/path/rebar was a valid place to load
>> modules from.
>>
>> Is there a magic function / command-line flag I can use to get the
>> contents of this escript archive into the code path?
>>
>> Cheers,
>> --
>> Geoff Cant
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120711/771d5d01/attachment.htm>


More information about the erlang-questions mailing list