Ejemplo n.º 1
0
    def incrementCounters(self, countertype):
        """Increment the counters for the given *countertype*.

        This should update the counter files accordingly.

        Returns a list of (counterattribute, value) tuples to set on the
        dataset.
        """
        exp = session.experiment
        if not path.isfile(path.join(exp.dataroot, exp.counterfile)):
            session.log.warning('creating new empty file counter file at %s',
                                path.join(exp.dataroot, exp.counterfile))
        # Keep track of which files we have already updated, since the proposal
        # and the sample specific counter might be the same file.
        seen = set()
        result = []
        for directory, attr in [(exp.dataroot, 'counter'),
                                (exp.proposalpath, 'propcounter'),
                                (exp.samplepath, 'samplecounter')]:
            counterpath = path.normpath(path.join(directory, exp.counterfile))
            nextnum = readFileCounter(counterpath, countertype) + 1
            if counterpath not in seen:
                updateFileCounter(counterpath, countertype, nextnum)
                seen.add(counterpath)
            else:
                nextnum -= 1
            result.append((attr, nextnum))
        return result
Ejemplo n.º 2
0
    def _assignCounter(self):
        # Adapted from DataManager.assignCounter function
        if self.dataset.counter != 0:
            return

        exp = session.experiment
        if not path.isfile(path.join(exp.dataroot, exp.counterfile)):
            session.log.warning('creating new empty file counter file at %s',
                                path.join(exp.dataroot, exp.counterfile))

        if session.mode == SIMULATION:
            raise ProgrammingError('assignCounter should not be called in '
                                   'simulation mode')

        # Read the counter from SICS file
        counter = exp.sicscounter + 1

        # Keep track of which files we have already updated, since the proposal
        # and the sample specific counter might be the same file.
        seen = set()
        for directory, attr in [(exp.dataroot, 'counter'),
                                (exp.proposalpath, 'propcounter'),
                                (exp.samplepath, 'samplecounter')]:
            counterpath = path.normpath(path.join(directory, exp.counterfile))
            readFileCounter(counterpath, self.dataset.countertype)
            if counterpath not in seen:
                updateFileCounter(counterpath, self.dataset.countertype,
                                  counter)
                seen.add(counterpath)

            setattr(self.dataset, attr, counter)

        # Update the counter in SICS file
        exp.updateSicsCounterFile(counter)

        session.experiment._setROParam('lastpoint', self.dataset.counter)
Ejemplo n.º 3
0
def test_experiment(session, cleanup):
    exp = session.experiment

    # setup test scenario
    exp._setROParam('dataroot', path.join(runtime_root, 'data'))
    exp.proposal = 'service'
    exp._setROParam('proptype', 'service')
    # if there is no exp.new, we need to adjust proposalpath ourselfs!
    exp.proposalpath = exp.proposalpath_of(exp.proposal)

    # create the needed script file
    spath = path.join(runtime_root, 'data', year, 'service', 'scripts')

    assert exp.scriptpath == spath
    ensureDirectory(spath)
    with open(path.join(spath, 'servicestart.py'), 'w') as fp:
        fp.write('Remark("service time")\n')

    # first, go in service mode
    exp.servicescript = 'servicestart.py'
    try:
        exp.new('service', localcontact=exp.localcontact)
    finally:
        exp.servicescript = ''
    assert exp.proposal == 'service'
    assert exp.proptype == 'service'
    assert exp.remark == 'service time'
    assert exp.scriptpath == spath

    # check correct operation of sampledir
    exp.sampledir = 'sample'
    assert exp.datapath == path.join(exp.dataroot, year, 'service', 'sample',
                                     'data')
    exp.sampledir = ''

    # for this proposal, remove access rights after switching back
    exp._setROParam('managerights', dict(disableFileMode=0, disableDirMode=0))

    # then, go in proposal mode
    exp.new(999, 'etitle', 'me <*****@*****.**>', 'you')
    # check that all properties have been set accordingly
    assert exp.proposal == 'p999'
    assert exp.proptype == 'user'
    assert exp.title == 'etitle'
    assert exp.localcontact == 'me <*****@*****.**>'
    assert exp.users == 'you'
    assert exp.remark == ''  #pylint: disable=compare-to-empty-string

    # check that directories have been created
    assert path.isdir(datapath('p999'))
    assert path.isdir(datapath('p999', 'scripts'))
    assert path.isdir(datapath('p999', 'data'))

    # check that templating works
    assert path.isfile(datapath('p999', 'scripts', 'start_p999.py'))
    run('start_p999.py')
    assert exp.remark == 'proposal p999 started by you; sample is unknown'

    # try a small scan; check for data file written
    scan(session.getDevice('axis'), 0, 1, 5, 0.01, u'Meßzeit')
    assert path.isfile(datapath('..', 'counters'))
    nr = readFileCounter(datapath('..', 'counters'), 'scan')
    fn = datapath('p999', 'data', 'p999_%08d.dat' % nr)
    assert path.isfile(fn)
    with open(fn, 'rb') as fp:
        assert u'Meßzeit'.encode('utf-8') in fp.read()

    # now, finish the experiment
    thd = exp.finish()
    if thd:
        thd.join()
    # have the access rights been revoked?
    if os.name != 'nt':
        assert not os.access(datapath('p999'), os.X_OK)

    # did we switch back to service proposal?
    assert exp.proposal == 'service'

    # switch back to proposal (should re-enable directory)
    exp.new('p999', localcontact=exp.localcontact)
    assert os.access(datapath('p999'), os.X_OK)
    assert exp.users == ''  #pylint: disable=compare-to-empty-string
    # has the zip file been created?
    assert path.isfile(datapath('p999', 'p999.zip'))

    exp.addUser('A User')

    assert exp.users == 'A User'
    exp.addUser('Another User', '*****@*****.**')
    assert exp.users == 'A User, Another User <*****@*****.**>'
    exp.addUser('Athird User', '*****@*****.**',
                'An Institute, Anywhere street, 12345 Anywhere')
    assert exp.users == 'A User, Another User <*****@*****.**>, '\
                        'Athird User <*****@*****.**> ' \
                        '(An Institute, Anywhere street, 12345 Anywhere)'

    exp.users = 'Jülich'
    exp.users = u'Jülich'

    exp.scripts = ['Test ümlauts']
    exp.scripts = [u'Test ümlauts']
    assert exp.scripts == ['Test ümlauts']

    # and back to service
    exp.new('service', localcontact=exp.localcontact)