View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2013, VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(pack_analyzer,
   31	  [ pack_analysis/2,			% +Pack, -Results
   32	    xref_pack/1,			% +Pack
   33	    pack_prolog_entry/1,		% +Entry
   34	    xref_pack_file/2,			% +Pack, +File
   35	    pack_members/2,			% +Pack, -Members
   36	    pack_open_entry/3			% +Pack, +Entry, -Stream
   37	  ]).   38:- use_module(library(lists)).   39:- use_module(library(apply)).   40:- use_module(library(archive)).   41:- use_module(library(filesex)).   42:- use_module(library(prolog_xref)).   43:- use_module(library(prolog_source)).   44:- use_module(library(option)).   45:- use_module(library(debug)).   46:- use_module(library(git)).   47
   48:- use_module(pack_mirror).   49:- use_module(pack_info).

Analyse the content of a Prolog pack

This module analyses a Prolog pack without loading it.

   56:- dynamic
   57	pack_dependency/4.   58
   59:- meta_predicate
   60	find_unique(?, 0, ?, ?).
 pack_analysis(+Pack, -Results)
Result is the analysis result for Pack. Results is a list, containing the following elements:
size(SizeBytes)
Summed size of all files in Pack. Always exactly one.
files(Files)
List of files in the pack. Each file is represented by a term file(Name, SizeBytes).
prolog_source(File, Size, Module, Exports, Extra)
One for each Prolog file in pack. If the file is not a module file, Module is unified to - and Exports is []. Each member of Exports is a term export(PI, Info), where Info is a list with additional properties. Currently defined Info terms are:
  • doc(Summary, Comment)

Extra is reserved for additional information. Currently defines:

  • doc(Title, Comment)
   82pack_analysis(Pack,
   83	      [ size(Size),
   84		files(Members)
   85	      | Results
   86	      ]) :-
   87	pack_members(Pack, Members),
   88	maplist(arg(2), Members, SizeByFile),
   89	sum_list(SizeByFile, Size),
   90	xref_pack(Pack),
   91	find_unique(Readme, readme(Pack, Members, Readme), Results, Res0),
   92	find_unique(Info,   file_info(Pack, Members, Info), Res0, Res1),
   93	find_unique(ReqF,   required_file(Pack, ReqF), Res1, Res2),
   94	find_unique(ReqP,   required_predicate(Pack, ReqP), Res2, []).
   95
   96find_unique(Templ, Goal, List, Tail) :-
   97	findall(Templ, Goal, List0),
   98	sort(List0, List1),
   99	append(List1, Tail, List).
  100
  101readme(Pack, Members, readme(Text)) :-
  102	member(file(Name, _Size), Members),
  103	downcase_atom(Name, Down),
  104	readme_file(Down), !,
  105	setup_call_cleanup(
  106	    pack_open_entry(Pack, Name, Stream),
  107	    read_stream_to_codes(Stream, Codes),
  108	    close(Stream)),
  109	string_to_list(Text, Codes).
  110
  111readme_file(readme).
  112readme_file('readme.txt').
  113
  114
  115file_info(Pack, Members,
  116	  prolog_source(Entry, Size, Module, Exports, Documentation)) :-
  117	xref_pack_source(Pack, Entry, Source),
  118	memberchk(file(Entry,Size), Members),
  119	(   xref_module(Source, Module)
  120	->  findall(export(Name/Arity, Info),
  121		    ( xref_exported(Source, Head),
  122		      functor(Head, Name, Arity),
  123		      (	  xref_comment(Source, Head, Summary, PredComment)
  124		      ->  Info = [doc(Summary, PredComment)]
  125		      ;	  Info = []
  126		      )
  127		    ),
  128		    Exports)
  129	;   Module = (-),			% Warning?
  130	    Exports = []
  131	),
  132	(   xref_comment(Source, Title, FileComment)
  133	->  Documentation = [ doc(Title, FileComment) ]
  134	;   Documentation = []
  135	).
  136
  137required_file(Pack, required_file(Spec, From)) :-
  138	xref_pack_source(Pack, _, Source),
  139	xref_uses_file(Source, Spec, File),
  140	classify_file(Pack, File, From0),
  141	from_class(From0, From).
  142
  143from_class(pack(Pack), pack(Pack)) :- !.
  144from_class(Alias, From) :-
  145	functor(Alias, From, 1), !.
  146from_class(From, From).
  147
  148classify_file(Pack, File, From) :-
  149	(   xref_pack_source(Pack, _, File)
  150	->  From = pack
  151	;   absolute_file_name(pack(.), PackRoot,
  152			       [ file_type(directory),
  153				 solutions(all)
  154			       ]),
  155	    atom_concat(PackRoot, Local, File)
  156	->  atomic_list_concat(Segments, /, Local),
  157	    Segments = [FromPack|_],
  158	    From = pack(FromPack)
  159	;   absolute_file_name(swi(.), SwiRoot,
  160			       [ file_type(directory)
  161			       ]),
  162	    atom_concat(SwiRoot, _, File)
  163	->  file_name_on_path(File, From)
  164	;   From = File				% Needs further classification
  165	).
 required_predicate(+Pack, -Required) is nondet
True if Required is a predicate that is required by Pack.
  171required_predicate(Pack, required_predicate(Name/Arity, From)) :-
  172	xref_pack_source(Pack, _, Source),
  173	xref_called(Source, Head, _By),
  174	functor(Head, Name, Arity),
  175	(   xref_defined(Source, Head, How)
  176	->  classify_predicate_source(How, Pack, From)
  177	;   predicate_property(Head, iso)
  178	->  From = iso
  179	;   predicate_property(system:Head, visible)
  180	->  From = swi
  181	;   predicate_property(Head, autoload(Autoload))
  182	->  classify_file(Pack, Autoload, From)
  183	;   From = undefined
  184	).
  185
  186classify_predicate_source(imported(File), Pack, From) :-
  187	classify_file(Pack, File, From).
 xref_pack(+Pack) is det
Run the cross-referencer on all Prolog files inside pack.
  194xref_pack(Pack) :-
  195	absolute_file_name(Pack, PackPath),
  196	retractall(pack_dependency(PackPath, _Spec, _How, _Dep)),
  197	pack_members(PackPath, Members),
  198	maplist(arg(1), Members, Entries),
  199	include(pack_prolog_entry, Entries, PrologEntries),
  200	maplist(xref_pack_file(PackPath), PrologEntries).
  201
  202pack_prolog_entry(Entry) :-
  203	sub_atom(Entry, 0, _, _, 'prolog/'),
  204	file_name_extension(_, Ext, Entry),
  205	user:prolog_file_type(Ext, prolog), !.
 xref_pack_file(+Pack, +File) is det
Run the cross-referencer on File inside Pack.
  212xref_pack_file(Pack, File) :-
  213	catch(xref_pack_file_2(Pack, File),
  214	      E, print_message(error, E)), !.
  215xref_pack_file(Pack, File) :-
  216	print_message(warning,
  217		      error(goal_failed(xref_pack_file(Pack, File)),
  218			    _)).
  219
  220xref_pack_file_2(Pack, File) :-
  221	exists_directory(Pack), !,
  222	directory_file_path(Pack, File, Path),
  223	xref_source(Path, [register_called(all)]).
  224xref_pack_file_2(Pack, File) :-
  225	absolute_file_name(Pack, AbsPack,
  226			   [ access(read)
  227			   ]),
  228	directory_file_path(AbsPack, File, Path),
  229	xref_source(Path, [register_called(all)]).
  230
  231
  232		 /*******************************
  233		 *	      MEMBERS		*
  234		 *******************************/
 pack_open_entry(+Pack, +Entry, -Stream) is det
Open an entry in the Pack for reading. The entry must be closed with close/1.
  241pack_open_entry(Directory, Entry, Stream) :-
  242	exists_directory(Directory), !,
  243	safe_entry_path(Directory, Entry, File),
  244	open(File, read, Stream).
  245pack_open_entry(Archive, Entry, Stream) :-
  246	ar_prefix(Archive, Prefix),
  247	atom_concat(Prefix, Entry, Name),
  248	setup_call_cleanup(
  249	    archive_open(Archive, Handle, []),
  250	    ( archive_next_header(Handle, Name),
  251	      archive_open_entry(Handle, Stream)
  252	    ),
  253	    archive_close(Handle)),
  254	format(atom(StreamName), '~w/~w', [Archive, Entry]),
  255	set_stream(Stream, file_name(StreamName)).
  256
  257%	safe_entry_path(+Directory, +Entry, -File) is det.
  258%
  259%	File is the path of Entry  in   Directory.  Raise  a permission
  260%	error if Entry is not a plain relative path or one of its
  261%	components is a symbolic link: a pack may contain links to
  262%	arbitrary files on the server.
  263
  264safe_entry_path(Directory, Entry, File) :-
  265	atomic_list_concat(Segments, /, Entry),
  266	(   Segments \== [],
  267	    \+ ( member(Segment, Segments),
  268		 ( Segment == '' ; Segment == '.' ; Segment == '..' )
  269	       ),
  270	    no_links(Segments, Directory, File)
  271	->  true
  272	;   permission_error(open, pack_entry, Entry)
  273	).
  274
  275no_links([], File, File).
  276no_links([H|T], Dir, File) :-
  277	directory_file_path(Dir, H, Path),
  278	\+ read_link(Path, _, _),
  279	no_links(T, Path, File).
  280
  281:- dynamic
  282	ar_prefix_cache/2,
  283	ar_members_cache/3.  284
  285ar_prefix(Archive, Prefix) :-
  286	ar_prefix_cache(Archive, Prefix0), !,
  287	Prefix = Prefix0.
  288ar_prefix(Archive, Prefix) :-
  289	ar_pack_members(Archive, _, Prefix),
  290	assertz(ar_prefix_cache(Archive, Prefix)).
 pack_members(+Pack, -Members:list) is det
Members is a list of file(File,Size) that represent the files in Pack. Pack is either a git repository, a directory holding files or an archive.
  298:- dynamic
  299	pack_member_cache/3.  300
  301pack_members(Dir, Members) :-
  302	time_file(Dir, T),
  303	pack_member_cache(Dir, T, Members0), !,
  304	Members = Members0.
  305pack_members(Dir, Members) :-
  306	pack_members_no_cache(Dir, Members0),
  307	time_file(Dir, T),
  308	asserta(pack_member_cache(Dir, T, Members0)),
  309	Members = Members0.
  310
  311pack_members_no_cache(Directory, Members) :-
  312	is_git_directory(Directory), !,
  313	git_ls_tree(Entries, [directory(Directory)]),
  314	include(git_blob, Entries, Blobs),
  315	maplist(git_entry(Directory), Blobs, Members).
  316pack_members_no_cache(Directory, Members) :-
  317	exists_directory(Directory), !,
  318	recursive_directory_files(Directory, Files),
  319	maplist(file_entry(Directory), Files, Members).
  320pack_members_no_cache(Archive, Members) :-
  321	E = error(archive_error(_,_),_),
  322	catch(ar_pack_members(Archive, Members0, Prefix),
  323	      E, bad_archive(Archive, E)),
  324	exclude(macos_file, Members0, Members1),
  325	maplist(strip_prefix(Prefix), Members1, Members).
  326
  327bad_archive(Archive, Error) :-
  328	delete_file(Archive),
  329	throw(Error).
  330
  331git_blob(object(_Mode, blob, _Hash, _Size, _Name)).
  332
  333%	git_entry(+Dir, +Object, -Entry) is det.
  334%
  335%	Symbolic links (mode 0o120000) are mapped to link(Name, Target),
  336%	as for archives.  Links are never opened, as they may point
  337%	outside the pack.
  338
  339git_entry(Dir, object(0o120000, blob, _Hash, _Size, Name),
  340	  link(Name, Target)) :- !,
  341	directory_file_path(Dir, Name, Path),
  342	(   read_link(Path, Target0, _)
  343	->  Target = Target0
  344	;   Target = '?'
  345	).
  346git_entry(_, object(_Mode, blob, _Hash, Size, Name), file(Name, Size)).
  347
  348ar_pack_members(Archive, Members, Prefix) :-
  349	(   ar_members_cache(Archive, Members0, Prefix0)
  350	->  true
  351	;   read_ar_pack_members(Archive, Members0, Prefix0)
  352	->  asserta(ar_members_cache(Archive, Members0, Prefix0))
  353	),
  354	Members = Members0,
  355	Prefix  = Prefix0.
  356
  357read_ar_pack_members(Archive, Members0, Prefix) :-
  358	setup_call_cleanup(
  359	    archive_open(Archive, Handle, []),
  360	    findall(Member, ar_member(Handle, Member), Members0),
  361	    archive_close(Handle)),
  362	(   member(file(InfoFile,_), Members0),
  363	    atom_concat(Prefix, 'pack.pl', InfoFile)
  364	->  true
  365	;   existence_error(pack_file, 'pack.pl')
  366	).
  367
  368ar_member(Handle, Entry) :-
  369	repeat,
  370	(   archive_next_header(Handle, File)
  371	->  true
  372	;   !, fail
  373	),
  374	archive_header_property(Handle, filetype(Type)),
  375	make_entry(Type, Handle, File, Entry).
  376
  377make_entry(file, Handle, File, file(File, Size)) :- !,
  378	archive_header_property(Handle, size(Size)).
  379make_entry(link, Handle, File, link(File, Target)) :- !,
  380	archive_header_property(Handle, link_target(Target)).
  381make_entry(directory, _, _, _) :- !,
  382	fail.
  383make_entry(Type, _, Name, Entry) :-
  384	atom(Type), !,
  385	Entry =.. [Type, Name].
  386make_entry(Type, _, Name, _Entry) :-
  387	print_message(warning, unknown_archive_type(Type, Name)),
  388	fail.
 macos_file(@Entry) is semidet
Delete MacOS metadata.
  394macos_file(file(Name, _Size)) :-
  395	sub_atom(Name, 0, _, _, '__MACOSX').
  396
  397strip_prefix(Prefix, Term0, Term) :-
  398	Term0 =.. [Type, Name, Size],
  399	atom_concat(Prefix, Stripped, Name),
  400	Term =.. [Type, Stripped, Size].
  401
  402file_entry(_Pack, link(File,Target), link(File,Target)) :- !.
  403file_entry(Pack, File, file(File,Size)) :-
  404	directory_file_path(Pack, File, Path),
  405	size_file(Path, Size).
 recursive_directory_files(+Dir, -Files) is det
True when Files is a list holding all files in Dir, recursively. Symbolic links are not followed and represented as link(File, Target).
  413recursive_directory_files(Dir, Files) :-
  414	dir_prefix(Dir, Prefix),
  415	recursive_directory_files(Dir, Prefix, Files, []).
  416
  417recursive_directory_files(Dir, Prefix, AllFiles, Rest) :-
  418	directory_files(Dir, Files),
  419	dir_files(Files, Dir, Prefix, AllFiles, Rest).
  420
  421dir_files([], _, _, Files, Files).
  422dir_files([H|T], Dir, Prefix, Files, Rest) :-
  423	(   special(H)
  424	->  dir_files(T, Dir, Prefix, Files, Rest)
  425	;   directory_file_path(Dir, H, Entry),
  426	    (	read_link(Entry, _, Target)		% do not follow links
  427	    ->	atom_concat(Prefix, File, Entry),
  428		Files = [link(File, Target)|Rest0]
  429	    ;	exists_directory(Entry)
  430	    ->	recursive_directory_files(Entry, Prefix, Files, Rest0)
  431	    ;	atom_concat(Prefix, File, Entry),
  432		Files = [File|Rest0]
  433	    ),
  434	    dir_files(T, Dir, Prefix, Rest0, Rest)
  435	).
  436
  437dir_prefix(., '') :- !.
  438dir_prefix(Dir, Prefix) :-
  439	(   sub_atom(Dir, _, _, 0, /)
  440	->  Prefix = Dir
  441	;   atom_concat(Dir, /, Prefix)
  442	).
  443
  444special(.).
  445special(..).
  446
  447		 /*******************************
  448		 *	     XREF HOOKS		*
  449		 *******************************/
  450
  451:- multifile
  452	prolog:xref_open_source/2,
  453	prolog:xref_source_identifier/2,
  454	prolog:xref_source_file/3.
 prolog:xref_open_source(+Id, -Stream) is semidet
If Id refers to a known Prolog pack, open the pack entry. A pack-file identifier is the path-name of the archive or pack directory, followed by the entry in the pack.
  462prolog:xref_open_source(File, Stream) :-
  463	atom(File),
  464	pack_prefix(Pack, Prefix),
  465	atom_concat(Prefix, Entry, File),
  466	pack_open_entry(Pack, Entry, Stream).
 prolog:xref_source_identifier(+Path, -Id) is semidet
  470prolog:xref_source_identifier(Path, Path) :-
  471	atom(Path),
  472	pack_mirror_directory(MirrorDir),
  473	sub_atom(Path, 0, _, _, MirrorDir),
  474	atom(Path),
  475	pack_prefix(_Pack, Prefix),
  476	sub_atom(Path, 0, _, _, Prefix), !.
 pack_file(+Path, -Pack, -Entry) is semidet
True if Path originates from Entry in Pack.
  482pack_file(Path, Pack, Entry) :-
  483	pack_prefix(Pack, Prefix),
  484	atom_concat(Prefix, Entry, Path),
  485	pack_members(Pack, Members),
  486	memberchk(file(Entry,_Size), Members).
 resolve_pack_file(+Spec, -Source, -SourcePack, -SourceEntry) is nondet
True if Spec appearing in OrgPack can be resolved by file Entry in ResPack.
  493resolve_pack_file(library(File), Source, SourcePack, SourceEntry) :-
  494	(   atom(File)
  495	->  FileName = File
  496	;   path_segments_atom(File, FileName)
  497	),
  498	directory_file_path(prolog, FileName, EntryNoExt),
  499	user:prolog_file_type(Ext, prolog),
  500	file_name_extension(EntryNoExt, Ext, SourceEntry),
  501	pack_file(Source, SourcePack, SourceEntry).
 assert_dependency(OrgPack, OrgSpec, How, Src) is det
  505assert_dependency(OrgPack, OrgSpec, How, Src) :-
  506	pack_dependency(OrgPack, OrgSpec, How, Src), !.
  507assert_dependency(OrgPack, OrgSpec, How, Src) :-
  508	asserta(pack_dependency(OrgPack, OrgSpec, How, Src)).
 prolog:xref_source_file(+Spec, -SourceID, +Options) is semidet
  512prolog:xref_source_file(library(File), Source, Options) :-
  513	option(relative_to(Origin), Options),
  514	pack_file(Origin, OrgPack, _OrigEntry),
  515	debug(pack(xref), 'Search for ~q from pack ~q',
  516	      [library(File), OrgPack]),
  517	findall(t(Src, SrcPack, SrcEntry),
  518		resolve_pack_file(library(File), Src, SrcPack, SrcEntry),
  519		Triples),
  520	(   select(t(Source, OrgPack, _), Triples, Alt)
  521	->  true
  522	;   select(t(Source, _, _), Triples, Alt),
  523	    assert_dependency(OrgPack, library(File), dep, Source)
  524	),
  525	forall(member(t(AltSrc,_,_), Alt),
  526	       assert_dependency(OrgPack, library(File), alt, AltSrc)).
  527prolog:xref_source_file(Spec, Source, _Options) :-
  528	atom(Spec),
  529	(   pack_file(Spec, _, _)
  530	->  Source = Spec
  531	;   user:prolog_file_type(Ext, prolog),
  532	    file_name_extension(Spec, Ext, Source),
  533	    pack_file(Source, _, _)
  534	),
  535	debug(pack(xref), 'Resolved ~q to ~q', [Spec, Source]).
 xref_pack_source(+Pack, ?Entry, ?Source) is nondet
True when Source is the canonical xref source of Entry in Pack.
  541xref_pack_source(Pack, Entry, Source) :-
  542	xref_current_source(Source),
  543	pack_prefix(Pack, Prefix),
  544	atom_concat(Prefix, Entry, Source).
  545
  546
  547pack_prefix(Archive, Prefix) :-
  548	pack_archive(_Pack, _Hash, Archive),
  549	atom_concat(Archive, /, Prefix)