File: packs.pl

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (199 lines) | stat: -rw-r--r-- 5,479 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@cs.vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2012, VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module('$pack',
	  [ attach_packs/0,
	    attach_packs/1,			% +Dir
	    '$pack_detach'/2,			% +Name, -Dir
	    '$pack_attach'/1			% +Dir
	  ]).

:- multifile user:file_search_path/2.
:- dynamic user:file_search_path/2.

:- dynamic
	pack_dir/3,				% Pack, Type, Dir
	pack/2.					% Pack, BaseDir


user:file_search_path(pack, app_data(pack)).
user:file_search_path(pack, swi(pack)).

user:file_search_path(library, PackLib) :-
	pack_dir(_Name, prolog, PackLib).
user:file_search_path(foreign, PackLib) :-
	pack_dir(_Name, foreign, PackLib).

%%	'$pack_detach'(+Name, -Dir) is det.
%
%	Detach the given package  from  the   search  paths  and list of
%	registered packages, but does not delete the files.

'$pack_detach'(Name, Dir) :-
	(   atom(Name)
	->  true
	;   throw(error(type_error(atom, Name), _))
	),
	(   retract(pack(Name, Dir))
	->  retractall(pack_dir(Name, _, _)),
	    reload_library_index
	;   throw(error(existence_error(pack, Name), _))
	).

%%	'$pack_attach'(+Dir) is det.
%
%	Attach the given package

'$pack_attach'(Dir) :-
	attach_package(Dir), !.
'$pack_attach'(Dir) :-
	(   exists_directory(Dir)
	->  throw(error(existence_error(directory, Dir), _))
	;   throw(error(domain_error(pack, Dir), _))
	).

%%	attach_packs
%
%	Attach packages from all package directories.

attach_packs :-
	findall(PackDir, absolute_file_name(pack(.), PackDir,
					    [ file_type(directory),
					      access(read),
					      solutions(all)
					    ]),
		PackDirs),
	(   PackDirs \== []
	->  remove_dups(PackDirs, UniquePackDirs, []),
	    forall('$member'(PackDir, UniquePackDirs),
		   attach_packs(PackDir))
	;   true
	).

%%	remove_dups(+List, -Unique, +Seen) is det.
%
%	Remove duplicates from List, keeping the first solution.

remove_dups([], [], _).
remove_dups([H|T0], T, Seen) :-
	memberchk(H, Seen), !,
	remove_dups(T0, T, Seen).
remove_dups([H|T0], [H|T], Seen) :-
	remove_dups(T0, T, [H|Seen]).


%%	attach_packs(+Dir)
%
%	Attach packages from directory Dir.

attach_packs(Dir) :-
	catch(directory_files(Dir, Entries), _, fail), !,
	ensure_slash(Dir, SDir),
	attach_packages(Entries, SDir).
attach_packs(_).

attach_packages([], _).
attach_packages([H|T], Dir) :-
	attach_package(H, Dir),
	attach_packages(T, Dir).

attach_package(Entry, Dir) :-
	\+ special(Entry),
	atom_concat(Dir, Entry, PackDir),
	attach_package(PackDir), !.
attach_package(_, _).

special(.).
special(..).


%%	attach_package(+PackDir) is semidet.
%
%	@tbd	Deal with autoload index.  Reload?

attach_package(PackDir) :-
	atomic_list_concat([PackDir, '/pack.pl'], InfoFile),
	access_file(InfoFile, read),
	file_base_name(PackDir, Pack),
	check_existing(Pack, PackDir),
	foreign_dir(Pack, PackDir, ForeignDir),
	prolog_dir(PackDir, PrologDir), !,
	assertz(pack(Pack, PackDir)),
	assertz(pack_dir(Pack, prolog, PrologDir)),
	update_autoload(PrologDir),
	(   ForeignDir \== (-)
	->  assertz(pack_dir(Pack, foreign, ForeignDir))
	;   true
	),
	print_message(silent, pack(attached(Pack, PackDir))).


%%	check_existing(+Pack, +PackDir) is semidet.
%
%	Verify that we did not load this package before.

check_existing(Entry, Dir) :-
	retract(pack(Entry, Dir)), !,		% registered from same place
	retractall(pack_dir(Entry, _, _)).
check_existing(Entry, Dir) :-
	pack(Entry, OldDir), !,
	print_message(warning, pack(duplicate(Entry, OldDir, Dir))),
	fail.
check_existing(_, _).


prolog_dir(PackDir, PrologDir) :-
	atomic_list_concat([PackDir, '/prolog'], PrologDir),
	exists_directory(PrologDir).

update_autoload(PrologDir) :-
	atom_concat(PrologDir, '/INDEX.pl', IndexFile),
	(   exists_file(IndexFile)
	->  reload_library_index
	;   true
	).

foreign_dir(Pack, PackDir, ForeignDir) :-
	current_prolog_flag(arch, Arch),
	atomic_list_concat([PackDir, '/lib'], ForeignBaseDir),
	exists_directory(ForeignBaseDir), !,
	atomic_list_concat([PackDir, '/lib/', Arch], ForeignDir),
	(   exists_directory(ForeignDir)
	->  assertz(pack_dir(Pack, foreign, ForeignDir))
	;   print_message(warning, pack(no_arch(Pack, Arch))),
	    fail
	).
foreign_dir(_, _, (-)).

ensure_slash(Dir, SDir) :-
	(   sub_atom(Dir, _, _, 0, /)
	->  SDir = Dir
	;   atom_concat(Dir, /, SDir)
	).