def loadCC3DFile(fileName, forceSingleRun=False): from FileLock import FileLock fLock = FileLock(file_name=fileName, timeout=10, delay=0.05) fLock.acquire() import CompuCellSetup import CC3DSimulationDataHandler as CC3DSimulationDataHandler cc3dSimulationDataHandler = CC3DSimulationDataHandler.CC3DSimulationDataHandler( None) cc3dSimulationDataHandler.readCC3DFileFormat(fileName) print cc3dSimulationDataHandler.cc3dSimulationData if forceSingleRun: # forces loadCC3D to behave as if it was running plane simulation without addons such as e.g. parameter scan prepareSingleRun(cc3dSimulationDataHandler) else: if cc3dSimulationDataHandler.cc3dSimulationData.parameterScanResource: preparationSuccessful = prepareParameterScan( cc3dSimulationDataHandler) if not preparationSuccessful: # raise AssertionError('Parameter Scan Complete') raise AssertionError( 'Parameter Scan ERRORCODE=' + str(ParameterScanEnums.SCAN_FINISHED_OR_DIRECTORY_ISSUE) + ': Parameter Scan Complete') else: prepareSingleRun(cc3dSimulationDataHandler) fLock.release() return cc3dSimulationDataHandler
def gatherdata(): logger.debug('gatherdata started') lock = FileLock('lock') result = [] if lock.acquire(): if gatherdatafromContestSite(hackerearth, HackerEarth_DB): result.append(True) else: result.append(False) if gatherdatafromContestSite(codechef, CodeChef_DB): result.append(True) else: result.append(False) lastf = LastFetch.objects.all() if (len(lastf) == 0): LastFetch(update_date=timezone.now()).save() else: lastf[0].update_date = timezone.now() lastf[0].save() lock.release() else: lock.poll() return result
def _file( self, filename ): #signal.signal( signal.SIGINT, self._kill) #signal.signal( signal.SIGTERM, self._kill) fLock = FileLock( filename ) with fLock: if os.path.isfile( filename ): os.unlink( filename ) while self._running: lines = list() with fLock: if os.path.isfile( filename ): with open( filename, 'r' ) as file: lines = file.readlines() os.unlink( filename ) for line in lines: cmd = None try: cmd = json.loads( line ) if 'type' in cmd: self._append( cmd ) else: self._append( { 'type' : 'error', 'verbose' : 'Neplatny syntax prikazu - prikaz neobsahuje "type".' } ) except: self._append( { 'type' : 'error', 'verbose' : 'Neplatny syntax prikazu - prikaz neni ve formatu JSON.' } ) sleep( 0.5 )
def getKeypair(self, session_id, selector): """<method internal="yes"> </method>""" if not self.initialized: log(session_id, CORE_ERROR, 3, "Keybridge not completely initialized, error generating keypair;") return (None, None) try: trusted = 1 orig_blob = selector['bridge-trusted-key'] hash_key = orig_blob + self.trusted_ca_pem + self.key_pem except KeyError: trusted = 0 orig_blob = selector['bridge-untrusted-key'] hash_key = orig_blob + self.untrusted_ca_pem + self.key_pem hash = hashlib.sha256(hash_key).hexdigest() if trusted: cert_file = '%s/trusted-%s.crt' % (self.cache_directory, hash) ca_pair = self.trusted_ca else: cert_file = '%s/untrusted-%s.crt' % (self.cache_directory, hash) ca_pair = self.untrusted_ca with FileLock("%s/.lock" % self.cache_directory): try: return self.getCachedKey(session_id, cert_file, orig_blob) except KeyError: log(session_id, CORE_DEBUG, 5, "Certificate not found in the cache, regenerating;") serial_file = self._get_serial_filename() serial_pos = "" try: serial_pos = "file open" serial_file_fd = open(serial_file, 'r') serial_pos = "file read" serial_file_data = serial_file_fd.read().strip() serial_pos = "turn to integer" serial = int(serial_file_data) serial_pos = None except (ValueError, IOError): serial = self.getLastSerial() log(session_id, CORE_ERROR, 3, "On-line CA serial file not found, reinitializing; file='%s', serial='%d', pos='%s'", (serial_file, serial, serial_pos)) serial = serial + 1 try: with open(serial_file, 'w') as f: f.write(str(serial)) except IOError, e: log(session_id, CORE_ERROR, 2, "Cannot write serial number of on-line CA; file='%s', error='%s'", (serial_file, e.strerror)) new_blob = self._save_new_cert(session_id, orig_blob, ca_pair, cert_file, serial) return (new_blob, self._dump_privatekey())
def make_cocktail(slot_volume_dict): # run a thread for each slot for el in slot_volume_dict: gpio_control = GpioControl(slot=el['slot_id'], volume=el['volume']) gpio_control.start() # get the timeout delay from this volume and the concerned slot timeout_delay = get_time_delay_for_slot_and_volume(slot_volume_dict) print "timeout max: " + str(timeout_delay) # create file locker filelock = FileLock("raspidrink", timeout=int(timeout_delay)) filelock.create_lock_file()
def post(self): # check lock file filelock = FileLock("raspidrink") if filelock.is_valide(): return {'status': 'working'} else: # No lock file, we can reverse all pump json_data = request.get_json(force=True) action = json_data['action'] if action not in ("on", "off"): return {'error': 'Bad action'} else: reverse_pump.delay(action) return {'status': 'ok'}
def post(self): # check lock file filelock = FileLock("raspidrink") if filelock.is_valide(): return {'status': 'working'} else: # No lock file, we can make the cocktail json_data = request.get_json(force=True) slot_volume_dict = json_data['data'] # decode json. This give a string slot_volume_dict = json.dumps(slot_volume_dict) # use ast to convert str from unicode to a dict slot_volume_dict = ast.literal_eval(slot_volume_dict) make_cocktail.delay(slot_volume_dict) timeout_delay = get_time_delay_for_slot_and_volume( slot_volume_dict) return {'status': 'ok', 'delay': timeout_delay}
def post(self): # check lock file filelock = FileLock("raspidrink") if filelock.is_valide(): return {'status': 'working'} else: json_data = request.get_json(force=True) action = json_data['action'] try: slot_id = json_data['slot_id'] action = json_data['action'] except KeyError, e: slot_id = None if action not in ("start", "stop"): return {'error': 'Bad action'} else: pump_management.delay(action, slot_id) return {'status': 'ok'}
# we reset max number of consecutive runs to 1 because we want each simulation in parameter scan # initiated by the psrun.py script to be an independent run after which runScript terminatesrestarts again for the next run if cmlParser.exitWhenDone: maxNumberOfConsecutiveRuns = 1 allowRelaunch = False except: # if for whatever reason we cannot do it we stay with the default value pass while ( True ): # by default we are looping the simulation to make sure parameter scans are handled properly cc3dSimulationDataHandler = None from FileLock import FileLock with FileLock(file_name=fileName, timeout=10, delay=0.05) as flock: CompuCellSetup.resetGlobals() CompuCellSetup.simulationPaths = CompuCellSetup.SimulationPaths() sim, simthread = CompuCellSetup.getCoreSimulationObjects(True) setSimulationResultStorageDirectory( cmlParser.customScreenshotDirectoryName ) # set Simulation output dir - it can be reset later - at this point only directory name is set. directory gets created later CompuCellSetup.simulationFileName = fileName # print 'GOT HERE' if re.match(".*\.xml$", fileName): # If filename ends with .xml print "GOT FILE ", fileName
else: sys.stderr.write(str(e) + '\n') exit(invalid_arguments) # kontrola ze nezadal --peer a --node if 'node' in settings and 'peer' in settings: sys.stderr.write( 'Nelze spustit program se zadanymi parametry --node a --peer, zadejte pouze jeden z nich.\n' ) exit(invalid_arguments) # zpracovani prikazu pro peer if 'peer' in settings: filename = '.' + settings['id'][0] + '.peercommands' # zamek na soubor lock = FileLock(filename) if settings['cmd'][0] == 'message': if 'to' not in settings: sys.stderr.write( 'Prikaz message vyzaduje parametry --from, --to a --message\n') exit(invalid_arguments) with lock: with open(filename, 'a') as file: file.write('{ "type" : "message", "from": ' + dumps(settings['from'][0]) + ', "to": ' + dumps(settings['to'][0]) + ', "message": ' + dumps(settings['message'][0]) + ' }\n') elif settings['cmd'][0] == 'reconnect': if 'ipv4' not in settings: sys.stderr.write(