root/honeyclient/branches/bug/51/create_pkg_dir.pl

Revision 13, 3.8 kB (checked in by kindlund, 2 years ago)

Initialized public repository with 0.9 release.

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