root/trunk/midgard/obs_upload.php

Revision 24119, 6.1 kB (checked in by piotras, 4 months ago)

Upload all files required to build package @OBS.

Line 
1 <?php
2
3 if (!function_exists("curl_exec"))   
4     die("Curl extension not loaded");
5
6 define("OBS_PROJECT", "home:midgardproject:unstable");
7
8 define("OBS_BASE_URL", "https://api.opensuse.org/source/". rawurlencode (OBS_PROJECT) . "/");
9 define("OBS_USER_FILE", "~/.midgard/obs_user");
10
11 define("CORE_PACKAGE""midgard2-core");
12 define("PYTHON_PACKAGE", "python-midgard2");
13 define("PHP_PACKAGE",   "php5-midgard2");
14
15 define("CORE_SVN_DIR", "core/midgard");
16 define("PHP_SVN_DIR", "apis/php5");
17 define("PYTHON_SVN_DIR", "apis/python");
18
19 /* get username and password */
20 $data_file = shell_exec ("cat " . OBS_USER_FILE);
21
22 if ($data_file == false)
23     die ("Can not read " . OBS_USER_FILE . " file \n");
24
25 $data = explode ("\n", $data_file);
26
27 class midgard_makedist_curl
28 {
29     var $username = null;
30     var $password = null;
31     var $curl = null;
32     var $cookie_file = "/tmp/obs_cookie";
33     var $debug = 0;
34
35     function __construct(array $data)
36     {
37         if ($data[0] == ""
38             || $data[0] == null)
39         {
40             die("Can not initialize curl with empty username");
41         }
42
43         if ($data[0] == ""
44             || $data[0] == null)
45         {
46             die("Can not initialize curl with empty username");
47         } 
48
49         $this->username = $data[0];
50         $this->password = $data[1];
51     }
52
53     private function debug($msg = "I have nothing to say", $level = 1)
54     {
55         if ($this->debug >= $level)
56             echo $msg . "\n";
57     }
58
59     private function _curl_init()
60     {
61         if ($this->curl != null)
62             throw new Exception ("Curl already being initialized");
63
64         $this->curl = curl_init();
65
66         /* Set common curl options */
67         curl_setopt ($this->curl, CURLOPT_FAILONERROR, 1);
68         curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 1);
69         curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER,1);
70         curl_setopt ($this->curl, CURLOPT_PORT, 443);     
71         curl_setopt ($this->curl, CURLOPT_TIMEOUT, 15);
72         curl_setopt ($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
73         curl_setopt ($this->curl, CURLOPT_SSL_VERIFYPEER, false);
74         curl_setopt ($this->curl, CURLOPT_USERPWD, $this->username . ":" . $this->password);
75     }
76
77     private function _curl_close()
78     {
79         if ($this->curl == null)
80             return;
81
82         curl_close($this->curl);
83
84         $this->curl = null;
85     }
86
87     private function build_dists_obs_path ($dir)
88     {
89         return $dir . "/dists/OBS";
90     }
91
92     private function list_dists_obs_files ($dir)
93     {
94         $ret = array();
95
96         $_obs_files_dir = self::build_dists_obs_path ($dir);
97         $handle = opendir($_obs_files_dir);
98
99         if (!$handle)
100             return $ret;
101
102         while (($filename = readdir ($handle)) !== false)
103         {
104             $filepath = $_obs_files_dir . "/" . $filename;
105
106             if (!is_file ($filepath))
107             {
108                 continue;
109             }
110
111             if ($filepath[0] == '.')
112             {
113                 continue;
114             }
115
116             $ret[] = $filepath;
117         }   
118
119         closedir ($handle);
120
121         return $ret;
122     }
123
124     private function remove_tarball($url, $filename)
125     {
126         /* TODO */   
127     }
128
129     private function _set_put_tarball_options ($package, $filename)
130     {
131         if ($this->curl == null)
132             throw new Exception ("Curl not initialized");
133
134         $url = OBS_BASE_URL . $package . "/" . basename ($filename);
135         $size = filesize ($filename);
136         $file = fopen ($filename,'r');
137
138         curl_setopt ($this->curl, CURLOPT_URL, $url);
139         curl_setopt ($this->curl, CURLOPT_PUT, true);
140         curl_setopt ($this->curl, CURLOPT_INFILESIZE, $size);
141         curl_setopt ($this->curl, CURLOPT_INFILE, $file);
142     }
143
144     private function _upload ()
145     {
146         $ret = curl_exec ($this->curl);
147
148         if (strstr ($ret, "<summary>Ok</summary>"))
149         {
150             echo " STATUS: OK \n";
151         }
152         else {
153
154             echo " STATUS: FAILED ";
155             echo curl_error ($this->curl) . "\n";
156         }
157
158         $this->_curl_close();
159     }
160
161     private function _upload_dists_obs_files ($dir, $package)
162     {
163         $ret = self::list_dists_obs_files ($dir);
164
165         if (empty ($ret))
166             return;
167
168         foreach ($ret as $filename)
169         {
170             $this->_curl_init();
171             $this->_set_put_tarball_options ($package, $filename);
172             echo "Uploading " . $filename;
173             $this->_upload();   
174         }
175     }
176
177     private function upload_package_files ($package, $tarball, $svndir)
178     {
179         if ($package === "" || $package == null)
180         {
181             echo "Can not upload files for empty package \n";
182             return;
183         }
184
185         if ($tarball === "" || $tarball == null)
186         {
187             echo "Can not upload tarball. Empty name given \n";
188             return;
189         }
190
191         if ($svndir === "" || $svndir == null)
192         {
193             echo "Can not upload files from svndir. Empty name given \n";
194         }
195
196         $this->_curl_init();
197         $this->_set_put_tarball_options ($package, $tarball);
198         echo "Uploading " . $tarball;
199         $this->_upload();
200
201         if ($svndir)
202         {
203             $this->_upload_dists_obs_files ($svndir, $package);
204         }
205     }
206
207     function upload_core($filename)
208     {
209         $this->_curl_init();
210         $this->_set_put_tarball_options (CORE_PACKAGE, $filename);
211         echo "Uploading " . $filename;
212         $this->_upload();
213     }
214
215     function upload_python($filename)
216     {
217         $this->_curl_init();
218         $this->_set_put_tarball_options (PYTHON_PACKAGE, $filename);
219         echo "Uploading " . $filename;
220         $this->_upload();
221     }
222
223     function upload_php($filename)
224     {
225          $this->_curl_init();
226         $this->_set_put_tarball_options (PHP_PACKAGE, $filename);
227         echo "Uploading " . $filename;
228         $this->_upload();
229     }
230
231     function execute($dir)
232     {
233         chdir($dir);
234
235         $core_file = glob("midgard2-core*.tar.gz");
236         $python_file = glob("python-midgard2*.tar.gz");
237         $php_file = glob("php5-midgard2*.tar.gz");
238
239         if (count($core_file) > 1)
240             throw new Exception("More than one core tarball found");
241
242         if (count($python_file) > 1)
243             throw new Exception("More than one python tarball found");
244
245         if (count($php_file) > 1)
246             throw new Exception("More than one php tarball found");
247
248         $this->upload_package_files (CORE_PACKAGE, $core_file[0], CORE_SVN_DIR);
249         $this->upload_package_files (PHP_PACKAGE, $php_file[0], PHP_SVN_DIR);
250         $this->upload_package_files (PYTHON_PACKAGE, $python_file[0], PYTHON_SVN_DIR);
251     }
252 }
253
254 $mmc = new midgard_makedist_curl($data);
255 $mmc->debug = 1;
256 $mmc->execute(getcwd());
257
258 ?>
259
Note: See TracBrowser for help on using the browser.