LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  April 2015

HPS-SVN April 2015

Subject:

r2705 - in /java/trunk/conditions/src/main/java/org/hps/conditions/cli: CommandLineTool.java TagCommand.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Wed, 15 Apr 2015 02:10:40 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (590 lines)

Author: [log in to unmask]
Date: Tue Apr 14 19:10:34 2015
New Revision: 2705

Log:
Add preliminary version of utility for tagging conditions records (needs more testing).

Added:
    java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java
Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java	Tue Apr 14 19:10:34 2015
@@ -32,202 +32,12 @@
     /**
      * Setup logging.
      */
-    private static final Logger LOGGER =
-            LogUtil.create(CommandLineTool.class.getSimpleName(), new DefaultLogFormatter(), Level.WARNING);
-
-    /**
-     * The top level options (does not include sub-command options).
-     */
-    private Options options = new Options();
-
-    /**
-     * The map of named command handlers.
-     */
-    private Map<String, AbstractCommand> commands = new HashMap<String, AbstractCommand>();
-
-    /**
-     * The options parser.
-     */
-    private PosixParser parser = new PosixParser();
-
-    /**
-     * The database conditions system manager.
-     */
-    private DatabaseConditionsManager conditionsManager;
-
-    /**
-     * The verbose setting.
-     */
-    private boolean verbose = false;
-
-    /**
-     * The main method for the class.
-     *
-     * @param arguments The command line arguments.
-     */
-    public static void main(final String[] arguments) {
-        CommandLineTool.create().run(arguments);
-    }
-
-    /**
-     * Run the command line tool, parsing the command line and sending arguments to sub-command handlers.
-     *
-     * @param arguments the command line arguments passed directly from {@link #main(String[])}
-     */
-    private void run(final String[] arguments) {
-        try {
-            if (arguments.length == 0) {
-                printUsage();
-                exit(0);
-            }
-
-            CommandLine commandLine = null;
-            try {
-                commandLine = parser.parse(options, arguments, true);
-            } catch (ParseException e) {
-                LOGGER.log(Level.SEVERE, "error parsing the options", e);
-                printUsage();
-                exit(1);
-            }
-
-            if (commandLine.hasOption("h") || commandLine.getArgs().length == 0) {
-                printUsage();
-                exit(0);
-            }
-
-            // Set verbosity.
-            if (commandLine.hasOption("v")) {
-                LOGGER.setLevel(Level.ALL);
-                LOGGER.getHandlers()[0].setLevel(Level.ALL);
-                verbose = true;
-                LOGGER.config("verbose mode enabled");
-            }
-
-            // Setup conditions manager from command line options.
-            setupConditionsManager(commandLine);
-
-            // Get the sub-command to use.
-            final String commandName = commandLine.getArgs()[0];
-            final AbstractCommand command = commands.get(commandName);
-            if (command == null) {
-                throw new IllegalArgumentException("Unknown command " + commandName);
-            }
-
-            // Copy remaining arguments for sub-command.
-            final String[] commandArguments = new String[commandLine.getArgs().length - 1];
-            System.arraycopy(commandLine.getArgs(), 1, commandArguments, 0, commandArguments.length);
-
-            // Excecute the sub-command.
-            command.setVerbose(verbose);
-            command.execute(commandArguments);
-        } catch (Exception e) {
-            e.printStackTrace();
-            System.exit(1);
-        } finally {
-            conditionsManager.closeConnection();
-        }
-    }
-
-    /**
-     * Setup the conditions system based on command line arguments.
-     *
-     * @param commandLine the parsed command line arguments
-     */
-    private void setupConditionsManager(final CommandLine commandLine) {
-
-        LOGGER.info("setting up conditions manager");
-
-        // Create new manager.
-        conditionsManager = DatabaseConditionsManager.getInstance();
-
-        // Set log level.
-        conditionsManager.setLogLevel(LOGGER.getLevel());
-
-        // Connection properties.
-        if (commandLine.hasOption("p")) {
-            final File connectionPropertiesFile = new File(commandLine.getOptionValue("p"));
-            conditionsManager.setConnectionProperties(connectionPropertiesFile);
-            LOGGER.config("connection properties -p " + connectionPropertiesFile);
-        }
-
-        // XML config.
-        if (commandLine.hasOption("x")) {
-            final File xmlConfigFile = new File(commandLine.getOptionValue("x"));
-            conditionsManager.setXmlConfig(xmlConfigFile);
-            LOGGER.config("XML config -x " + xmlConfigFile);
-        }
-
-        // If there is a run number or detector number then attempt to initialize the conditions system.
-        if (commandLine.hasOption("r") || commandLine.hasOption("d")) {
-
-            LOGGER.config("detector name or run number supplied so manager will be initialized");
-
-            // Set detector name.
-            String detectorName = null;
-            if (commandLine.hasOption("d")) {
-                detectorName = commandLine.getOptionValue("d");
-                LOGGER.config("detector -d " + detectorName);
-            } else {
-                detectorName = "HPS-ECalCommissioning-v2";
-                LOGGER.config("default detector " + detectorName + " is being used");
-            }
-
-            // Get run number.
-            Integer run = null;
-            if (commandLine.hasOption("r")) {
-                run = Integer.parseInt(commandLine.getOptionValue("r"));
-                LOGGER.config("run -r " + run);
-            } else {
-                run = 0;
-                LOGGER.config("default run number " + run + " is being used");
-            }
-
-            // Setup the conditions manager with user detector name and run number.
-            try {
-                LOGGER.config("initializing conditions manager with detector " + detectorName + " and run " + run);
-                DatabaseConditionsManager.getInstance().setDetector(detectorName, run);
-                LOGGER.config("conditions manager initialized successfully");
-                LOGGER.getHandlers()[0].flush();
-            } catch (ConditionsNotFoundException e) {
-                throw new RuntimeException(e);
-            }
-        }
-    }
-
-    /**
-     * Print the usage statement for this tool to the console.
-     */
-    final void printUsage() {
-        final HelpFormatter help = new HelpFormatter();
-        final StringBuffer s = new StringBuffer();
-        for (String command : commands.keySet()) {
-            s.append(command + '\n');
-        }
-        help.printHelp("CommandLineTool", "Commands:\n" + s.toString(), options, "");
-    }
-
-    /**
-     * Exit with the given status.
-     *
-     * @param status the exit status
-     */
-    private void exit(final int status) {
-        System.exit(status);
-    }
-
-    /**
-     * Register a sub-command handler.
-     * @param command the sub-command handler
-     */
-    private void registerCommand(final AbstractCommand command) {
-        if (commands.containsKey(command.getName())) {
-            throw new IllegalArgumentException("There is already a command called " + command.getName());
-        }
-        commands.put(command.getName(), command);
-    }
+    private static final Logger LOGGER = LogUtil.create(CommandLineTool.class.getSimpleName(),
+            new DefaultLogFormatter(), Level.WARNING);
 
     /**
      * Create a basic instance of this class.
+     * 
      * @return the instance of this class
      */
     private static CommandLineTool create() {
@@ -241,6 +51,199 @@
         cli.registerCommand(new LoadCommand());
         cli.registerCommand(new PrintCommand());
         cli.registerCommand(new AddCommand());
+        cli.registerCommand(new TagCommand());
         return cli;
     }
+
+    /**
+     * The main method for the class.
+     *
+     * @param arguments The command line arguments.
+     */
+    public static void main(final String[] arguments) {
+        CommandLineTool.create().run(arguments);
+    }
+
+    /**
+     * The map of named command handlers.
+     */
+    private final Map<String, AbstractCommand> commands = new HashMap<String, AbstractCommand>();
+
+    /**
+     * The database conditions system manager.
+     */
+    private DatabaseConditionsManager conditionsManager;
+
+    /**
+     * The top level options (does not include sub-command options).
+     */
+    private final Options options = new Options();
+
+    /**
+     * The options parser.
+     */
+    private final PosixParser parser = new PosixParser();
+
+    /**
+     * The verbose setting.
+     */
+    private boolean verbose = false;
+
+    /**
+     * Exit with the given status.
+     *
+     * @param status the exit status
+     */
+    private void exit(final int status) {
+        System.exit(status);
+    }
+
+    /**
+     * Print the usage statement for this tool to the console.
+     */
+    final void printUsage() {
+        final HelpFormatter help = new HelpFormatter();
+        final StringBuffer s = new StringBuffer();
+        for (final String command : this.commands.keySet()) {
+            s.append(command + '\n');
+        }
+        help.printHelp("CommandLineTool", "Commands:\n" + s.toString(), this.options, "");
+    }
+
+    /**
+     * Register a sub-command handler.
+     * 
+     * @param command the sub-command handler
+     */
+    private void registerCommand(final AbstractCommand command) {
+        if (this.commands.containsKey(command.getName())) {
+            throw new IllegalArgumentException("There is already a command called " + command.getName());
+        }
+        this.commands.put(command.getName(), command);
+    }
+
+    /**
+     * Run the command line tool, parsing the command line and sending arguments to sub-command handlers.
+     *
+     * @param arguments the command line arguments passed directly from {@link #main(String[])}
+     */
+    private void run(final String[] arguments) {
+        try {
+            if (arguments.length == 0) {
+                printUsage();
+                exit(0);
+            }
+
+            CommandLine commandLine = null;
+            try {
+                commandLine = this.parser.parse(this.options, arguments, true);
+            } catch (final ParseException e) {
+                LOGGER.log(Level.SEVERE, "error parsing the options", e);
+                printUsage();
+                exit(1);
+            }
+
+            if (commandLine.hasOption("h") || commandLine.getArgs().length == 0) {
+                printUsage();
+                exit(0);
+            }
+
+            // Set verbosity.
+            if (commandLine.hasOption("v")) {
+                LOGGER.setLevel(Level.ALL);
+                LOGGER.getHandlers()[0].setLevel(Level.ALL);
+                this.verbose = true;
+                LOGGER.config("verbose mode enabled");
+            }
+
+            // Setup conditions manager from command line options.
+            setupConditionsManager(commandLine);
+
+            // Get the sub-command to use.
+            final String commandName = commandLine.getArgs()[0];
+            final AbstractCommand command = this.commands.get(commandName);
+            if (command == null) {
+                throw new IllegalArgumentException("Unknown command " + commandName);
+            }
+
+            // Copy remaining arguments for sub-command.
+            final String[] commandArguments = new String[commandLine.getArgs().length - 1];
+            System.arraycopy(commandLine.getArgs(), 1, commandArguments, 0, commandArguments.length);
+
+            // Excecute the sub-command.
+            command.setVerbose(this.verbose);
+            command.execute(commandArguments);
+        } catch (final Exception e) {
+            e.printStackTrace();
+            System.exit(1);
+        } finally {
+            this.conditionsManager.closeConnection();
+        }
+    }
+
+    /**
+     * Setup the conditions system based on command line arguments.
+     *
+     * @param commandLine the parsed command line arguments
+     */
+    private void setupConditionsManager(final CommandLine commandLine) {
+
+        LOGGER.info("setting up conditions manager");
+
+        // Create new manager.
+        this.conditionsManager = DatabaseConditionsManager.getInstance();
+
+        // Set log level.
+        this.conditionsManager.setLogLevel(LOGGER.getLevel());
+
+        // Connection properties.
+        if (commandLine.hasOption("p")) {
+            final File connectionPropertiesFile = new File(commandLine.getOptionValue("p"));
+            this.conditionsManager.setConnectionProperties(connectionPropertiesFile);
+            LOGGER.config("connection properties -p " + connectionPropertiesFile);
+        }
+
+        // XML config.
+        if (commandLine.hasOption("x")) {
+            final File xmlConfigFile = new File(commandLine.getOptionValue("x"));
+            this.conditionsManager.setXmlConfig(xmlConfigFile);
+            LOGGER.config("XML config -x " + xmlConfigFile);
+        }
+
+        // If there is a run number or detector number then attempt to initialize the conditions system.
+        if (commandLine.hasOption("r") || commandLine.hasOption("d")) {
+
+            LOGGER.config("detector name or run number supplied so manager will be initialized");
+
+            // Set detector name.
+            String detectorName = null;
+            if (commandLine.hasOption("d")) {
+                detectorName = commandLine.getOptionValue("d");
+                LOGGER.config("detector -d " + detectorName);
+            } else {
+                detectorName = "HPS-ECalCommissioning-v2";
+                LOGGER.config("default detector " + detectorName + " is being used");
+            }
+
+            // Get run number.
+            Integer run = null;
+            if (commandLine.hasOption("r")) {
+                run = Integer.parseInt(commandLine.getOptionValue("r"));
+                LOGGER.config("run -r " + run);
+            } else {
+                run = 0;
+                LOGGER.config("default run number " + run + " is being used");
+            }
+
+            // Setup the conditions manager with user detector name and run number.
+            try {
+                LOGGER.config("initializing conditions manager with detector " + detectorName + " and run " + run);
+                DatabaseConditionsManager.getInstance().setDetector(detectorName, run);
+                LOGGER.config("conditions manager initialized successfully");
+                LOGGER.getHandlers()[0].flush();
+            } catch (final ConditionsNotFoundException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
 }

Added: java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java	(added)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/TagCommand.java	Tue Apr 14 19:10:34 2015
@@ -0,0 +1,161 @@
+package org.hps.conditions.cli;
+
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.Set;
+import java.util.logging.Level;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.hps.conditions.api.ConditionsObjectCollection;
+import org.hps.conditions.api.ConditionsObjectException;
+import org.hps.conditions.api.ConditionsRecord;
+import org.hps.conditions.api.ConditionsRecord.ConditionsRecordCollection;
+import org.hps.conditions.database.DatabaseConditionsManager;
+import org.hps.conditions.database.TableMetaData;
+import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException;
+
+/**
+ * Create a conditions system tag.
+ *
+ * @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a>
+ */
+public class TagCommand extends AbstractCommand {
+
+    /**
+     * The default detector name (dummy detector).
+     */
+    private static final String DETECTOR_NAME = "HPS-dummy-detector";
+
+    /**
+     * Defines command options.
+     */
+    private static Options OPTIONS = new Options();
+
+    /**
+     * Define command options.
+     */
+    static {
+        OPTIONS.addOption(new Option("h", false, "Show help for tag command"));
+        OPTIONS.addOption(new Option("r", true, "List of run numbers to scan (at least one must be provided)"));
+        OPTIONS.getOption("r").setArgs(Option.UNLIMITED_VALUES);
+        OPTIONS.getOption("r").setRequired(true);
+        OPTIONS.addOption(new Option("t", true, "The new conditions tag"));
+        OPTIONS.getOption("t").setRequired(true);
+        OPTIONS.addOption(new Option("X", true, "Actually make the new tag in the database (dry run is default)"));
+    }
+
+    /**
+     * Class constructor.
+     */
+    TagCommand() {
+        super("tag", "Tag a set of collections by copying their conditions records", OPTIONS);
+    }
+
+    /**
+     * Execute the tag command.
+     */
+    @Override
+    void execute(final String[] arguments) {
+
+        final CommandLine commandLine = parse(arguments);
+
+        final Set<Integer> runNumbers = new LinkedHashSet<Integer>();
+        for (final String value : commandLine.getOptionValues("r")) {
+            runNumbers.add(Integer.parseInt(value));
+        }
+        if (runNumbers.size() == 0) {
+            throw new RuntimeException("At least one run number must be provided with the -r switch.");
+        }
+
+        final String newTag;
+        if (commandLine.hasOption("t")) {
+            newTag = commandLine.getOptionValue("t");
+        } else {
+            throw new RuntimeException("Missing required -t argument with the tag name.");
+        }
+
+        boolean makeTag = false;
+        if (commandLine.hasOption("X")) {
+            makeTag = true;
+        }
+
+        final ConditionsRecordCollection tagRecords = new ConditionsRecordCollection();
+        final Set<Integer> addedIds = new HashSet<Integer>();
+
+        final DatabaseConditionsManager manager = DatabaseConditionsManager.getInstance();
+        manager.setXmlConfig("/org/hps/conditions/config/conditions_database_no_svt.xml");
+        manager.setLogLevel(Level.ALL);
+
+        // DEBUG: Hard-coded to development connection for now.
+        manager.setConnectionResource("/org/hps/conditions/config/jlab_dev_connection.prop");
+
+        // Scan through all the runs between the start and end run, inclusive.
+        for (final Integer run : runNumbers) {
+            try {
+                // Setup the conditions manager with the run number.
+                manager.setDetector(TagCommand.DETECTOR_NAME, run);
+            } catch (final ConditionsNotFoundException e) {
+                throw new RuntimeException(e);
+            }
+
+            // The records from this run.
+            final ConditionsRecordCollection records = manager.getConditionsRecords();
+
+            // The unique conditions keys from this run.
+            final Set<String> keys = records.getConditionsKeys();
+
+            // Scan through all the unique keys.
+            for (final String key : keys) {
+
+                // Get the table meta data for the key.
+                final TableMetaData tableMetaData = manager.findTableMetaData(key);
+
+                // Get the default collection for this key in the run.
+                final ConditionsObjectCollection<?> collection = manager.getCachedConditions(
+                        tableMetaData.getCollectionClass(), tableMetaData.getTableName()).getCachedData();
+
+                // Get the ConditionsRecord from the collection.
+                final ConditionsRecord record = collection.getConditionsRecord();
+
+                // Is this record already part of the new tag?
+                if (!addedIds.contains(record.getRowId())) {
+                    // Create a new record copied from the old one.
+                    final ConditionsRecord newRecord = new ConditionsRecord(record);
+
+                    // Set the tag value.
+                    newRecord.setFieldValue("tag", newTag);
+
+                    // Add the record to the tag.
+                    tagRecords.add(newRecord);
+
+                    // Flag the record's ID as used so it is only added once.
+                    addedIds.add(record.getRowId());
+                }
+            }
+        }
+
+        // Print out all the records that were found.
+        System.out.println("found ConditionsRecords for tag " + newTag + " ...");
+        for (final ConditionsRecord record : tagRecords) {
+            System.out.println(record.toString());
+        }
+
+        // TODO: Could have command line "Y/N" confirmation here to apply the tag.
+
+        // A tag will only be made if the -X was present in the command line arguments.
+        if (makeTag) {
+            try {
+                tagRecords.insert();
+            } catch (ConditionsObjectException | SQLException e) {
+                throw new RuntimeException(e);
+            }
+        } else {
+            System.out.println("tag was NOT applied (use -X to do this)");
+        }
+
+        System.out.println("DONE!");
+    }
+}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use