Parcourir la source

Fixed bug with file creation. Added new drush commands following new style for existing drush functions for tripal_core module

Stephen Ficklin il y a 10 ans
Parent
commit
40ac6bc6ef
2 fichiers modifiés avec 320 ajouts et 117 suppressions
  1. 3 2
      tripal_core/api/tripal_core.files.api.inc
  2. 317 115
      tripal_core/tripal_core.drush.inc

+ 3 - 2
tripal_core/api/tripal_core.files.api.inc

@@ -49,9 +49,10 @@ function tripal_create_files_dir($module_name, $path = FALSE) {
     tripal_create_files_dir($module_name);
 
     // now make sure the sub dir exists
-    $sub_dir = tripal_get_files_dir() . $module_name . $path;
+    $sub_dir = tripal_get_files_dir() . '/' . $module_name . '/' . $path;
+    print "$sub_dir\n";
     if (!file_prepare_directory($sub_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
-      $message = "Cannot create directory $sub_dir. ";
+      $message = "Can not create directory $sub_dir. ";
       drupal_set_message(check_plain(t($message)), 'error');
       tripal_report_error('tripal_core', TRIPAL_ERROR, $message, array());
     }

+ 317 - 115
tripal_core/tripal_core.drush.inc

@@ -21,29 +21,55 @@
  */
 function tripal_core_drush_help($command) {
   switch ($command) {
-    // Tripal Materialized Views
-    case 'drush:tripal-update-mview':
-      return dt('Updates the specified materialized view.');
-
-    // Tripal Jobs
+    
+    // TRIPAL JOBS
+    case 'trp-run-jobs':
+      return dt('Launches pending jobs waiting in the queue.');
+      break;
+    case 'trp-rerun-job':
+      return dt('Rerun a job in the queue.');
+      break;
+    case 'trp-get-currjob':
+      return dt('Returns details about the currently running tripal job including percent complete.');
+      break;
+    // Placeholders for unimplmeneted jobs
+    case 'trp-show-job':
+      break;
+    case 'trp-revert-jobs':
+      break;
+    case 'trp-cancel-job':
+      break;
+    case 'trp-list-jobs':
+      break;
+    // Deprecated functions
     case 'drush:tripal-launch-jobs':
-      return dt('Launches any Tripal jobs waiting in the queue.');
+      return dt('DEPRECATED. Please use: trp-run-jobs.');
+      break;
     case 'drush:tripal-current-job':
-      return dt('Returns details about the currently running tripal job including percent complete.');
+      return dt('DEPRECATED. Please use: trp-get-currjob.');
+      break;
     case 'drush:tripal-rerun-job':
-      return dt('Rerun any tripal job in the queue.');
+      return dt('DEPRECATED. Please use: trp-rerun-job.');
+      break;
+
+    // Tripal Materialized Views
+    case 'drush:tripal-update-mview':
+      return dt('Updates the specified materialized view.');
+      break;
 
-    // Chado-specific
+    // Chado Specific
     case 'drush:tripal-chado-version':
       return dt('Returns the current version of chado associated with this drupal site.');
+      break;
     case 'drush:tripal-chadotable-desc':
       return dt('Returns the table description as specified in the Tripal Schema API for the supplied table.');
+      break;
 
   }
 }
 
 /**
- * Registers a drush command and constructs the full help for that command
+ * Registers a drush command and constructs the full help for that command.
  *
  * @return
  *   And array of command descriptions
@@ -53,39 +79,121 @@ function tripal_core_drush_help($command) {
 function tripal_core_drush_command() {
   $items = array();
 
-  // Materialized Views
-  $items['tripal-mview-update'] = array(
-    // used by drush help
-    'description' => dt('Updates the specified materialized view.'),
-    'arguments' => array(
-      'mview_id' => dt('The ID of the materialized view to update'),
-      'table_name' => dt('The name of the materialized view table to update.'),
+  $items['trp-refresh-mview'] = array(
+    'description' => dt('Refreshes the contents of the specified materialized view.'),
+    'arguments' => array(),
+    'examples' => array(
+      'By Materialized View ID' => 'drush trp-refresh-mview --mview=5',
+      'By Table Name' => 'drush trp-refresh-mview --table=organism_feature_count'
+    ),
+    'options' => array(
+      'mview' => dt('The ID of the materialized view to update'),
+      'table' => dt('The name of the materialized view table to update.'),
+    ),
+  );
+  $items['trp-get-currjob'] = array(
+    'description' => dt('Returns details about the currently running tripal job including percent complete.'),
+    'arguments'   => array(),
+    'examples' => array(
+      'Standard example' => 'drush trp-get-currjob',
+    ),
+  );
+  $items['trp-run-jobs'] = array(
+    'description' => dt('Launches jobs waiting in the queue. Only one job can execute at a time unless the --parllel=1 option is provided.'),
+    'examples' => array(
+      'Single Job' => 'drush trp-run-jobs --user=administrator',
+      'Parallel Job' => 'drush trp-run-jobs --user=administrator --parallel=1'
+    ),
+    'arguments' => array(),
+    'options' => array(
+      'user' => array(
+        'description' => dt('The Drupal user name for which the job should be run.  The permissions for this user will be used.'),
+        'required' => TRUE,
+      ),
+      'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
+      'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
     ),
+  );
+  $items['trp-rerun-job'] = array(
+    'description' => dt('Rerun any job in the queue.'),
+    'examples' => array(
+      'Single Job' => 'drush trp-rerun-job --user=administrator --job_id=2',
+      'Parallel Job' => 'drush trp-rerun-job --user=administrator  --job_id=2 --parallel=1'
+    ),
+    'arguments' => array(),
+    'options' => array(
+      'user' => array(
+        'description' => dt('The Drupal user name for which the job should be run.  The permissions for this user will be used.'),
+        'required' => TRUE,
+      ),
+      'job_id' => array(
+        'description' => dt('The job ID to run.'),
+        'required' => TRUE,
+      ),
+      'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
+    ),
+  );
+  $items['trp-get-cversion'] = array(
+    'description' => dt('Returns the current version of chado associated with this drupal site.'),
+    'arguments' => array(),
+    'examples' => array(
+      'Standard Example' => 'drush trp-get-cversion',
+    ),
+  );
+  $items['trp-get-table'] = array(
+    'description' => dt('Returns a table description in Drupal Schema API format.'),
+    'arguments' => array(),
+    'examples' => array(
+      'By Table Name' => 'drush trp-get-table --table=feature',
+      'By Section' => 'drush trp-get-table --table=feature --section=fields'
+    ),
+    'options' => array(
+      'table' => array(
+        'description' => dt('The name of the table. The table can be a true Chado table or a custom Chado table.'),
+        'required' => TRUE,
+      ),
+      'section' => dt('Only return the specified section of the schema array. Possible sections include: description, fields, primary key, unique keys, foreign keys, indexes, referring_tables.'),
+    ),
+  );
+  $items['trp-clean-nodes'] = array(
+    'description' => dt('Clean-up orphaned Drupal nodes and chado records.'),
+    'arguments' => array(),
+    'examples' => array(
+      'Standard Example' => 'drush trp-clean-nodes table=feature'
+    ),
+    'options' => array(
+      'table' => array(
+        'description' => dt('The name of the table that corresonds to the node type to ' .
+          'clean up. (e.g organism, feature, stock, library, analysis, pub, etc.)'),
+        'required' => TRUE,
+      ),
+    ),
+  );
+
+  // DEPRICATED COMMANDS. Depricated as of Tripal v2.0-rc
+  $items['tripal-mview-update'] = array(
+    'description' => dt('DEPRICATED. Please see: trp-refresh-mview.'),
+    'arguments' => array(),
     'examples' => array(
       'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
       'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
     ),
-    // supply options
     'options' => array(
-      'mview_id',
-      'table_name'
+      'mview_id' => dt('The ID of the materialized view to update'),
+      'table_name' => dt('The name of the materialized view table to update.'),
     ),
-    'aliases' => array('trpmv-up', 'tripal-update-mview')
+    'aliases' => array('trpmv-up')
   );
-
-  // Jobs
   $items['tripal-jobs-current'] = array(
-    'description' => dt('Returns details about the currently running tripal job including percent complete.'),
-    'arguments'   => array(
-    ),
+    'description' => dt('DEPRECATED. Please see: trp-get-currjob.'),
+    'arguments'   => array(),
     'examples' => array(
       'Standard example' => 'drush tripal-jobs-current',
     ),
-    'aliases' => array('trpjob-cur', 'tripal-current-job'),
+    'aliases' => array('trpjob-cur'),
   );
   $items['tripal-jobs-launch'] = array(
-    // used by drush help
-    'description' => dt('Lauches any jobs waiting in the queue.'),
+    'description' => dt('DEPRECATED. Please see: trp-run-jobs. '),
     'examples' => array(
       'Normal Job' => 'drush tripal-jobs-launch admin',
       'Parallel Job' => 'drush tripal-jobs-launch admin --parallel=1'
@@ -93,16 +201,14 @@ function tripal_core_drush_command() {
     'arguments' => array(
       'user' => dt('The Drupal username under which the job should be run.  The permissions for this user will be used.'),
     ),
-    // supply options
     'options' => array(
       'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
       'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
     ),
-    'aliases' => array('trpjob-run', 'tripal-launch-jobs')
+    'aliases' => array('trpjob-run')
   );
   $items['tripal-jobs-rerun'] = array(
-    // used by drush help
-    'description' => dt('Rerun any job in the queue.'),
+    'description' => dt('DEPRECATED. Please see: trp-rerun-job. '),
     'examples' => array(
       'Normal Job' => 'drush tripal-jobs-rerun admin 2',
       'Parallel Job' => 'drush tripal-jobs-rerun admin  2 --parallel=1'
@@ -111,67 +217,41 @@ function tripal_core_drush_command() {
       'user' => dt('The Drupal username under which the job should be run.  The permissions for this user will be used.'),
       'job_id' => dt('The job ID to run.'),
     ),
-    // supply options
     'options' => array(
       'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
     ),
-    'aliases' => array('trpjob-rerun', 'tripal-rerun-job')
+    'aliases' => array('trpjob-rerun')
   );
-
-  // Chado-specific
   $items['tripal-chado-version'] = array(
-    // used by drush help
-    'description' => dt('Returns the current version of chado associated with this drupal site.'),
-    'arguments' => array(
-    ),
+    'description' => dt('DEPRECATED. Please see: trp-get-cversion. '),
+    'arguments' => array(),
     'examples' => array(
       'Standard Example' => 'drush tripal-chado-version',
     ),
     'aliases' => array('trpchado-ver')
   );
   $items['tripal-chadotable-desc'] = array(
-    // used by drush help
-    'description' => dt('Returns the table description as specified in the Tripal Schema API for the supplied table.'),
+    'description' => dt('DEPRICATED. Please see: trp-get-table. '),
     'arguments' => array(
       'table_name' => dt('The name of the chado table.'),
     ),
     'examples' => array(
       'By Table Name' => 'drush tripal-chadotable-desc --table_name=feature'
     ),
-    // supply options
     'options' => array(
       'section' => dt('Only return the specified section of the schema table description. Possible sections include: description, fields, primary key, unique keys, foreign keys, indexes, referring_tables.'),
     ),
     'aliases' => array('trpschema-tbl')
   );
-
-  // Tripal Node-Module Specific
-  $items['tripal-node-sync'] = array(
-    // used by drush help
-    'description' => dt('Sync\'s chado records with drupal creating nodes for the given chado-centric module.'),
-    'arguments' => array(
-      'module' => dt('The name of the chado-centric module to sync.'),
-    ),
-    'examples' => array(
-      'By Module' => 'drush tripal-node-sync feature'
-    ),
-    // supply options
-    'options' => array(
-    ),
-    'aliases' => array('trpnode-sync')
-  );
   $items['tripal-node-clean'] = array(
-    // used by drush help
-    'description' => dt('Clean-up orphaned Drupal nodes and chado records.'),
+    'description' => dt('DEPRICATED. Please see: trp-clean-nodes.'),
     'arguments' => array(
       'module' => dt('The name of the chado-centric module to clean-up.'),
     ),
     'examples' => array(
       'By Table Name' => 'drush tripal-node-clean feature'
     ),
-    // supply options
-    'options' => array(
-    ),
+    'options' => array(),
     'aliases' => array('trpnode-cln')
   );
 
@@ -203,9 +283,36 @@ function drush_tripal_core_set_user($username) {
 }
 
 /**
- * Executes jobs in the Tripal Jobs Queue
+ * Executes jobs in the Tripal Jobs Queue.
  *
- * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
+ * Executed when 'drush trp-run-job' is called.
+ *
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_trp_run_jobs() {
+  $username = drush_get_option('user');
+  $parallel = drush_get_option('parallel');
+  $job_id   = drush_get_option('job_id');
+
+  drush_tripal_core_set_user($username);
+
+  if ($parallel) {
+    drush_print("Tripal Job Launcher (in parallel)");
+    drush_print("Running as user '$username'");
+    drush_print("-------------------");
+    tripal_launch_job($parallel, $job_id);
+  }
+  else {
+    drush_print("Tripal Job Launcher");
+    drush_print("Running as user '$username'");
+    drush_print("-------------------");
+    tripal_launch_job(0, $job_id);
+  }
+}
+/**
+ * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
+ *
+ * Executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called.
  *
  * @ingroup tripal_drush
  */
@@ -214,6 +321,9 @@ function drush_tripal_core_tripal_jobs_launch($username) {
   $job_id = drush_get_option('job_id');
 
   drush_tripal_core_set_user($username);
+  
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+    "continue to work but please consider using the 'trp-run-jobs' command.\n\n");
 
   if ($parallel) {
     drush_print("Tripal Job Launcher (in parallel)");
@@ -230,14 +340,47 @@ function drush_tripal_core_tripal_jobs_launch($username) {
 }
 
 /**
- * Executes jobs in the Tripal Jobs Queue
+ * Executes jobs in the Tripal Jobs Queue.
  *
- * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
+ * Executed when 'drush trp-rerun-job' is called.
  *
  * @ingroup tripal_drush
  */
-function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
+function drush_tripal_core_trp_rerun_job() {
+  $username = drush_get_option('user');
+  $parallel = drush_get_option('parallel');
+  $job_id   = drush_get_option('job_id');
+  
+  drush_tripal_core_set_user($username);
+  $new_job_id = tripal_rerun_job($job_id, FALSE);
+
+  if ($parallel) {
+    drush_print("Tripal Job Launcher (in parallel)");
+    drush_print("Running as user '$username'");
+    drush_print("-------------------");
+    tripal_launch_job($parallel, $new_job_id);
+  }
+  else {
+    drush_print("Tripal Job Launcher");
+    drush_print("Running as user '$username'");
+    drush_print("-------------------");
+    tripal_launch_job(0, $new_job_id);
+  }
 
+}
+
+/**
+ * DEPRECATED. Executes jobs in the Tripal Jobs Queue.
+ *
+ * Executed when 'drush tripal-jobs-rerun' is called.
+ *
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
+  
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+      "continue to work but please consider using the 'trp-rerun-job' command.\n\n");
+  
   drush_tripal_core_set_user($username);
   $new_job_id = tripal_rerun_job($job_id, FALSE);
   drush_tripal_core_tripal_jobs_launch($username, $new_job_id);
@@ -245,13 +388,13 @@ function drush_tripal_core_tripal_jobs_rerun($username, $job_id) {
 }
 
 /**
- * Prints details about the current running job
+ * Prints details about the current running job.
  *
- * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
+ * Executed when 'drush trp-get-currjob' is called.
  *
  * @ingroup tripal_drush
  */
-function drush_tripal_core_tripal_jobs_current() {
+function drush_tripal_core_trp_get_currjob() {
   $sql =  "
     SELECT *
     FROM {tripal_jobs} TJ
@@ -262,12 +405,12 @@ function drush_tripal_core_tripal_jobs_current() {
   foreach ($jobs as $job) {
     $job_pid = $job->pid;
     $output = "Name: " . $job->job_name . "\n" .
-              "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
-              "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
-              "Module: " . $job->modulename . "\n" .
-              "Callback: " . $job->callback . "\n" .
-              "Process ID: " . $job->pid . "\n" .
-              "Progress: " . $job->progress . "%\n";
+        "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
+        "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
+        "Module: " . $job->modulename . "\n" .
+        "Callback: " . $job->callback . "\n" .
+        "Process ID: " . $job->pid . "\n" .
+        "Progress: " . $job->progress . "%\n";
     drush_print($output);
   }
   if (!$job_pid) {
@@ -278,20 +421,67 @@ function drush_tripal_core_tripal_jobs_current() {
 }
 
 /**
- * Updates the specified materialized view
+ * DEPRECATED. Prints details about the current running job.
  *
- * @param $mview_id
- *   The ID of the materialized view (tripal_mview.mview_id)
- * @param $table_name
- *   The name of the table storing the materialized view (tripal_mview.mv_table)
+ * Executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called.
  *
- * Note: Either $mview_id OR $table_name is required
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_tripal_jobs_current() {
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+      "continue to work but please consider using the 'trp-get-currjob' command.\n\n");
+  
+  drush_tripal_core_trp_get_currjob();
+}
+
+/**
+ * Updates the specified materialized view
+ *
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_trp_refresh_mview() {
+  $mview_id   = drush_get_option('mview');
+  $table_name = drush_get_option('table');
+
+  // Either table_name or mview is required
+  if (!$mview_id) {
+    if ($table_name) {
+      // if table_name supplied use that to get mview_id
+      $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table = :mv_table";
+      $results = db_query($sql, array(':mv_table' => $table_name));
+      $r = $resuls->fetchObject();
+      if (!$r->mview_id) {
+        drush_set_error('No Materialized View associated with that table_name.');
+      }
+      $mview_id=$r->mview_id;
+    }
+    else {
+      drush_set_error('Plese provide one option of --mview or --table.');
+    }
+  }
+
+  drush_print('Updating the Materialized View with ID=' . $mview_id);
+  $status = tripal_populate_mview($mview_id);
+  if ($status) {
+    drush_log('Materialized View Updated', 'ok');
+  }
+  else {
+    drush_set_error('Update failed.');
+  }
+}
+
+/**
+ * DEPRECATED. Updates the specified materialized view.
  *
  * @ingroup tripal_drush
  */
 function drush_tripal_core_tripal_update_mview() {
+  
   $mview_id = drush_get_option('mview_id');
   $table_name = drush_get_option('table_name');
+  
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+      "continue to work but please consider using the 'trp-refresh-mview' command.\n\n");
 
   // Either table_name or mview is required
   if (!$mview_id) {
@@ -321,11 +511,23 @@ function drush_tripal_core_tripal_update_mview() {
 }
 
 /**
- * Returns the current version of chado
+ * DEPRECATED. Returns the current version of chado.
  *
  * @ingroup tripal_drush
  */
 function drush_tripal_core_tripal_chado_version() {
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+      "continue to work but please consider using the 'trp-get-cversion' command.\n\n");
+  
+  drush_tripal_core_trp_get_cversion();
+}
+
+/**
+ * Returns the current version of chado.
+ *
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_trp_get_cversion() {
   $version = $GLOBALS["exact_chado_version"];
   drush_print('Current Chado Version: ' . $version);
 }
@@ -333,6 +535,26 @@ function drush_tripal_core_tripal_chado_version() {
 /**
  * Returns the Tripal Schema API Description of the given table
  *
+ * @ingroup tripal_drush
+ */
+function drush_tripal_core_trp_get_table() {
+  $section = drush_get_option('section');
+  $table_name = drush_get_option('table');
+
+  drush_print("Schema API Description for $table_name:");
+  $desc = chado_get_schema($table_name);
+
+  if (!empty($section)) {
+    drush_print("$section = " . print_r($desc[$section], TRUE));
+  }
+  else {
+    drush_print(print_r($desc, TRUE));
+  }
+}
+
+/**
+ * DEPRICATED. Returns the Tripal Schema API Description of the given table.
+ *
  * @param $table_name
  *  The name of the table to return the description of
  *
@@ -340,6 +562,9 @@ function drush_tripal_core_tripal_chado_version() {
  */
 function drush_tripal_core_tripal_chadotable_desc($table_name) {
   $section = drush_get_option('section');
+  
+  drush_print("\n\nDEPRICATED: This drush command is outdated.\nIt will ".
+      "continue to work but please consider using the 'trp-get-table' command.\n\n");
 
   drush_print("Schema API Description for $table_name:");
   $desc = chado_get_schema($table_name);
@@ -353,41 +578,18 @@ function drush_tripal_core_tripal_chadotable_desc($table_name) {
 }
 
 /**
- * Sync's chado records with drupal creating nodes for the given chado-centric module.
- *
- * @param $module
- *  The name of a module with nodes associated with it. For example, feature
+ * Clean-up orphaned Drupal nodes and chado records.
  *
  * @ingroup tripal_drush
  */
-function drush_tripal_core_tripal_node_sync($module) {
-  switch ($module) {
-    case 'analysis':
-      call_user_func('tripal_analysis_sync_analyses');
-      break;
-    case 'feature':
-      call_user_func('tripal_feature_sync_features');
-      break;
-    case 'featuremap':
-      call_user_func('tripal_featuremap_sync_featuremaps');
-      break;
-    case 'library':
-      call_user_func('tripal_library_sync_libraries');
-      break;
-    case 'organism':
-      call_user_func('tripal_organism_sync_organisms');
-      break;
-    case 'project':
-      call_user_func('tripal_project_sync_all_projects');
-      break;
-    case 'stock':
-      call_user_func('tripal_stock_sync_stocks');
-      break;
-  }
+function drush_tripal_core_trp_clean_nodes() {
+  $table = drush_get_option('table');
+  
+  chado_cleanup_orphaned_nodes($table, 0);
 }
 
 /**
- * Clean-up orphaned Drupal nodes and chado records.
+ * DEPRICATED. Clean-up orphaned Drupal nodes and chado records.
  *
  * @param $module
  *  The name of a module with nodes associated with it. For example, feature