Changeset f544216 for perl


Ignore:
Timestamp:
Sep 11, 2010, 5:25:04 PM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.7, release-1.8, release-1.9
Children:
0743696
Parents:
8106870
git-author:
Nelson Elhage <nelhage@mit.edu> (09/11/10 16:46:51)
git-committer:
Nelson Elhage <nelhage@mit.edu> (09/11/10 17:25:04)
Message:
Fix module-loading priorities.

I think we actually get this right, now. Local modules are preferred to system
modules, and unpacked modules are preferred to PAR modules in the same
directory.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl/ModuleLoader.pm

    r41bbb8a rf544216  
    44package BarnOwl::ModuleLoader;
    55
    6 use lib (BarnOwl::get_data_dir() . "/modules/");
    7 use PAR (BarnOwl::get_data_dir() . "/modules/*.par");
    8 use PAR (BarnOwl::get_config_dir() . "/modules/*.par");
     6use PAR;
    97
    108our %modules;
     
    2422}
    2523
     24=h2 rescan_modules
     25
     26Re-compute the list of available modules, and add the necessary items to @INC
     27and @PAR_INC.
     28
     29We load modules from two directories, the system module dir, and the user module
     30directory. Modules can be in either of two forms: ${modname}.par, or else a
     31${modname}/ directory containing lib/${modname}.
     32
     33We prefer to load modules from the user's directory, and if a module exists in
     34both packed and unpacked form in the same directory, we prefer the unpacked
     35module.
     36
     37We walk the module directories in order of ascending priority -- user directory,
     38and then system directory.
     39
     40When we walk the directories, we first check all things that are not named
     41Foo.par, add them to @INC, and add them to the module list. We then walk the
     42.par files and add them to @PAR_INC and update the module list.
     43
     44It is important that we never add a module to @INC (or @PAR_INC) if we already
     45have a source for it, in order to get priorities right. The reason is that @INC
     46is processed before @PAR_INC, so if we had an unpacked system module and a
     47packed local module, if we added both, the system module would take priority.
     48
     49=cut
     50
    2651sub rescan_modules {
    27     PAR->import(BarnOwl::get_data_dir() . "/modules/*.par");
    28     PAR->import(BarnOwl::get_config_dir() . "/modules/*.par");
    29     my @modules;
     52    @PAR::PAR_INC = ();
    3053
    3154    %modules = ();
    3255
    3356    my @moddirs = ();
     57    push @moddirs, BarnOwl::get_config_dir() . "/modules";
    3458    push @moddirs, BarnOwl::get_data_dir() . "/modules";
    35     push @moddirs, BarnOwl::get_config_dir() . "/modules";
    36    
     59
    3760    for my $dir (@moddirs) {
     61        # Strip defunct entries from @INC
     62        @INC = grep {!/^\Q$dir/} @INC;
     63
    3864        opendir(my $dh, $dir) or next;
    39         while(defined(my $f = readdir($dh))) {
    40             next if $f =~ /^\./;
    41             if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
    42                 $modules{$1} = 1;
    43             } elsif(-d "$dir/$f" && -d "$dir/$f/lib") {
     65        my @ents = grep {!/^\./} readdir($dh);
     66
     67        # Walk unpacked modules
     68        for my $f (grep {!/\.par$/} @ents) {
     69            if(-d "$dir/$f" && -d "$dir/$f/lib") {
     70                next if $modules{$f};
     71
    4472                unshift @INC, "$dir/$f/lib" unless grep m{^$dir/$f/lib$}, @INC;
    4573                $modules{$f} = 1;
    4674            }
    4775        }
    48         @modules = grep /\.par$/, readdir($dh);
     76
     77        # Walk parfiles
     78        for my $f (grep /\.par$/, @ents) {
     79            if(-f "$dir/$f" && $f =~ /^(.+)\.par$/) {
     80                next if $modules{$1};
     81
     82                PAR->import("$dir/$f");
     83                $modules{$1} = 1;
     84            }
     85        }
     86
    4987        closedir($dh);
    50         for my $mod (@modules) {
    51             my ($class) = ($mod =~ /^(.+)\.par$/);
    52             $modules{$class} = 1;
    53         }
    5488    }
    5589}
Note: See TracChangeset for help on using the changeset viewer.