root/honeyclient/tags/exp/UP1-kindlund-simpler_agent/create_pkg_dir.pl

Revision 787, 4.2 kB (checked in by kindlund, 1 year ago)

Fixed issue w/ unit tests. Included more unit tests in packaging.

  • Property svn:keywords set to Id "$file"
Line 
1 #!/usr/bin/perl
2
3 # $Id$
4
5 use warnings;
6 use strict;
7
8 use File::Copy;
9 use File::Copy::Recursive qw(dircopy pathrm pathmk);
10 use File::Find;
11 use Test::Inline::Extract;
12
13 #--------------------------------------#
14 # Go through each module or the ones specified
15 if(!@ARGV){@ARGV = qw(HoneyClient::Util HoneyClient::Agent HoneyClient::Manager)}
16
17 #   The path where the packages are
18 my $src_path  = "lib";
19
20 #   Directories to include in the distribution
21 my %inc_dirs = (etc                            => $src_path . '/etc',
22                 bin                            => $src_path . '/bin',
23                 inc                            => 'thirdparty/inc',
24                 'thirdparty'                   => 'thirdparty',
25                 't'                            => 't',
26                );
27 #--------------------------------------#
28
29
30 # Clean up some stuff
31 $src_path =~ s/\/+$//;
32 my $pkg_name;
33 foreach(@ARGV){
34     # The name of the package, most of the paths are derived from this
35     $pkg_name = $_;
36     $pkg_name =~ s/::/-/g;
37
38     l("Packaging $_...");
39     # Magic fun with arrays
40     #   Get the last one, thats the module name, then join the rest back together
41     my ($module,@path) = reverse(split(/-/,$pkg_name));
42     my $path = join('/',reverse(@path));
43
44     # Clean up the old stuff
45     l("Cleaning house",1);
46     if(-e $pkg_name){ my_pathrm($pkg_name,2) }
47
48     # Get the stuff for the stub folder from wherever we want
49     if(-e 'stub'){ my_pathrm('stub',2) }
50
51     # Copy included directories
52     while(my($name,$path) = each %inc_dirs){
53         my_dircopy($path,$pkg_name,$name,1);
54         # Suppress errors generated by removing .svn directories.
55         eval {
56             no warnings;
57             find({wanted => \&remove_svn, no_chdir => 0}, "$pkg_name/$name");
58         };
59     }
60
61     # Create the target directory
62     my_pathmk("$pkg_name/lib/$path/$module",1);
63
64     # Copy pm files to the lib
65     if(-f "$src_path/$path/$module.pm"){
66         my($src,$dest) = ("$src_path/$path/$module.pm","$pkg_name/lib/$path");
67         my_copy($src,$dest,2);
68     }
69     find({wanted => \&process, no_chdir => 1} ,"$src_path/$path/$module");
70
71     # Copy the LICENSE and INSTALL files.
72     my_copy("LICENSE",$pkg_name,1);
73     my_copy("INSTALL",$pkg_name,1);
74
75     l('');
76 }
77
78 sub remove_svn {
79     if(/\.svn$/) {
80         my_pathrm($_, 2);
81     }
82 }
83
84 # Process the files after we find them
85 sub process { #{{{
86     # Make sure we only get .pm files
87     if(/\.pm$/){
88         my $src = $File::Find::name;
89         l("Found $src",3);
90         # Because messing with arrays is more fun than strings
91         my @dest = split(/\//,$src);
92         shift(@dest);
93         my $name = pop(@dest);
94         $name =~ s/\.pm$//;
95         unshift(@dest,$pkg_name,'lib');
96         my $dest = join('/',@dest);
97         if(!-e $dest){ my_pathmk($dest,4) }
98         my_copy($src,$dest,3);
99
100 #------------------------
101 # TODO: Get it to create tests, works intermitantly
102 #------------------------
103 #        # Get the inline test stuff from the file
104 #        my $inline = Test::Inline::Extract->new($src)->elements;
105 #        if( $inline ne "" ){
106 #            l("Making test file",2);
107 #            my_pathmk("$pkg_name/t",3);
108 #            open(TEST,">$pkg_name/t/$pkg_name-$name.t");
109 #            foreach(@{$inline}){ print TEST; }
110 #            close TEST;
111 #        }
112     }
113 }#}}}
114
115
116 # All of the calls are the same, and I wanted to unclutter the above code
117 sub my_copy { #{{{
118     my($src,$dest,$log) = @_;
119     l("Copying $src to $dest",$log);
120     copy($src,$dest) or die "Cannot copy $src to $dest: $!";
121 }#}}}
122 sub my_dircopy { #{{{
123     my($src,$dest,$name,$log) = @_;
124     l("Copying $src to $dest as $name",$log);
125     dircopy($src,"$dest/$name") or die "Couldn't copy $src to $dest: $!";
126 }#}}}
127 sub my_pathrm { #{{{
128     my($pkg_name,$log) = @_;
129     l("Removing $pkg_name directory",$log);
130     pathrm($pkg_name,1) or die "Can't rmdir $pkg_name: $!";
131 }#}}}
132 sub my_pathmk { #{{{
133     my($path,$log) = @_;
134     l("$path does not exist, creating...",$log);
135     pathmk($path) or die "Cannot mkdir $path: $!";
136 }#}}}
137
138 # Log with pretty tabbing
139 sub l { #{{{
140     my $string = shift;
141     my $level = shift || 0;
142     while($level-- > 0){
143         print "\t";
144         if(!$level){ print "- " }
145     }
146     print "$string\n";
147 }#}}}
148
Note: See TracBrowser for help on using the browser.