PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/import-changed.php

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