Example #1
0
    def start(self):
        """ When the bus starts, the plugin is also started
        and we load the Django application. We then mount it on
        the CherryPy engine for serving as a WSGI application.
        We let CherryPy serve the application's static files.
        """
        cherrypy.log("Loading and serving the Django application")
        cherrypy.tree.graft(WSGIHandler())

        # Serve the content files
        static_handler = cherrypy.tools.staticdir.handler(
            section="/",
            dir=os.path.split(settings.CONTENT_ROOT)[1],
            root=os.path.abspath(os.path.split(settings.CONTENT_ROOT)[0])
        )
        cherrypy.tree.mount(static_handler, settings.CONTENT_URL)

        # Serve the static media files
        static_handler = cherrypy.tools.staticdir.handler(
            section="/",
            dir=os.path.split(settings.MEDIA_ROOT)[1],
            root=os.path.abspath(os.path.split(settings.MEDIA_ROOT)[0])
        )
        cherrypy.tree.mount(static_handler, settings.MEDIA_URL)

        # Serve the static admin media. From django's internal (django.core.servers.basehttp)
        admin_static_dir = os.path.join(django.__path__[0], 'contrib', 'admin', 'static')
        admin_static_handler = cherrypy.tools.staticdir.handler(
            section='/',
            dir='admin',
            root=admin_static_dir
        )
        cherrypy.tree.mount(admin_static_handler, urlparse.urljoin(settings.STATIC_URL, 'admin'))
Example #2
0
    def __init__(self, app, config, mount):
        """
        :arg app: reference to application object; passed to all entities.
        :arg config: reference to configuration; passed to all entities.
        :arg str mount: API URL mount point; passed to all entities."""

        RESTApi.__init__(self, app, config, mount)

        cherrypy.log("ReqMgr entire configuration:\n%s" % Configuration.getInstance())
        cherrypy.log("ReqMgr REST hub configuration subset:\n%s" % config)

        # Makes raw format as default
        # self.formats.insert(0, ('application/raw', RawFormat()))
        self._add({"about": Info(app, IndividualCouchManager(config), config, mount),
                   "info": Info(app, IndividualCouchManager(config), config, mount),
                   "app_config": ReqMgrConfigData(app, IndividualCouchManager(config), config, mount),
                   "request": Request(app, IndividualCouchManager(config), config, mount),
                   "cmsswversions": CMSSWVersions(app, IndividualCouchManager(config), config, mount),
                   "wmagentconfig": WMAgentConfig(app, IndividualCouchManager(config), config, mount),
                   "permissions": PermissionsConfig(app, IndividualCouchManager(config), config, mount),
                   "campaignconfig": WMAgentConfig(app, IndividualCouchManager(config), config, mount),
                   "unifiedconfig": UnifiedConfig(app, IndividualCouchManager(config), config, mount),
                   "status": RequestStatus(app, IndividualCouchManager(config), config, mount),
                   "type": RequestType(app, IndividualCouchManager(config), config, mount),
                   "spec_template": RequestSpec(app, IndividualCouchManager(config), config, mount),
                   "workload_config": WorkloadConfig(app, IndividualCouchManager(config), config, mount),
                   "splitting": WorkloadSplitting(app, IndividualCouchManager(config), config, mount),
                   "wmstats_info": WMStatsInfo(app, IndividualCouchManager(config), config, mount),
                   "proc_status": ProcessMatrix(app, self, config, mount)
                  })
Example #3
0
 def _start(self):
     # Output config options to log
     if cherrypy.config.get("server.logConfigOptions", True):
         cherrypy.config.outputConfigMap()
     
     try:
         configure()
         
         for func in cherrypy.server.onStartServerList:
             func()
         self.start_http_server()
         self.state = STARTED
         
         if self.blocking:
             # Block forever (wait for KeyboardInterrupt or SystemExit).
             while True:
                 time.sleep(.1)
                 if self.interrupt:
                     raise self.interrupt
     except KeyboardInterrupt:
         cherrypy.log("<Ctrl-C> hit: shutting down server", "HTTP")
         self.stop()
     except SystemExit:
         cherrypy.log("SystemExit raised: shutting down server", "HTTP")
         self.stop()
Example #4
0
File: main.py Project: SecPi/SecPi
	def __init__(self):
		cherrypy.log("Initializing Webserver")
		
		cherrypy.config.update({'request.error_response': self.handle_error})
		cherrypy.config.update({'error_page.404': self.error_404})
		cherrypy.config.update({'error_page.401': self.error_401})
		
		self.sensors = SensorsPage()
		self.zones = ZonesPage()
		self.setups = SetupsPage()
		self.alarms = AlarmsPage()
		self.workers = WorkersPage()
		self.actions = ActionsPage()
		self.notifiers = NotifiersPage()
		self.sensorparams = SensorParamsPage()
		self.actionparams = ActionParamsPage()
		self.notifierparams = NotifierParamsPage()
		self.logs = LogEntriesPage()
		self.setupszones = SetupsZonesPage()
		self.workersactions = WorkersActionsPage()
		
		self.alarmdata = AlarmDataPage()
		
		self.connect()
		cherrypy.log("Finished initialization")
Example #5
0
def addautoreloaddir(root, verbose=False):
    for path, dirs, files in os.walk(root):
        for filename in glob.glob(os.path.join(path,"*")):
            if not ".svn" in filename:
                if verbose:
                    cherrypy.log("Adding %s to autoreload files" % filename, "ENGINE")
                cherrypy.engine.autoreload.files.add(filename)
Example #6
0
 def trap(self, func, *args, **kwargs):
     try:
         return func(*args, **kwargs)
     except self.throws:
         raise
     except StopIteration:
         raise
     except:
         tb = _cperror.format_exc()
         #print('trapped (started %s):' % self.started_response, tb)
         _cherrypy.log(tb, severity=40)
         if not _cherrypy.request.show_tracebacks:
             tb = ""
         s, h, b = _cperror.bare_error(tb)
         if self.started_response:
             # Empty our iterable (so future calls raise StopIteration)
             self.iter_response = iter([])
         else:
             self.iter_response = iter(b)
         
         try:
             self.start_response(s, h, _sys.exc_info())
         except:
             # "The application must not trap any exceptions raised by
             # start_response, if it called start_response with exc_info.
             # Instead, it should allow such exceptions to propagate
             # back to the server or gateway."
             # But we still log and call close() to clean up ourselves.
             _cherrypy.log(traceback=True, severity=40)
             raise
         
         if self.started_response:
             return ntob("").join(b)
         else:
             return b
Example #7
0
  def _update(self, passwords, contacts, deletions):
    """Perform an actual database update for `passwords`, `contacts` and
    `deletions`. Only statements needing to be executed are executed.
    Commits automatically at the end if necessary."""

    if passwords:
      self.api.executemany("""merge into user_passwd u
                              using dual on (u.username = :username)
                              when not matched then
                                insert (username, passwd)
                                values (:username, :passwd)
                              when matched then update
                                set u.passwd = :passwd
                           """, passwords)

    for contact in contacts:
      try:
        self.api.execute("""merge into contact c
                            using dual on (c.username = :username)
                            when not matched then
                              insert (id, username, forename, surname, email, dn)
                              values (contact_sq.nextval, :username, :forename,
                                      :surname, :email, :dn)
                            when matched then update
                              set c.forename = :forename,
                                  c.surname = :surname,
                                  c.email = :email,
                                  c.dn = :dn
                         """, contact)
      except Exception, e:
        # Ignore constraint errors for individual users instead of failing
        cherrypy.log("WARNING: failed to update user %s, DN %s: %s"
                     %(contact['username'], contact['dn'], str(e)))
        continue
Example #8
0
def getSeqs(sequences):
  
    '''
    In this function, we "clean up" a FASTA file. The input is a FASTA file containing the 16S rDNA sequences of all the OTUs in the microbiome study the user is exploring. This function makes sure that each genomic sequence only spans one line in the file.

    :param sequences: FASTA file with 16S dRNA sequences spanning multiple lines in addition to the sequence identifier line.
    :returns seqs: list of 16s dRNA sequences in the FASTA format spanning only one line in addition to the sequence identifier line.
    '''
    

    cherrypy.log("We are now going to clean up the FASTA file given (%s) so that each DNA sequence does not span multiple lines."%sequences)

    userSeqs = open(sequences,'r')

    seq = ''

    for line in userSeqs:
        if line.startswith('>'):
            seq += '+++++\n'
            seq += line
        else:
            seq += line.rstrip()
        
    
    seqs = seq.split('+++++\n')

    userSeqs.close()


    cherrypy.log("Clean up is finished.  We are ready to fetch only the representative OTU sequences that we are interested in using in the rest of our analysis." )

    return seqs
Example #9
0
def getUniqueOTU(corrs):
    '''

    In this function we use a file with three columns: columns 1 and 2 are lists of identifiers of OTUs from the microbiome study the user is interested in exploring. The third column has some value of associaltion between each pair of OTUs. The usual measure is correlation or co-occurrence, but it can be any measure of association really. This function goes through all the identifiers and creates a list of unique identifiers in the table, that is, all the different OTUs that are going to be part of the analsysi.

    :param corrs: file with the values of association between pairs of OTUs
    :returns uniqueOTUs: list with unique identifiers of OTUs
    '''

    cherrypy.log('We started the function to get the list of all unique OTUs from the file that lists how pairs of OTUs are correlated. The file we are using is %s .' %corrs)


    correlations = open(corrs,'r')
    correlations.readline()

    OTUs = []
    for line in correlations:
        line = line.rstrip().split()
        OTUs.append(line[0])
        OTUs.append(line[1])
    
    uniqueOTUs = list(set(OTUs))

    cherrypy.log('We created a list with %d unique OTUS.' %len(uniqueOTUs))


    return uniqueOTUs
Example #10
0
    def _handleAssignmentStateTransition(self, workload, request_args, dn):
        
        req_status = request_args["RequestStatus"]
        if req_status == "assigned" and not request_args.get('Team', '').strip():
            raise InvalidSpecParameterValue("Team must be set during workflow assignment: %s" % request_args)
            
        if ('SoftTimeout' in request_args) and ('GracePeriod' in request_args):
            request_args['SoftTimeout'] = int(request_args['SoftTimeout'])
            #TODO: not sure why GracePeriod when passed from web ingerface but convert here
            request_args['GracePeriod'] = int(request_args['GracePeriod'])
            request_args['HardTimeout'] = request_args['SoftTimeout'] + request_args['GracePeriod']
        
        #Only allow extra value update for assigned status
        cherrypy.log("INFO: Assign request, input args: %s ..." % request_args)
        try:
            workload.updateArguments(request_args)
        except Exception as ex:
            msg = traceback.format_exc()
            cherrypy.log("Error for request args %s: %s" % (request_args, msg))
            raise InvalidSpecParameterValue(str(ex))
        
        # validate/update OutputDatasets after ProcessingString and AcquisionEra is updated
        request_args['OutputDatasets'] = workload.listOutputDatasets()
        validateOutputDatasets(request_args['OutputDatasets'], workload.getDbsUrl())

        # legacy update schema to support ops script
        loadRequestSchema(workload, request_args)

        report = self.reqmgr_db_service.updateRequestProperty(workload.name(), request_args, dn)
        workload.saveCouch(self.config.couch_host, self.config.couch_reqmgr_db)
        return report
Example #11
0
                def file_ranges():
                    # Apache compatibility:
                    yield ntob('\r\n')

                    for start, stop in r:
                        if debug:
                            cherrypy.log(
                                'Multipart; start: %r, stop: %r' % (
                                    start, stop),
                                'TOOLS.STATIC')
                        yield ntob('--' + boundary, 'ascii')
                        yield ntob('\r\nContent-type: %s' % content_type,
                                   'ascii')
                        yield ntob(
                            '\r\nContent-range: bytes %s-%s/%s\r\n\r\n' % (
                                start, stop - 1, content_length),
                            'ascii')
                        fileobj.seek(start)
                        gen = file_generator_limited(fileobj, stop - start)
                        for chunk in gen:
                            yield chunk
                        yield ntob('\r\n')
                    # Final boundary
                    yield ntob('--' + boundary + '--', 'ascii')

                    # Apache compatibility:
                    yield ntob('\r\n')
Example #12
0
def _connect(force=False):
    """
    ensure that _db points to an active database connection
    """
    global _db
    try:
        if force or not _db or not _db.ping():
            print(dict(config['mysql']))
            mysql = config['mysql']
            host   = mysql['host']
            port   = mysql['port']
            user   = mysql['username']
            passwd = mysql['password']
            db     = mysql['database']

            _db = pymysql.connect(
                    host   = host,
                    port   = int(port),
                    user   = user,
                    passwd = passwd,
                    db     = db
            )
        _db.autocommit(True)
    except Exception as  e:
        import traceback
        cherrypy.log(traceback.format_exc())
        if not force:
            _connect(force=True)
Example #13
0
def init(storage_type = 'ram', path = None, path_header = None, name = 'session_id', timeout = 60, domain = None, secure = False, clean_freq = 5, persistent = True, debug = False, **kwargs):
    request = cherrypy.serving.request
    if hasattr(request, '_session_init_flag'):
        return
    request._session_init_flag = True
    id = None
    if name in request.cookie:
        id = request.cookie[name].value
        if debug:
            cherrypy.log('ID obtained from request.cookie: %r' % id, 'TOOLS.SESSIONS')
    storage_class = storage_type.title() + 'Session'
    storage_class = globals()[storage_class]
    if not hasattr(cherrypy, 'session'):
        if hasattr(storage_class, 'setup'):
            storage_class.setup(**kwargs)
    kwargs['timeout'] = timeout
    kwargs['clean_freq'] = clean_freq
    cherrypy.serving.session = sess = storage_class(id, **kwargs)
    sess.debug = debug

    def update_cookie(id):
        cherrypy.serving.response.cookie[name] = id

    sess.id_observers.append(update_cookie)
    if not hasattr(cherrypy, 'session'):
        cherrypy.session = cherrypy._ThreadLocalProxy('session')
    if persistent:
        cookie_timeout = timeout
    else:
        cookie_timeout = None
    set_response_cookie(path=path, path_header=path_header, name=name, timeout=cookie_timeout, domain=domain, secure=secure)
Example #14
0
File: main.py Project: mcXrd/travel
    def getPage(self,pageName=None,language=None):
        self.init()
        ds = DataStore()
        
        r = ds.getPage(pageName,language)
        ds.close()
        

        
        ds = DataStore()
        
        layout = ds.getPage('layout',self.language)
        ds.close()
        
        res = {}
        
        cherrypy.log("layout"*20)
        cherrypy.log(str(layout))
        
        
        for one in r:
            res[one[5]]=one[2]
            
        for one in layout:
            res[one[5]]=one[2]
        
        if self.session.get('user'):
            res['user']='******';
        else:
            res['user']='******';
        
        self.addTodoPerexes(res)
        return res
Example #15
0
 def __call__(self):
     context = {}
     try:
         r = self.next_handler()
         context.update(r)
     except ValueError, e:
         cherrypy.log('%s (handler for "%s" returned "%s")\n' % (e, self.template_name, repr(r)), traceback=True)
Example #16
0
  def _GetConstrainedPort(self, f=None, bandwidth=None, latency=None, loss=None,
                          new_port=False, **kwargs):
    """Creates or gets a port with specified network constraints.

    See ServeConstrained() for more details.
    """
    # Validate inputs. isdigit() guarantees a natural number.
    bandwidth = self._ParseIntParameter(
        bandwidth, 'Invalid bandwidth constraint.', lambda x: x > 0)
    latency = self._ParseIntParameter(
        latency, 'Invalid latency constraint.', lambda x: x >= 0)
    loss = self._ParseIntParameter(
        loss, 'Invalid loss constraint.', lambda x: x <= 100 and x >= 0)

    redirect_port = self._options.port
    if self._options.local_server_port:
      redirect_port = self._options.local_server_port

    start_time = time.time()
    # Allocate a port using the given constraints. If a port with the requested
    # key and kwargs already exist then reuse that port.
    constrained_port = self._port_allocator.Get(
        cherrypy.request.remote.ip, server_port=redirect_port,
        interface=self._options.interface, bandwidth=bandwidth, latency=latency,
        loss=loss, new_port=new_port, file=f, **kwargs)

    cherrypy.log('Time to set up port %d = %.3fsec.' %
                 (constrained_port, time.time() - start_time))

    if not constrained_port:
      raise cherrypy.HTTPError(503, 'Service unavailable. Out of ports.')
    return constrained_port
Example #17
0
def Main():
  """Configure and start the ConstrainedNetworkServer."""
  options = ParseArgs()

  try:
    traffic_control.CheckRequirements()
  except traffic_control.TrafficControlError as e:
    cherrypy.log(e.msg)
    return

  cherrypy.config.update({'server.socket_host': '::',
                          'server.socket_port': options.port})

  if options.threads:
    cherrypy.config.update({'server.thread_pool': options.threads})

  if options.socket_timeout:
    cherrypy.config.update({'server.socket_timeout': options.socket_timeout})

  # Setup port allocator here so we can call cleanup on failures/exit.
  pa = PortAllocator(options.port_range, expiry_time_secs=options.expiry_time)

  try:
    cherrypy.quickstart(ConstrainedNetworkServer(options, pa))
  finally:
    # Disable Ctrl-C handler to prevent interruption of cleanup.
    signal.signal(signal.SIGINT, lambda signal, frame: None)
    pa.Cleanup(all_ports=True)
Example #18
0
 def start(self):
   msgq = self.msgq
   nextpipe = None
   while True:
     m = msgq.get()
     if m == MSG_PLAYER_PLAY:
       if self.play_pipe is not None:
         nextpipe = msgq.get()
         self._stop()
       else:
         self._play(msgq.get())
     elif m == MSG_PLAYER_STOP:
        self._stop()
     elif m == MSG_PLAYER_PIPE_STOPPED:
       self.error = msgq.get()
       self.play_thread.join()
       cherrypy.log("PIPE STOPPED")
       if nextpipe is not None:
         self._play(nextpipe)
         nextpipe = None
       else:
         self.play_pipe = None
         self.play_thread = None
     elif m == MSG_PLAYER_QUIT:
       self._stop()
       break
Example #19
0
 def build(self):
     """
     program build/update
     """
     # store common file path in variables
     tgz_file = self.dl_dir + "io_png.tar.gz"
     prog_file = self.bin_dir + "axpb"
     log_file = self.base_dir + "build.log"
     # get the latest source archive
     build.download("https://tools.ipol.im/wiki/doc/"
                    + "io_png.tar.gz" , tgz_file)
     # test if the dest file is missing, or too old
     if (os.path.isfile(prog_file)
         and ctime(tgz_file) < ctime(prog_file)):
         cherrypy.log("not rebuild needed",
                      context='BUILD', traceback=False)
     else:
         # extract the archive
         build.extract(tgz_file, self.src_dir)
         # build the program
         build.run("make -j4 -C %s %s" % (self.src_dir + "io_png",
                                          os.path.join("example", "axpb")),
                   stdout=log_file)
         # save into bin dir
         if os.path.isdir(self.bin_dir):
             shutil.rmtree(self.bin_dir)
         os.mkdir(self.bin_dir)
         shutil.copy(self.src_dir + os.path.join("io_png", "example",
                                                 "axpb"), prog_file)
         # cleanup the source dir
         shutil.rmtree(self.src_dir)
     return
Example #20
0
def response_headers(headers=None, debug=False):
    """Set headers on the response."""
    if debug:
        cherrypy.log('Setting response headers: %s' % repr(headers),
                     'TOOLS.RESPONSE_HEADERS')
    for name, value in (headers or []):
        cherrypy.serving.response.headers[name] = value
Example #21
0
 def build_demoUtils(self):
     """
     built the demo auxiliary programs (stored with the demo)
     """
     ## store common file path in variables
     tgz_file = self.base_dir + "support/demoUtils.tar.gz"
     prog_file = self.bin_dir + "plambda"
     log_file = self.base_dir + "build_utils.log"
     ## test if the dest file is missing, or too old
     if (os.path.isfile(prog_file)
         and ctime(tgz_file) < ctime(prog_file)):
         cherrypy.log("no rebuild needed",
                   context='BUILD', traceback=False)
     else:
         # extract the archive
         build.extract(tgz_file, self.src_dir)
         # build the program
         build.run("cd %s; cmake ." %
                      (self.src_dir +"demoUtils"),
                         stdout=log_file)
         build.run("make -C %s" %
                      (self.src_dir +"demoUtils"),
                         stdout=log_file)
         # save into bin dir
         #try:
         import glob
         for f in glob.glob(os.path.join(self.src_dir,
                         "demoUtils", "bin", "*")):
             shutil.copy(f, self.bin_dir)
         #except:
         #   print("some error occurred copying files")
         # cleanup the source dir
         shutil.rmtree(self.src_dir)
     return
Example #22
0
def modified_since(path, stat=None):
    """Check whether a file has been modified since the date
    provided in 'If-Modified-Since'
    It doesn't check if the file exists or not
    Return True if has been modified, False otherwise
    """
    # serveFile already creates a stat object so let's not
    # waste our energy to do it again
    if not stat:
        try:
            stat = os.stat(path)
        except OSError:
            if cherrypy.config.get("server.log_file_not_found", False):
                cherrypy.log("    NOT FOUND file: %s" % path, "DEBUG")
            raise cherrypy.NotFound()

    response = cherrypy.response
    strModifTime = httptools.HTTPDate(time.gmtime(stat.st_mtime))
    if cherrypy.request.headers.has_key("If-Modified-Since"):
        if cherrypy.request.headers["If-Modified-Since"] == strModifTime:
            response.status = "304 Not Modified"
            response.body = None
            if getattr(cherrypy, "debug", None):
                cherrypy.log("    Found file (304 Not Modified): %s" % path, "DEBUG")
            return False
    response.headers["Last-Modified"] = strModifTime
    return True
Example #23
0
    def connection_log(self, action):
        """Logs actions on the sqlite3 connection

        :param str action: is the action on the connection
        """
        if cherrypy is not None:  # This happens while everything is being deleted
            cherrypy.log('Connection {0} with timestamp {1}'.format(action, self.timestamp))
Example #24
0
    def update(self,handle=None,email=None,password1=None,
                    password2=None,action=None,user_id=None):
        """ users can only edit themselves """

        # if they don't supply the user id, we are editing current user
        if user_id:
            user = m.User.get(user_id)
        else:
            user = cherrypy.request.user

        try:
            if action:
                # update what we were passed
                if handle and user.handle != handle:
                    if m.User.get_by(handle=handle):
                        raise e.ValidationException('Handle taken')
                    user.handle = handle
                if email:
                    cherrypy.log('email:%s' % email)
                    user.email_address = email
                if password1:
                    if password1 != password2:
                        raise e.ValidationException('Passwords do not match')
                    user.password = password1

                # save our changes
                m.session.commit()

                # take a look
                redirect('/user/%s' % user.id)
        except e.ValidationException, ex:
            add_flash('error','%s' % ex)
    def create_or_update_ref_datastream(self, uri, ref):
        '''Create or update a datastream with an externally referenced content.

        @param uri (string) URI of the datastream node to be created or updated.
        @param ref (string) External source as a HTTP URL.

        @eturn (string) New datasteram URI if a new one is crated.
        '''

        cherrypy.log('Creating an externally referenced node: ' + uri)
        # Check that external reference exists
        check = self.request('head',ref, headers=self.headers)
        check.raise_for_status()

        res = self.request('put',
            uri,
            headers = dict(chain(
                self.headers.items(),
                [('content-type', 'message/external-body; access-type=URL; URL="{}"'.format(ref))]
            ))
        )
        res.raise_for_status()

        #cherrypy.log('Create/update datastream response:' + str(res.status_code))

        cherrypy.log('Response headers for reference DS:' + str(res.headers))
        if 'location' in res.headers:
            return res.headers['location']
Example #26
0
 def build_demo(self):
     """
     download and built the demo auxiliary programs
     """
     ## store common file path in variables
     tgz_file = self.dl_dir + "imscript.tar.gz"
     prog_file = self.bin_dir + "plambda"
     log_file = self.base_dir + "build_imscript.log"
     ## get the latest source archive
     build.download(app.xlink_src_demo, tgz_file)
     ## test if the dest file is missing, or too old
     if (os.path.isfile(prog_file)
         and ctime(tgz_file) < ctime(prog_file)):
         cherrypy.log("no rebuild needed",
                   context='BUILD', traceback=False)
     else:
         # extract the archive
         build.extract(tgz_file, self.src_dir)
         # build the program
         build.run("make -j CFLAGS=-O3 -C %s" %
                      (self.src_dir +"imscript"),
                         stdout=log_file)
         # save into bin dir
         #try:
         for f in glob.glob(os.path.join(self.src_dir,
                         "imscript", "bin", "*")):
             shutil.copy(f, self.bin_dir)
         #except:
         #   print("some error occurred copying files")
         # cleanup the source dir
         shutil.rmtree(self.src_dir)
     return
Example #27
0
 def build_algo(self):
     """
     program build/update
     """
     ## store common file path in variables
     tgz_file = self.dl_dir + "tvl1flow_3.tar.gz"
     prog_file = self.bin_dir + "tvl1flow"
     log_file = self.base_dir + "build.log"
     ## get the latest source archive
     build.download(app.xlink_src, tgz_file)
     ## test if the dest file is missing, or too old
     if (os.path.isfile(prog_file)
         and ctime(tgz_file) < ctime(prog_file)):
         cherrypy.log("no rebuild needed",
                   context='BUILD', traceback=False)
     else:
         # extract the archive
         build.extract(tgz_file, self.src_dir)
         # build the program
         build.run("make -C %s" % (self.src_dir +"tvl1flow_3"),
                               stdout=log_file)
         # save into bin dir
         #if os.path.isdir(self.bin_dir):
         #        shutil.rmtree(self.bin_dir)
         #try:
         shutil.copy(self.src_dir +
                 os.path.join("tvl1flow_3",
                     "tvl1flow"), prog_file)
         #except:
         #   print("some error occurred copying files")
         # cleanup the source dir
         shutil.rmtree(self.src_dir)
     return
Example #28
0
    def init_plugins(self, pluginlist):
        """
        Initialize plugins. Assumes that each plugin contains a class of the
        same name as the file. If it does, this function attaches an instance 
        of that class to self, and adds that instance to the plugins array.
        """
        plugindir = config('plugin_dir', None)
        if not plugindir or not os.path.isdir(plugindir):
            cpy.log("Invalid Plugin Directory, no plugins loaded")
            return
        else:
            sys.path.append(plugindir)

        #XXX: Should we just scan for *.py files in the plugin dir? Should the
        # mechanism to remove a plugin be renaming it or taking it out of 
        # conf?
        for p in pluginlist:
            try:
                mod = __import__(p)
                if not hasattr(self, p):
                    instance = getattr(mod, p)(self)
                    setattr(self, p, instance)
                    self.plugins.append(instance)
                    cpy.log("successfully imported plugin module %s" % p)
                else:
                    raise ImportError
            #bare except, because the modules could raise any number of errors
            #on import, and we want them not to kill our server
            except:
                cpy.log("import failed on module %s, module not loaded" % p)
                cpy.log("%s" % sys.exc_info()[0])
                cpy.log("%s" % traceback.format_exc())
Example #29
0
def json_out(content_type='application/json', debug=False,
             handler=json_handler):
    """Wrap request.handler to serialize its output to JSON. Sets Content-Type.

    If the given content_type is None, the Content-Type response header
    is not set.

    Provide your own handler to use a custom encoder.  For example
    cherrypy.config['tools.json_out.handler'] = <function>, or
    @json_out(handler=function).

    You must be using Python 2.6 or greater, or have the 'simplejson'
    package importable; otherwise, ValueError is raised during processing.
    """
    request = cherrypy.serving.request
    # request.handler may be set to None by e.g. the caching tool
    # to signal to all components that a response body has already
    # been attached, in which case we don't need to wrap anything.
    if request.handler is None:
        return
    if debug:
        cherrypy.log('Replacing %s with JSON handler' % request.handler,
                     'TOOLS.JSON_OUT')
    request._json_inner_handler = request.handler
    request.handler = handler
    if content_type is not None:
        if debug:
            cherrypy.log('Setting Content-Type to %s' %
                         content_type, 'TOOLS.JSON_OUT')
        cherrypy.serving.response.headers['Content-Type'] = content_type
Example #30
0
def allow(methods=None, debug=False):
    """Raise 405 if request.method not in methods (default ['GET', 'HEAD']).

    The given methods are case-insensitive, and may be in any order.
    If only one method is allowed, you may supply a single string;
    if more than one, supply a list of strings.

    Regardless of whether the current method is allowed or not, this
    also emits an 'Allow' response header, containing the given methods.
    """
    if not isinstance(methods, (tuple, list)):
        methods = [methods]
    methods = [m.upper() for m in methods if m]
    if not methods:
        methods = ['GET', 'HEAD']
    elif 'GET' in methods and 'HEAD' not in methods:
        methods.append('HEAD')

    cherrypy.response.headers['Allow'] = ', '.join(methods)
    if cherrypy.request.method not in methods:
        if debug:
            cherrypy.log('request.method %r not in methods %r' %
                         (cherrypy.request.method, methods), 'TOOLS.ALLOW')
        raise cherrypy.HTTPError(405)
    else:
        if debug:
            cherrypy.log('request.method %r in methods %r' %
                         (cherrypy.request.method, methods), 'TOOLS.ALLOW')
Example #31
0
 def stop(self):
     cherrypy.log("stopping ticker")
     self.running = False
Example #32
0
def validate_etags(autotags=False, debug=False):
    """Validate the current ETag against If-Match, If-None-Match headers.

    If autotags is True, an ETag response-header value will be provided
    from an MD5 hash of the response body (unless some other code has
    already provided an ETag header). If False (the default), the ETag
    will not be automatic.

    WARNING: the autotags feature is not designed for URL's which allow
    methods other than GET. For example, if a POST to the same URL returns
    no content, the automatic ETag will be incorrect, breaking a fundamental
    use for entity tags in a possibly destructive fashion. Likewise, if you
    raise 304 Not Modified, the response body will be empty, the ETag hash
    will be incorrect, and your application will break.
    See :rfc:`2616` Section 14.24.
    """
    response = cherrypy.serving.response

    # Guard against being run twice.
    if hasattr(response, 'ETag'):
        return

    status, reason, msg = _httputil.valid_status(response.status)

    etag = response.headers.get('ETag')

    # Automatic ETag generation. See warning in docstring.
    if etag:
        if debug:
            cherrypy.log('ETag already set: %s' % etag, 'TOOLS.ETAGS')
    elif not autotags:
        if debug:
            cherrypy.log('Autotags off', 'TOOLS.ETAGS')
    elif status != 200:
        if debug:
            cherrypy.log('Status not 200', 'TOOLS.ETAGS')
    else:
        etag = response.collapse_body()
        etag = '"%s"' % md5(etag).hexdigest()
        if debug:
            cherrypy.log('Setting ETag: %s' % etag, 'TOOLS.ETAGS')
        response.headers['ETag'] = etag

    response.ETag = etag

    # "If the request would, without the If-Match header field, result in
    # anything other than a 2xx or 412 status, then the If-Match header
    # MUST be ignored."
    if debug:
        cherrypy.log('Status: %s' % status, 'TOOLS.ETAGS')
    if status >= 200 and status <= 299:
        request = cherrypy.serving.request

        conditions = request.headers.elements('If-Match') or []
        conditions = [str(x) for x in conditions]
        if debug:
            cherrypy.log('If-Match conditions: %s' % repr(conditions),
                         'TOOLS.ETAGS')
        if conditions and not (conditions == ['*'] or etag in conditions):
            raise cherrypy.HTTPError(
                412, 'If-Match failed: ETag %r did '
                'not match %r' % (etag, conditions))

        conditions = request.headers.elements('If-None-Match') or []
        conditions = [str(x) for x in conditions]
        if debug:
            cherrypy.log('If-None-Match conditions: %s' % repr(conditions),
                         'TOOLS.ETAGS')
        if conditions == ['*'] or etag in conditions:
            if debug:
                cherrypy.log('request.method: %s' % request.method,
                             'TOOLS.ETAGS')
            if request.method in ('GET', 'HEAD'):
                raise cherrypy.HTTPRedirect([], 304)
            else:
                raise cherrypy.HTTPError(
                    412, 'If-None-Match failed: ETag %r '
                    'matched %r' % (etag, conditions))
Example #33
0
    def download(self,
                 public=None,
                 private=None,
                 nocache=None,
                 nostore=None,
                 notransform=None,
                 mustrevalidate=None,
                 maxage=None,
                 pragma=None,
                 expires=None,
                 disposition='inline',
                 format='pdf'):

        resp = cherrypy.response

        format = format.lower().strip()
        disposition = 'inline' if disposition == 'inline' else 'attachment'

        cache_control = []

        if public == 'on':
            cherrypy.log("Setting Cache-Control: public")
            cache_control.append('public')

        if private == 'on':
            cherrypy.log("Setting Cache-Control: private")
            cache_control.append('private')

        if nocache == 'on':
            cherrypy.log("Setting Cache-Control: no-cache")
            cache_control.append('no-cache')

        if nostore == 'on':
            cherrypy.log("Setting Cache-Control: no-store")
            cache_control.append('no-store')

        if notransform == 'on':
            cherrypy.log("Setting Cache-Control: no-transform")
            cache_control.append('no-transform')

        if mustrevalidate == 'on':
            cherrypy.log("Setting Cache-Control: must-revalidate")
            cache_control.append('must-revalidate')

        if maxage is not None and maxage.strip().isidigit():
            maxage = int(maxage.strip())
            cherrypy.log("Setting Cache-Control: max-age=%d" % (maxage, ))
            cache_control.append('max-age=%d' % (maxage, ))

        if pragma == 'on':
            cherrypy.log("Setting Pragma: no-cache")
            resp.headers['Pragma'] = 'no-cache'

        if cache_control:
            resp.headers['Cache-Control'] = ', '.join(cache_control)

        if expires is not None:
            expires = parse_expires(expires)
            cherrypy.log("Setting Expires: %r" % (expires, ))
            resp.headers['Expires'] = expires

        if format in FORMATS:
            resp.headers['Content-Type'] = FORMATS.get(format, 'pdf')

            filename = "test.%s" % (format, )

            cherrypy.log("Setting Content-Disposition: %s; filename=%s" %
                         (disposition, filename))

            resp.headers['Content-Disposition'] = "%s; filename=%s" % (
                disposition, filename)

            outfile = open(filename, 'rb')
            return file2Generator(outfile)
        else:
            outfile = open('download.html', 'rb')
            return file2Generator(outfile)
Example #34
0
def get(invalid_methods=("POST", "PUT", "DELETE"), debug=False, **kwargs):
    """Try to obtain cached output. If fresh enough, raise HTTPError(304).

    If POST, PUT, or DELETE:
        * invalidates (deletes) any cached response for this resource
        * sets request.cached = False
        * sets request.cacheable = False

    else if a cached copy exists:
        * sets request.cached = True
        * sets request.cacheable = False
        * sets response.headers to the cached values
        * checks the cached Last-Modified response header against the
          current If-(Un)Modified-Since request headers; raises 304
          if necessary.
        * sets response.status and response.body to the cached values
        * returns True

    otherwise:
        * sets request.cached = False
        * sets request.cacheable = True
        * returns False
    """
    request = cherrypy.serving.request
    response = cherrypy.serving.response

    if not hasattr(cherrypy, "_cache"):
        # Make a process-wide Cache object.
        cherrypy._cache = kwargs.pop("cache_class", MemoryCache)()

        # Take all remaining kwargs and set them on the Cache object.
        for k, v in kwargs.items():
            setattr(cherrypy._cache, k, v)
        cherrypy._cache.debug = debug

    # POST, PUT, DELETE should invalidate (delete) the cached copy.
    # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
    if request.method in invalid_methods:
        if debug:
            cherrypy.log('request.method %r in invalid_methods %r' %
                         (request.method, invalid_methods), 'TOOLS.CACHING')
        cherrypy._cache.delete()
        request.cached = False
        request.cacheable = False
        return False

    if 'no-cache' in [e.value for e in request.headers.elements('Pragma')]:
        request.cached = False
        request.cacheable = True
        return False

    cache_data = cherrypy._cache.get()
    request.cached = bool(cache_data)
    request.cacheable = not request.cached
    if request.cached:
        # Serve the cached copy.
        max_age = cherrypy._cache.delay
        for v in [e.value for e in request.headers.elements('Cache-Control')]:
            atoms = v.split('=', 1)
            directive = atoms.pop(0)
            if directive == 'max-age':
                if len(atoms) != 1 or not atoms[0].isdigit():
                    raise cherrypy.HTTPError(
                        400, "Invalid Cache-Control header")
                max_age = int(atoms[0])
                break
            elif directive == 'no-cache':
                if debug:
                    cherrypy.log(
                        'Ignoring cache due to Cache-Control: no-cache',
                        'TOOLS.CACHING')
                request.cached = False
                request.cacheable = True
                return False

        if debug:
            cherrypy.log('Reading response from cache', 'TOOLS.CACHING')
        s, h, b, create_time = cache_data
        age = int(response.time - create_time)
        if (age > max_age):
            if debug:
                cherrypy.log('Ignoring cache due to age > %d' % max_age,
                             'TOOLS.CACHING')
            request.cached = False
            request.cacheable = True
            return False

        # Copy the response headers. See
        # https://bitbucket.org/cherrypy/cherrypy/issue/721.
        response.headers = rh = httputil.HeaderMap()
        for k in h:
            dict.__setitem__(rh, k, dict.__getitem__(h, k))

        # Add the required Age header
        response.headers["Age"] = str(age)

        try:
            # Note that validate_since depends on a Last-Modified header;
            # this was put into the cached copy, and should have been
            # resurrected just above (response.headers = cache_data[1]).
            cptools.validate_since()
        except cherrypy.HTTPRedirect:
            x = sys.exc_info()[1]
            if x.status == 304:
                cherrypy._cache.tot_non_modified += 1
            raise

        # serve it & get out from the request
        response.status = s
        response.body = b
    else:
        if debug:
            cherrypy.log('request is not cached', 'TOOLS.CACHING')
    return request.cached
Example #35
0
    home_dir = '/Users/bobby/Downloads'
    #home_dir = '/home/stufs1/vgottipati'
    translate_folder = home_dir + '/tensorflow/tensorflow/models/rnn/translate'
    translate_args = '--decode --data_dir {0} --train_dir {1} --size=256 --num_layers=1 --steps_per_checkpoint=10000'

    trump_args = (translate_folder + '/trump_data_dir',
                  translate_folder + '/trump_checkpoint_dir')
    trump_bot_cmd = 'python ' + translate_folder + '/translate.py ' + (
        translate_args.format(*trump_args))
    trump_bot = Popen(trump_bot_cmd.split(),
                      shell=False,
                      stdin=PIPE,
                      stdout=PIPE)
    # flush out the intial info line
    trump_bot.stdout.readline()
    cherrypy.log('Started Trump bot')

    clinton_args = (translate_folder + '/clinton_data_dir',
                    translate_folder + '/clinton_checkpoint_dir')
    clinton_bot_cmd = 'python ' + translate_folder + '/translate.py ' + (
        translate_args.format(*clinton_args))
    clinton_bot = Popen(clinton_bot_cmd.split(),
                        shell=False,
                        stdin=PIPE,
                        stdout=PIPE)
    # flush out the intial info line
    clinton_bot.stdout.readline()
    cherrypy.log('Started Clinton bot')

    con = sqlite3.connect(home_dir + '/feedback_v2.db',
                          isolation_level=None,
Example #36
0
    def find_acceptable_charset(self):
        request = cherrypy.serving.request
        response = cherrypy.serving.response

        if self.debug:
            cherrypy.log('response.stream %r' % response.stream,
                         'TOOLS.ENCODE')
        if response.stream:
            encoder = self.encode_stream
        else:
            encoder = self.encode_string
            if "Content-Length" in response.headers:
                # Delete Content-Length header so finalize() recalcs it.
                # Encoded strings may be of different lengths from their
                # unicode equivalents, and even from each other. For example:
                # >>> t = u"\u7007\u3040"
                # >>> len(t)
                # 2
                # >>> len(t.encode("UTF-8"))
                # 6
                # >>> len(t.encode("utf7"))
                # 8
                del response.headers["Content-Length"]

        # Parse the Accept-Charset request header, and try to provide one
        # of the requested charsets (in order of user preference).
        encs = request.headers.elements('Accept-Charset')
        charsets = [enc.value.lower() for enc in encs]
        if self.debug:
            cherrypy.log('charsets %s' % repr(charsets), 'TOOLS.ENCODE')

        if self.encoding is not None:
            # If specified, force this encoding to be used, or fail.
            encoding = self.encoding.lower()
            if self.debug:
                cherrypy.log('Specified encoding %r' % encoding,
                             'TOOLS.ENCODE')
            if (not charsets) or "*" in charsets or encoding in charsets:
                if self.debug:
                    cherrypy.log('Attempting encoding %r' % encoding,
                                 'TOOLS.ENCODE')
                if encoder(encoding):
                    return encoding
        else:
            if not encs:
                if self.debug:
                    cherrypy.log(
                        'Attempting default encoding %r' %
                        self.default_encoding, 'TOOLS.ENCODE')
                # Any character-set is acceptable.
                if encoder(self.default_encoding):
                    return self.default_encoding
                else:
                    raise cherrypy.HTTPError(
                        500, self.failmsg % self.default_encoding)
            else:
                for element in encs:
                    if element.qvalue > 0:
                        if element.value == "*":
                            # Matches any charset. Try our default.
                            if self.debug:
                                cherrypy.log(
                                    'Attempting default encoding due '
                                    'to %r' % element, 'TOOLS.ENCODE')
                            if encoder(self.default_encoding):
                                return self.default_encoding
                        else:
                            encoding = element.value
                            if self.debug:
                                cherrypy.log(
                                    'Attempting encoding %s (qvalue >'
                                    '0)' % element, 'TOOLS.ENCODE')
                            if encoder(encoding):
                                return encoding

                if "*" not in charsets:
                    # If no "*" is present in an Accept-Charset field, then all
                    # character sets not explicitly mentioned get a quality
                    # value of 0, except for ISO-8859-1, which gets a quality
                    # value of 1 if not explicitly mentioned.
                    iso = 'iso-8859-1'
                    if iso not in charsets:
                        if self.debug:
                            cherrypy.log('Attempting ISO-8859-1 encoding',
                                         'TOOLS.ENCODE')
                        if encoder(iso):
                            return iso

        # No suitable encoding found.
        ac = request.headers.get('Accept-Charset')
        if ac is None:
            msg = "Your client did not send an Accept-Charset header."
        else:
            msg = "Your client sent this Accept-Charset header: %s." % ac
        msg += " We tried these charsets: %s." % ", ".join(
            self.attempted_charsets)
        raise cherrypy.HTTPError(406, msg)
Example #37
0
def gzip(compress_level=5,
         mime_types=['text/html', 'text/plain'],
         debug=False):
    """Try to gzip the response body if Content-Type in mime_types.

    cherrypy.response.headers['Content-Type'] must be set to one of the
    values in the mime_types arg before calling this function.

    The provided list of mime-types must be of one of the following form:
        * type/subtype
        * type/*
        * type/*+subtype

    No compression is performed if any of the following hold:
        * The client sends no Accept-Encoding request header
        * No 'gzip' or 'x-gzip' is present in the Accept-Encoding header
        * No 'gzip' or 'x-gzip' with a qvalue > 0 is present
        * The 'identity' value is given with a qvalue > 0.

    """
    request = cherrypy.serving.request
    response = cherrypy.serving.response

    set_vary_header(response, "Accept-Encoding")

    if not response.body:
        # Response body is empty (might be a 304 for instance)
        if debug:
            cherrypy.log('No response body', context='TOOLS.GZIP')
        return

    # If returning cached content (which should already have been gzipped),
    # don't re-zip.
    if getattr(request, "cached", False):
        if debug:
            cherrypy.log('Not gzipping cached response', context='TOOLS.GZIP')
        return

    acceptable = request.headers.elements('Accept-Encoding')
    if not acceptable:
        # If no Accept-Encoding field is present in a request,
        # the server MAY assume that the client will accept any
        # content coding. In this case, if "identity" is one of
        # the available content-codings, then the server SHOULD use
        # the "identity" content-coding, unless it has additional
        # information that a different content-coding is meaningful
        # to the client.
        if debug:
            cherrypy.log('No Accept-Encoding', context='TOOLS.GZIP')
        return

    ct = response.headers.get('Content-Type', '').split(';')[0]
    for coding in acceptable:
        if coding.value == 'identity' and coding.qvalue != 0:
            if debug:
                cherrypy.log('Non-zero identity qvalue: %s' % coding,
                             context='TOOLS.GZIP')
            return
        if coding.value in ('gzip', 'x-gzip'):
            if coding.qvalue == 0:
                if debug:
                    cherrypy.log('Zero gzip qvalue: %s' % coding,
                                 context='TOOLS.GZIP')
                return

            if ct not in mime_types:
                # If the list of provided mime-types contains tokens
                # such as 'text/*' or 'application/*+xml',
                # we go through them and find the most appropriate one
                # based on the given content-type.
                # The pattern matching is only caring about the most
                # common cases, as stated above, and doesn't support
                # for extra parameters.
                found = False
                if '/' in ct:
                    ct_media_type, ct_sub_type = ct.split('/')
                    for mime_type in mime_types:
                        if '/' in mime_type:
                            media_type, sub_type = mime_type.split('/')
                            if ct_media_type == media_type:
                                if sub_type == '*':
                                    found = True
                                    break
                                elif '+' in sub_type and '+' in ct_sub_type:
                                    ct_left, ct_right = ct_sub_type.split('+')
                                    left, right = sub_type.split('+')
                                    if left == '*' and ct_right == right:
                                        found = True
                                        break

                if not found:
                    if debug:
                        cherrypy.log('Content-Type %s not in mime_types %r' %
                                     (ct, mime_types),
                                     context='TOOLS.GZIP')
                    return

            if debug:
                cherrypy.log('Gzipping', context='TOOLS.GZIP')
            # Return a generator that compresses the page
            response.headers['Content-Encoding'] = 'gzip'
            response.body = compress(response.body, compress_level)
            if "Content-Length" in response.headers:
                # Delete Content-Length header so finalize() recalcs it.
                del response.headers["Content-Length"]

            return

    if debug:
        cherrypy.log('No acceptable encoding found.', context='GZIP')
    cherrypy.HTTPError(406, "identity, gzip").set_response()
Example #38
0
 def acquire_lock(self):
     """Acquire an exclusive lock on the currently-loaded session data."""
     self.locked = True
     self.locks.setdefault(self.id, threading.RLock()).acquire()
     if self.debug:
         cherrypy.log('Lock acquired.', 'TOOLS.SESSIONS')
Example #39
0
def check_session(session, can_refresh=False):
    """If session is None, fills it.

    :param cherrypy.Session session: the current session
    :param bool can_refresh: tells the function if it is safe to refresh
                             and close the old database
    :returns: ErrorInfo of the session
    :rtype: ErrorInfo
    """

    if session:
        cherrypy.log('Getting global lock: 1')
        GLOBAL_LOCK.acquire()
        if session.get('lock') is None:
            session['lock'] = threading.Lock()
        cherrypy.log('Releasing global lock: 1')
        GLOBAL_LOCK.release()

        cherrypy.log('Getting session lock')
        session['lock'].acquire()

        if session.get('info') is None:
            session['info'] = ErrorInfo()
        theinfo = session.get('info')

    else:
        cherrypy.log('Getting global lock: 2')
        GLOBAL_LOCK.acquire()
        global GLOBAL_INFO
        if GLOBAL_INFO is None:
            GLOBAL_INFO = ErrorInfo()

        theinfo = GLOBAL_INFO
        cherrypy.log('Releasing global lock: 2')
        GLOBAL_LOCK.release()

    # If session ErrorInfo is old, set up another connection
    if can_refresh and theinfo.timestamp < time.time() - \
            60*serverconfig.config_dict()['refresh_period']:
        theinfo.teardown()
        theinfo.setup()

    if session:
        cherrypy.log('Releasing session lock')
        session['lock'].release()

    return theinfo
Example #40
0
def init(storage_type='ram', path=None, path_header=None, name='session_id',
         timeout=60, domain=None, secure=False, clean_freq=5,
         persistent=True, httponly=False, debug=False, **kwargs):
    """Initialize session object (using cookies).

    storage_type
        One of 'ram', 'file', 'postgresql', 'memcached'. This will be
        used to look up the corresponding class in cherrypy.lib.sessions
        globals. For example, 'file' will use the FileSession class.

    path
        The 'path' value to stick in the response cookie metadata.

    path_header
        If 'path' is None (the default), then the response
        cookie 'path' will be pulled from request.headers[path_header].

    name
        The name of the cookie.

    timeout
        The expiration timeout (in minutes) for the stored session data.
        If 'persistent' is True (the default), this is also the timeout
        for the cookie.

    domain
        The cookie domain.

    secure
        If False (the default) the cookie 'secure' value will not
        be set. If True, the cookie 'secure' value will be set (to 1).

    clean_freq (minutes)
        The poll rate for expired session cleanup.

    persistent
        If True (the default), the 'timeout' argument will be used
        to expire the cookie. If False, the cookie will not have an expiry,
        and the cookie will be a "session cookie" which expires when the
        browser is closed.

    httponly
        If False (the default) the cookie 'httponly' value will not be set.
        If True, the cookie 'httponly' value will be set (to 1).

    Any additional kwargs will be bound to the new Session instance,
    and may be specific to the storage type. See the subclass of Session
    you're using for more information.
    """

    request = cherrypy.serving.request

    # Guard against running twice
    if hasattr(request, "_session_init_flag"):
        return
    request._session_init_flag = True

    # Check if request came with a session ID
    id = None
    if name in request.cookie:
        id = request.cookie[name].value
        if debug:
            cherrypy.log('ID obtained from request.cookie: %r' % id,
                         'TOOLS.SESSIONS')

    # Find the storage class and call setup (first time only).
    storage_class = storage_type.title() + 'Session'
    storage_class = globals()[storage_class]
    if not hasattr(cherrypy, "session"):
        if hasattr(storage_class, "setup"):
            storage_class.setup(**kwargs)

    # Create and attach a new Session instance to cherrypy.serving.
    # It will possess a reference to (and lock, and lazily load)
    # the requested session data.
    kwargs['timeout'] = timeout
    kwargs['clean_freq'] = clean_freq
    cherrypy.serving.session = sess = storage_class(id, **kwargs)
    sess.debug = debug

    def update_cookie(id):
        """Update the cookie every time the session id changes."""
        cherrypy.serving.response.cookie[name] = id
    sess.id_observers.append(update_cookie)

    # Create cherrypy.session which will proxy to cherrypy.serving.session
    if not hasattr(cherrypy, "session"):
        cherrypy.session = cherrypy._ThreadLocalProxy('session')

    if persistent:
        cookie_timeout = timeout
    else:
        # See http://support.microsoft.com/kb/223799/EN-US/
        # and http://support.mozilla.com/en-US/kb/Cookies
        cookie_timeout = None
    set_response_cookie(path=path, path_header=path_header, name=name,
                        timeout=cookie_timeout, domain=domain, secure=secure,
                        httponly=httponly)
Example #41
0
 def evalPython(self, funcBody):
     cherrypy.log("function body:" + funcBody)
     ns = {}
     code = compile(funcBody, '<string>', 'exec')
     exec code in ns
     return ns['result']
Example #42
0
 def delete(self):
     """Delete stored session data."""
     self._delete()
     if self.debug:
         cherrypy.log('Deleted session %s.' % self.id,
                      'TOOLS.SESSIONS')
Example #43
0
 def cmd(self, comando):
     cherrypy.log("Yacare[cmd]: %s"%comando)
     respuesta=self._enviar_comando(comando, {})
     return respuesta
Example #44
0
    def run(self, method, path, query_string, req_protocol, headers, rfile):
        """Process the Request. (Core)
        
        method, path, query_string, and req_protocol should be pulled directly
            from the Request-Line (e.g. "GET /path?key=val HTTP/1.0").
        path should be %XX-unquoted, but query_string should not be.
        headers should be a list of (name, value) tuples.
        rfile should be a file-like object containing the HTTP request entity.
        
        When run() is done, the returned object should have 3 attributes:
          status, e.g. "200 OK"
          header_list, a list of (name, value) tuples
          body, an iterable yielding strings
        
        Consumer code (HTTP servers) should then access these response
        attributes to build the outbound stream.
        
        """
        self.stage = 'run'
        try:
            self.error_response = cherrypy.HTTPError(500).set_response

            self.method = method
            path = path or "/"
            self.query_string = query_string or ''

            # Compare request and server HTTP protocol versions, in case our
            # server does not support the requested protocol. Limit our output
            # to min(req, server). We want the following output:
            #     request    server     actual written   supported response
            #     protocol   protocol  response protocol    feature set
            # a     1.0        1.0           1.0                1.0
            # b     1.0        1.1           1.1                1.0
            # c     1.1        1.0           1.0                1.0
            # d     1.1        1.1           1.1                1.1
            # Notice that, in (b), the response will be "HTTP/1.1" even though
            # the client only understands 1.0. RFC 2616 10.5.6 says we should
            # only return 505 if the _major_ version is different.
            rp = int(req_protocol[5]), int(req_protocol[7])
            sp = int(self.server_protocol[5]), int(self.server_protocol[7])
            self.protocol = min(rp, sp)

            # Rebuild first line of the request (e.g. "GET /path HTTP/1.0").
            url = path
            if query_string:
                url += '?' + query_string
            self.request_line = '%s %s %s' % (method, url, req_protocol)

            self.header_list = list(headers)
            self.rfile = rfile
            self.headers = http.HeaderMap()
            self.cookie = Cookie.SimpleCookie()
            self.handler = None

            # path_info should be the path from the
            # app root (script_name) to the handler.
            self.script_name = self.app.script_name
            self.path_info = pi = path[len(self.script_name):]

            self.stage = 'respond'
            self.respond(pi)

        except self.throws:
            raise
        except:
            if self.throw_errors:
                raise
            else:
                # Failure in setup, error handler or finalize. Bypass them.
                # Can't use handle_error because we may not have hooks yet.
                cherrypy.log(traceback=True, severity=40)
                if self.show_tracebacks:
                    body = format_exc()
                else:
                    body = ""
                r = bare_error(body)
                response = cherrypy.response
                response.status, response.header_list, response.body = r

        if self.method == "HEAD":
            # HEAD requests MUST NOT return a message-body in the response.
            cherrypy.response.body = []

        try:
            cherrypy.log.access()
        except:
            cherrypy.log.error(traceback=True)

        if cherrypy.response.timed_out:
            raise cherrypy.TimeoutError()

        return cherrypy.response
Example #45
0
 def on_connect(self, client, userdata, flags, rc):
   applyai.log("Connected with result code "+str(rc), self.logname)
   # Subscribing in on_connect() means that if we lose the connection and
   # reconnect then subscriptions will be renewed.
   client.subscribe("$SYS/#")
Example #46
0
 def postData(self, funcBody, funcData):
     cherrypy.log("function data:" + funcData)
     return xpathTest.extractPost(funcData)
Example #47
0
    def respond(self):
        req = self.req
        try:
            # Obtain a Request object from CherryPy
            local = req.server.bind_addr
            local = httputil.Host(local[0], local[1], '')
            remote = req.conn.remote_addr, req.conn.remote_port
            remote = httputil.Host(remote[0], remote[1], '')

            scheme = req.scheme
            sn = cherrypy.tree.script_name(req.uri or '/')
            if sn is None:
                self.send_response('404 Not Found', [], [''])
            else:
                app = cherrypy.tree.apps[sn]
                method = req.method
                path = req.path
                qs = req.qs or ''
                headers = req.inheaders.items()
                rfile = req.rfile
                prev = None

                try:
                    redirections = []
                    while True:
                        request, response = app.get_serving(
                            local, remote, scheme, 'HTTP/1.1')
                        request.multithread = True
                        request.multiprocess = False
                        request.app = app
                        request.prev = prev

                        # Run the CherryPy Request object and obtain the
                        # response
                        try:
                            request.run(method, path, qs, req.request_protocol,
                                        headers, rfile)
                            break
                        except cherrypy.InternalRedirect:
                            ir = sys.exc_info()[1]
                            app.release_serving()
                            prev = request

                            if not self.recursive:
                                if ir.path in redirections:
                                    raise RuntimeError(
                                        'InternalRedirector visited the same '
                                        'URL twice: %r' % ir.path)
                                else:
                                    # Add the *previous* path_info + qs to
                                    # redirections.
                                    if qs:
                                        qs = '?' + qs
                                    redirections.append(sn + path + qs)

                            # Munge environment and try again.
                            method = 'GET'
                            path = ir.path
                            qs = ir.query_string
                            rfile = io.BytesIO()

                    self.send_response(response.output_status,
                                       response.header_list, response.body)
                finally:
                    app.release_serving()
        except:
            tb = format_exc()
            # print tb
            cherrypy.log(tb, 'NATIVE_ADAPTER', severity=logging.ERROR)
            s, h, b = bare_error()
            self.send_response(s, h, b)
Example #48
0
    def main(self, params):

      params['status'] = 0
      frameInSource  = self.getCfgVal('frameIn')
      frameInChannel = self.getCfgVal('channel')
      frameIn = self.store.fetchFrameOut(frameInSource, frameInChannel)
      frameOut = frameIn.copy()

      plan = params['plan']
      order = params['order']
      record = params['record']

      # get the date string
      date = record['date'].split('T')[0]
      zeit = record['date'].split('T')[1]

      # create stats directory if required
      path = '../projects/' + self.project + '/stats/' + record['part']
      if not os.path.isdir(path):
        os.mkdir(path)
      path += '/' + date
      if not os.path.isdir(path):
        os.mkdir(path)

      filename = record['part'] + '~' + record['order'] + '.json'
      path += '/' + filename

      # save to stats
      self.writeStatsFile(path, record)

      # TODO Watermark images make to flexible
      txt = 'applyAI Vision '
      txt += record['date'].replace('T',' ')
      txt += '/' + record['part']
      txt += '/' + record['order']
      txt += '/' + record['machine']
      txt += '/' + str(record['status'])
      for v in record['values']:
        txt += '/' + str(v['v'])

      # Watermark image if required
      if self.getCfgVal('watermark') == 'Standard':
        cv2.putText(frameOut, txt, (10, frameOut.shape[0]-20), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 1)
      
      # TODO if nok and selected archive image(s)
      # create image directory if required
      path = '../projects/' + self.project + '/images/' + record['part']
      if not os.path.isdir(path):
        os.mkdir(path)
      path += '/' + date
      if not os.path.isdir(path):
        os.mkdir(path)

      support = (self.getCfgVal('imageStorageType') == 'Support')
      applyai.log('Support flag: ' + str(support), self.logname)
      #support = False
      self.store.updateFrameOut(self.name, 0, frameOut)
      if self.getCfgVal('saveImages') == '100%':
        self.flushImages(path, zeit, support)
      elif (record['status'] != 0 and self.getCfgVal('saveImages') == 'NOK'):
        self.flushImages(path, zeit, support)

      # TODO demoVar replace with some global counter
      #demoVar = int(self.getCfgVal('demo'))
      #demoVar += 1
      #self.setCfgVal('demo', demoVar)
      #cv2.putText(frameOut, 'demoVar:' + str(demoVar), (100, 200), cv2.FONT_HERSHEY_SIMPLEX, 3, (0,255,0), 5)

      params['record'] = record
      params['targets'] = self.targets.fetch()
      self.mqttPublish(params)
      return params
Example #49
0
    def getHTML(self, params):
        """ Run Widget 5 and generate HTML output for Results tab. """

        # Validate input parameters.
        if not exists(params['analysis_folder']):
            try:
                makedirs(params['analysis_folder'])
            except Exception as e:
                cherrypy.log(
                    'Widget 5: Error creating folder "{0}" for analysis files: {1}'
                    .format(params['analysis_folder'], e))
                return 'Sorry, something went wrong creating the folder "{0}" for the analysis files. Make sure ' \
                       'the path to the file is correct.<br><br>Exception: {1}'.format(params['analysis_folder'], e)
        if not exists(params['diet_file']):
            cherrypy.log('Widget 5: diet file "{0}" was not found'.format(
                params['diet_file']))
            return 'Sorry, diet file "{0}" was not found. Make sure the path to the file is correct.' \
                   .format(params['diet_file'])
        pair_models_file = join(params['analysis_folder'],
                                params['pair_models_file'])
        if not exists(pair_models_file):
            cherrypy.log('Widget 5: pair list file "{0}" was not found'.format(
                pair_models_file))
            return 'Sorry, pair list file "{0}" was not found. Make sure the path to the file is correct.' \
                   .format(pair_models_file)

        # Calculate growth rates for the two species models.
        try:
            with open(pair_models_file, 'r') as handle:
                pair_models = [line.strip() for line in handle]
            cherrypy.log(
                "Widget 5: Starting calculating growth rates for {0} pair models"
                .format(len(pair_models)))
            medium = read_diet_file(params['diet_file'])
            growth_rates = calculate_growth_rates(pair_models, medium)
            output_file = join(params['analysis_folder'],
                               params['growth_rates_file'])
            growth_rates.to_csv(output_file, index=False)
            cherrypy.log("Widget 5: Finished calculating growth rates")

        except Exception as e:
            cherrypy.log(
                "Widget 5: Error calculating growth rates: {0}".format(e))
            return "Sorry, something went wrong. Make sure the path to your file is correct.<br><br>" \
                   "Exception: {0}".format(e)

        # Generate the output for the Results tab.
        text = [
            "Here's a glimpse of what the first few lines of your growth rates file look like.<br><br>"
        ]
        with open(output_file, 'r') as handle:
            for index in range(15):
                text.append(handle.readline())
        return text
Example #50
0
 def more(self):
     with open("more.html", "r") as file:
         cherrypy.log("Visitor Log Entry")
     return "Your visit has been logged."
     return file.read()
Example #51
0
def log_request_headers(debug=False):
    """Write request headers to the cherrypy error log."""
    h = ["  %s: %s" % (k, v) for k, v in cherrypy.serving.request.header_list]
    cherrypy.log('\nRequest Headers:\n' + '\n'.join(h), "HTTP")
Example #52
0
def submitaction(user, workflows, action, session=None, **kwargs):
    """Writes the action to Unified and notifies the user that this happened

    :param str user: is the user that submitted the action
    :param str workflows: is the original workflow name or workflows
    :param str action: is the suggested action for Unified to take
    :param cherrypy.Session session: the current session
    :param kwargs: can include various reasons and additional datasets
    :returns: a tuple of workflows, reasons, and params for the action
    :rtype: list, str, list of dicts, dict
    """

    cherrypy.log('args: {0}'.format(kwargs))
    dotasks = kwargs.get('dotasks', [])
    if not isinstance(dotasks, list):
        dotasks = [dotasks]

    reasons, params = extract_reasons_params(action, **kwargs)

    cherrypy.log('Parameters: {0}'.format(params))

    coll = get_actions_collection()

    error_info = check_session(session)

    # Let's define some lambdas to use to filter our ACDCs (sanity checks)
    # First a quick shorthand for workflow parameters
    get_params = lambda wkf: error_info.get_workflow(
        wkf).get_workflow_parameters()

    # We want request type to be resubmission in our ACDC
    is_resub = lambda wkf: \
        get_params(wkf)['RequestType'] == 'Resubmission'

    # We want our ACDC to be submitted after the original request
    is_new = lambda wkf, workflow: \
        datetime.datetime(*(get_params(wkf)['RequestDate'])) > \
        datetime.datetime(*(get_params(workflow)['RequestDate']))

    if not isinstance(workflows, list):
        workflows = [workflows]

    for workflow in workflows:
        wf_params = dict(params)
        step_list = error_info.get_step_list(workflow)
        short_step_list = ['/'.join(step.split('/')[2:]) for step in step_list]
        # For recovery, get the proper sites and parameters out for each step
        if action in ['acdc', 'recovery']:
            all_steps = wf_params.pop('AllSteps', {})
            banned_sites = wf_params.pop('Ban', {'sites': []})['sites']

            # Fill empty parameters for each step from AllSteps
            for short_step_name, step_name in zip(short_step_list, step_list):
                # Get any existing thing (most likely not there)
                step_params = wf_params.get(short_step_name, {})

                for key, val in all_steps.iteritems():
                    # This also includes if the key value is set but blank
                    if not step_params.get(key):
                        step_params[key] = val

                wf_params[short_step_name] = step_params

                # Set the sites
                if kwargs.get('method', 'Manual') != 'Manual':
                    # Banned sites would show up under 'AllSteps'
                    wf_params[short_step_name]['sites'] = \
                        [site for site in \
                             check_session(session).get_workflow(workflow).\
                             site_to_run(step_name) if site not in banned_sites]

            # Only keep the workflow parameters with steps that occur in given workflow
            wf_params = {key: wf_params[key] for key in wf_params \
                             if key in short_step_list and \
                             (key in dotasks or not dotasks)}

        document = {
            'Action': action,
            'Parameters': wf_params,
            'Reasons': [reason['long'] for reason in reasons],
            'user': user,
            'ACDCs': [wkf for wkf in error_info.get_prepid(
                error_info.get_workflow(workflow).get_prep_id()).get_workflows() \
                          if wkf != workflow and is_resub(wkf) and is_new(wkf, workflow)]
            }

        cherrypy.log('About to insert workflow: %s action: %s' %
                     (workflow, document))

        coll.update_one({'workflow': workflow}, {
            '$set': {
                'timestamp': int(time.time()),
                'parameters': document,
                'acted': 0
            }
        },
                        upsert=True)

    return workflows, reasons, params
Example #53
0
def proxy(base=None,
          local='X-Forwarded-Host',
          remote='X-Forwarded-For',
          scheme='X-Forwarded-Proto',
          debug=False):
    """Change the base URL (scheme://host[:port][/path]).
    
    For running a CP server behind Apache, lighttpd, or other HTTP server.
    
    If you want the new request.base to include path info (not just the host),
    you must explicitly set base to the full base path, and ALSO set 'local'
    to '', so that the X-Forwarded-Host request header (which never includes
    path info) does not override it. Regardless, the value for 'base' MUST
    NOT end in a slash.
    
    cherrypy.request.remote.ip (the IP address of the client) will be
    rewritten if the header specified by the 'remote' arg is valid.
    By default, 'remote' is set to 'X-Forwarded-For'. If you do not
    want to rewrite remote.ip, set the 'remote' arg to an empty string.
    """

    request = cherrypy.serving.request

    if scheme:
        s = request.headers.get(scheme, None)
        if debug:
            cherrypy.log('Testing scheme %r:%r' % (scheme, s), 'TOOLS.PROXY')
        if s == 'on' and 'ssl' in scheme.lower():
            # This handles e.g. webfaction's 'X-Forwarded-Ssl: on' header
            scheme = 'https'
        else:
            # This is for lighttpd/pound/Mongrel's 'X-Forwarded-Proto: https'
            scheme = s
    if not scheme:
        scheme = request.base[:request.base.find("://")]

    if local:
        lbase = request.headers.get(local, None)
        if debug:
            cherrypy.log('Testing local %r:%r' % (local, lbase), 'TOOLS.PROXY')
        if lbase is not None:
            base = lbase.split(',')[0]
    if not base:
        port = request.local.port
        if port == 80:
            base = '127.0.0.1'
        else:
            base = '127.0.0.1:%s' % port

    if base.find("://") == -1:
        # add http:// or https:// if needed
        base = scheme + "://" + base

    request.base = base

    if remote:
        xff = request.headers.get(remote)
        if debug:
            cherrypy.log('Testing remote %r:%r' % (remote, xff), 'TOOLS.PROXY')
        if xff:
            if remote == 'X-Forwarded-For':
                # See http://bob.pythonmac.org/archives/2005/09/23/apache-x-forwarded-for-caveat/
                xff = xff.split(',')[-1].strip()
            request.remote.ip = xff
Example #54
0
def accept(media=None, debug=False):
    """Return the client's preferred media-type (from the given Content-Types).
    
    If 'media' is None (the default), no test will be performed.
    
    If 'media' is provided, it should be the Content-Type value (as a string)
    or values (as a list or tuple of strings) which the current resource
    can emit. The client's acceptable media ranges (as declared in the
    Accept request header) will be matched in order to these Content-Type
    values; the first such string is returned. That is, the return value
    will always be one of the strings provided in the 'media' arg (or None
    if 'media' is None).
    
    If no match is found, then HTTPError 406 (Not Acceptable) is raised.
    Note that most web browsers send */* as a (low-quality) acceptable
    media range, which should match any Content-Type. In addition, "...if
    no Accept header field is present, then it is assumed that the client
    accepts all media types."
    
    Matching types are checked in order of client preference first,
    and then in the order of the given 'media' values.
    
    Note that this function does not honor accept-params (other than "q").
    """
    if not media:
        return
    if isinstance(media, basestring):
        media = [media]
    request = cherrypy.serving.request

    # Parse the Accept request header, and try to match one
    # of the requested media-ranges (in order of preference).
    ranges = request.headers.elements('Accept')
    if not ranges:
        # Any media type is acceptable.
        if debug:
            cherrypy.log('No Accept header elements', 'TOOLS.ACCEPT')
        return media[0]
    else:
        # Note that 'ranges' is sorted in order of preference
        for element in ranges:
            if element.qvalue > 0:
                if element.value == "*/*":
                    # Matches any type or subtype
                    if debug:
                        cherrypy.log('Match due to */*', 'TOOLS.ACCEPT')
                    return media[0]
                elif element.value.endswith("/*"):
                    # Matches any subtype
                    mtype = element.value[:-1]  # Keep the slash
                    for m in media:
                        if m.startswith(mtype):
                            if debug:
                                cherrypy.log('Match due to %s' % element.value,
                                             'TOOLS.ACCEPT')
                            return m
                else:
                    # Matches exact value
                    if element.value in media:
                        if debug:
                            cherrypy.log('Match due to %s' % element.value,
                                         'TOOLS.ACCEPT')
                        return element.value

    # No suitable media-range found.
    ah = request.headers.get('Accept')
    if ah is None:
        msg = "Your client did not send an Accept header."
    else:
        msg = "Your client sent this Accept header: %s." % ah
    msg += (" But this resource only emits these media types: %s." %
            ", ".join(media))
    raise cherrypy.HTTPError(406, msg)
Example #55
0
 def log(self, msg):
     """Instruct the server to log the provided message to its error
             logs.
             """
     return cherrypy.log(msg)
Example #56
0
def log_traceback(severity=logging.ERROR, debug=False):
    """Write the last error's traceback to the cherrypy error log."""
    cherrypy.log("", "HTTP", severity=severity, traceback=True)
Example #57
0
    def build(self):
        """
        program build/update
        """
        # store common file path in variables
        tgz_file = self.dl_dir + self.demo_src_filename
        prog_names = ["pgm2freeman", "displayContours", "extract3D"]
        script_names = ["convert.sh", "convertFig.sh", "transformBG.sh"]
        prog_bin_files = []

        for a_prog in prog_names:
            prog_bin_files.append(self.bin_dir + a_prog)

        log_file = self.base_dir + "build.log"
        # get the latest source archive

        build.download(self.xlink_src, tgz_file)

        # test if the dest file is missing, or too old
        if (os.path.isfile(prog_bin_files[0])
                and ctime(tgz_file) < ctime(prog_bin_files[0])):
            cherrypy.log("not rebuild needed",
                         context='BUILD',
                         traceback=False)
        else:
            # extract the archive
            build.extract(tgz_file, self.src_dir)
            # build the program
            os.mkdir(self.src_dir + self.demo_src_dir + "/build")
            build.run("cd %s; cmake .. -DBUILD_EXAMPLES=false \
                       -DCMAKE_BUILD_TYPE=Release \
                       -DDGTAL_BUILD_TESTING=false;\
                       make -j 4"                                 %(self.src_dir+ self.demo_src_dir + "/build")\
                      , stdout=log_file)

            # save into bin dir
            if os.path.isdir(self.bin_dir):
                shutil.rmtree(self.bin_dir)
            os.mkdir(self.bin_dir)
            for i in range(0, len(prog_bin_files)):
                shutil.copy(
                    self.src_dir +
                    os.path.join(self.demo_src_dir, "build",
                                 "demoIPOL_ExtrConnectedReg", prog_names[i]),
                    prog_bin_files[i])
            for s_name in script_names:
                shutil.copy(
                    self.src_dir +
                    os.path.join(self.demo_src_dir,
                                 "demoIPOL_ExtrConnectedReg", s_name),
                    self.bin_dir)
            # copy Dynamic lib
            shutil.copy(
                self.src_dir + self.demo_src_dir + "/build/src/libDGtal.so",
                self.bin_dir)
            shutil.copy(
                self.src_dir + self.demo_src_dir + "/build/src/libDGtalIO.so",
                self.bin_dir)
            # cleanup the source dir
            shutil.rmtree(self.src_dir)
        return
Example #58
0
 def append_headers(header_list, debug=False):
     if debug:
         cherrypy.log(
             'Extending response headers with %s' % repr(header_list),
             'TOOLS.APPEND_HEADERS')
     cherrypy.serving.response.header_list.extend(header_list)
Example #59
0
 def delete(self, filename):
     try:
         os.unlink(os.path.join(self.commentdir, filename))
     except OSError:
         cpy.log("unable to delete file %s" % filename)
Example #60
0
 def ws(self):
     cherrypy.log("Handler created: %s" % repr(cherrypy.request.ws_handler))