/supported/versions/elib1_publish.erl

https://github.com/midnightskinhead/elib1 · Erlang · 143 lines · 118 code · 16 blank · 9 comment · 3 complexity · f72c8e93e6bc8960c81bb7315aa38a5c MD5 · raw file

  1. %% Copyright (c) 2006-2009 Joe Armstrong
  2. %% See MIT-LICENSE for licensing information.
  3. -module(elib1_publish).
  4. -compile(export_all).
  5. -import(lists, [reverse/1]).
  6. %% run elib1_rsa:make_sig("joe").
  7. %% publish:publish()
  8. %% c(nmap)
  9. %% publish:validate()
  10. compile() ->
  11. L = filelib:wildcard("*.erl"),
  12. [compile_file(I) || I <- L].
  13. test() ->
  14. compile_file("elib1_rsa.erl").
  15. compile_file(File) ->
  16. {ok, Mod, Bin} = compile:file(File, [no_error_module_mismatch,
  17. binary,{parse_transform, ?MODULE}]),
  18. OF = atom_to_list(Mod) ++ ".beam",
  19. io:format("creating:~p~n",[OF]),
  20. file:write_file(OF, Bin).
  21. parse_transform(X, Opts) ->
  22. elib1_misc:dump("foo", {X,Opts}),
  23. Y = xform(X),
  24. elib1_misc:dump("bar", Y),
  25. Y.
  26. xform({attribute,Ln,module,Mod}) ->
  27. {attribute,Ln,module,rename(Mod)};
  28. %% Is this a bug or a feature?
  29. xform({attribute,Ln,import,{Mod,L}}) ->
  30. {attribute,Ln,import, {rename(Mod), L}};
  31. xform({call,Ln,{remote,Ln1,{atom,Ln2, Mod }, Func}, Args }) ->
  32. {call, Ln, {remote,Ln1,{atom,Ln2,rename(Mod)}, Func}, xform(Args)};
  33. xform(T) when is_tuple(T) ->
  34. %% io:format("T=~p~n",[T]),
  35. L = tuple_to_list(T),
  36. L1 = [xform(I) || I <- L],
  37. list_to_tuple(L1);
  38. xform([H|T]) ->
  39. [xform(H)|xform(T)];
  40. xform(X) ->
  41. X.
  42. rename(Mod) ->
  43. Data = nmap:map(),
  44. case lists:keysearch(Mod,1,Data) of
  45. {value, Tuple} ->
  46. New = element(4, Tuple),
  47. %% io:format("Rename:~p -> ~w~n",[Mod,New]),
  48. New;
  49. false ->
  50. Mod
  51. end.
  52. publish() ->
  53. elib1_misc:show_loaded(fun() ->
  54. publish1()
  55. end).
  56. publish1() ->
  57. Key = find_key(),
  58. L = filelib:wildcard("*.erl"),
  59. Data = [process(I,Key) || I <- L],
  60. {ok, S} = file:open("nmap.erl", [write]),
  61. io:format(S,"-module(nmap).~n",[]),
  62. io:format(S,"%% autogenerated do not edit by hand.~n",[]),
  63. io:format(S,"-export([map/0]).~n",[]),
  64. io:format(S,"map()->~n~p.~n",[Data]),
  65. file:close(S),
  66. ok.
  67. validate() ->
  68. Expected = nmap:map(),
  69. case check(Expected, []) of
  70. [] ->
  71. ok;
  72. L ->
  73. {failed, L}
  74. end.
  75. check([{Mod,File,Key,NewModName,Sign}|T], L) ->
  76. io:format("checking:~s ",[File]),
  77. [$m|M] = atom_to_list(NewModName),
  78. Md5= elib1_misc:hex2bin(M),
  79. case file:read_file(File) of
  80. {ok, B} ->
  81. case erlang:md5(B) of
  82. Md5 ->
  83. io:format("md5 ok"),
  84. case file:read_file(Key++".pub") of
  85. {ok, Bin} ->
  86. Code = binary_to_term(Bin),
  87. case elib1_rsa:decode_sig(Sign , Code) of
  88. Md5 ->
  89. io:format(" signed~n"),
  90. check(T, L);
  91. _ ->
  92. io:format(" bad sig~n"),
  93. check(T, [File|L])
  94. end;
  95. _ ->
  96. io:format("no keyfile~n"),
  97. check(T, [File|L])
  98. end;
  99. _ ->
  100. io:format("bad checksum~n"),
  101. check(T, [File|L])
  102. end;
  103. _ ->
  104. io:format(" enreadable~n"),
  105. check(T, [File|L])
  106. end;
  107. check([], L) ->
  108. L.
  109. find_key() ->
  110. case filelib:wildcard("*.pri") of
  111. [X] ->
  112. Key = elib1_rsa:get_key(X),
  113. "irp." ++ X1 = reverse(X),
  114. Who = reverse(X1),
  115. {Who, Key};
  116. [] ->
  117. exit(eNoKey);
  118. O ->
  119. exit({badKeys,O})
  120. end.
  121. process(File,{Who,Key}) ->
  122. {ok, Bin} = file:read_file(File),
  123. Md5Bin = erlang:md5(Bin),
  124. Sig = elib1_rsa:sign_bin(Md5Bin, Key),
  125. Mod = list_to_atom(filename:rootname(File)),
  126. NewName = list_to_atom("m" ++ elib1_misc:bin2hex(Md5Bin)),
  127. {Mod,File,Who,NewName,Sig}.