octave.m 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  1. ## -*- octave -*-
  2. function res = tcomp (fn)
  3. %% res = tcomp (fn)
  4. %% imports components and rearranges them.
  5. if nargin ~= 1
  6. print_usage()
  7. end
  8. data = dlmread(fn, 3, 0);
  9. x = data(:,2:end);
  10. y = 'hello';
  11. z = y';
  12. cnty = repmat(x(:,1)(:), 10, 1);
  13. pop = x(:,1:10)(:);
  14. ## Here and below, we test if the indentation aligns with a previous
  15. ## fixindented line. This is important so as to make it easier for the
  16. ## user to override some indentation somewhere, and also because it
  17. ## reflects the fact that the indentation decision is taken with a minimum
  18. ## amount of work (i.e. in the present case, without having to walk back
  19. ## until the `function' line).
  20. bir = x(:,11:20)(:); # fixindent
  21. dth = x(:,21:30)(:);
  22. imig = x(:,31:40)(:);
  23. dmig = x(:,41:50)(:);
  24. gq = x(:,51:60)(:);
  25. yrs = repmat(2000:2009, 39, 1)(:);
  26. res = [yrs, cnty, pop, bir, dth, imig, dmig, gq];
  27. endfunction
  28. ## Copyright (C) 2005, 2006, 2007, 2008, 2009 S�ren Hauberg
  29. ##
  30. ## This file is part of Octave.
  31. ##
  32. ## Octave is free software; you can redistribute it and/or modify it
  33. ## under the terms of the GNU General Public License as published by
  34. ## the Free Software Foundation; either version 3 of the License, or (at
  35. ## your option) any later version.
  36. ##
  37. ## Octave is distributed in the hope that it will be useful, but
  38. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  39. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  40. ## General Public License for more details.
  41. ##
  42. ## You should have received a copy of the GNU General Public License
  43. ## along with Octave; see the file COPYING. If not, see
  44. ## <http://www.gnu.org/licenses/>.
  45. ## -*- texinfo -*-
  46. ## @deftypefn {Command} pkg @var{command} @var{pkg_name}
  47. ## @deftypefnx {Command} pkg @var{command} @var{option} @var{pkg_name}
  48. ## This command interacts with the package manager. Different actions will
  49. ## be taken depending on the value of @var{command}.
  50. ##
  51. ## @table @samp
  52. ## @item install
  53. ## Install named packages. For example,
  54. ## @example
  55. ## pkg install image-1.0.0.tar.gz
  56. ## @end example
  57. ## @noindent
  58. ## installs the package found in the file @file{image-1.0.0.tar.gz}.
  59. ##
  60. ## The @var{option} variable can contain options that affect the manner
  61. ## in which a package is installed. These options can be one or more of
  62. ##
  63. ## @table @code
  64. ## @item -nodeps
  65. ## The package manager will disable the dependency checking. That way it
  66. ## is possible to install a package even if it depends on another package
  67. ## that's not installed on the system. @strong{Use this option with care.}
  68. ##
  69. ## @item -noauto
  70. ## The package manager will not automatically load the installed package
  71. ## when starting Octave, even if the package requests that it is.
  72. ##
  73. ## @item -auto
  74. ## The package manager will automatically load the installed package when
  75. ## starting Octave, even if the package requests that it isn't.
  76. ##
  77. ## @item -local
  78. ## A local installation is forced, even if the user has system privileges.
  79. ##
  80. ## @item -global
  81. ## A global installation is forced, even if the user doesn't normally have
  82. ## system privileges
  83. ##
  84. ## @item -verbose
  85. ## The package manager will print the output of all of the commands that are
  86. ## performed.
  87. ## @end table
  88. ##
  89. ## @item uninstall
  90. ## Uninstall named packages. For example,
  91. ## @example
  92. ## pkg uninstall image
  93. ## @end example
  94. ## @noindent
  95. ## removes the @code{image} package from the system. If another installed
  96. ## package depends on the @code{image} package an error will be issued.
  97. ## The package can be uninstalled anyway by using the @code{-nodeps} option.
  98. ## @item load
  99. ## Add named packages to the path. After loading a package it is
  100. ## possible to use the functions provided by the package. For example,
  101. ## @example
  102. ## pkg load image
  103. ## @end example
  104. ## @noindent
  105. ## adds the @code{image} package to the path. It is possible to load all
  106. ## installed packages at once with the command
  107. ## @example
  108. ## pkg load all
  109. ## @end example
  110. ## @item unload
  111. ## Removes named packages from the path. After unloading a package it is
  112. ## no longer possible to use the functions provided by the package.
  113. ## This command behaves like the @code{load} command.
  114. ## @item list
  115. ## Show a list of the currently installed packages. By requesting one or two
  116. ## output argument it is possible to get a list of the currently installed
  117. ## packages. For example,
  118. ## @example
  119. ## installed_packages = pkg list;
  120. ## @end example
  121. ## @noindent
  122. ## returns a cell array containing a structure for each installed package.
  123. ## The command
  124. ## @example
  125. ## [@var{user_packages}, @var{system_packages}] = pkg list
  126. ## @end example
  127. ## @noindent
  128. ## splits the list of installed packages into those who are installed by
  129. ## the current user, and those installed by the system administrator.
  130. ## @item describe
  131. ## Show a short description of the named installed packages, with the option
  132. ## '-verbose' also list functions provided by the package, e.g.:
  133. ## @example
  134. ## pkg describe -verbose all
  135. ## @end example
  136. ## @noindent
  137. ## will describe all installed packages and the functions they provide.
  138. ## If one output is requested a cell of structure containing the
  139. ## description and list of functions of each package is returned as
  140. ## output rather than printed on screen:
  141. ## @example
  142. ## desc = pkg ("describe", "secs1d", "image")
  143. ## @end example
  144. ## @noindent
  145. ## If any of the requested packages is not installed, pkg returns an
  146. ## error, unless a second output is requested:
  147. ## @example
  148. ## [ desc, flag] = pkg ("describe", "secs1d", "image")
  149. ## @end example
  150. ## @noindent
  151. ## @var{flag} will take one of the values "Not installed", "Loaded" or
  152. ## "Not loaded" for each of the named packages.
  153. ## @item prefix
  154. ## Set the installation prefix directory. For example,
  155. ## @example
  156. ## pkg prefix ~/my_octave_packages
  157. ## @end example
  158. ## @noindent
  159. ## sets the installation prefix to @file{~/my_octave_packages}.
  160. ## Packages will be installed in this directory.
  161. ##
  162. ## It is possible to get the current installation prefix by requesting an
  163. ## output argument. For example,
  164. ## @example
  165. ## p = pkg prefix
  166. ## @end example
  167. ##
  168. ## The location in which to install the architecture dependent files can be
  169. ## independent specified with an addition argument. For example
  170. ##
  171. ## @example
  172. ## pkg prefix ~/my_octave_packages ~/my_arch_dep_pkgs
  173. ## @end example
  174. ## @item local_list
  175. ## Set the file in which to look for information on the locally
  176. ## installed packages. Locally installed packages are those that are
  177. ## typically available only to the current user. For example
  178. ## @example
  179. ## pkg local_list ~/.octave_packages
  180. ## @end example
  181. ## It is possible to get the current value of local_list with the following
  182. ## @example
  183. ## pkg local_list
  184. ## @end example
  185. ## @item global_list
  186. ## Set the file in which to look for, for information on the globally
  187. ## installed packages. Globally installed packages are those that are
  188. ## typically available to all users. For example
  189. ## @example
  190. ## pkg global_list /usr/share/octave/octave_packages
  191. ## @end example
  192. ## It is possible to get the current value of global_list with the following
  193. ## @example
  194. ## pkg global_list
  195. ## @end example
  196. ## @item rebuild
  197. ## Rebuilds the package database from the installed directories. This can
  198. ## be used in cases where for some reason the package database is corrupted.
  199. ## It can also take the @code{-auto} and @code{-noauto} options to allow the
  200. ## autoloading state of a package to be changed. For example
  201. ##
  202. ## @example
  203. ## pkg rebuild -noauto image
  204. ## @end example
  205. ##
  206. ## will remove the autoloading status of the image package.
  207. ## @item build
  208. ## Builds a binary form of a package or packages. The binary file produced
  209. ## will itself be an Octave package that can be installed normally with
  210. ## @code{pkg}. The form of the command to build a binary package is
  211. ##
  212. ## @example
  213. ## pkg build builddir image-1.0.0.tar.gz @dots{}
  214. ## @end example
  215. ##
  216. ## @noindent
  217. ## where @code{builddir} is the name of a directory where the temporary
  218. ## installation will be produced and the binary packages will be found.
  219. ## The options @code{-verbose} and @code{-nodeps} are respected, while
  220. ## the other options are ignored.
  221. ## @end table
  222. ## @end deftypefn
  223. function [local_packages, global_packages] = pkg (varargin)
  224. ## Installation prefix (FIXME: what should these be on windows?)
  225. persistent user_prefix = false;
  226. persistent prefix = -1;
  227. persistent archprefix = -1;
  228. persistent local_list = tilde_expand (fullfile ("~", ".octave_packages"));
  229. persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave",
  230. "octave_packages");
  231. mlock ();
  232. global_install = issuperuser ();
  233. if (prefix == -1)
  234. if (global_install)
  235. prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
  236. archprefix = fullfile (octave_config_info ("libexecdir"),
  237. "octave", "packages");
  238. else
  239. prefix = fullfile ("~", "octave");
  240. archprefix = prefix;
  241. endif
  242. prefix = tilde_expand (prefix);
  243. archprefix = tilde_expand (archprefix);
  244. endif
  245. available_actions = {"list", "install", "uninstall", "load", ...
  246. "unload", "prefix", "local_list", ...
  247. "global_list", "rebuild", "build","describe"};
  248. ## Handle input
  249. if (length (varargin) == 0 || ! iscellstr (varargin))
  250. print_usage ();
  251. endif
  252. files = {};
  253. deps = true;
  254. auto = 0;
  255. action = "none";
  256. verbose = false;
  257. for i = 1:length (varargin)
  258. switch (varargin{i})
  259. case "-nodeps"
  260. deps = false;
  261. case "-noauto"
  262. auto = -1;
  263. case "-auto"
  264. auto = 1;
  265. case "-verbose"
  266. verbose = true;
  267. case "-local"
  268. global_install = false;
  269. if (! user_prefix)
  270. prefix = tilde_expand (fullfile ("~", "octave"));
  271. archprefix = prefix;
  272. endif
  273. case "-global"
  274. global_install = true;
  275. if (! user_prefix)
  276. prefix = fullfile (OCTAVE_HOME (), "share", "octave", "packages");
  277. archprefix = fullfile (octave_config_info ("libexecdir"),
  278. "octave", "packages");
  279. endif
  280. case available_actions
  281. if (strcmp (action, "none"))
  282. action = varargin{i};
  283. else
  284. error ("more than one action specified");
  285. endif
  286. otherwise
  287. files{end+1} = varargin{i};
  288. endswitch
  289. endfor
  290. ## Take action
  291. switch (action)
  292. case "list"
  293. if (nargout == 0)
  294. installed_packages (local_list, global_list);
  295. elseif (nargout == 1)
  296. local_packages = installed_packages (local_list, global_list);
  297. elseif (nargout == 2)
  298. [local_packages, global_packages] = installed_packages (local_list,
  299. global_list);
  300. else
  301. error ("too many output arguments requested");
  302. endif
  303. case "install"
  304. if (length (files) == 0)
  305. error ("you must specify at least one filename when calling 'pkg install'");
  306. endif
  307. install (files, deps, auto, prefix, archprefix, verbose, local_list,
  308. global_list, global_install);
  309. case "uninstall"
  310. if (length (files) == 0)
  311. error ("you must specify at least one package when calling 'pkg uninstall'");
  312. endif
  313. uninstall (files, deps, verbose, local_list,
  314. global_list, global_install);
  315. case "load"
  316. if (length (files) == 0)
  317. error ("you must specify at least one package, 'all' or 'auto' when calling 'pkg load'");
  318. endif
  319. load_packages (files, deps, local_list, global_list);
  320. case "unload"
  321. if (length (files) == 0)
  322. error ("you must specify at least one package or 'all' when calling 'pkg unload'");
  323. endif
  324. unload_packages (files, deps, local_list, global_list);
  325. case "prefix"
  326. if (length (files) == 0 && nargout == 0)
  327. printf ("Installation prefix: %s\n", prefix);
  328. printf ("Architecture dependent prefix: %s\n", archprefix);
  329. elseif (length (files) == 0 && nargout >= 1)
  330. local_packages = prefix;
  331. global_packages = archprefix;
  332. elseif (length (files) >= 1 && nargout <= 2 && ischar (files{1}))
  333. prefix = files{1};
  334. prefix = absolute_pathname (prefix);
  335. local_packages = prefix;
  336. user_prefix = true;
  337. if (length (files) >= 2 && ischar (files{2}))
  338. archprefix = files{2};
  339. try
  340. archprefix = absolute_pathname (archprefix);
  341. catch
  342. mkdir (archprefix);
  343. warning ("creating the directory %s\n", archprefix);
  344. archprefix = absolute_pathname (archprefix);
  345. end_try_catch
  346. global_packages = archprefix;
  347. endif
  348. else
  349. error ("you must specify a prefix directory, or request an output argument");
  350. endif
  351. case "local_list"
  352. if (length (files) == 0 && nargout == 0)
  353. disp (local_list);
  354. elseif (length (files) == 0 && nargout == 1)
  355. local_packages = local_list;
  356. elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
  357. try
  358. local_list = absolute_pathname (files{1});
  359. catch
  360. ## Force file to be created
  361. fclose (fopen (files{1}, "wt"));
  362. local_list = absolute_pathname (files{1});
  363. end_try_catch
  364. else
  365. error ("you must specify a local_list file, or request an output argument");
  366. endif
  367. case "global_list"
  368. if (length (files) == 0 && nargout == 0)
  369. disp(global_list);
  370. elseif (length (files) == 0 && nargout == 1)
  371. local_packages = global_list;
  372. elseif (length (files) == 1 && nargout == 0 && ischar (files{1}))
  373. try
  374. global_list = absolute_pathname (files{1});
  375. catch
  376. ## Force file to be created
  377. fclose (fopen (files{1}, "wt"));
  378. global_list = absolute_pathname (files{1});
  379. end_try_catch
  380. else
  381. error ("you must specify a global_list file, or request an output argument");
  382. endif
  383. case "rebuild"
  384. if (global_install)
  385. global_packages = rebuild (prefix, archprefix, global_list, files,
  386. auto, verbose);
  387. global_packages = save_order (global_packages);
  388. save (global_list, "global_packages");
  389. if (nargout > 0)
  390. local_packages = global_packages;
  391. endif
  392. else
  393. local_packages = rebuild (prefix, archprefix, local_list, files, auto,
  394. verbose);
  395. local_packages = save_order (local_packages);
  396. save (local_list, "local_packages");
  397. if (nargout == 0)
  398. clear ("local_packages");
  399. endif
  400. endif
  401. case "build"
  402. if (length (files) < 2)
  403. error ("you must specify at least the build directory and one filename\nwhen calling 'pkg build'");
  404. endif
  405. build (files, deps, auto, verbose);
  406. case "describe"
  407. if (length (files) == 0)
  408. error ("you must specify at least one package or 'all' when calling 'pkg describe'");
  409. endif
  410. ## FIXME: the name of the output variables is inconsistent
  411. ## with their content
  412. switch (nargout)
  413. case 0
  414. describe (files, verbose, local_list, global_list);
  415. case 1
  416. pkg_desc_list = describe (files, verbose, local_list, ...
  417. global_list);
  418. local_packages = pkg_desc_list;
  419. case 2
  420. [pkg_desc_list, flag] = describe (files, verbose, local_list, ...
  421. global_list);
  422. local_packages = pkg_desc_list;
  423. global_packages = flag;
  424. otherwise
  425. error ("you can request at most two outputs when calling 'pkg describe'");
  426. endswitch
  427. otherwise
  428. error ("you must specify a valid action for 'pkg'. See 'help pkg' for details");
  429. endswitch
  430. endfunction
  431. function descriptions = rebuild (prefix, archprefix, list, files, auto, verbose)
  432. if (isempty (files))
  433. [dirlist, err, msg] = readdir (prefix);
  434. if (err)
  435. error ("couldn't read directory %s: %s", prefix, msg);
  436. endif
  437. ## the two first entries of dirlist are "." and ".."
  438. dirlist([1,2]) = [];
  439. else
  440. old_descriptions = installed_packages (list, list);
  441. wd = pwd ();
  442. unwind_protect
  443. cd (prefix);
  444. dirlist = glob (cellfun(@(x) cstrcat(x, '-*'), files, 'UniformOutput', 0));
  445. unwind_protect_cleanup
  446. cd (wd);
  447. end_unwind_protect
  448. endif
  449. descriptions = {};
  450. for k = 1:length (dirlist)
  451. descfile = fullfile (prefix, dirlist{k}, "packinfo", "DESCRIPTION");
  452. if (verbose)
  453. printf ("recreating package description from %s\n", dirlist{k});
  454. endif
  455. if (exist (descfile, "file"))
  456. desc = get_description (descfile);
  457. desc.dir = fullfile (prefix, dirlist{k});
  458. desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
  459. desc.version));
  460. if (auto != 0)
  461. if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
  462. unlink (fullfile (desc.dir, "packinfo", ".autoload"));
  463. endif
  464. if (auto < 0)
  465. desc.autoload = 0;
  466. elseif (auto > 0)
  467. desc.autoload = 1;
  468. fclose (fopen (fullfile (desc.dir, "packinfo", ".autoload"), "wt"));
  469. endif
  470. else
  471. if (exist (fullfile (desc.dir, "packinfo", ".autoload"), "file"))
  472. desc.autoload = 1;
  473. else
  474. desc.autoload = 0;
  475. endif
  476. endif
  477. descriptions{end + 1} = desc;
  478. elseif (verbose)
  479. warning ("directory %s is not a valid package", dirlist{k});
  480. endif
  481. endfor
  482. if (! isempty (files))
  483. ## We are rebuilding for a particular package(s) so we should take
  484. ## care to keep the other untouched packages in the descriptions
  485. descriptions = {descriptions{:}, old_descriptions{:}};
  486. dup = [];
  487. for i = 1:length (descriptions)
  488. if (find (dup, i))
  489. continue;
  490. endif
  491. for j = (i+1):length (descriptions)
  492. if (find (dup, j))
  493. continue;
  494. endif
  495. if (strcmp (descriptions{i}.name, descriptions{j}.name))
  496. dup = [dup, j];
  497. endif
  498. endfor
  499. endfor
  500. if (! isempty (dup))
  501. descriptions (dup) = [];
  502. endif
  503. endif
  504. endfunction
  505. function build (files, handle_deps, autoload, verbose)
  506. if (length (files) < 1)
  507. error ("insufficient number of files");
  508. endif
  509. builddir = files{1};
  510. if (! exist (builddir, "dir"))
  511. warning ("creating build directory %s", builddir);
  512. [status, msg] = mkdir (builddir);
  513. if (status != 1)
  514. error ("could not create installation directory: %s", msg);
  515. endif
  516. endif
  517. builddir = absolute_pathname (builddir);
  518. installdir = fullfile (builddir, "install");
  519. if (! exist (installdir, "dir"))
  520. [status, msg] = mkdir (installdir);
  521. if (status != 1)
  522. error ("could not create installation directory: %s", msg);
  523. endif
  524. endif
  525. files(1) = [];
  526. buildlist = fullfile (builddir, "octave_packages");
  527. install (files, handle_deps, autoload, installdir, installdir, verbose,
  528. buildlist, "", false);
  529. unwind_protect
  530. repackage (builddir, buildlist);
  531. unwind_protect_cleanup
  532. unload_packages ({"all"}, handle_deps, buildlist, "");
  533. if (exist (installdir, "dir"))
  534. rm_rf (installdir);
  535. endif
  536. if (exist (buildlist, "file"))
  537. unlink (buildlist);
  538. endif
  539. end_unwind_protect
  540. endfunction
  541. function install (files, handle_deps, autoload, prefix, archprefix, verbose,
  542. local_list, global_list, global_install)
  543. ## Check that the directory in prefix exist. If it doesn't: create it!
  544. if (! exist (prefix, "dir"))
  545. warning ("creating installation directory %s", prefix);
  546. [status, msg] = mkdir (prefix);
  547. if (status != 1)
  548. error ("could not create installation directory: %s", msg);
  549. endif
  550. endif
  551. ## Get the list of installed packages.
  552. [local_packages, global_packages] = installed_packages (local_list,
  553. global_list);
  554. installed_pkgs_lst = {local_packages{:}, global_packages{:}};
  555. if (global_install)
  556. packages = global_packages;
  557. else
  558. packages = local_packages;
  559. endif
  560. ## Uncompress the packages and read the DESCRIPTION files.
  561. tmpdirs = packdirs = descriptions = {};
  562. try
  563. ## Warn about non existent files.
  564. for i = 1:length (files)
  565. if (isempty (glob(files{i})))
  566. warning ("file %s does not exist", files{i});
  567. endif
  568. endfor
  569. ## Unpack the package files and read the DESCRIPTION files.
  570. files = glob (files);
  571. packages_to_uninstall = [];
  572. for i = 1:length (files)
  573. tgz = files{i};
  574. if (exist (tgz, "file"))
  575. ## Create a temporary directory.
  576. tmpdir = tmpnam ();
  577. tmpdirs{end+1} = tmpdir;
  578. if (verbose)
  579. printf ("mkdir (%s)\n", tmpdir);
  580. endif
  581. [status, msg] = mkdir (tmpdir);
  582. if (status != 1)
  583. error ("couldn't create temporary directory: %s", msg);
  584. endif
  585. ## Uncompress the package.
  586. if (verbose)
  587. printf ("untar (%s, %s)\n", tgz, tmpdir);
  588. endif
  589. untar (tgz, tmpdir);
  590. ## Get the name of the directories produced by tar.
  591. [dirlist, err, msg] = readdir (tmpdir);
  592. if (err)
  593. error ("couldn't read directory produced by tar: %s", msg);
  594. endif
  595. if (length (dirlist) > 3)
  596. error ("bundles of packages are not allowed")
  597. endif
  598. endif
  599. ## The filename pointed to an uncompressed package to begin with.
  600. if (exist (tgz, "dir"))
  601. dirlist = {".", "..", tgz};
  602. endif
  603. if (exist (tgz, "file") || exist (tgz, "dir"))
  604. ## The two first entries of dirlist are "." and "..".
  605. if (exist (tgz, "file"))
  606. packdir = fullfile (tmpdir, dirlist{3});
  607. else
  608. packdir = fullfile (pwd(), dirlist{3});
  609. endif
  610. packdirs{end+1} = packdir;
  611. ## Make sure the package contains necessary files.
  612. verify_directory (packdir);
  613. ## Read the DESCRIPTION file.
  614. filename = fullfile (packdir, "DESCRIPTION");
  615. desc = get_description (filename);
  616. ## Verify that package name corresponds with filename.
  617. [dummy, nm] = fileparts (tgz);
  618. if ((length (nm) >= length (desc.name))
  619. && ! strcmp (desc.name, nm(1:length(desc.name))))
  620. error ("package name '%s' doesn't correspond to its filename '%s'",
  621. desc.name, nm);
  622. endif
  623. ## Set default installation directory.
  624. desc.dir = fullfile (prefix, cstrcat (desc.name, "-", desc.version));
  625. ## Set default architecture dependent installation directory.
  626. desc.archprefix = fullfile (archprefix, cstrcat (desc.name, "-",
  627. desc.version));
  628. ## Save desc.
  629. descriptions{end+1} = desc;
  630. ## Are any of the new packages already installed?
  631. ## If so we'll remove the old version.
  632. for j = 1:length (packages)
  633. if (strcmp (packages{j}.name, desc.name))
  634. packages_to_uninstall(end+1) = j;
  635. endif
  636. endfor
  637. endif
  638. endfor
  639. catch
  640. ## Something went wrong, delete tmpdirs.
  641. for i = 1:length (tmpdirs)
  642. rm_rf (tmpdirs{i});
  643. endfor
  644. rethrow (lasterror ());
  645. end_try_catch
  646. ## Check dependencies.
  647. if (handle_deps)
  648. ok = true;
  649. error_text = "";
  650. for i = 1:length (descriptions)
  651. desc = descriptions{i};
  652. idx2 = complement (i, 1:length(descriptions));
  653. if (global_install)
  654. ## Global installation is not allowed to have dependencies on locally
  655. ## installed packages.
  656. idx1 = complement (packages_to_uninstall,
  657. 1:length(global_packages));
  658. pseudo_installed_packages = {global_packages{idx1}, ...
  659. descriptions{idx2}};
  660. else
  661. idx1 = complement (packages_to_uninstall,
  662. 1:length(local_packages));
  663. pseudo_installed_packages = {local_packages{idx1}, ...
  664. global_packages{:}, ...
  665. descriptions{idx2}};
  666. endif
  667. bad_deps = get_unsatisfied_deps (desc, pseudo_installed_packages);
  668. ## Are there any unsatisfied dependencies?
  669. if (! isempty (bad_deps))
  670. ok = false;
  671. for i = 1:length (bad_deps)
  672. dep = bad_deps{i};
  673. error_text = cstrcat (error_text, " ", desc.name, " needs ",
  674. dep.package, " ", dep.operator, " ",
  675. dep.version, "\n");
  676. endfor
  677. endif
  678. endfor
  679. ## Did we find any unsatisfied dependencies?
  680. if (! ok)
  681. error ("the following dependencies where unsatisfied:\n %s", error_text);
  682. endif
  683. endif
  684. ## Prepare each package for installation.
  685. try
  686. for i = 1:length (descriptions)
  687. desc = descriptions{i};
  688. pdir = packdirs{i};
  689. prepare_installation (desc, pdir);
  690. configure_make (desc, pdir, verbose);
  691. endfor
  692. catch
  693. ## Something went wrong, delete tmpdirs.
  694. for i = 1:length (tmpdirs)
  695. rm_rf (tmpdirs{i});
  696. endfor
  697. rethrow (lasterror ());
  698. end_try_catch
  699. ## Uninstall the packages that will be replaced.
  700. try
  701. for i = packages_to_uninstall
  702. if (global_install)
  703. uninstall ({global_packages{i}.name}, false, verbose, local_list,
  704. global_list, global_install);
  705. else
  706. uninstall ({local_packages{i}.name}, false, verbose, local_list,
  707. global_list, global_install);
  708. endif
  709. endfor
  710. catch
  711. ## Something went wrong, delete tmpdirs.
  712. for i = 1:length (tmpdirs)
  713. rm_rf (tmpdirs{i});
  714. endfor
  715. rethrow (lasterror ());
  716. end_try_catch
  717. ## Install each package.
  718. try
  719. for i = 1:length (descriptions)
  720. desc = descriptions{i};
  721. pdir = packdirs{i};
  722. copy_files (desc, pdir, global_install);
  723. create_pkgadddel (desc, pdir, "PKG_ADD", global_install);
  724. create_pkgadddel (desc, pdir, "PKG_DEL", global_install);
  725. finish_installation (desc, pdir, global_install);
  726. generate_lookfor_cache (desc);
  727. endfor
  728. catch
  729. ## Something went wrong, delete tmpdirs.
  730. for i = 1:length (tmpdirs)
  731. rm_rf (tmpdirs{i});
  732. endfor
  733. for i = 1:length (descriptions)
  734. rm_rf (descriptions{i}.dir);
  735. rm_rf (getarchdir (descriptions{i}));
  736. endfor
  737. rethrow (lasterror ());
  738. end_try_catch
  739. ## Check if the installed directory is empty. If it is remove it
  740. ## from the list.
  741. for i = length (descriptions):-1:1
  742. if (dirempty (descriptions{i}.dir, {"packinfo", "doc"}) &&
  743. dirempty (getarchdir (descriptions{i})))
  744. warning ("package %s is empty\n", descriptions{i}.name);
  745. rm_rf (descriptions{i}.dir);
  746. rm_rf (getarchdir (descriptions{i}));
  747. descriptions(i) = [];
  748. endif
  749. endfor
  750. ## If the package requested that it is autoloaded, or the installer
  751. ## requested that it is, then mark the package as autoloaded.
  752. for i = length (descriptions):-1:1
  753. if (autoload > 0 || (autoload == 0 && isautoload (descriptions(i))))
  754. fclose (fopen (fullfile (descriptions{i}.dir, "packinfo",
  755. ".autoload"), "wt"));
  756. descriptions{i}.autoload = 1;
  757. endif
  758. endfor
  759. ## Add the packages to the package list.
  760. try
  761. if (global_install)
  762. idx = complement (packages_to_uninstall, 1:length(global_packages));
  763. global_packages = save_order ({global_packages{idx}, descriptions{:}});
  764. save (global_list, "global_packages");
  765. installed_pkgs_lst = {local_packages{:}, global_packages{:}};
  766. else
  767. idx = complement (packages_to_uninstall, 1:length(local_packages));
  768. local_packages = save_order ({local_packages{idx}, descriptions{:}});
  769. save (local_list, "local_packages");
  770. installed_pkgs_lst = {local_packages{:}, global_packages{:}};
  771. endif
  772. catch
  773. ## Something went wrong, delete tmpdirs.
  774. for i = 1:length (tmpdirs)
  775. rm_rf (tmpdirs{i});
  776. endfor
  777. for i = 1:length (descriptions)
  778. rm_rf (descriptions{i}.dir);
  779. endfor
  780. if (global_install)
  781. printf ("error: couldn't append to %s\n", global_list);
  782. else
  783. printf ("error: couldn't append to %s\n", local_list);
  784. endif
  785. rethrow (lasterror ());
  786. end_try_catch
  787. ## All is well, let's clean up.
  788. for i = 1:length (tmpdirs)
  789. [status, msg] = rm_rf (tmpdirs{i});
  790. if (status != 1)
  791. warning ("couldn't clean up after my self: %s\n", msg);
  792. endif
  793. endfor
  794. ## Add the newly installed packages to the path, so the user
  795. ## can begin using them. Only load them if they are marked autoload.
  796. if (length (descriptions) > 0)
  797. idx = [];
  798. for i = 1:length (descriptions)
  799. if (isautoload (descriptions(i)))
  800. nm = descriptions{i}.name;
  801. for j = 1:length (installed_pkgs_lst)
  802. if (strcmp (nm, installed_pkgs_lst{j}.name))
  803. idx (end + 1) = j;
  804. break;
  805. endif
  806. endfor
  807. endif
  808. endfor
  809. load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
  810. global_install);
  811. endif
  812. endfunction
  813. function uninstall (pkgnames, handle_deps, verbose, local_list,
  814. global_list, global_install)
  815. ## Get the list of installed packages.
  816. [local_packages, global_packages] = installed_packages(local_list,
  817. global_list);
  818. if (global_install)
  819. installed_pkgs_lst = {local_packages{:}, global_packages{:}};
  820. else
  821. installed_pkgs_lst = local_packages;
  822. endif
  823. num_packages = length (installed_pkgs_lst);
  824. delete_idx = [];
  825. for i = 1:num_packages
  826. cur_name = installed_pkgs_lst{i}.name;
  827. if (any (strcmp (cur_name, pkgnames)))
  828. delete_idx(end+1) = i;
  829. endif
  830. endfor
  831. ## Are all the packages that should be uninstalled already installed?
  832. if (length (delete_idx) != length (pkgnames))
  833. if (global_install)
  834. ## Try again for a locally installed package.
  835. installed_pkgs_lst = local_packages;
  836. num_packages = length (installed_pkgs_lst);
  837. delete_idx = [];
  838. for i = 1:num_packages
  839. cur_name = installed_pkgs_lst{i}.name;
  840. if (any (strcmp (cur_name, pkgnames)))
  841. delete_idx(end+1) = i;
  842. endif
  843. endfor
  844. if (length (delete_idx) != length (pkgnames))
  845. ## FIXME: We should have a better error message.
  846. warning ("some of the packages you want to uninstall are not installed");
  847. endif
  848. else
  849. ## FIXME: We should have a better error message.
  850. warning ("some of the packages you want to uninstall are not installed");
  851. endif
  852. endif
  853. ## Compute the packages that will remain installed.
  854. idx = complement (delete_idx, 1:num_packages);
  855. remaining_packages = {installed_pkgs_lst{idx}};
  856. ## Check dependencies.
  857. if (handle_deps)
  858. error_text = "";
  859. for i = 1:length (remaining_packages)
  860. desc = remaining_packages{i};
  861. bad_deps = get_unsatisfied_deps (desc, remaining_packages);
  862. ## Will the uninstallation break any dependencies?
  863. if (! isempty (bad_deps))
  864. for i = 1:length (bad_deps)
  865. dep = bad_deps{i};
  866. error_text = cstrcat (error_text, " ", desc.name, " needs ",
  867. dep.package, " ", dep.operator, " ",
  868. dep.version, "\n");
  869. endfor
  870. endif
  871. endfor
  872. if (! isempty (error_text))
  873. error ("the following dependencies where unsatisfied:\n %s", error_text);
  874. endif
  875. endif
  876. ## Delete the directories containing the packages.
  877. for i = delete_idx
  878. desc = installed_pkgs_lst{i};
  879. ## If an 'on_uninstall.m' exist, call it!
  880. if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file"))
  881. wd = pwd ();
  882. cd (fullfile (desc.dir, "packinfo"));
  883. on_uninstall (desc);
  884. cd (wd);
  885. endif
  886. ## Do the actual deletion.
  887. if (desc.loaded)
  888. rmpath (desc.dir);
  889. if (exist (getarchdir (desc)))
  890. rmpath (getarchdir (desc));
  891. endif
  892. endif
  893. if (exist (desc.dir, "dir"))
  894. [status, msg] = rm_rf (desc.dir);
  895. if (status != 1)
  896. error ("couldn't delete directory %s: %s", desc.dir, msg);
  897. endif
  898. [status, msg] = rm_rf (getarchdir (desc));
  899. if (status != 1)
  900. error ("couldn't delete directory %s: %s", getarchdir (desc), msg);
  901. endif
  902. if (dirempty (desc.archprefix))
  903. rm_rf (desc.archprefix);
  904. endif
  905. else
  906. warning ("directory %s previously lost", desc.dir);
  907. endif
  908. endfor
  909. ## Write a new ~/.octave_packages.
  910. if (global_install)
  911. if (length (remaining_packages) == 0)
  912. unlink (global_list);
  913. else
  914. global_packages = save_order (remaining_packages);
  915. save (global_list, "global_packages");
  916. endif
  917. else
  918. if (length (remaining_packages) == 0)
  919. unlink (local_list);
  920. else
  921. local_packages = save_order (remaining_packages);
  922. save (local_list, "local_packages");
  923. endif
  924. endif
  925. endfunction
  926. function [pkg_desc_list, flag] = describe (pkgnames, verbose,
  927. local_list, global_list)
  928. ## Get the list of installed packages.
  929. installed_pkgs_lst = installed_packages(local_list, global_list);
  930. num_packages = length (installed_pkgs_lst);
  931. describe_all = false;
  932. if (any (strcmp ("all", pkgnames)))
  933. describe_all = true;
  934. flag(1:num_packages) = {"Not Loaded"};
  935. num_pkgnames = num_packages;
  936. else
  937. num_pkgnames = length (pkgnames);
  938. flag(1:num_pkgnames) = {"Not installed"};
  939. endif
  940. for i = 1:num_packages
  941. curr_name = installed_pkgs_lst{i}.name;
  942. if (describe_all)
  943. name_pos = i;
  944. else
  945. name_pos = find(strcmp (curr_name, pkgnames));
  946. endif
  947. if (! isempty (name_pos))
  948. if (installed_pkgs_lst{i}.loaded)
  949. flag{name_pos} = "Loaded";
  950. else
  951. flag{name_pos} = "Not loaded";
  952. endif
  953. pkg_desc_list{name_pos}.name = installed_pkgs_lst{i}.name;
  954. pkg_desc_list{name_pos}.version = installed_pkgs_lst{i}.version;
  955. pkg_desc_list{name_pos}.description = installed_pkgs_lst{i}.description;
  956. pkg_desc_list{name_pos}.provides = parse_pkg_idx (installed_pkgs_lst{i}.dir);
  957. endif
  958. endfor
  959. non_inst = find (strcmp (flag, "Not installed"));
  960. if (! isempty (non_inst))
  961. if (nargout < 2)
  962. non_inst_str = sprintf (" %s ", pkgnames{non_inst});
  963. error ("some packages are not installed: %s", non_inst_str);
  964. else
  965. pkg_desc_list{non_inst} = struct ("name", {}, "description",
  966. {}, "provides", {});
  967. endif
  968. endif
  969. if (nargout == 0)
  970. for i = 1:num_pkgnames
  971. print_package_description (pkg_desc_list{i}.name,
  972. pkg_desc_list{i}.version,
  973. pkg_desc_list{i}.provides,
  974. pkg_desc_list{i}.description,
  975. flag{i}, verbose);
  976. endfor
  977. endif
  978. endfunction
  979. ## AUXILIARY FUNCTIONS
  980. ## Read an INDEX file.
  981. function [pkg_idx_struct] = parse_pkg_idx (packdir)
  982. index_file = fullfile (packdir, "packinfo", "INDEX");
  983. if (! exist (index_file, "file"))
  984. error ("could not find any INDEX file in directory %s, try 'pkg rebuild all' to generate missing INDEX files", packdir);
  985. endif
  986. [fid, msg] = fopen (index_file, "r");
  987. if (fid == -1)
  988. error ("the INDEX file %s could not be read: %s",
  989. index_file, msg);
  990. endif
  991. cat_num = 1;
  992. pkg_idx_struct{1}.category = "Uncategorized";
  993. pkg_idx_struct{1}.functions = {};
  994. line = fgetl (fid);
  995. while (isempty (strfind (line, ">>")) && ! feof (fid))
  996. line = fgetl (fid);
  997. endwhile
  998. while (! feof (fid) || line != -1)
  999. if (! any (! isspace (line)) || line(1) == "#" || any (line == "="))
  1000. ## Comments, blank lines or comments about unimplemented
  1001. ## functions: do nothing
  1002. ## FIXME: probably comments and pointers to external functions
  1003. ## could be treated better when printing to screen?
  1004. elseif (! isempty (strfind (line, ">>")))
  1005. ## Skip package name and description as they are in DESCRIPTION
  1006. ## already.
  1007. elseif (! isspace (line(1)))
  1008. ## Category.
  1009. if (! isempty (pkg_idx_struct{cat_num}.functions))
  1010. pkg_idx_struct{++cat_num}.functions = {};
  1011. endif
  1012. pkg_idx_struct{cat_num}.category = deblank (line);
  1013. else
  1014. ## Function names.
  1015. while (any (! isspace (line)))
  1016. [fun_name, line] = strtok (line);
  1017. pkg_idx_struct{cat_num}.functions{end+1} = deblank (fun_name);
  1018. endwhile
  1019. endif
  1020. line = fgetl (fid);
  1021. endwhile
  1022. fclose (fid);
  1023. endfunction
  1024. function print_package_description (pkg_name, pkg_ver, pkg_idx_struct,
  1025. pkg_desc, status, verbose)
  1026. printf ("---\nPackage name:\n\t%s\n", pkg_name);
  1027. printf ("Version:\n\t%s\n", pkg_ver);
  1028. printf ("Short description:\n\t%s\n", pkg_desc);
  1029. printf ("Status:\n\t%s\n", status);
  1030. if (verbose)
  1031. printf ("---\nProvides:\n");
  1032. for i = 1:length(pkg_idx_struct)
  1033. if (! isempty (pkg_idx_struct{i}.functions))
  1034. printf ("%s\n", pkg_idx_struct{i}.category);
  1035. for j = 1:length(pkg_idx_struct{i}.functions)
  1036. printf ("\t%s\n", pkg_idx_struct{i}.functions{j});
  1037. endfor
  1038. endif
  1039. endfor
  1040. endif
  1041. endfunction
  1042. function pth = absolute_pathname (pth)
  1043. [status, msg, msgid] = fileattrib (pth);
  1044. if (status != 1)
  1045. error ("could not find the file or path %s", pth);
  1046. else
  1047. pth = msg.Name;
  1048. endif
  1049. endfunction
  1050. function repackage (builddir, buildlist)
  1051. packages = installed_packages (buildlist, buildlist);
  1052. wd = pwd();
  1053. for i = 1 : length(packages)
  1054. pack = packages{i};
  1055. unwind_protect
  1056. cd (builddir);
  1057. mkdir (pack.name);
  1058. mkdir (fullfile (pack.name, "inst"));
  1059. copyfile (fullfile (pack.dir, "*"), fullfile (pack.name, "inst"));
  1060. movefile (fullfile (pack.name, "inst","packinfo", "*"), pack.name);
  1061. if (exist (fullfile (pack.name, "inst","packinfo", ".autoload"), "file"))
  1062. unlink (fullfile (pack.name, "inst","packinfo", ".autoload"));
  1063. endif
  1064. rmdir (fullfile (pack.name, "inst", "packinfo"));
  1065. if (exist (fullfile (pack.name, "inst", "doc"), "dir"))
  1066. movefile (fullfile (pack.name, "inst", "doc"), pack.name);
  1067. endif
  1068. if (exist (fullfile (pack.name, "inst", "bin"), "dir"))
  1069. movefile (fullfile (pack.name, "inst", "bin"), pack.name);
  1070. endif
  1071. archdir = fullfile (pack.archprefix, cstrcat (pack.name, "-",
  1072. pack.version), getarch ());
  1073. if (exist (archdir, "dir"))
  1074. if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
  1075. unlink (fullfile (pack.name, "inst", "PKG_ADD"));
  1076. endif
  1077. if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
  1078. unlink (fullfile (pack.name, "inst", "PKG_DEL"));
  1079. endif
  1080. if (exist (fullfile (archdir, "PKG_ADD"), "file"))
  1081. movefile (fullfile (archdir, "PKG_ADD"),
  1082. fullfile (pack.name, "PKG_ADD"));
  1083. endif
  1084. if (exist (fullfile (archdir, "PKG_DEL"), "file"))
  1085. movefile (fullfile (archdir, "PKG_DEL"),
  1086. fullfile (pack.name, "PKG_DEL"));
  1087. endif
  1088. else
  1089. if (exist (fullfile (pack.name, "inst", "PKG_ADD"), "file"))
  1090. movefile (fullfile (pack.name, "inst", "PKG_ADD"),
  1091. fullfile (pack.name, "PKG_ADD"));
  1092. endif
  1093. if (exist (fullfile (pack.name, "inst", "PKG_DEL"), "file"))
  1094. movefile (fullfile (pack.name, "inst", "PKG_DEL"),
  1095. fullfile (pack.name, "PKG_DEL"));
  1096. endif
  1097. endif
  1098. tfile = cstrcat (pack.name, "-", pack.version, ".tar");
  1099. tar (tfile, pack.name);
  1100. try
  1101. gzip (tfile);
  1102. unlink (tfile);
  1103. catch
  1104. warning ("failed to compress %s", tfile);
  1105. end_try_catch
  1106. unwind_protect_cleanup
  1107. if (exist (pack.name, "dir"))
  1108. rm_rf (pack.name);
  1109. endif
  1110. cd (wd);
  1111. end_unwind_protect
  1112. endfor
  1113. endfunction
  1114. function auto = isautoload (desc)
  1115. auto = false;
  1116. if (isfield (desc{1}, "autoload"))
  1117. a = desc{1}.autoload;
  1118. if ((isnumeric (a) && a > 0)
  1119. || (ischar (a) && (strcmpi (a, "true")
  1120. || strcmpi (a, "on")
  1121. || strcmpi (a, "yes")
  1122. || strcmpi (a, "1"))))
  1123. auto = true;
  1124. endif
  1125. endif
  1126. endfunction
  1127. function prepare_installation (desc, packdir)
  1128. ## Is there a pre_install to call?
  1129. if (exist (fullfile (packdir, "pre_install.m"), "file"))
  1130. wd = pwd ();
  1131. try
  1132. cd (packdir);
  1133. pre_install (desc);
  1134. cd (wd);
  1135. catch
  1136. cd (wd);
  1137. rethrow (lasterror ());
  1138. end_try_catch
  1139. endif
  1140. ## If the directory "inst" doesn't exist, we create it.
  1141. inst_dir = fullfile (packdir, "inst");
  1142. if (! exist (inst_dir, "dir"))
  1143. [status, msg] = mkdir (inst_dir);
  1144. if (status != 1)
  1145. rm_rf (desc.dir);
  1146. error ("the 'inst' directory did not exist and could not be created: %s",
  1147. msg);
  1148. endif
  1149. endif
  1150. endfunction
  1151. function configure_make (desc, packdir, verbose)
  1152. ## Perform ./configure, make, make install in "src".
  1153. if (exist (fullfile (packdir, "src"), "dir"))
  1154. src = fullfile (packdir, "src");
  1155. ## Configure.
  1156. if (exist (fullfile (src, "configure"), "file"))
  1157. flags = "";
  1158. if (isempty (getenv ("CC")))
  1159. flags = cstrcat (flags, " CC=\"", octave_config_info ("CC"), "\"");
  1160. endif
  1161. if (isempty (getenv ("CXX")))
  1162. flags = cstrcat (flags, " CXX=\"", octave_config_info ("CXX"), "\"");
  1163. endif
  1164. if (isempty (getenv ("AR")))
  1165. flags = cstrcat (flags, " AR=\"", octave_config_info ("AR"), "\"");
  1166. endif
  1167. if (isempty (getenv ("RANLIB")))
  1168. flags = cstrcat (flags, " RANLIB=\"", octave_config_info ("RANLIB"), "\"");
  1169. endif
  1170. [status, output] = shell (strcat ("cd '", src, "'; ./configure --prefix=\"",
  1171. desc.dir, "\"", flags));
  1172. if (status != 0)
  1173. rm_rf (desc.dir);
  1174. error ("the configure script returned the following error: %s", output);
  1175. elseif (verbose)
  1176. printf("%s", output);
  1177. endif
  1178. endif
  1179. ## Make.
  1180. if (exist (fullfile (src, "Makefile"), "file"))
  1181. [status, output] = shell (cstrcat ("export INSTALLDIR=\"", desc.dir,
  1182. "\"; make -C '", src, "'"));
  1183. if (status != 0)
  1184. rm_rf (desc.dir);
  1185. error ("'make' returned the following error: %s", output);
  1186. elseif (verbose)
  1187. printf("%s", output);
  1188. endif
  1189. endif
  1190. ## Copy files to "inst" and "inst/arch" (this is instead of 'make
  1191. ## install').
  1192. files = fullfile (src, "FILES");
  1193. instdir = fullfile (packdir, "inst");
  1194. archdir = fullfile (packdir, "inst", getarch ());
  1195. ## Get file names.
  1196. if (exist (files, "file"))
  1197. [fid, msg] = fopen (files, "r");
  1198. if (fid < 0)
  1199. error ("couldn't open %s: %s", files, msg);
  1200. endif
  1201. filenames = char (fread (fid))';
  1202. fclose (fid);
  1203. if (filenames(end) == "\n")
  1204. filenames(end) = [];
  1205. endif
  1206. filenames = split_by (filenames, "\n");
  1207. delete_idx = [];
  1208. for i = 1:length (filenames)
  1209. if (! all (isspace (filenames{i})))
  1210. filenames{i} = fullfile (src, filenames{i});
  1211. else
  1212. delete_idx(end+1) = i;
  1213. endif
  1214. endfor
  1215. filenames(delete_idx) = [];
  1216. else
  1217. m = dir (fullfile (src, "*.m"));
  1218. oct = dir (fullfile (src, "*.oct"));
  1219. mex = dir (fullfile (src, "*.mex"));
  1220. filenames = cellfun (@(x) fullfile (src, x),
  1221. {m.name, oct.name, mex.name},
  1222. "UniformOutput", false);
  1223. endif
  1224. ## Split into architecture dependent and independent files.
  1225. if (isempty (filenames))
  1226. idx = [];
  1227. else
  1228. idx = cellfun (@is_architecture_dependent, filenames);
  1229. endif
  1230. archdependent = filenames (idx);
  1231. archindependent = filenames (!idx);
  1232. ## Copy the files.
  1233. if (! all (isspace ([filenames{:}])))
  1234. if (! exist (instdir, "dir")) # fixindent
  1235. mkdir (instdir);
  1236. endif
  1237. if (! all (isspace ([archindependent{:}])))
  1238. if (verbose)
  1239. printf ("copyfile");
  1240. printf (" %s", archindependent{:});
  1241. printf ("%s\n", instdir);
  1242. endif
  1243. [status, output] = copyfile (archindependent, instdir);
  1244. if (status != 1)
  1245. rm_rf (desc.dir);
  1246. error ("Couldn't copy files from 'src' to 'inst': %s", output);
  1247. endif
  1248. endif
  1249. if (! all (isspace ([archdependent{:}])))
  1250. if (verbose)
  1251. printf ("copyfile");
  1252. printf (" %s", archdependent{:});
  1253. printf (" %s\n", archdir);
  1254. endif
  1255. if (! exist (archdir, "dir"))
  1256. mkdir (archdir);
  1257. endif
  1258. [status, output] = copyfile (archdependent, archdir);
  1259. if (status != 1)
  1260. rm_rf (desc.dir);
  1261. error ("Couldn't copy files from 'src' to 'inst': %s", output);
  1262. endif
  1263. endif
  1264. endif
  1265. endif
  1266. endfunction
  1267. function pkg = extract_pkg (nm, pat)
  1268. fid = fopen (nm, "rt");
  1269. pkg = "";
  1270. if (fid >= 0)
  1271. while (! feof (fid))
  1272. ln = fgetl (fid);
  1273. if (ln > 0)
  1274. t = regexp (ln, pat, "tokens");
  1275. if (! isempty (t))
  1276. pkg = cstrcat (pkg, "\n", t{1}{1});
  1277. endif
  1278. endif
  1279. endwhile
  1280. if (! isempty (pkg))
  1281. pkg = cstrcat (pkg, "\n");
  1282. endif
  1283. fclose (fid);
  1284. endif
  1285. endfunction
  1286. function create_pkgadddel (desc, packdir, nm, global_install)
  1287. instpkg = fullfile (desc.dir, nm);
  1288. instfid = fopen (instpkg, "wt");
  1289. ## If it is exists, most of the PKG_* file should go into the
  1290. ## architecture dependent directory so that the autoload/mfilename
  1291. ## commands work as expected. The only part that doesn't is the
  1292. ## part in the main directory.
  1293. archdir = fullfile (getarchprefix (desc), cstrcat (desc.name, "-",
  1294. desc.version), getarch ());
  1295. if (exist (getarchdir (desc, global_install), "dir"))
  1296. archpkg = fullfile (getarchdir (desc, global_install), nm);
  1297. archfid = fopen (archpkg, "at");
  1298. else
  1299. archpkg = instpkg;
  1300. archfid = instfid;
  1301. endif
  1302. if (archfid >= 0 && instfid >= 0)
  1303. ## Search all dot-m files for PKG commands.
  1304. lst = dir (fullfile (packdir, "inst", "*.m"));
  1305. for i = 1:length (lst)
  1306. nam = fullfile (packdir, "inst", lst(i).name);
  1307. fwrite (instfid, extract_pkg (nam, ['^[#%][#%]* *' nm ': *(.*)$']));
  1308. endfor
  1309. ## Search all C++ source files for PKG commands.
  1310. lst = dir (fullfile (packdir, "src", "*.cc"));
  1311. for i = 1:length (lst)
  1312. nam = fullfile (packdir, "src", lst(i).name);
  1313. fwrite (archfid, extract_pkg (nam, ['^//* *' nm ': *(.*)$']));
  1314. fwrite (archfid, extract_pkg (nam, ['^/\** *' nm ': *(.*) *\*/$']));
  1315. endfor
  1316. ## Add developer included PKG commands.
  1317. packdirnm = fullfile (packdir, nm);
  1318. if (exist (packdirnm, "file"))
  1319. fid = fopen (packdirnm, "rt");
  1320. if (fid >= 0)
  1321. while (! feof (fid))
  1322. ln = fgets (fid);
  1323. if (ln > 0)
  1324. fwrite (archfid, ln);
  1325. endif
  1326. endwhile
  1327. fclose (fid);
  1328. endif
  1329. endif
  1330. ## If the files is empty remove it.
  1331. fclose (instfid);
  1332. t = dir (instpkg);
  1333. if (t.bytes <= 0)
  1334. unlink (instpkg);
  1335. endif
  1336. if (instfid != archfid)
  1337. fclose (archfid);
  1338. t = dir (archpkg);
  1339. if (t.bytes <= 0)
  1340. unlink (archpkg);
  1341. endif
  1342. endif
  1343. endif
  1344. endfunction
  1345. function copy_files (desc, packdir, global_install)
  1346. ## Create the installation directory.
  1347. if (! exist (desc.dir, "dir"))
  1348. [status, output] = mkdir (desc.dir);
  1349. if (status != 1)
  1350. error ("couldn't create installation directory %s : %s",
  1351. desc.dir, output);
  1352. endif
  1353. endif
  1354. octfiledir = getarchdir (desc);
  1355. ## Copy the files from "inst" to installdir.
  1356. instdir = fullfile (packdir, "inst");
  1357. if (! dirempty (instdir))
  1358. [status, output] = copyfile (fullfile (instdir, "*"), desc.dir);
  1359. if (status != 1)
  1360. rm_rf (desc.dir);
  1361. error ("couldn't copy files to the installation directory");
  1362. endif
  1363. if (exist (fullfile (desc.dir, getarch ()), "dir") &&
  1364. ! strcmp (fullfile (desc.dir, getarch ()), octfiledir))
  1365. if (! exist (octfiledir, "dir"))
  1366. ## Can be required to create upto three levels of dirs.
  1367. octm1 = fileparts (octfiledir);
  1368. if (! exist (octm1, "dir"))
  1369. octm2 = fileparts (octm1);
  1370. if (! exist (octm2, "dir"))
  1371. octm3 = fileparts (octm2);
  1372. if (! exist (octm3, "dir"))
  1373. [status, output] = mkdir (octm3);
  1374. if (status != 1)
  1375. rm_rf (desc.dir);
  1376. error ("couldn't create installation directory %s : %s",
  1377. octm3, output);
  1378. endif
  1379. endif
  1380. [status, output] = mkdir (octm2);
  1381. if (status != 1)
  1382. rm_rf (desc.dir);
  1383. error ("couldn't create installation directory %s : %s",
  1384. octm2, output);
  1385. endif
  1386. endif
  1387. [status, output] = mkdir (octm1);
  1388. if (status != 1)
  1389. rm_rf (desc.dir);
  1390. error ("couldn't create installation directory %s : %s",
  1391. octm1, output);
  1392. endif
  1393. endif
  1394. [status, output] = mkdir (octfiledir);
  1395. if (status != 1)
  1396. rm_rf (desc.dir);
  1397. error ("couldn't create installation directory %s : %s",
  1398. octfiledir, output);
  1399. endif
  1400. endif
  1401. [status, output] = movefile (fullfile (desc.dir, getarch (), "*"),
  1402. octfiledir);
  1403. rm_rf (fullfile (desc.dir, getarch ()));
  1404. if (status != 1)
  1405. rm_rf (desc.dir);
  1406. rm_rf (octfiledir);
  1407. error ("couldn't copy files to the installation directory");
  1408. endif
  1409. endif
  1410. endif
  1411. ## Create the "packinfo" directory.
  1412. packinfo = fullfile (desc.dir, "packinfo");
  1413. [status, msg] = mkdir (packinfo);
  1414. if (status != 1)
  1415. rm_rf (desc.dir);
  1416. rm_rf (octfiledir);
  1417. error ("couldn't create packinfo directory: %s", msg);
  1418. endif
  1419. ## Copy DESCRIPTION.
  1420. [status, output] = copyfile (fullfile (packdir, "DESCRIPTION"), packinfo);
  1421. if (status != 1)
  1422. rm_rf (desc.dir);
  1423. rm_rf (octfiledir);
  1424. error ("couldn't copy DESCRIPTION: %s", output);
  1425. endif
  1426. ## Copy COPYING.
  1427. [status, output] = copyfile (fullfile (packdir, "COPYING"), packinfo);
  1428. if (status != 1)
  1429. rm_rf (desc.dir);
  1430. rm_rf (octfiledir);
  1431. error ("couldn't copy COPYING: %s", output);
  1432. endif
  1433. ## If the file ChangeLog exists, copy it.
  1434. changelog_file = fullfile (packdir, "ChangeLog");
  1435. if (exist (changelog_file, "file"))
  1436. [status, output] = copyfile (changelog_file, packinfo);
  1437. if (status != 1)
  1438. rm_rf (desc.dir);
  1439. rm_rf (octfiledir);
  1440. error ("couldn't copy ChangeLog file: %s", output);
  1441. endif
  1442. endif
  1443. ## Is there an INDEX file to copy or should we generate one?
  1444. index_file = fullfile (packdir, "INDEX");
  1445. if (exist(index_file, "file"))
  1446. [status, output] = copyfile (index_file, packinfo);
  1447. if (status != 1)
  1448. rm_rf (desc.dir);
  1449. rm_rf (octfiledir);
  1450. error ("couldn't copy INDEX file: %s", output);
  1451. endif
  1452. else
  1453. try
  1454. write_index (desc, fullfile (packdir, "inst"),
  1455. fullfile (packinfo, "INDEX"), global_install);
  1456. catch
  1457. rm_rf (desc.dir);
  1458. rm_rf (octfiledir);
  1459. rethrow (lasterror ());
  1460. end_try_catch
  1461. endif
  1462. ## Is there an 'on_uninstall.m' to install?
  1463. fon_uninstall = fullfile (packdir, "on_uninstall.m");
  1464. if (exist (fon_uninstall, "file"))
  1465. [status, output] = copyfile (fon_uninstall, packinfo);
  1466. if (status != 1)
  1467. rm_rf (desc.dir);
  1468. rm_rf (octfiledir);
  1469. error ("couldn't copy on_uninstall.m: %s", output);
  1470. endif
  1471. endif
  1472. ## Is there a doc/ directory that needs to be installed?
  1473. docdir = fullfile (packdir, "doc");
  1474. if (exist (docdir, "dir") && ! dirempty (docdir))
  1475. [status, output] = copyfile (docdir, desc.dir);
  1476. endif
  1477. ## Is there a bin/ directory that needs to be installed?
  1478. ## FIXME: Need to treat architecture dependent files in bin/
  1479. bindir = fullfile (packdir, "bin");
  1480. if (exist (bindir, "dir") && ! dirempty (bindir))
  1481. [status, output] = copyfile (bindir, desc.dir);
  1482. endif
  1483. endfunction
  1484. function finish_installation (desc, packdir, global_install)
  1485. ## Is there a post-install to call?
  1486. if (exist (fullfile (packdir, "post_install.m"), "file"))
  1487. wd = pwd ();
  1488. try
  1489. cd (packdir);
  1490. post_install (desc);
  1491. cd (wd);
  1492. catch
  1493. cd (wd);
  1494. rm_rf (desc.dir);
  1495. rm_rf (getarchdir (desc), global_install);
  1496. rethrow (lasterror ());
  1497. end_try_catch
  1498. endif
  1499. endfunction
  1500. function generate_lookfor_cache (desc)
  1501. dirs = split_by (genpath (desc.dir), pathsep ());
  1502. for i = 1 : length (dirs)
  1503. gen_doc_cache (fullfile (dirs{i}, "doc-cache"), dirs{i});
  1504. endfor
  1505. endfunction
  1506. ## Make sure the package contains the essential files.
  1507. function verify_directory (dir)
  1508. needed_files = {"COPYING", "DESCRIPTION"};
  1509. for f = needed_files
  1510. if (! exist (fullfile (dir, f{1}), "file"))
  1511. error ("package is missing file: %s", f{1});
  1512. endif
  1513. endfor
  1514. endfunction
  1515. ## Parse the DESCRIPTION file.
  1516. function desc = get_description (filename)
  1517. [fid, msg] = fopen (filename, "r");
  1518. if (fid == -1)
  1519. error ("the DESCRIPTION file %s could not be read: %s", filename, msg);
  1520. endif
  1521. desc = struct ();
  1522. line = fgetl (fid);
  1523. while (line != -1)
  1524. if (line(1) == "#")
  1525. ## Comments, do nothing.
  1526. elseif (isspace(line(1)))
  1527. ## Continuation lines
  1528. if (exist ("keyword", "var") && isfield (desc, keyword))
  1529. desc.(keyword) = cstrcat (desc.(keyword), " ", rstrip(line));
  1530. endif
  1531. else
  1532. ## Keyword/value pair
  1533. colon = find (line == ":");
  1534. if (length (colon) == 0)
  1535. disp ("skipping line");
  1536. else
  1537. colon = colon(1);
  1538. keyword = tolower (strip (line(1:colon-1)));
  1539. value = strip (line (colon+1:end));
  1540. if (length (value) == 0)
  1541. fclose (fid);
  1542. error ("the keyword %s has an empty value", desc.keywords{end});
  1543. endif
  1544. desc.(keyword) = value;
  1545. endif
  1546. endif
  1547. line = fgetl (fid);
  1548. endwhile
  1549. fclose (fid);
  1550. ## Make sure all is okay.
  1551. needed_fields = {"name", "version", "date", "title", ...
  1552. "author", "maintainer", "description"};
  1553. for f = needed_fields
  1554. if (! isfield (desc, f{1}))
  1555. error ("description is missing needed field %s", f{1});
  1556. endif
  1557. endfor
  1558. desc.version = fix_version (desc.version);
  1559. if (isfield (desc, "depends"))
  1560. desc.depends = fix_depends (desc.depends);
  1561. else
  1562. desc.depends = "";
  1563. endif
  1564. desc.name = tolower (desc.name);
  1565. endfunction
  1566. ## Make sure the version string v is a valid x.y.z version string
  1567. ## Examples: "0.1" => "0.1.0", "monkey" => error(...).
  1568. function out = fix_version (v)
  1569. dots = find (v == ".");
  1570. if (length (dots) == 1)
  1571. major = str2num (v(1:dots-1));
  1572. minor = str2num (v(dots+1:end));
  1573. if (length (major) != 0 && length (minor) != 0)
  1574. out = sprintf ("%d.%d.0", major, minor);
  1575. return;
  1576. endif
  1577. elseif (length (dots) == 2)
  1578. major = str2num (v(1:dots(1)-1));
  1579. minor = str2num (v(dots(1)+1:dots(2)-1));
  1580. rev = str2num (v(dots(2)+1:end));
  1581. if (length (major) != 0 && length (minor) != 0 && length (rev) != 0)
  1582. out = sprintf ("%d.%d.%d", major, minor, rev);
  1583. return;
  1584. endif
  1585. endif
  1586. error ("bad version string: %s", v);
  1587. endfunction
  1588. ## Make sure the depends field is of the right format.
  1589. ## This function returns a cell of structures with the following fields:
  1590. ## package, version, operator
  1591. function deps_cell = fix_depends (depends)
  1592. deps = split_by (tolower (depends), ",");
  1593. deps_cell = cell (1, length (deps));
  1594. ## For each dependency.
  1595. for i = 1:length (deps)
  1596. dep = deps{i};
  1597. lpar = find (dep == "(");
  1598. rpar = find (dep == ")");
  1599. ## Does the dependency specify a version
  1600. ## Example: package(>= version).
  1601. if (length (lpar) == 1 && length (rpar) == 1)
  1602. package = tolower (strip (dep(1:lpar-1)));
  1603. sub = dep(lpar(1)+1:rpar(1)-1);
  1604. parts = strsplit (sub, " ", true);
  1605. if (length (parts) != 2)
  1606. error ("incorrect syntax for dependency `%s' in the DESCRIPTION file\n",
  1607. dep);
  1608. endif
  1609. operator = parts{1};
  1610. if (! any (strcmp (operator, {">", ">=", "<=", "<", "=="})))
  1611. error ("unsupported operator: %s", operator);
  1612. endif
  1613. version = fix_version (parts{2});
  1614. ## If no version is specified for the dependency
  1615. ## we say that the version should be greater than
  1616. ## or equal to "0.0.0".
  1617. else
  1618. package = tolower (strip (dep));
  1619. operator = ">=";
  1620. version = "0.0.0";
  1621. endif
  1622. deps_cell{i} = struct ("package", package, "operator", operator,
  1623. "version", version);
  1624. endfor
  1625. endfunction
  1626. ## Strip the text of spaces from the right
  1627. ## Example: " hello world " => " hello world"
  1628. ## FIXME -- is this the same as deblank?
  1629. function text = rstrip (text)
  1630. chars = find (! isspace (text));
  1631. if (length (chars) > 0)
  1632. ## FIXME: shouldn't it be text = text(1:chars(end));
  1633. text = text (chars(1):end);
  1634. else
  1635. text = "";
  1636. endif
  1637. endfunction
  1638. ## Strip the text of spaces from the left and the right.
  1639. ## Example: " hello world " => "hello world"
  1640. function text = strip (text)
  1641. chars = find (! isspace (text));
  1642. if (length (chars) > 0)
  1643. text = text(chars(1):chars(end));
  1644. else
  1645. text = "";
  1646. endif
  1647. endfunction
  1648. ## Split the text into a cell array of strings by sep.
  1649. ## Example: "A, B" => {"A", "B"} (with sep = ",")
  1650. function out = split_by (text, sep)
  1651. out = strtrim (strsplit (text, sep));
  1652. endfunction
  1653. ## Create an INDEX file for a package that doesn't provide one.
  1654. ## 'desc' describes the package.
  1655. ## 'dir' is the 'inst' directory in temporary directory.
  1656. ## 'index_file' is the name (including path) of resulting INDEX file.
  1657. function write_index (desc, dir, index_file, global_install)
  1658. ## Get names of functions in dir
  1659. [files, err, msg] = readdir (dir);
  1660. if (err)
  1661. error ("couldn't read directory %s: %s", dir, msg);
  1662. endif
  1663. ## Check for architecture dependent files.
  1664. tmpdir = getarchdir (desc);
  1665. if (exist (tmpdir, "dir"))
  1666. [files2, err, msg] = readdir (tmpdir);
  1667. if (err)
  1668. error ("couldn't read directory %s: %s", tmpdir, msg);
  1669. endif
  1670. files = [files; files2];
  1671. endif
  1672. functions = {};
  1673. for i = 1:length (files)
  1674. file = files{i};
  1675. lf = length (file);
  1676. if (lf > 2 && strcmp (file(end-1:end), ".m"))
  1677. functions{end+1} = file(1:end-2);
  1678. elseif (lf > 4 && strcmp (file(end-3:end), ".oct"))
  1679. functions{end+1} = file(1:end-4);
  1680. endif
  1681. endfor
  1682. ## Does desc have a categories field?
  1683. if (! isfield (desc, "categories"))
  1684. error ("the DESCRIPTION file must have a Categories field, when no INDEX file is given");
  1685. endif
  1686. categories = split_by (desc.categories, ",");
  1687. if (length (categories) < 1)
  1688. error ("the Category field is empty");
  1689. endif
  1690. ## Write INDEX.
  1691. fid = fopen (index_file, "w");
  1692. if (fid == -1)
  1693. error ("couldn't open %s for writing.", index_file);
  1694. endif
  1695. fprintf (fid, "%s >> %s\n", desc.name, desc.title);
  1696. fprintf (fid, "%s\n", categories{1});
  1697. fprintf (fid, " %s\n", functions{:});
  1698. fclose (fid);
  1699. endfunction
  1700. function bad_deps = get_unsatisfied_deps (desc, installed_pkgs_lst)
  1701. bad_deps = {};
  1702. ## For each dependency.
  1703. for i = 1:length (desc.depends)
  1704. dep = desc.depends{i};
  1705. ## Is the current dependency Octave?
  1706. if (strcmp (dep.package, "octave"))
  1707. if (! compare_versions (OCTAVE_VERSION, dep.version, dep.operator))
  1708. bad_deps{end+1} = dep;
  1709. endif
  1710. ## Is the current dependency not Octave?
  1711. else
  1712. ok = false;
  1713. for i = 1:length (installed_pkgs_lst)
  1714. cur_name = installed_pkgs_lst{i}.name;
  1715. cur_version = installed_pkgs_lst{i}.version;
  1716. if (strcmp (dep.package, cur_name)
  1717. && compare_versions (cur_version, dep.version, dep.operator))
  1718. ok = true;
  1719. break;
  1720. endif
  1721. endfor
  1722. if (! ok)
  1723. bad_deps{end+1} = dep;
  1724. endif
  1725. endif
  1726. endfor
  1727. endfunction
  1728. function [out1, out2] = installed_packages (local_list, global_list)
  1729. ## Get the list of installed packages.
  1730. try
  1731. local_packages = load (local_list).local_packages;
  1732. catch
  1733. local_packages = {};
  1734. end_try_catch
  1735. try
  1736. global_packages = load (global_list).global_packages;
  1737. catch
  1738. global_packages = {};
  1739. end_try_catch
  1740. installed_pkgs_lst = {local_packages{:}, global_packages{:}};
  1741. ## Eliminate duplicates in the installed package list.
  1742. ## Locally installed packages take precedence.
  1743. dup = [];
  1744. for i = 1:length (installed_pkgs_lst)
  1745. if (find (dup, i))
  1746. continue;
  1747. endif
  1748. for j = (i+1):length (installed_pkgs_lst)
  1749. if (find (dup, j))
  1750. continue;
  1751. endif
  1752. if (strcmp (installed_pkgs_lst{i}.name, installed_pkgs_lst{j}.name))
  1753. dup = [dup, j];
  1754. endif
  1755. endfor
  1756. endfor
  1757. if (! isempty(dup))
  1758. installed_pkgs_lst(dup) = [];
  1759. endif
  1760. ## Now check if the package is loaded.
  1761. tmppath = strrep (path(), "\\", "/");
  1762. for i = 1:length (installed_pkgs_lst)
  1763. if (findstr (tmppath, strrep (installed_pkgs_lst{i}.dir, "\\", "/")))
  1764. installed_pkgs_lst{i}.loaded = true;
  1765. else
  1766. installed_pkgs_lst{i}.loaded = false;
  1767. endif
  1768. endfor
  1769. for i = 1:length (local_packages)
  1770. if (findstr (tmppath, strrep (local_packages{i}.dir, "\\", "/")))
  1771. local_packages{i}.loaded = true;
  1772. else
  1773. local_packages{i}.loaded = false;
  1774. endif
  1775. endfor
  1776. for i = 1:length (global_packages)
  1777. if (findstr (tmppath, strrep (global_packages{i}.dir, "\\", "/")))
  1778. global_packages{i}.loaded = true;
  1779. else
  1780. global_packages{i}.loaded = false;
  1781. endif
  1782. endfor
  1783. ## Should we return something?
  1784. if (nargout == 2)
  1785. out1 = local_packages;
  1786. out2 = global_packages;
  1787. return;
  1788. elseif (nargout == 1)
  1789. out1 = installed_pkgs_lst;
  1790. return;
  1791. endif
  1792. ## We shouldn't return something, so we'll print something.
  1793. num_packages = length (installed_pkgs_lst);
  1794. if (num_packages == 0)
  1795. printf ("no packages installed.\n");
  1796. return;
  1797. endif
  1798. ## Compute the maximal lengths of name, version, and dir.
  1799. h1 = "Package Name";
  1800. h2 = "Version";
  1801. h3 = "Installation directory";
  1802. max_name_length = length (h1);
  1803. max_version_length = length (h2);
  1804. names = cell (num_packages, 1);
  1805. for i = 1:num_packages
  1806. max_name_length = max (max_name_length,
  1807. length (installed_pkgs_lst{i}.name));
  1808. max_version_length = max (max_version_length,
  1809. length (installed_pkgs_lst{i}.version));
  1810. names{i} = installed_pkgs_lst{i}.name;
  1811. endfor
  1812. max_dir_length = terminal_size()(2) - max_name_length - ...
  1813. max_version_length - 7;
  1814. if (max_dir_length < 20)
  1815. max_dir_length = Inf;
  1816. endif
  1817. h1 = postpad (h1, max_name_length + 1, " ");
  1818. h2 = postpad (h2, max_version_length, " ");;
  1819. ## Print a header.
  1820. header = sprintf("%s | %s | %s\n", h1, h2, h3);
  1821. printf (header);
  1822. tmp = sprintf (repmat ("-", 1, length(header)-1));
  1823. tmp(length(h1)+2) = "+";
  1824. tmp(length(h1)+length(h2)+5) = "+";
  1825. printf ("%s\n", tmp);
  1826. ## Print the packages.
  1827. format = sprintf ("%%%ds %%1s| %%%ds | %%s\n", max_name_length,
  1828. max_version_length);
  1829. [dummy, idx] = sort (names);
  1830. for i = 1:num_packages
  1831. cur_name = installed_pkgs_lst{idx(i)}.name;
  1832. cur_version = installed_pkgs_lst{idx(i)}.version;
  1833. cur_dir = installed_pkgs_lst{idx(i)}.dir;
  1834. if (length (cur_dir) > max_dir_length)
  1835. first_char = length (cur_dir) - max_dir_length + 4;
  1836. first_filesep = strfind (cur_dir(first_char:end), filesep());
  1837. if (! isempty (first_filesep))
  1838. cur_dir = cstrcat ("...",
  1839. cur_dir((first_char + first_filesep(1) - 1):end));
  1840. else
  1841. cur_dir = cstrcat ("...", cur_dir(first_char:end));
  1842. endif
  1843. endif
  1844. if (installed_pkgs_lst{idx(i)}.loaded)
  1845. cur_loaded = "*";
  1846. else
  1847. cur_loaded = " ";
  1848. endif
  1849. printf (format, cur_name, cur_loaded, cur_version, cur_dir);
  1850. endfor
  1851. endfunction
  1852. function load_packages (files, handle_deps, local_list, global_list)
  1853. installed_pkgs_lst = installed_packages (local_list, global_list);
  1854. num_packages = length (installed_pkgs_lst);
  1855. ## Read package names and installdirs into a more convenient format.
  1856. pnames = pdirs = cell (1, num_packages);
  1857. for i = 1:num_packages
  1858. pnames{i} = installed_pkgs_lst{i}.name;
  1859. pdirs{i} = installed_pkgs_lst{i}.dir;
  1860. endfor
  1861. ## Load all.
  1862. if (length (files) == 1 && strcmp (files{1}, "all"))
  1863. idx = [1:length(installed_pkgs_lst)];
  1864. ## Load auto.
  1865. elseif (length (files) == 1 && strcmp (files{1}, "auto"))
  1866. idx = [];
  1867. for i = 1:length (installed_pkgs_lst)
  1868. if (exist (fullfile (pdirs{i}, "packinfo", ".autoload"), "file"))
  1869. idx (end + 1) = i;
  1870. endif
  1871. endfor
  1872. ## Load package_name1 ...
  1873. else
  1874. idx = [];
  1875. for i = 1:length (files)
  1876. idx2 = find (strcmp (pnames, files{i}));
  1877. if (! any (idx2))
  1878. error ("package %s is not installed", files{i});
  1879. endif
  1880. idx (end + 1) = idx2;
  1881. endfor
  1882. endif
  1883. ## Load the packages, but take care of the ordering of dependencies.
  1884. load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst, true);
  1885. endfunction
  1886. function unload_packages (files, handle_deps, local_list, global_list)
  1887. installed_pkgs_lst = installed_packages (local_list, global_list);
  1888. num_packages = length (installed_pkgs_lst);
  1889. ## Read package names and installdirs into a more convenient format.
  1890. pnames = pdirs = cell (1, num_packages);
  1891. for i = 1:num_packages
  1892. pnames{i} = installed_pkgs_lst{i}.name;
  1893. pdirs{i} = installed_pkgs_lst{i}.dir;
  1894. pdeps{i} = installed_pkgs_lst{i}.depends;
  1895. endfor
  1896. ## Get the current octave path.
  1897. p = split_by (path(), pathsep ());
  1898. if (length (files) == 1 && strcmp (files{1}, "all"))
  1899. ## Unload all.
  1900. dirs = pdirs;
  1901. desc = installed_pkgs_lst;
  1902. else
  1903. ## Unload package_name1 ...
  1904. dirs = {};
  1905. desc = {};
  1906. for i = 1:length (files)
  1907. idx = strcmp (pnames, files{i});
  1908. if (! any (idx))
  1909. error ("package %s is not installed", files{i});
  1910. endif
  1911. dirs{end+1} = pdirs{idx};
  1912. desc{end+1} = installed_pkgs_lst{idx};
  1913. endfor
  1914. endif
  1915. ## Check for architecture dependent directories.
  1916. archdirs = {};
  1917. for i = 1:length (dirs)
  1918. tmpdir = getarchdir (desc{i});
  1919. if (exist (tmpdir, "dir"))
  1920. archdirs{end+1} = dirs{i};
  1921. archdirs{end+1} = tmpdir;
  1922. else
  1923. archdirs{end+1} = dirs{i};
  1924. endif
  1925. endfor
  1926. ## Unload the packages.
  1927. for i = 1:length (archdirs)
  1928. d = archdirs{i};
  1929. idx = strcmp (p, d);
  1930. if (any (idx))
  1931. rmpath (d);
  1932. ## FIXME: We should also check if we need to remove items from
  1933. ## EXEC_PATH.
  1934. endif
  1935. endfor
  1936. endfunction
  1937. function [status_out, msg_out] = rm_rf (dir)
  1938. if (exist (dir))
  1939. crr = confirm_recursive_rmdir ();
  1940. unwind_protect
  1941. confirm_recursive_rmdir (false);
  1942. [status, msg] = rmdir (dir, "s");
  1943. unwind_protect_cleanup
  1944. confirm_recursive_rmdir (crr);
  1945. end_unwind_protect
  1946. else
  1947. status = 1;
  1948. msg = "";
  1949. endif
  1950. if (nargout > 0)
  1951. status_out = status;
  1952. endif
  1953. if (nargout > 1)
  1954. msg_out = msg;
  1955. endif
  1956. endfunction
  1957. function emp = dirempty (nm, ign)
  1958. if (exist (nm, "dir"))
  1959. if (nargin < 2)
  1960. ign = {".", ".."};
  1961. else
  1962. ign = [{".", ".."}, ign];
  1963. endif
  1964. l = dir (nm);
  1965. for i = 1:length (l)
  1966. found = false;
  1967. for j = 1:length (ign)
  1968. if (strcmp (l(i).name, ign{j}))
  1969. found = true;
  1970. break;
  1971. endif
  1972. endfor
  1973. if (! found)
  1974. emp = false;
  1975. return
  1976. endif
  1977. endfor
  1978. emp = true;
  1979. else
  1980. emp = true;
  1981. endif
  1982. endfunction
  1983. function arch = getarch ()
  1984. persistent _arch = cstrcat (octave_config_info("canonical_host_type"), ...
  1985. "-", octave_config_info("api_version"));
  1986. arch = _arch;
  1987. endfunction
  1988. function archprefix = getarchprefix (desc, global_install)
  1989. if ((nargin == 2 && global_install) || (nargin < 2 && issuperuser ()))
  1990. archprefix = fullfile (octave_config_info ("libexecdir"), "octave",
  1991. "packages", cstrcat(desc.name, "-", desc.version));
  1992. else
  1993. archprefix = desc.dir;
  1994. endif
  1995. endfunction
  1996. function archdir = getarchdir (desc)
  1997. archdir = fullfile (desc.archprefix, getarch());
  1998. endfunction
  1999. function s = issuperuser ()
  2000. if ((ispc () && ! isunix ()) || (geteuid() == 0))
  2001. s = true;
  2002. else
  2003. s = false;
  2004. endif
  2005. endfunction
  2006. function [status, output] = shell (cmd)
  2007. persistent have_sh;
  2008. cmd = strrep (cmd, "\\", "/");
  2009. if (ispc () && ! isunix ())
  2010. if (isempty(have_sh))
  2011. if (system("sh.exe -c \"exit\""))
  2012. have_sh = false;
  2013. else
  2014. have_sh = true;
  2015. endif
  2016. endif
  2017. if (have_sh)
  2018. [status, output] = system (cstrcat ("sh.exe -c \"", cmd, "\""));
  2019. else
  2020. error ("Can not find the command shell")
  2021. endif
  2022. else
  2023. [status, output] = system (cmd);
  2024. endif
  2025. endfunction
  2026. function newdesc = save_order (desc)
  2027. newdesc = {};
  2028. for i = 1 : length(desc)
  2029. deps = desc{i}.depends;
  2030. if (isempty (deps) || (length (deps) == 1 &&
  2031. strcmp(deps{1}.package, "octave")))
  2032. newdesc {end + 1} = desc{i};
  2033. else
  2034. tmpdesc = {};
  2035. for k = 1 : length (deps)
  2036. for j = 1 : length (desc)
  2037. if (strcmp (desc{j}.name, deps{k}.package))
  2038. tmpdesc{end+1} = desc{j};
  2039. break;
  2040. endif
  2041. endfor
  2042. endfor
  2043. if (! isempty (tmpdesc))
  2044. newdesc = {newdesc{:}, save_order(tmpdesc){:}, desc{i}};
  2045. else
  2046. newdesc{end+1} = desc{i};
  2047. endif
  2048. endif
  2049. endfor
  2050. ## Eliminate the duplicates.
  2051. idx = [];
  2052. for i = 1 : length (newdesc)
  2053. for j = (i + 1) : length (newdesc)
  2054. if (strcmp (newdesc{i}.name, newdesc{j}.name))
  2055. idx (end + 1) = j;
  2056. endif
  2057. endfor
  2058. endfor
  2059. newdesc(idx) = [];
  2060. endfunction
  2061. function load_packages_and_dependencies (idx, handle_deps, installed_pkgs_lst,
  2062. global_install)
  2063. idx = load_package_dirs (idx, [], handle_deps, installed_pkgs_lst);
  2064. dirs = {};
  2065. execpath = EXEC_PATH ();
  2066. for i = idx;
  2067. ndir = installed_pkgs_lst{i}.dir;
  2068. dirs{end+1} = ndir;
  2069. if (exist (fullfile (dirs{end}, "bin"), "dir"))
  2070. execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
  2071. endif
  2072. tmpdir = getarchdir (installed_pkgs_lst{i});
  2073. if (exist (tmpdir, "dir"))
  2074. dirs{end + 1} = tmpdir;
  2075. if (exist (fullfile (dirs{end}, "bin"), "dir"))
  2076. execpath = cstrcat (fullfile (dirs{end}, "bin"), ":", execpath);
  2077. endif
  2078. endif
  2079. endfor
  2080. ## Load the packages.
  2081. if (length (dirs) > 0)
  2082. addpath (dirs{:});
  2083. endif
  2084. ## Add the binaries to exec_path.
  2085. if (! strcmp (EXEC_PATH, execpath))
  2086. EXEC_PATH (execpath);
  2087. endif
  2088. endfunction
  2089. function idx = load_package_dirs (lidx, idx, handle_deps, installed_pkgs_lst)
  2090. for i = lidx
  2091. if (isfield (installed_pkgs_lst{i}, "loaded") &&
  2092. installed_pkgs_lst{i}.loaded)
  2093. continue;
  2094. else
  2095. if (handle_deps)
  2096. deps = installed_pkgs_lst{i}.depends;
  2097. if ((length (deps) > 1) || (length (deps) == 1 &&
  2098. ! strcmp(deps{1}.package, "octave")))
  2099. tmplidx = [];
  2100. for k = 1 : length (deps)
  2101. for j = 1 : length (installed_pkgs_lst)
  2102. if (strcmp (installed_pkgs_lst{j}.name, deps{k}.package))
  2103. tmplidx (end + 1) = j;
  2104. break;
  2105. endif
  2106. endfor
  2107. endfor
  2108. idx = load_package_dirs (tmplidx, idx, handle_deps,
  2109. installed_pkgs_lst);
  2110. endif
  2111. endif
  2112. if (isempty (find(idx == i)))
  2113. idx (end + 1) = i;
  2114. endif
  2115. endif
  2116. endfor
  2117. endfunction
  2118. function dep = is_architecture_dependent (nm)
  2119. persistent archdepsuffix = {".oct",".mex",".a",".lib",".so",".so.*",".dll","dylib"};
  2120. dep = false;
  2121. for i = 1 : length (archdepsuffix)
  2122. ext = archdepsuffix{i};
  2123. if (ext(end) == "*")
  2124. isglob = true;
  2125. ext(end) = [];
  2126. else
  2127. isglob = false;
  2128. endif
  2129. pos = findstr (nm, ext);
  2130. if (pos)
  2131. if (! isglob && (length(nm) - pos(end) != length(ext) - 1))
  2132. continue;
  2133. endif
  2134. dep = true;
  2135. break;
  2136. endif
  2137. endfor
  2138. endfunction