def update_curators(pulsar_id, to_add_ids=[], to_rm_ids=[], existdb=None): """Update the list of curators for the given pulsar. Note: if a user is specified to be added and removed, the removal will take precedence. Inputs: pulsar_id: The ID of the pulsar to edit curators for. to_add_ids: List of user IDs to add as curators. to_rm_ids: List of user IDs to remove as curators. existdb: A (optional) existing database connection object. (Default: Establish a db connection) Outputs: None """ to_add_ids = set(to_add_ids) to_rm_ids = set(to_rm_ids) to_add_ids.difference_update(to_rm_ids) # Remove user_ids that will # lose curator privileges if config.cfg.verbosity >= 2: msg = "Updating curator privileges for %s" % \ utils.get_pulsarname(pulsar_id) for uid in to_add_ids: if uid is None: msg += "\n + Wildcard" else: msg += "\n + %s" % utils.get_userinfo(uid)['real_name'] for uid in to_rm_ids: if uid is None: msg += "\n - Wildcard" else: msg += "\n - %s" % utils.get_userinfo(uid)['real_name'] utils.print_info(msg, 2) # Connect to the database db = existdb or database.Database() db.connect() trans = db.begin() try: # Fetch list of curators select = db.select([db.curators.c.user_id]).\ where(db.curators.c.pulsar_id==pulsar_id) result = db.execute(select) rows = result.fetchall() result.close() # Don't re-add existing curators curators = [row['user_id'] for row in rows] to_add_ids.difference_update(curators) # Add curators ins = db.curators.insert() values=list() for add_id in to_add_ids: values.append({'pulsar_id':pulsar_id, \ 'user_id':add_id}) result = db.execute(ins, values) result.close() # Only add curators that are present to_rm_ids.intersection_update(curators) # Remove curators delete = db.curators.delete().\ where( (db.curators.c.pulsar_id==pulsar_id) & \ (db.curators.c.user_id.in_(to_rm_ids)) ) result = db.execute(delete) result.close() except: trans.rollback() raise else: trans.commit() finally: if not existdb: db.close()
import os import simlibio as sio import OpsimObs.opsimObs as op #import opsim2simlib import utils import argparse #use environment variable setup by setup.sh to obtain the directory of opsims opsimdir = os.environ.get("OPSIMOBS") mydir = os.getcwd() + "/" programdir = os.path.dirname(os.path.realpath(__file__)) #Initialize a logstring logstring = "" logstring += utils.get_userinfo() #parse command line arguments parser = argparse.ArgumentParser(prog="lsstpipeline.py", description= """Runs the lsst pipeline based on several input files, but takes the jobs file as an argument """) parser.add_argument("--inputfile", type=str, default=None, required=True, help="Absolute path to the input file for the pipeline") args = parser.parse_args() inputfile = args.inputfile #Start #record on log file logstring += __file__ + " on inputfile " + inputfile +" started. \n" #Read input file and find out what to do jobdict = utils.processfile(inputfile)
def replace_rawfile(obsolete_id, replace_id, comments, existdb=None): """In the database, mark an obsolete data file as being replaced. Inputs: obsolete_id: The rawfile_id of the data file being replaced. replace_id: The rawfile_id of the replacement data file. comments: A comment describing the replacement. existdb: An (optional) existing database connection object. (Default: Establish a db connection) Outputs: None """ # Connect to the database db = existdb or database.Database() db.connect() # Check if obsolete_id exists in rawfiles. If not, fail. select = db.select([db.rawfiles.c.rawfile_id, \ db.replacement_rawfiles.c.replacement_rawfile_id.\ label("existing_replace_id")], \ from_obj=[db.rawfiles. \ outerjoin(db.replacement_rawfiles, \ onclause=db.replacement_rawfiles.c.obsolete_rawfile_id == \ db.rawfiles.c.rawfile_id)]).\ where(db.rawfiles.c.rawfile_id == obsolete_id) result = db.execute(select) rows = result.fetchall() if len(rows) > 1: raise errors.InconsistentDatabaseError("There are multiple (%d) " \ "rawfiles with ID=%d. Each ID should be unique!" % \ (len(rows), obsolete_id)) elif len(rows) != 1: raise errors.BadInputError("The obsolete rawfile being replaced " \ "(ID:%d) does not exist!" % obsolete_id) row = rows[0] # There is only one row # Check if obsolete_id is already replaced. If so, list replacement and fail. if row['existing_replace_id'] is not None: raise errors.RawfileSuperseded("The rawfile (ID=%d) has already been " \ "replaced by ID=%d. Perhaps it is the " \ "latter file that should be replaced, or " \ "perhaps no additional replacement is " \ "required." % \ (obsolete_id, row['existing_replace_id'])) # Log the replacement user_id = utils.get_userid() ins = db.replacement_rawfiles.insert() values = {'obsolete_rawfile_id':obsolete_id, \ 'replacement_rawfile_id':replace_id, \ 'user_id':user_id, \ 'comments':comments} result = db.execute(ins, values) result.close() # Check if obsolete_id is itself a replacement for other files # If so, mark all with newest replacement and # append comment (tag with date/time)? select = db.select([db.replacement_rawfiles.c.obsolete_rawfile_id, \ db.replacement_rawfiles.c.comments]).\ where(db.replacement_rawfiles.c.replacement_rawfile_id == \ obsolete_id) result = db.execute(select) rows = result.fetchall() result.close() user = utils.get_userinfo() for row in rows: newcomments = row['comments']+"\n%s (%d -> %d at %s): %s" % \ (user, obsolete_id, replace_id, utils.Give_UTC_now(), comments) values = {'replacement_rawfile_id':replace_id, \ 'comments':newcomments} update = db.replacement_rawfiles.c.update().\ where(db.replacement_rawfiles.c.replacement_rawfile_id == \ obsolete_id) results = db.execute(update, values) results.close()