Пример #1
0
def get_boot_cron(ignored_params, extra=""):
    '''Turn current python script into a crontab reboot entry'''
    job_args = sanitize(sys.argv, ignored_params)
    job = " ".join(job_args)
    out_fname = get_outfname()

    short_job = " ".join([common.get_cmd()] + job_args[1:])
    msg = "Job '%s' will write output to '%s'" % (short_job, out_fname)

    sys.stderr.write("%s %d seconds after reboot.\n" % (msg, DELAY))

    # Create sleep and wall commands which will countdown DELAY seconds
    # before executing the job
    cmds = ["sleep %d" % DELAY_INTERVAL]
    delay_rem = DELAY - DELAY_INTERVAL
    while delay_rem > 0:
        wmsg = "Restarting experiments in %d seconds. %s" % (delay_rem, extra)
        cmds += [wall(wmsg)]
        cmds += ["sleep %d" % min(DELAY_INTERVAL, delay_rem)]
        delay_rem -= DELAY_INTERVAL
    delay_cmd = ";".join(cmds)

    # Create command which will only execute if the same kernel is running
    kern = common.kernel()
    fail_wall = wall("Need matching kernel '%s' to run!" % kern)
    run_cmd = "echo '%s' | grep -q `uname -r` && %s && %s && %s >> %s 2>>%s || %s" %\
      (kern, wall(msg), wall("Starting..."), job, out_fname, out_fname, fail_wall)

    return "@reboot cd %s; %s; %s;" % (os.getcwd(), delay_cmd, run_cmd)
Пример #2
0
def remove_boot_job():
    """Remove installed reboot job from crontab"""
    data = get_cron_data()
    regex = re.compile(r".*%s.*" % re.escape(common.get_cmd()), re.M)

    if regex.search(data):
        new_cron = regex.sub("", data)
        write_cron_data(new_cron)

        set_panic_restart(False)
Пример #3
0
def remove_boot_job():
    '''Remove installed reboot job from crontab'''
    data = get_cron_data()
    regex = re.compile(r".*%s.*" % re.escape(common.get_cmd()), re.M)

    if regex.search(data):
        new_cron = regex.sub("", data)
        write_cron_data(new_cron)

        set_panic_restart(False)
Пример #4
0
def kill_boot_job():
    remove_boot_job()

    cmd = common.get_cmd()

    procs = check_output("ps -eo pid,args".split(" "))
    pairs = re.findall("(\d+) (.*)", procs)

    for pid, args in pairs:
        if re.search(r"/bin/sh -c.*%s" % cmd, args):
            sys.stderr.write("Killing job %s\n" % pid)
            check_output(("kill -9 %s" % pid).split(" "))
Пример #5
0
def kill_boot_job():
    remove_boot_job()

    cmd = common.get_cmd()

    procs = check_output("ps -eo pid,args".split(" "))
    pairs = re.findall("(\d+) (.*)", procs)

    for pid, args in pairs:
        if re.search(r"/bin/sh -c.*%s" % cmd, args):
            sys.stderr.write("Killing job %s\n" % pid)
            check_output(("kill -9 %s" % pid).split(" "))
Пример #6
0
def get_boot_cron(ignored_params, extra=""):
    """Turn current python script into a crontab reboot entry"""
    job_args = sanitize(sys.argv, ignored_params)
    job = " ".join(job_args)
    out_fname = get_outfname()

    short_job = " ".join([common.get_cmd()] + job_args[1:])
    msg = "Job '%s' will write output to '%s'" % (short_job, out_fname)

    sys.stderr.write("%s %d seconds after reboot.\n" % (msg, DELAY))

    # Create sleep and wall commands which will countdown DELAY seconds
    # before executing the job
    cmds = ["sleep %d" % DELAY_INTERVAL]
    delay_rem = DELAY - DELAY_INTERVAL
    while delay_rem > 0:
        wmsg = "Restarting experiments in %d seconds. %s" % (delay_rem, extra)
        cmds += [wall(wmsg)]
        cmds += ["sleep %d" % min(DELAY_INTERVAL, delay_rem)]
        delay_rem -= DELAY_INTERVAL
    delay_cmd = ";".join(cmds)

    # Create command which will only execute if the same kernel is running
    kern = common.kernel()
    fail_wall = wall("Need matching kernel '%s' to run!" % kern)
    run_cmd = "echo '%s' | grep -q `uname -r` && %s && %s && %s >> %s 2>>%s || %s" % (
        kern,
        wall(msg),
        wall("Starting..."),
        job,
        out_fname,
        out_fname,
        fail_wall,
    )

    return "@reboot cd %s; %s; %s;" % (os.getcwd(), delay_cmd, run_cmd)
Пример #7
0
def main():
    opts, args = parse_args()

    if opts.kill:
        cron.kill_boot_job()
        sys.exit(1)

    email = setup_email(opts.email) if opts.email else None

    # Create base output directory for run data
    out_base = os.path.abspath(opts.out_dir)
    created = False
    if not os.path.exists(out_base):
        created = True
        os.mkdir(out_base)

    exps = get_exps(opts, args, out_base)

    if opts.crontab:
        # Resume script on startup
        opts.retry = True
        cron.install_boot_job(['f', '--forced'],
                              "Stop with %s -k" % com.get_cmd())

    if opts.force or not opts.retry:
        cron.clean_output()
        for e in exps:
            set_tries(e, 0)

    try:
        run_exps(exps, opts)
    finally:
        # Remove persistent state
        for e in exps:
            set_tries(e, 0)
        cron.remove_boot_job()

    def state_count(state):
        return len(filter(lambda x: x.state is state, exps))

    ran = len(filter(lambda x: x.state is not ExpState.None, exps))
    succ = state_count(ExpState.Succeeded)

    message = "Experiments ran:\t%d of %d" % (ran, len(exps)) +\
      "\n  Successful:\t\t%d" % succ +\
      "\n  Failed:\t\t%d" % state_count(ExpState.Failed) +\
      "\n  Already Done:\t\t%d" % state_count(ExpState.Done) +\
      "\n  Invalid Environment:\t%d" % state_count(ExpState.Invalid)

    print(message)

    if email:
        email.send(message)
        email.close()

    if succ:
        sys.stderr.write("Successful experiment data saved in %s.\n" %
                         opts.out_dir)
    elif not os.listdir(out_base) and created:
        # Remove directory if no data was put into it
        os.rmdir(out_base)
Пример #8
0
def get_outfname():
    return "cron-%s.txt" % common.get_cmd()
Пример #9
0
dev = usb.core.find(idVendor=0x24aa, idProduct=0x1000)
if not dev:
    print("No spectrometers found")
    sys.exit()

GET_CMD = 0x35
TEST_COUNT = 5


def twos_comp(val, bits):
    if (val & (1 <<
               (bits - 1))) != 0:  # if sign bit is set e.g., 8bit: 128-255
        val = val - (1 << bits)  # compute negative value
    return val


for i in range(TEST_COUNT):
    print("reading ambient temperature %d of %d" % (i + 1, TEST_COUNT))

    # read raw ambient temperature
    raw = common.get_cmd(dev, GET_CMD, msb_len=2)

    # convert to degrees Celsius
    msb = (raw >> 8) & 0xff
    lsb = raw & 0xff
    degC = 0.125 * twos_comp(raw, 11)

    print("ambient temperature %d of %d was 0x%04x raw (%0.2f degC)" %
          (i + 1, TEST_COUNT, raw, degC))
    sleep(1)
Пример #10
0
def get_outfname():
    return "cron-%s.txt" % common.get_cmd()
Пример #11
0
def main():
    opts, args = parse_args()

    if opts.kill:
        cron.kill_boot_job()
        sys.exit(1)

    email = setup_email(opts.email) if opts.email else None

    # Create base output directory for run data
    out_base = os.path.abspath(opts.out_dir)
    created  = False
    if not os.path.exists(out_base):
        created = True
        os.mkdir(out_base)

    exps = get_exps(opts, args, out_base)

    if opts.crontab:
        # Resume script on startup
        opts.retry = True
        cron.install_boot_job(['f', '--forced'],
                              "Stop with %s -k" % com.get_cmd())

    if opts.force or not opts.retry:
        cron.clean_output()
        for e in exps:
            set_tries(e, 0)

    try:
        run_exps(exps, opts)
    finally:
        # Remove persistent state
        for e in exps:
            set_tries(e, 0)
        cron.remove_boot_job()

    def state_count(state):
        return len(filter(lambda x: x.state is state, exps))

    ran  = len(filter(lambda x: x.state is not ExpState.None, exps))
    succ = state_count(ExpState.Succeeded)

    message = "Experiments ran:\t%d of %d" % (ran, len(exps)) +\
      "\n  Successful:\t\t%d" % succ +\
      "\n  Failed:\t\t%d" % state_count(ExpState.Failed) +\
      "\n  Already Done:\t\t%d" % state_count(ExpState.Done) +\
      "\n  Invalid Environment:\t%d" % state_count(ExpState.Invalid)

    print(message)

    if email:
        email.send(message)
        email.close()

    if succ:
        sys.stderr.write("Successful experiment data saved in %s.\n" %
                         opts.out_dir)
    elif not os.listdir(out_base) and created:
        # Remove directory if no data was put into it
        os.rmdir(out_base)