1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\rpm; |
3
|
|
|
|
4
|
|
|
use PharData; |
5
|
|
|
use DirectoryIterator; |
6
|
|
|
|
7
|
|
|
class Packager { |
8
|
|
|
private $_spec; |
9
|
|
|
private $mountPoints = array(); |
10
|
|
|
private $outputPath; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Set the control file |
14
|
|
|
* |
15
|
|
|
* This file contains the base package information |
16
|
|
|
* |
17
|
|
|
* @param Spec $spec |
18
|
|
|
* @return \wapmorgan\rpm\Spec |
19
|
|
|
*/ |
20
|
|
|
public function setSpec(Spec $spec) |
21
|
|
|
{ |
22
|
|
|
$this->_spec = $spec; |
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Return the actual spec file |
28
|
|
|
* |
29
|
|
|
* @return Spec |
30
|
|
|
*/ |
31
|
|
|
public function getSpec() |
32
|
|
|
{ |
33
|
|
|
return $this->_spec; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setOutputPath($path) { |
37
|
|
|
$this->outputPath = $path; |
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getOutputPath() { |
42
|
|
|
return $this->outputPath; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addMount($sourcePath, $destinationPath) { |
46
|
|
|
$this->mountPoints[$sourcePath] = $destinationPath; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function movePackage($dest) { |
|
|
|
|
51
|
|
|
return rename($_SERVER['HOME'].'/rpmbuild/RPMS/'.$this->_spec->BuildArch.'/'.$this->_spec->Name.'-'.$this->_spec->Version.'-'.$this->_spec->Release.'.'.$this->_spec->BuildArch.'.rpm', $dest); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function run() { |
|
|
|
|
55
|
|
View Code Duplication |
if (!is_dir($_SERVER['HOME'].'/rpmbuild/SOURCES')) |
|
|
|
|
56
|
|
|
mkdir($_SERVER['HOME'].'/rpmbuild/SOURCES', 0777); |
57
|
|
View Code Duplication |
if (!is_dir($_SERVER['HOME'].'/rpmbuild/SPECS')) |
|
|
|
|
58
|
|
|
mkdir($_SERVER['HOME'].'/rpmbuild/SPECS', 0777); |
59
|
|
|
|
60
|
|
|
if (file_exists($this->getOutputPath())) { |
61
|
|
|
$iterator = new DirectoryIterator($this->getOutputPath()); |
62
|
|
|
foreach ($iterator as $path) { |
63
|
|
|
if ($path != '.' && $path != '..') { |
64
|
|
|
echo "OUTPUT DIRECTORY MUST BE EMPTY! Something exists, exit immediately!" . PHP_EOL; |
65
|
|
|
exit(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!is_dir($this->getOutputPath())) |
71
|
|
|
mkdir($this->getOutputPath(), 0777); |
72
|
|
|
|
73
|
|
|
foreach ($this->mountPoints as $path => $dest) { |
74
|
|
|
$this->pathToPath($path, $this->getOutputPath().DIRECTORY_SEPARATOR.$dest); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$spec = $this->_spec; |
78
|
|
|
$spec->setPrep('%autosetup -c package'); |
79
|
|
|
$install_section = 'rm -rf %{buildroot}'."\n".'mkdir -p %{buildroot}'."\n".'cp -rp * %{buildroot}'; |
80
|
|
|
$spec->setInstall($install_section); |
81
|
|
|
|
82
|
|
|
$files_section = null; |
83
|
|
|
foreach ($this->mountPoints as $sourcePath => $destinationPath) { |
84
|
|
|
if (is_dir($sourcePath)) { |
85
|
|
|
$files_section .= (strlen($files_section) > 0 ? "\n" : null).rtrim($destinationPath, '/').'/'; |
86
|
|
|
} else { |
87
|
|
|
$files_section .= (strlen($files_section) > 0 ? "\n" : null).'%attr('.substr(sprintf('%o', fileperms($sourcePath)), -4).',root,root) '.$destinationPath; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$spec->setFiles($files_section); |
92
|
|
|
// $used_dirs = array(); |
|
|
|
|
93
|
|
|
// foreach ($this->mountPoints as $source => $dest) { |
|
|
|
|
94
|
|
|
// if (is_dir($source)) { |
|
|
|
|
95
|
|
|
// $files[] = $dest; |
|
|
|
|
96
|
|
|
// } else { |
|
|
|
|
97
|
|
|
// $dir = dirname($dest); |
|
|
|
|
98
|
|
|
// // directory exists on a real machine - add files one by one |
99
|
|
|
// if (is_dir($dir)) { |
|
|
|
|
100
|
|
|
// $files[] = $dest; |
|
|
|
|
101
|
|
|
// } else { // add all files within directory |
|
|
|
|
102
|
|
|
// if (!in_array($dir, $files)) { |
|
|
|
|
103
|
|
|
// $files[] = $dir; |
|
|
|
|
104
|
|
|
// } |
105
|
|
|
// } |
106
|
|
|
// } |
107
|
|
|
// } |
108
|
|
|
// $spec->setFiles(implode("\n", $files)); |
|
|
|
|
109
|
|
|
|
110
|
|
|
if (file_exists($_SERVER['HOME'].'/rpmbuild/SOURCES/'.$this->_spec->Name.'.tar')) { |
|
|
|
|
111
|
|
|
unlink($_SERVER['HOME'].'/rpmbuild/SOURCES/'.$this->_spec->Name.'.tar'); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
$tar = new PharData($_SERVER['HOME'].'/rpmbuild/SOURCES/'.$this->_spec->Name.'.tar'); |
|
|
|
|
114
|
|
|
$tar->buildFromDirectory($this->outputPath); |
115
|
|
|
$spec->setKey('Source0', $this->_spec->Name.'.tar'); |
|
|
|
|
116
|
|
|
|
117
|
|
|
file_put_contents($_SERVER['HOME'].'/rpmbuild/SPECS/'.$this->_spec->Name.'.spec', (string)$this->_spec); |
|
|
|
|
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function build() { |
|
|
|
|
123
|
|
|
$command = 'rpmbuild -bb '.$_SERVER['HOME'].'/rpmbuild/SPECS/'.$this->_spec->Name.'.spec'; |
|
|
|
|
124
|
|
|
return $command; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function pathToPath($path, $dest) { |
128
|
|
|
if (is_dir($path)) { |
129
|
|
|
$iterator = new DirectoryIterator($path); |
130
|
|
|
foreach ($iterator as $element) { |
131
|
|
|
if ($element != '.' && $element != '..') { |
132
|
|
|
$fullPath = $path . DIRECTORY_SEPARATOR . $element; |
133
|
|
|
if (is_dir($fullPath)) { |
134
|
|
|
$this->pathToPath($fullPath, $dest . DIRECTORY_SEPARATOR . $element); |
135
|
|
|
} else { |
136
|
|
|
$this->copy($fullPath, $dest . DIRECTORY_SEPARATOR . $element); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} else if (is_file($path)) { |
141
|
|
|
$this->copy($path, $dest); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function copy($source, $dest) { |
146
|
|
|
$destFolder = dirname($dest); |
147
|
|
|
if (!file_exists($destFolder)) { |
148
|
|
|
mkdir($destFolder, 0777, true); |
149
|
|
|
} |
150
|
|
|
copy($source, $dest); |
151
|
|
|
if (fileperms($source) != fileperms($dest)) |
152
|
|
|
chmod($dest, fileperms($source)); |
153
|
|
|
} |
154
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: