Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created May 15, 2012 10:59
Show Gist options
  • Select an option

  • Save nfreear/2700840 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/2700840 to your computer and use it in GitHub Desktop.
Quickly checkout a bunch of Drupal contributed modules on *nux by branch or tag, and git-describe sub-directories
#!/usr/bin/env php
<?php
/**
* Nick's OU-Drupal Git CLI.
* Quickly checkout a bunch of Drupal contributed modules on Redhat 6, by branch or tag, and git-describe sub-directories.
*
* > php ../../../../../ndrupalgit.php
*
* Issues: views?
*
* Copyright 2012-05-01,-05-14, N.D.Freear.
*/
define('DRUPAL_GIT_CLONE_PATTERN', 'git clone http://git.drupal.org/project/__PROJECT__.git');
define('DRUPAL_VERSION', '6.x');
##define('PROJECT_CONTRIB_ROOT', '__FILE__/pd_approval/sites/all/modules/contrib');
define('PROJECT_CONTRIB_ROOT', '__FILE__/pd_dev/sites/all/modules/contrib');
define('OU_DRUPAL_WIKI_URL',
'http://www.open.ac.uk/wikis/drupal/index.php?title=Drupal_supported_modules_(6.21)&action=raw');
$do_checkout = FALSE;
$do_branch = FALSE; #TRUE;
/*
See:
http://drupal.org/node/1083580
http://www.open.ac.uk/wikis/drupal/Drupal_supported_modules_(6.21)
*/
$__contrib_modules = 'ctools|oembed|views|date|calendar|fckeditor|location|feeds|tagadelic'; # ckeditor, tinymce, views?
$__contrib_modules = 'global_redirect|nodewords|page_title|pathauto';
$__contrib_modules = 'token|job_scheduler|pathauto|fckeditor|location|feeds|tagadelic|parser_ical';
$__contrib_modules = 'ldap_integration|ckeditor|cck|linkchecker';
$__contrib_modules = 'link|interwiki|masquerade|devel';
$_x_contrib_modules = 'ckeditor|token|job_scheduler|ctools|oembed|views|cck|masquerade';
$contrib_modules = 'date|calendar|location|feeds|parser_ical|interwiki|linkchecker|pathauto';
$base = dirname(__FILE__);
$contrib_root = str_replace('__FILE__', $base, PROJECT_CONTRIB_ROOT);
$projects_list = explode('|', $contrib_modules);
$ou_describe = ou_supported_drupal(OU_DRUPAL_WIKI_URL);
echo '> OU describe count: '. count($ou_describe) .PHP_EOL;
if ($do_checkout) {
foreach ($projects_list as $module) {
chdir( $contrib_root );
if ($do_checkout) {
// Todo: check if the module is already present.
// Clone the module..
$clone_cmd = str_replace('__PROJECT__', $module, DRUPAL_GIT_CLONE_PATTERN);
$res = my_system($clone_cmd);
chdir($module);
$res = my_system('ls -al');
if ($do_branch) { // Find a branch to checkout..
$remote_branches = my_system('git branch -a');
if (preg_match('#.*(remotes\/origin\/'. DRUPAL_VERSION .'-[\w\.]+)#', $remote_branches, $matches)) {
$rem_branch = $matches[1];
$branch = str_replace('remotes/origin/', '', $rem_branch);
$res = my_system("git checkout -b $branch $rem_branch");
$res = my_system('git log -1');
$res = my_system('git branch -a');
} else {
echo '> Error, no branch found. Aborting..'.PHP_EOL;
exit (1);
}
} else { // Find a tag to checkout..
$tags = my_system('git tag -l 6.x-*'); #, true);
echo $tags .PHP_EOL;
$tags = explode("\n", $tags);
end($tags);
$last_id = key($tags);
unset($tags[$last_id]);
end($tags);
$last_id = key($tags);
$atag = $tags[$last_id];
echo "> Chosen tag: $atag" .PHP_EOL;
$res = my_system("git checkout $atag");
}
} else {
chdir($module);
}
$res = my_system('git describe --tags');
} //foreach
} //if $checkout.
git_describe_all($contrib_root);
function git_describe_all($contrib_root) {
$dir_list = scandir($contrib_root);
$log_file = str_replace('.php', '', __FILE__) .'.log';
$describe_r = array();
foreach ($dir_list as $module) {
if ($module[0] == '.') continue;
if (! is_dir($contrib_root .'/'. $module)) continue;
chdir( $contrib_root .'/'. $module );
$describe_res = my_system('git describe --tags');
$describe_r[$module] = "$module,$describe_res";
}
$describe = implode('', array_values($describe_r));
echo '> describe all..'.PHP_EOL. $describe;
$bytes = file_put_contents($log_file, $describe); #FILE_APPEND.
}
function ou_supported_drupal($url_raw) {
$res = _http_request_curl($url_raw);
$ouput = $human = '';
$describe = array();
if (preg_match('#Alphabetical.+?Version.+?\|-(.+)#ms', $res->data, $matches_outer)) {
#var_dump($matches_outer);
$data = $matches_outer[1];
if (preg_match_all('#\|([\w ]+?)\s\|(http:.+?)\s\|(6.\w-[\w-.]+)#ms', $data, $matches, PREG_SET_ORDER)) {
#var_dump(count($matches), $matches);
foreach ($matches as $record) {
$label = trim($record[1]);
$url = $record[2];
$tag = $record[3];
$version = preg_replace('/.*-([\d\.x]{3,5}).*/', "$1", $tag);
$project = NULL; #$label;
if (preg_match('#.org\/project\/(.+)#', $url, $matches_url)) {
$project = $matches_url[1];
}
if ($project) {
$ouput .= "$project,$tag".PHP_EOL;
$human .= "$label\t$project\t$tag\t$url".PHP_EOL;
$describe[] = (object) array(
'project' => $project,
'tag' => $tag,
'version' => $version,
);
}
}
}
}
return $describe;
}
function my_system($cmd, $geterr=false) {
$cmd = escapeshellcmd($cmd);
if ($geterr) $cmd .= " 2>&1";
echo '> '. $cmd .PHP_EOL;
$handle = popen($cmd, 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);
#echo "> $result" .PHP_EOL;
// Todo: need to check stderr!
if (FALSE !== strpos($result, 'fatal')) {
echo '> Fatal error. Aborting..'.PHP_EOL;
exit (1);
}
return $result;
}
/** Perform the HTTP request using cURL.
*/
function _http_request_curl($url, $spoof=false, $options=array()) {
if (!function_exists('curl_init')) die('Error, cURL is required.');
$result = (object)array();
$h_curl = curl_init($url);
#curl_setopt($h_curl, CURLOPT_USERAGENT, $options['ua']);
if (!$spoof) {
#curl_setopt($h_curl, CURLOPT_REFERER, base_url());
}
if (isset($options['cookie'])) {
curl_setopt($h_curl, CURLOPT_COOKIE, $options['cookie']);
header('X-Proxy-Cookie: '.$options['cookie']);
}
curl_setopt($h_curl, CURLOPT_AUTOREFERER, TRUE);
#curl_setopt($h_curl, CURLOPT_MAXREDIRS, $options['max_redirects']);
curl_setopt($h_curl, CURLOPT_FOLLOWLOCATION, TRUE);
#curl_setopt($h_curl, CURLOPT_TIMEOUT, $options['timeout']);
$http_proxy = 'wwwcache.open.ac.uk:80'; #$this->CI->config->item('http_proxy');
if ($http_proxy) {
curl_setopt($h_curl, CURLOPT_PROXY, $http_proxy);
}
curl_setopt($h_curl, CURLOPT_RETURNTRANSFER, TRUE);
$result->data = curl_exec($h_curl);
if ($errno = curl_errno($h_curl)) {
//Error. Quietly log?
#$this->CI->_log('error', "cURL $errno, ".curl_error($h_curl)." GET $url");
#$this->CI->firephp->fb("cURL $errno", "cURL error", "ERROR");
echo "cURL error $errno, ".curl_error($h_curl)." GET $url".PHP_EOL;
exit (1);
}
$result->info = curl_getinfo($h_curl);
$result->success = ($result->info['http_code'] < 300);
return (object) $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment