View source with formatted comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(prolog_version,
   37          [ check_prolog_version/1,     % +NumericVersion
   38            register_git_module/2,      % +Name, +Options
   39            git_module_property/2,      % ?Name, ?Property
   40            git_update_versions/1       % ?Name
   41          ]).   42:- use_module(library(process)).   43:- use_module(library(option)).   44:- use_module(library(readutil)).   45:- use_module(library(git)).   46
   47
   48/** <module> Manage software versions
   49
   50The module deals with software  versions.   It  currently implements two
   51features:  test  whether   SWI-Prolog   is    sufficiently   new   using
   52check_prolog_version/1 and find GIT version   signatures for the running
   53server. Modules that want  their  version   info  available  through the
   54web-page can do so using a call to register_git_module/2.
   55*/
   56
   57:- multifile
   58    git_module_hook/3.              % Name, Dir, Options
   59
   60%!  check_prolog_version(+Required)
   61%
   62%   Validate the program is running under Prolog version Required or
   63%   newer. Required is in numeric notation (e.g. 50317 for 5.3.17)
   64
   65check_prolog_version(Required) :-
   66    prolog_version_ok(Required),
   67    !.
   68check_prolog_version(Required) :-
   69    print_message(error,
   70                  required_prolog_version(Required)),
   71    format(user_error, '~nPress any key to exit> ', []),
   72    get_single_char(_), nl(user_error),
   73    halt(1).
   74
   75prolog_version_ok(or(V1, V2)) :-
   76    !,
   77    (   prolog_version_ok(V1)
   78    ->  true
   79    ;   prolog_version_ok(V2)
   80    ).
   81prolog_version_ok(Required) :-
   82    current_prolog_flag(version, MyVersion),
   83    MyVersion >= Required.
   84
   85:- multifile
   86    prolog:message/3.   87
   88prolog:message(required_prolog_version(Required)) -->
   89    { current_prolog_flag(version, MyVersion),
   90      user_version(MyVersion, MyV),
   91      user_version(Required, Req)
   92    },
   93    [ 'This program requires SWI-Prolog ~w'-[Req], nl,
   94      'while you are running version ~w.'-[MyV], nl,
   95      'Please visit http://www.swi-prolog.org and', nl,
   96      'upgrade your version of SWI-Prolog.'
   97    ].
   98prolog:message(git(no_version)) -->
   99    [ 'Sorry, cannot retrieve version stamp from GIT.' ].
  100prolog:message(git(update_versions)) -->
  101    [ 'Updating GIT version stamps in the background.' ].
  102
  103
  104user_version(or(V1,V2), Version) :-
  105    !,
  106    user_version(V1, A1),
  107    user_version(V2, A2),
  108    format(atom(Version), '~w or ~w', [A1, A2]).
  109user_version(N, Version) :-
  110    Major is N // 10000,
  111    Minor is (N // 100) mod 100,
  112    Patch is N mod 100,
  113    atomic_list_concat([Major, Minor, Patch], '.', Version).
  114
  115
  116                 /*******************************
  117                 *         REGISTRATION         *
  118                 *******************************/
  119
  120:- dynamic
  121    git_module/3,           % Name, Dir, Options
  122    git_module_version/2.   % Name, Version
  123
  124%!  register_git_module(+Name, +Options)
  125%
  126%   Register the directory from which the  Prolog file was loaded as
  127%   a GIT component about which to  report version information. This
  128%   should be used as a directive.  Defined options:
  129%
  130%       * directory(Dir)
  131%       Use Dir as the location of the GIT repository instead of the
  132%       directory of the file from which this directive was called.
  133%       If Dir is not absolute, it is taken relative to the
  134%       directory holding the file from which this directive was called.
  135%
  136%       * home_url(URL)
  137%       Used to create a link to the components home-page.
  138
  139register_git_module(Name, Options) :-
  140    (   prolog_load_context(directory, BaseDir)
  141    ->  true
  142    ;   working_directory(BaseDir, BaseDir)
  143    ),
  144    select_option(directory(Dir), Options, RestOptions, '.'),
  145    absolute_file_name(Dir, AbsDir,
  146                       [ file_type(directory),
  147                         relative_to(BaseDir),
  148                         access(read)
  149                       ]),
  150    retractall(git_module(Name, _, _)),
  151    assert(git_module(Name, AbsDir, RestOptions)).
  152
  153git_update_versions(Name) :-
  154    catch(forall(current_git_module(Name, _, _),
  155                 update_version(Name)),
  156          _,
  157          print_message(warning, git(no_version))).
  158
  159update_version(Name) :-
  160    current_git_module(Name, Dir, Options),
  161    (   catch(git_describe(GitVersion, [directory(Dir)|Options]), _, fail)
  162    ->  true
  163    ;   GitVersion = unknown
  164    ),
  165    retractall(git_module_version(Name, _)),
  166    assert(git_module_version(Name, GitVersion)).
  167
  168current_git_module(Name, Dir, Options) :-
  169    git_module(Name, Dir, Options).
  170current_git_module(Name, Dir, Options) :-
  171    git_module_hook(Name, Dir, Options).
  172
  173
  174%!  git_module_property(?Name, ?Property) is nondet.
  175%
  176%   Property is a property of the named git-component. Defined
  177%   properties are:
  178%
  179%       * version(Version)
  180%       git-describe like version information
  181%       * directory(Dir)
  182%       Base directory of the component
  183%
  184%   @tbd Extend with more detailed version (e.g., _remote_)
  185
  186git_module_property(Name, Property) :-
  187    (   var(Name)
  188    ->  current_git_module(Name, _, _),
  189        git_module_property(Name, Property)
  190    ;   compound(Property)
  191    ->  once(gen_module_property(Name, Property))
  192    ;   gen_module_property(Name, Property)
  193    ).
  194
  195gen_module_property(Name, version(Version)) :-
  196    (   git_module_version(Name, Version0)
  197    ->  true
  198    ;   git_update_versions(Name),
  199        git_module_version(Name, Version0)
  200    ),
  201    Version0 \== unknown,
  202    Version = Version0.
  203gen_module_property(Name, directory(Dir)) :-
  204    current_git_module(Name, Dir, _).
  205gen_module_property(Name, remote(Alias, Remote)) :-
  206    (   ground(Alias)
  207    ->  true
  208    ;   Alias = origin
  209    ),
  210    current_git_module(Name, Dir, _),
  211    git_remote_url(Alias, Remote, [directory(Dir)]).
  212gen_module_property(Name, Term) :-
  213    current_git_module(Name, _, Options),
  214    member(Term, Options).
  215
  216
  217
  218                 /*******************************
  219                 *        KEEP UP-TO-DATE       *
  220                 *******************************/
  221
  222bg_git_update_versions :-
  223    print_message(informational, git(update_versions)),
  224    thread_create(git_update_versions(_), _,
  225                  [ detached(true)
  226                  ]).
  227
  228:- multifile
  229    user:message_hook/3.  230
  231user:message_hook(make(done(_)), _, _) :-
  232    bg_git_update_versions,
  233    fail.
  234
  235% do not update versions in background because we need to fork
  236:- if(current_predicate(http_unix_daemon:http_daemon/0)).  237:- initialization git_update_versions(_).  238:- else.  239:- initialization bg_git_update_versions.  240:- endif.