-
Notifications
You must be signed in to change notification settings - Fork 9
/
import-changed.php
executable file
·148 lines (129 loc) · 3.73 KB
/
import-changed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
// Allow the build interval to be passed to the script. If none is provided, we
// default to 5 minutes.
// Must be provided in seconds.
$build_interval = getenv('BUILD_INTERVAL') ? getenv('BUILD_INTERVAL') : '300';
$last_poll = time() - $build_interval;
// Number of threads to allow.
$threads = 8;
// Config template file.
$config_template = realpath('./cvs2git.options');
// Repository locations.
$repository = '/var/git/cvsmirror';
$destination = '/var/git/repositories';
require_once './shared.php';
// Allow paging through the RSS results to prevent commit overuns.
$page = 0;
// These variables track which projects to rebuild.
$core = FALSE;
$contributions = array();
while (!fetch_projects($last_poll, $page, $core, $contributions)) {
++$page;
echo "Fetching more! $page\n";
}
// Tracking variable for how many forks we have running.
$forks = 0;
if ($core) {
$pid = pcntl_fork();
if ($pid == -1) {
die("oh noes! no fork!");
}
elseif ($pid) {
// Parent
$forks++;
}
else {
// Child
import_directory($config_template, $repository, 'drupal', "$destination/project/drupal.git" );
exit;
}
}
$success = TRUE;
while (!empty($contributions)) {
$project_dir = array_pop($contributions);
$tmp = explode('/', $project_dir);
$project = isset($tmp[2]) ? $tmp[2] : $tmp[1];
$pid = pcntl_fork();
if ($pid == -1) {
die("oh noes! no fork!");
}
elseif ($pid) {
// Parent
$forks++;
// If we've run out of headroom, wait for a process to finish.
if ($forks >= $threads) {
$pid = pcntl_wait($status);
$success &= pcntl_wifstopped($status);
$forks--;
}
}
else {
// Child
$success = import_directory($config_template, $repository, $project_dir, "$destination/project/$project.git" );
if (!$success) {
exit('Failed to import ' . $source_dir);
}
exit;
}
}
// Make sure all process finish before exiting.
while ($forks) {
$pid = pcntl_wait($status);
$success &= pcntl_wifstopped($status);
$forks--;
}
exit(!$success);
/*******************************************************************************
* Helper functions
******************************************************************************/
/**
* Fetch a list of projects up until the given poll_date.
*/
function fetch_projects($last_poll, $page, &$core, &$contributions) {
$url = 'http://drupal.org/cvs?rss=true';
if ($page) {
$url .= "&page=$page";
}
$xml = fetch_rss($url);
foreach ($xml->channel->item as $item) {
$matches = array();
if (strpos($item->description, 'viewvc/drupal/drupal')) {
$core = TRUE;
# echo "Commit to core\n";
}
elseif (preg_match('#contributions/(modules|themes|theme-engines|profiles|translations|docs|tricks)/([^/]+)#', $item->description, $matches)) {
// We don't really care about translations.
if ($matches[1] == 'translations') continue;
if ($matches[1] == 'docs' || $matches[1] == 'tricks') {
$project = $matches[1];
$contributions[$project] = "contributions/$project";
}
else {
$project = $matches[2];
$contributions[$project] = $matches[0];
}
# echo "Commit to $project: $item->link\n";
}
else {
print_r($item);
echo "woops... $item->link\n";
}
$commit_date = strtotime($item->pubDate);
if ($commit_date < $last_poll) {
return TRUE;
}
}
return FALSE;
}
function fetch_rss($url) {
$data = array();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($return);
return $xml;
}