You are here

function patch_manager_runpatch in Patch manager 7

Same name and namespace in other branches
  1. 6 patch_manager.module \patch_manager_runpatch()

Run a patch operation

@todo Get field value by appropriate way.

4 calls to patch_manager_runpatch()
patch_manager_apply_action in ./patch_manager.module
Patch apply action
patch_manager_node_actions_form_apply_submit in ./patch_manager.module
Apply the patch in a node view context
patch_manager_node_actions_form_reverse_submit in ./patch_manager.module
Reverse the patch in a node view context
patch_manager_revert_action in ./patch_manager.module
Patch revert action

File

./patch_manager.module, line 385
Patch manager provides developers with tools for managing patches.

Code

function patch_manager_runpatch($node, $flags = '') {

  // Pull the values from the node
  // TODO Get field value by appropriate way.
  $patchfile = $node->field_patch['und'][0]['uri'];
  $module = $node->field_module['und'][0]['value'];

  // Get the path from which to apply this patch
  // We start with the path to drupal core, then if it's a contrib module
  // try and find that patch. If we can't find the contrib module, stay with
  // drupal core.
  $root = dirname($_SERVER['SCRIPT_FILENAME']);
  if ($module !== 'core') {
    if ($modulepath = drupal_get_path('module', $module)) {
      $root = realpath($modulepath);
    }
    else {
      drupal_set_message(t('Unable to find the specified module ... trying anyway.'), 'warning');
    }
  }

  // Give the patchfile an absolute path
  $patchfile = drupal_realpath($patchfile);

  // Run the command
  $patch = variable_get('patch_manager_path_patch', '/usr/bin/patch');
  foreach (array(
    '-p1',
    '-p0',
  ) as $pn) {
    $cmd = sprintf('%s %s --verbose %s -d %s -i %s', $patch, $pn, $flags, escapeshellarg($root), escapeshellarg($patchfile));
    exec($cmd, $output, $ret);
    if ($ret < 2) {
      break;

      // ret = 0: success, ret = 1: partial apply
    }
  }
  watchdog('patch_manager', 'Ran shell command (%command) which finished with status @status', array(
    '%command' => $cmd,
    '@status' => $ret,
  ));

  // Return the results
  $status = new stdClass();
  $status->cmd = $cmd;
  $status->output = $output;
  $status->status = (int) $ret;
  return $status;
}