PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/run-full-import.php

https://github.com/mrsinguyen/drupalorg-git
PHP | 153 lines | 111 code | 27 blank | 15 comment | 30 complexity | c4e9a4e733b548694616c3b30ccf03c7 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. $concurrency = getenv('C2G_CONCURRENCY');
  4. $concurrency = is_string($concurrency) ? $concurrency : 13;
  5. $srcrepo = getenv('C2G_REPOSITORY');
  6. $srcrepo = is_string($srcrepo) ? $srcrepo : '/var/git/cvsmirror';
  7. $destpath = getenv('C2G_DESTINATION');
  8. $destpath = is_string($destpath) ? $destpath : '/var/git/repositories';
  9. $optsfile = getenv('C2G_CVS2GIT_OPTIONS');
  10. $optsfile = is_string($optsfile) ? $optsfile : dirname(__FILE__) . '/cvs2git-trunk.options';
  11. set_time_limit(0); // Ensure we don't time out.
  12. // Load shared functions.
  13. require_once dirname(__FILE__) . '/shared.php';
  14. if (!file_exists(dirname(__FILE__) . '/project-migrate-info')) {
  15. // No source transform file, bail out
  16. echo 'No project migrate info file, cannot proceed.';
  17. exit(1);
  18. }
  19. // Format: <srcpath> <dest> <strip translations>
  20. $list = file(dirname(__FILE__) . '/project-migrate-info');
  21. // Run forked subprocesses
  22. $status = 0;
  23. $forks = 0;
  24. $empties = new SplFileObject(dirname(__FILE__) . '/empties', 'w');
  25. $emptylist = array();
  26. // Scrub the output dir, it needs to be clean for errors to be real
  27. $shell_dest = escapeshellarg("$destpath/project");
  28. `rm -rf $shell_dest`;
  29. git_log("\n*****************\nBegin forking import processes\n*****************\n", 'DEBUG');
  30. foreach ($list as $n => $line) {
  31. if ($forks >= $concurrency) {
  32. $pid = pcntl_wait($status);
  33. $status = pcntl_wifstopped($status);
  34. $forks--;
  35. if (!empty($status)) {
  36. break;
  37. }
  38. }
  39. $projectdata = explode(',', $line);
  40. // Core is stupid, as always. Here's hoping this is one of the last special cases we write for it
  41. if ($projectdata[1] != 'drupal') {
  42. if (file_exists("$destpath/project/{$projectdata[1]}.git")) {
  43. git_log('Crap on a cracker, the target dir already exists!', 'WARN', $projectdata[1]);
  44. continue;
  45. }
  46. if (empty($projectdata[0]) || !is_cvs_dir($srcrepo . '/contributions' . $projectdata[0])) {
  47. git_log('No CVS source information for project; will spawn an empty repo for it later.', 'INFO', $projectdata[1]);
  48. $empties->fwrite(sprintf('%s,%d' . PHP_EOL, $projectdata[1], $projectdata[3]));
  49. $emptylist[] = $n;
  50. continue;
  51. }
  52. }
  53. // OK, we're ready to proceed. fork it FORK IT GOOD
  54. $pid = pcntl_fork();
  55. if ($pid == -1) {
  56. die("oh noes! no fork!");
  57. }
  58. else if ($pid) {
  59. // Parent; increment fork counter.
  60. $forks++;
  61. }
  62. else {
  63. if (preg_match('/^git:/', $projectdata[0])) {
  64. git_invoke("git clone --bare $projectdata[0] $destpath/project/{$projectdata[1]}.git", FALSE);
  65. convert_project_branches($projectdata[1], "$destpath/project/{$projectdata[1]}.git", $rename_patterns['contrib']['branches']);
  66. convert_project_tags($projectdata[1], "$destpath/project/{$projectdata[1]}.git", $rename_patterns['contrib']['tagmatch'], $rename_patterns['contrib']['tags']);
  67. exit(0);
  68. }
  69. else {
  70. $success = import_directory($optsfile, $srcrepo, ($projectdata[1] == 'drupal' ? 'drupal' : 'contributions') . $projectdata[0], "$destpath/project/{$projectdata[1]}.git", TRUE);
  71. }
  72. exit(empty($success));
  73. }
  74. git_log("Finished import #$n\n", 'DEBUG');
  75. }
  76. git_log("\n*****************\nFinished forking import processes, now waiting for all children to complete\n*****************\n", 'DEBUG');
  77. // Make sure all forked children finish.
  78. while ($forks) {
  79. git_log("Fork count remaining: $forks\n", 'DEBUG');
  80. pcntl_wait($status);
  81. $forks--;
  82. }
  83. if (!empty($status)) {
  84. exit($status);
  85. }
  86. git_log("Empties list:\n" . print_r($emptylist, TRUE), 'DEBUG');
  87. // Now do any necessary cleanup/stripping.
  88. git_log("\n*****************\nBegin forking cleanup processes\n*****************\n", 'DEBUG');
  89. $forks = 0; // Reinit just to be sure
  90. foreach ($list as $n => $line) {
  91. if ($forks >= $concurrency) {
  92. $pid = pcntl_wait($status);
  93. $status = pcntl_wifstopped($status);
  94. $forks--;
  95. if (!empty($status)) {
  96. break;
  97. }
  98. }
  99. // Skip this step if we know the repo doesn't exist.
  100. if (in_array($n, $emptylist)) {
  101. continue;
  102. }
  103. $projectdata = explode(',', $line);
  104. // fork it FORK IT _BETTER_
  105. $pid = pcntl_fork();
  106. if ($pid == -1) {
  107. die("oh noes! no fork!");
  108. }
  109. else if ($pid) {
  110. // Parent; increment fork counter.
  111. $forks++;
  112. }
  113. else {
  114. $success = cleanup_migrated_repo($projectdata[1], "$destpath/project/{$projectdata[1]}.git", TRUE, $projectdata[2] == '1');
  115. exit(empty($success));
  116. }
  117. git_log("Finished cleanup #$n\n", 'DEBUG');
  118. }
  119. // Make sure all forked children finish.
  120. git_log("\n*****************\nFinished forking cleanup processes, now waiting for all children to complete\n*****************\n", 'DEBUG');
  121. while ($forks) {
  122. git_log("Fork count remaining: $forks\n", 'DEBUG');
  123. pcntl_wait($status);
  124. $forks--;
  125. }
  126. exit($status);