示例#1
0
文件: vfs.py 项目: mmacaulay/drydrop
 def get_resource(self, path):
     domain = os.environ['SERVER_NAME']
     resource = Resource.find(path=path,
                              generation=self.settings.version,
                              domain=domain)
     if resource is None:
         content = self.fetch_resource_content(path)
         created_on = self.fetch_file_timestamp(path)
         resource = Resource(path=path,
                             content=content,
                             generation=self.settings.version)
         if created_on is not None:
             resource.created_on = created_on
         try:
             length = len(resource.content)
         except:
             length = 0
         if length > 0:
             log_event("Caching resource <code>%s</code> (%d bytes)" %
                       (path, length))
         logging.debug("VFS: caching resource %s (%d bytes) for %s", path,
                       length, domain)
         resource.domain = domain
         if content != None:
             resource.save()
     try:
         length = len(resource.content)
     except:
         length = 0
     logging.debug("VFS: serving resource %s (%d bytes) for %s", path,
                   length, domain)
     return resource
示例#2
0
 def get_resource(self, path):
     domain = os.environ['SERVER_NAME']
     resource = Resource.find(path=path, generation=self.settings.version, domain=domain)
     if resource is None:
         content = self.fetch_resource_content(path)
         created_on = self.fetch_file_timestamp(path)
         resource = Resource(path=path, content=content, generation=self.settings.version)
         if created_on is not None:
             resource.created_on = created_on
         try:
             length = len(resource.content)
         except:
             length = 0
         if length>0:
             log_event("Caching resource <code>%s</code> (%d bytes)" % (path, length))
         logging.debug("VFS: caching resource %s (%d bytes) for %s", path, length, domain)
         resource.domain = domain
         if content!=None:
           resource.save()
     try:
         length = len(resource.content)
     except:
         length = 0
     logging.debug("VFS: serving resource %s (%d bytes) for %s", path, length, domain)
     return resource
示例#3
0
 def flusher(self):
     log_event("Flushed resource cache")
     vfs = self.handler.vfs
     done, num = vfs.flush_resources()
     message = 'flushed %d resource(s)' % num
     if not done: message += ' ...'
     return self.render_json_response({
         'finished': done,
         'message': message
     })
示例#4
0
 def flusher(self):
     log_event("Flushed resource cache")
     vfs = self.handler.vfs
     done, num = vfs.flush_resources()
     message = 'flushed %d resource(s)' % num
     if not done: message += ' ...'
     return self.render_json_response({
         'finished': done,
         'message': message
     })
示例#5
0
 def events_flusher(self):
     domain = os.environ['SERVER_NAME']
     deleted = Event.clear(False, 1000, domain=domain)
     done = deleted<1000
     log_event("Removed all events")
     message = 'removed %d event(s)' % deleted
     if not done: message += ' ...'
     return self.render_json_response({
         'finished': done,
         'message': message
     })
示例#6
0
 def events_flusher(self):
     domain = os.environ['SERVER_NAME']
     deleted = Event.clear(False, 1000, domain=domain)
     done = deleted < 1000
     log_event("Removed all events")
     message = 'removed %d event(s)' % deleted
     if not done: message += ' ...'
     return self.render_json_response({
         'finished': done,
         'message': message
     })
示例#7
0
    def update_option(self):
        id = self.params.get('id')
        domain=os.environ['SERVER_NAME']
        if not id:
            return self.json_error('No option id specified')
            
        known_options = ['source', 'config', 'github_login', 'github_token']
        if not id in known_options:
            return self.json_error('Unknown option id (%s)' % id)

        value = self.params.get('value') or ""
        log_event("Changed <code>%s</code> to <code>%s</code>" % (id, value))
        settings = self.handler.settings
        settings.__setattr__(id, value)
        settings.version = settings.version + 1 # this effectively invalidates cache
        settings.domain = domain
        settings.put()
            
        return self.render_text(value)
示例#8
0
    def update_option(self):
        id = self.params.get('id')
        domain = os.environ['SERVER_NAME']
        if not id:
            return self.json_error('No option id specified')

        known_options = ['source', 'config', 'github_login', 'github_token']
        if not id in known_options:
            return self.json_error('Unknown option id (%s)' % id)

        value = self.params.get('value') or ""
        log_event("Changed <code>%s</code> to <code>%s</code>" % (id, value))
        settings = self.handler.settings
        settings.__setattr__(id, value)
        settings.version = settings.version + 1  # this effectively invalidates cache
        settings.domain = domain
        settings.put()

        return self.render_text(value)
示例#9
0
    def update_optimization(self):
        id = self.params.get('id')
        domain=os.environ['SERVER_NAME']
        if not id:
            return self.json_error('No option id specified')
            
        known_options = ['minify_html', 'expires_js', 'expires_css', 'minify_js', 'minify_css', 'expires_images', 'smush_png', 'smush_jpg']
        if not id in known_options:
            return self.json_error('Unknown option id (%s)' % id)

        value = loads(self.params.get('value')) or False
        log_event("Changed <code>%s</code> to <code>%s</code>" % (id, value))
        optimizations = self.handler.optimizations
        optimizations.__setattr__(id, value)
        optimizations.version = optimizations.version + 1 # this effectively invalidates cache
        optimizations.domain = domain
        optimizations.put()
            
        return self.render_text(value)
示例#10
0
文件: vfs.py 项目: blanu/freefallcdn
 def get_resource(self, path):
     domain = os.environ['SERVER_NAME']
     resource = Resource.find(path=path, generation=self.settings.version, domain=domain)
     if resource is None:
         content = self.fetch_resource_content(path)
         created_on = self.fetch_file_timestamp(path)
         resource = Resource(path=path, content=content, generation=self.settings.version)
         if created_on is not None:
             resource.created_on = created_on
         try:
             length = len(resource.content)
         except:
             length = 0
         if length>0:
             log_event("Caching resource <code>%s</code> (%d bytes)" % (path, length))
         logging.debug("VFS: caching resource %s (%d bytes) for %s", path, length, domain)
         resource.domain = domain
         if content!=None:
             basename, extension = os.path.splitext(path)
             optimizations = Optimizations.all().filter("domain =", os.environ['SERVER_NAME']).get()
             logging.info('optimizations: '+str(optimizations))
             if extension=='.js' and optimizations and optimizations.minify_js:
                 logging.info('Deferring to minify')
                 logging.debug(str(path)+' '+str(self.settings.version)+' '+str(domain))
                 deferred.defer(minify_js, path, self.settings.version, domain)
             elif extension=='.css' and optimizations and optimizations.minify_css:
                 from drydrop.lib.slimmer import css_slimmer
                 resource.content=css_slimmer(resource.content)
             elif (extension=='.html' or (extension==None and path[-1]=='/')) and optimizations and optimizations.minify_html:
                 from drydrop.lib.slimmer import html_slimmer
                 resource.content=html_slimmer(resource.content)
             elif (extension=='.png' or extension=='.jpg' or extension=='.jpeg') and optimizations and optimizations.minify_css:
                 logging.info('Deferring to smush')
                 logging.debug(str(path)+' '+str(self.settings.version)+' '+str(domain))
                 deferred.defer(smush, path, self.settings.version, domain)
             resource.save()
     try:
         length = len(resource.content)
     except:
         length = 0
     logging.debug("VFS: serving resource %s (%d bytes) for %s", path, length, domain)
     return resource
示例#11
0
    def github(self):
        payload = self.params.get('payload', None)
        logging.info("Received github hook: %s", payload)
        if not payload:
            return
        
        data = json_parse(payload)
        paths = []
        names = []
        info = ""
        for commit in data['commits']:
            author = commit['author']['email']
            try:
                info += "<a target=\"_blank\" href=\"%s\">%s</a>: %s<br/>" % (commit['url'], commit['id'][:6], commit['message'].split("\n")[0])
            except:
                info += "?<br/>"
            try:
                names.index(author)
            except:
                names.append(author)
            try:
                paths.extend(commit['added'])
            except:
                pass
            try:
                paths.extend(commit['removed'])
            except:
                pass
            try:
                paths.extend(commit['modified'])
            except:
                pass
                
        before_url = "%s/commit/%s" % (data['repository']['url'], data['before'])
        after_url = "%s/commit/%s" % (data['repository']['url'], data['after'])
        before = "?"
        try:
            before = data['before'][:6]
        except:
            pass

        after = "?"
        try:
            after = data['after'][:6]
        except:
            pass
        
        plural = ''
        if len(paths)!=1:
            plural = 's'
        authors = string.join(names, ',')
        log_event("Received github hook for commits <a target=\"_blank\" href=\"%s\">%s</a>..<a target=\"_blank\" href=\"%s\">%s</a> (%d change%s)" % (before_url, before, after_url, after, len(paths), plural), 0, authors, info)

        repo_url = data['repository']['url'] # like http://github.com/darwin/drydrop
        branch = data['ref'].split('/').pop() # takes 'master' from 'refs/heads/master'
        
        root_url = "%s/raw/%s" % (repo_url, branch) # creates http://github.com/darwin/drydrop/raw/master
        if not root_url.endswith('/'):
            root_url = root_url + '/'
        source_url = self.handler.settings.source
        if not source_url.endswith('/'):
            source_url = source_url + '/'
            
        # now we have:
        # http://github.com/darwin/drydrop/raw/master/ in root_url
        # http://github.com/darwin/drydrop/raw/master/tutorial/ in source_url
        
        # safety check
        if not source_url.startswith(root_url):
            log_event("<a target=\"_blank\" href=\"%s\"><code>%s</code></a><br/>is not affected by incoming changeset for<br/><a target=\"_blank\" href=\"%s\"><code>%s</code></a>" % (source_url, source_url, root_url, root_url), 0, authors)
            logging.info("Source url '%s' is not affected by incoming changeset for '%s'", source_url, root_url)
            return
        
        vfs = self.handler.vfs
        for path in paths:
            prefix = source_url[len(root_url):] # prefix is 'tutorial/'
            if not path.startswith(prefix):
                logging.warning("Unexpected: path '%s' should begin with '%s'. Skipping file.", path, prefix)
            else:
                # path is something like tutorial/start.html
                path = path[len(prefix):] # stripped to 'start.html'
                logging.info("Flushing resource %s", path)
                vfs.flush_resource(path)