root/trunk/midcom/build/installMidcom.php

Revision 14327, 6.2 kB (checked in by flack, 9 months ago)

some small PHPdoc fixes and a lot of spelling (mostly apostrophes)

Line 
1 <?php
2
3 /**
4  * Created on 27/07/2006
5  * @author tarjei huse
6  * @package midcom.admin.aegir
7  * @copyright The Midgard Project, http://www.midgard-project.org
8  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
9  *
10  * Creates the midcom directory and its subdirectories and
11  * makes symlinks to the files in question.
12  *
13  * The major
14  *
15  */
16
17 require_once "phing/Task.php";
18
19 class installMidcom extends Task
20 {
21
22     /**
23      * The name of the module
24      */
25     private $module = 'midcom.core';
26     /**
27      * The name of the module when installed
28      */
29     private $module_to = 'midcom';
30     
31     /**
32      * The path to install the module, should be set
33      * in build properties
34      */
35     protected $install_dir = null;
36
37     public function setInstall_dir($str)
38     {
39         $this->install_dir = $str;
40     }
41     /**
42      * The setter for the attribute "project_dir"
43      */
44     protected $project_dir = null;
45
46     public function setProject_dir($str)
47     {
48         $this->project_dir = $str;
49     }
50     /**
51      * Directories that we make so that packages may be installed
52      * bellow them.
53      */
54     protected $subdirs = array (
55         'admin',
56         'helper',
57         'services'
58     );
59     /**
60      * files that should not be symlinked for different
61      * reasons.
62      */
63     protected $skip_dirs = array (
64         '.svn',
65         '.',
66         '..'
67     );
68     /**
69      * The installdir/midcom path
70      */
71     protected $to = false;
72     /**
73      * The project_dir/midcom.core path
74      */
75     protected $from = false;
76     /**
77      * The init method: Do init steps.
78      */
79     public function init()
80     {
81         // invert the skipdirs and symlink arrays so we can use
82         // array_key_exists
83         $this->subdirs = array_flip($this->subdirs);
84         $this->skip_dirs = array_flip($this->skip_dirs);
85     }
86
87     /**
88      * Create the projectdir and then make a symlink into the structure.
89      */
90     public function main()
91     {
92         $this->check();
93         
94         $root_files = $this->get_module_dirs($this->project_dir . "/" . $this->module );
95         foreach ($root_files as $file => $value)
96         {
97             $to = sprintf("%s/%s", $this->install_dir, $file);
98             $from = sprintf("%s/%s/%s", $this->project_dir,$this->module, $file);
99             if (!is_dir($from))
100             {
101                 $this->make_symlink("$from", "$to");
102             }
103         }
104         
105         $dirs = $this->get_module_dirs($this->from);
106
107         foreach ($dirs as $dir => $value)
108         {
109             if (array_key_exists($dir, $this->subdirs))
110             {
111                 $this->make_sub_dir($dir);
112             }
113             else
114             {
115                 $this->make_symlink("{$this->from}/$dir","{$this->to}/$dir");
116             }
117         }
118     }
119     /**
120      * Creates the subdir and symlinks the files in it
121      */
122     function make_sub_dir($dir)
123     {
124
125         // midcom/$dir   
126         if (!is_dir($this->to . "/" . $dir ))
127         {
128             mkdir($this->to . "/" . $dir, 0777, true);
129         }
130         $files = dir($this->from . "/" . $dir);
131         // quick fix.
132         
133         // symlink the files below
134         if ($files)
135             while (($file = $files->read()) !== false)
136             {
137                 if (array_key_exists($file, $this->skip_dirs))
138                 {
139                     continue;
140                 }
141                 $link = sprintf("%s/%s/%s",$this->to, $dir, $file );
142                 if (is_link($link))
143                 {
144                     continue;
145                 }
146                 
147                 $command = sprintf("ln -s %s/%s/%s %s",
148                             $this->from,$dir, $file,
149                             $link
150                             );
151                 $this->exec_command($command);
152             }
153
154     }
155     
156     
157     /**
158      * Creates a symlink to the file or directory
159      * @param string  paramname
160      */
161     private function make_symlink($from , $link)
162     {
163         if (is_link($link))
164         {
165             return;
166         }   
167         $command = sprintf("ln -s %s %s", $from, $link);
168         $this->exec_command($command);
169     }
170     /**
171      * returns a list of subdirectories and files as an associative
172      * array
173      * dirname => dirname
174      */
175     private function get_module_dirs($from)
176     {
177         $dirs = dir($from);
178         $ret = array ();
179         while (($dir = $dirs->read()) !== false)
180         {
181             if (array_key_exists($dir, $this->skip_dirs))
182             {
183                 continue;
184             }
185             $ret[$dir] = $dir;
186         }
187         
188         return $ret;
189     }
190     
191     /**
192      * Executes a given command.
193      * @return none
194      * @throws exception
195      * @param string $command the command to be executed
196      * @param boolean $debug set to true if you want to just se the
197      * command to be executed.
198      */
199     private function exec_command($command, $debug = false) {
200         if ($debug) {
201             echo $command . "\n";
202             return;
203         }
204         $ret = "";
205         exec($command, & $output, $ret);
206         if ($ret !== 0)
207         {
208             throw new Exception("Exec of $command returned non zero code $ret");
209         }
210         
211     }
212     /**
213      * Checks that the correct dirs exists, defines this->from, this->to,
214      * Also responsible for converting the path midcom.core to midcom.
215      */
216     private function check() {
217         if ($this->install_dir === null)
218         {
219             throw new Exception("Path must be set for this task to work!");
220         }
221         // this is the midcom.core => midcom directory conversin.
222         $this->to = $this->install_dir . "/" . $this->module_to;
223         $this->from = $this->project_dir . "/" . $this->module . "/" . $this->module_to;
224         
225         if (!is_dir($this->to))
226         {
227             throw new Exception("The directory {$this->to} does not exist!");
228         }
229         
230         if (!is_dir($this->from))
231         {
232             throw new Exception("The directory {$this->from} does not exist!");
233         }
234         
235         if (!file_exists($this->install_dir) && !mkdir($this->install_dir, 0777, true))
236         {
237             echo "Failed to create the needed directory {$this->install_dir}\n";
238             return;
239         }
240
241         
242     }
243 }
244 ?>
Note: See TracBrowser for help on using the browser.