Exemplo n.º 1
0
    def __init__(self, tree, config, namespace, test_portal=None):
        """Initialize and associate with a given tree.

    Args:
      tree: A mimic.common.Tree object.
      config: The app's config loaded from the app's app.yaml.
      namespace: The datastore and memcache namespace used for metadata.
      test_portal: An object that can be used to exchange data with target code
          during tests.  If this value is not None, then any loaded modules will
          be initialized with a _test_portal attribute that points to the
          supplied object.
    """
        # throws BadValueError
        namespace_manager.validate_namespace(namespace)
        self._tree = tree
        self._namespace = namespace
        self._active = False
        self._saved_sys_path = None
        self._saved_sys_modules = None
        self._test_portal = test_portal
        self._saved_open = open
        self._main_method = ''
        self._wsgi_app_name = None
        self._patches = []
        self._static_file_patterns = self._CreateStaticFilePatterns(config)
        self._skip_files_pattern = self._CreateSkipFilesPattern(config)

        # TODO: separate out the patches into separate classes to reduce
        # dependency creep and clean up this class.

        # patches for builtins
        for name, value in [('open', self._CustomOpen), ('file', MimicFile)]:
            self.AddPatch(patch.BuiltinPatch(name, value))
        # patches for the os module
        #
        # TODO: need to patch lstat, open, readlink, stat, walk
        for name, value in [
            ('access', self._Access),
            ('getcwd', self._GetCwd),
            ('getcwdu', lambda: unicode(self._GetCwd())),
            ('listdir', self._ListDir),
            ('remove', self._Unlink),
            ('rename', self._Rename),
            ('unlink', self._Unlink),
            ('stat', self._Stat),
        ]:
            self.AddPatch(patch.AttributePatch(os, name, value))

        # patches for os.path
        for name, value in [
            ('isdir', self._IsDir),
            ('isfile', self._IsFile),
            ('islink', self._IsLink),
        ]:
            self.AddPatch(patch.AttributePatch(os.path, name, value))

        # patch for datastore
        self.AddPatch(composite_query.CompositeQueryPatch())
Exemplo n.º 2
0
  def __init__(self, tree, config, namespace, test_portal=None):
    """Initialize and associate with a given tree.

    Args:
      tree: A mimic.common.Tree object.
      config: The app's config loaded from the app's app.yaml.
      namespace: The datastore and memcache namespace used for metadata.
      test_portal: An object that can be used to exchange data with target code
          during tests.  If this value is not None, then any loaded modules will
          be initialized with a _test_portal attribute that points to the
          supplied object.
    """
    # throws BadValueError
    namespace_manager.validate_namespace(namespace)
    self._tree = tree
    self._namespace = namespace
    self._active = False
    self._saved_sys_path = None
    self._saved_sys_modules = None
    self._test_portal = test_portal
    self._saved_open = open
    self._main_method = ''
    self._wsgi_app_name = None
    self._patches = []
    self._static_file_patterns = self._CreateStaticFilePatterns(config)
    self._skip_files_pattern = self._CreateSkipFilesPattern(config)

    # TODO: separate out the patches into separate classes to reduce
    # dependency creep and clean up this class.

    # patches for builtins
    for name, value in [('open', self._CustomOpen),
                        ('file', MimicFile)]:
      self.AddPatch(patch.BuiltinPatch(name, value))
    # patches for the os module
    #
    # TODO: need to patch lstat, open, readlink, stat, walk
    for name, value in [('access', self._Access),
                        ('getcwd', self._GetCwd),
                        ('getcwdu', lambda: unicode(self._GetCwd())),
                        ('listdir', self._ListDir),
                        ('remove', self._Unlink),
                        ('rename', self._Rename),
                        ('unlink', self._Unlink),
                        ('stat', self._Stat),
                       ]:
      self.AddPatch(patch.AttributePatch(os, name, value))

    # patches for os.path
    for name, value in [('isdir', self._IsDir),
                        ('isfile', self._IsFile),
                        ('islink', self._IsLink),
                       ]:
      self.AddPatch(patch.AttributePatch(os.path, name, value))

    # patch for datastore
    self.AddPatch(composite_query.CompositeQueryPatch())
Exemplo n.º 3
0
    def __init__(self,config=False,**kwargs):
        
        if config is False:
            raise exceptions.ConfigRequired()
        else:
            self.config = config
            
        # :: Namespace is usually extracted from subdomain - set via initialized keyword parameter
        if self.config.get('enable','multitenancy',False):
            
            _m_log = self.config.get('logging','multitenancy',False)
            
            if self.config.get('force_namespace','multitenancy', False):
                if isinstance(self.config.get('force_namespace','multitenancy',False), (str, unicode)) and self.config.get('force_namespace','multitenancy',False) is not '':
                    if namespace_manager.validate_namespace(self.config.get('force_namespace','multitenancy',False)):

                        if _m_log: logging.info('[i]: Setting request namespace to \''+config_get('force_namespace','multitenancy',False)+'\'.')
                        namespace_manager.set_namespace(self.config.get('force_namespace','multitenancy',False))
            
            else:
            
                if 'namespace' in kwargs or self.config.get('apps_mode_force', 'multitenancy', False):
                    if kwargs['namespace'] == self.config.get('apps_subdomain','multitenancy','apps') or self.config.get('apps_mode_force', 'multitenancy', False):

                        if _m_log: logging.info('[i]: Setting request namespace to \''+namespace_manager.google_apps_namespace()+'\' per APPS procedure.')
                        namespace_manager.set_namespace(namespace_manager.google_apps_namespace())

                    else:
                        if isinstance(kwargs['namespace'], (str, unicode)) and namespace_manager.validate_namespace(kwargs['namespace']):

                            if _m_log: logging.info('[i]: Setting request namespace to \''+kwargs['namespace']+'\' per domain split procedure.')
                            namespace_manager.set_namespace(kwargs['namespace'])
                            
                        else:
                            if kwargs['namespace'] is not None:
                                if _m_log: logging.info('[i]: Given namespace \''+str(kwargs['namespace'])+'\' failed to pass validation. Ignoring.')

        if 'version' in kwargs:
            self.version = kwargs['version']
        
        super(Platform, self).__init__()
Exemplo n.º 4
0
def get_all_courses(rules_text=None):
    """Reads all course rewrite rule definitions from environment variable."""
    # Normalize text definition.
    if not rules_text:
        rules_text = GCB_COURSES_CONFIG.value
    rules_text = rules_text.replace(',', '\n')

    # Use cached value if exists.
    cached = ApplicationContext.ALL_COURSE_CONTEXTS_CACHE.get(rules_text)
    if cached:
        return cached

    # Compute the list of contexts.
    rules = rules_text.split('\n')
    slugs = {}
    namespaces = {}
    all_contexts = []
    for rule in rules:
        rule = rule.strip()
        if not rule or rule.startswith('#'):
            continue
        parts = rule.split(':')

        # validate length
        if len(parts) < 3:
            raise Exception('Expected rule definition of the form '
                            ' \'type:slug:folder[:ns]\', got %s: ' % rule)

        # validate type
        if parts[0] != SITE_TYPE_COURSE:
            raise Exception('Expected \'%s\', found: \'%s\'.'
                            % (SITE_TYPE_COURSE, parts[0]))
        site_type = parts[0]

        # validate slug
        slug = parts[1]
        slug_parts = urlparse.urlparse(slug)
        if slug != slug_parts[2]:
            raise Exception(
                'Bad rule: \'%s\'. '
                'Course URL prefix \'%s\' must be a simple URL fragment.' % (
                    rule, slug))
        if slug in slugs:
            raise Exception(
                'Bad rule: \'%s\'. '
                'Course URL prefix \'%s\' is already defined.' % (rule, slug))
        slugs[slug] = True

        # validate folder name
        if parts[2]:
            folder = parts[2]
            # pylint: disable-msg=g-long-lambda
            create_fs = lambda unused_ns: LocalReadOnlyFileSystem(
                logical_home_folder=folder)
        else:
            folder = '/'
            # pylint: disable-msg=g-long-lambda
            create_fs = lambda ns: DatastoreBackedFileSystem(
                ns=ns,
                logical_home_folder=appengine_config.BUNDLE_ROOT,
                inherits_from=LocalReadOnlyFileSystem(logical_home_folder='/'),
                inheritable_folders=GCB_INHERITABLE_FOLDER_NAMES)

        # validate or derive namespace
        namespace = appengine_config.DEFAULT_NAMESPACE_NAME
        if len(parts) == 4:
            namespace = parts[3]
        else:
            if folder and folder != '/':
                namespace = '%s%s' % (GCB_BASE_COURSE_NAMESPACE,
                                      folder.replace('/', '-'))
        try:
            namespace_manager.validate_namespace(namespace)
        except Exception as e:
            raise Exception(
                'Error validating namespace "%s" in rule "%s"; %s.' % (
                    namespace, rule, e))

        if namespace in namespaces:
            raise Exception(
                'Bad rule \'%s\'. '
                'Namespace \'%s\' is already defined.' % (rule, namespace))
        namespaces[namespace] = True

        all_contexts.append(ApplicationContext(
            site_type, slug, folder, namespace,
            AbstractFileSystem(create_fs(namespace)),
            raw=rule))

    _validate_appcontext_list(all_contexts)

    # Cache result to avoid re-parsing over and over.
    ApplicationContext.ALL_COURSE_CONTEXTS_CACHE = {rules_text: all_contexts}

    return all_contexts
Exemplo n.º 5
0
def GetNamespace():
  namespace = GetProjectId(os.environ, common.IsDevMode()) or ''
  # throws BadValueError
  namespace_manager.validate_namespace(namespace)
  return namespace
Exemplo n.º 6
0
def validate_namespace(namespace):
    if namespace is not None:
        if not namespace:
            raise ValueError("Invalid namespace: {!r}".format(namespace))
        namespace_manager.validate_namespace(namespace, exception=ValueError)
Exemplo n.º 7
0
def validate_namespace(namespace):
  if namespace is not None:
    if not namespace:
      raise ValueError('Invalid namespace: {!r}'.format(namespace))
    namespace_manager.validate_namespace(namespace, exception=ValueError)
Exemplo n.º 8
0
    def __init__(self,config_override=None, *args, **kwargs):
        
        ## Setup globals
        global version_major
        global version_minor
        global config_module
        global import_item
        global build
        
        version = {'major':version_major,'minor':version_minor,'full':str(version_major)+'.'+str(version_minor)+' '+str(build),'build':build}
        
        ## Path Inserts
        if '.' not in sys.path:
            sys.path.insert(0,'.')
            sys.path.insert(1,'lib')
            sys.path.insert(2, 'distlib')

        ## Setup capabilities sets
        _cap = capabilities.CapabilitySet
        
        ## Setup object proxies
        self.config = c_config.PCConfigProxy()
        self.log = c_log.PCLogProxy()
        self.clock = c_clock.PCClockProxy()        
        self.state = c_state.PCStateProxy()
        self.api = c_api.PCAPIProxy()
        self.ext = c_ext.PCExtProxy()
        
        ## Link up with Platform
        self.api._setPlatformParent(self)
        self.log._setPlatformParent(self)
        self.state._setPlatformParent(self)
        self.clock._setPlatformParent(self)
        self.ext._setPlatformParent(self)
        self.config._setPlatformParent(self)
        
        ## Setup initial state
        self.state.set('env',os.environ)
        self.state.set('quotas', None)
        self.state.set('namespace',namespace_manager.get_namespace())
        self.state.set('capabilities', {'api':{
                                            'images':{
                                                'enabled':_cap('images').is_enabled(),
                                                'crop':_cap('images',methods=['crop']).is_enabled(),
                                                'get_serving_url':_cap('images',methods=['get_serving_url']).is_enabled(),
                                                'resize':_cap('images',methods=['resize']).is_enabled(),
                                                'rotate':_cap('images',methods=['rotate']).is_enabled()},
                                            'datastore_v3':{
                                                'enabled':_cap('datastore_v3').is_enabled(),
                                                'write':_cap('datastore_v3',capabilities=['write']).is_enabled(),
                                                'read':_cap('datastore_v3',capabilities=['read']).is_enabled(),
                                                'put':_cap('datastore_v3',methods=['put']).is_enabled(),
                                                'delete':_cap('datastore_v3',methods=['delete']).is_enabled(),
                                                'get':_cap('datastore_v3',methods=['get']).is_enabled(),
                                                'run_in_transaction':_cap('datastore_v3',methods=['run_in_transaction']).is_enabled(),
                                                'run_in_transaction_custom_retries':_cap('datastore_v3',methods=['run_in_transaction']).is_enabled()},
                                            'users':{
                                                'enabled':_cap('users').is_enabled(),
                                                'get_current_user':_cap('users',methods=['get_current_user']).is_enabled(),
                                                'is_current_user_admin':_cap('users',methods=['is_current_user_admin']).is_enabled()},
                                            'mail':{
                                                'enabled':_cap('mail').is_enabled(),
                                                'send_mail':_cap('mail',methods=['send_mail']).is_enabled(),
                                                'send_mail_to_admins':_cap('mail',methods=['send_mail_to_admins']).is_enabled()},
                                            'memcache':{
                                                'enabled':_cap('memcache').is_enabled(),
                                                'get':_cap('memcache',methods=['get']).is_enabled(),
                                                'set':_cap('memcache',methods=['set']).is_enabled(),
                                                'delete':_cap('memcache',methods=['delete']).is_enabled()},
                                            'oauth':{
                                                'enabled':_cap('oauth').is_enabled(),
                                                'get_current_user':_cap('oauth',methods=['get_current_user']).is_enabled(),
                                                'is_current_user_admin':_cap('oauth',methods=['is_current_user_admin']).is_enabled()},
                                            'multitenancy':{
                                                'enabled':_cap('multitenancy').is_enabled(),
                                                'get_namespace':_cap('multitenancy',methods=['get_namespace']).is_enabled(),
                                                'set_namespace':_cap('multitenancy',methods=['set_namespace']).is_enabled()},
                                            'blobstore':{
                                                'enabled':_cap('blobstore').is_enabled(),
                                                'get':_cap('blobstore',methods=['get']).is_enabled(),
                                                'delete':_cap('blobstore',methods=['delete']).is_enabled()},
                                            'xmpp':{
                                                'enabled':_cap('xmpp').is_enabled(),
                                                'send_message':_cap('xmpp',methods=['send_message']).is_enabled(),
                                                'send_invite':_cap('xmpp',methods=['send_invite']).is_enabled()},                         
                                            'urlfetch':{
                                                'enabled':_cap('urlfetch').is_enabled(),
                                                'fetch':_cap('urlfetch',methods=['fetch']).is_enabled()}
                                            }
                                        })
                                        
        ## Load/resolve Config
        if config_override is None:
            config_mod, props = import_helper(['ProvidenceClarity','pc_config'],['get','dump','config'])
            
        elif config_override is False or config_override == '':
            raise exceptions.ConfigRequired()

        else:

            if isinstance(config_override, type(os)):
                config_mod = config_override

            elif isinstance(config_override, (str, basestring, unicode)):
                config_mod, props = import_helper(config_override,['get','dump','config'])

            elif isinstance(config_override, list):
                config_mod, props = import_helper('.'.join(config_override))
                
            else:
                try:
                    config_mod, props = import_helper(['ProvidenceClarity',config_override],['get','dump','config'])
            
                except ImportError: raise exceptions.InvalidConfig()
                
        ## Set configuration
        self.config.setConfig(config_mod)
          
        ## Environment Vars - Split for Namespace
        software_t = '/'.split(os.environ['SERVER_SOFTWARE'])
        if software_t[0].lower() == 'development': platform = 'Dev';
        else: platform = 'Production'
    
        domain = os.environ['HTTP_HOST'].split(':')[0].split('.')
        if domain[-1] == 'com':
            subdomain = domain[0]
        else:
            subdomain = None                                        
                    
        # :: Namespace is usually extracted from subdomain - set via initialized keyword parameter
        if self.config.get('enable','multitenancy',False):
            
            _m_log = self.config.get('logging','multitenancy',False)
            
            if self.config.get('force_namespace','multitenancy', False):
                if isinstance(self.config.get('force_namespace','multitenancy',False), (str, unicode)) and self.config.get('force_namespace','multitenancy',False) is not '':
                    if namespace_manager.validate_namespace(self.config.get('force_namespace','multitenancy',False)):

                        if _m_log: self.log.info('Setting request namespace to "%s".' % config_get('force_namespace','multitenancy',False))
                        namespace_manager.set_namespace(self.config.get('force_namespace','multitenancy',False))
            
            else:
            
                if 'namespace' in kwargs or self.config.get('apps_mode_force', 'multitenancy', False):
                    if kwargs['namespace'] == self.config.get('apps_subdomain','multitenancy','apps') or self.config.get('apps_mode_force', 'multitenancy', False):

                        if _m_log: self.log.info('Setting request namespace to Google Apps domain "%s".' % namespace_manager.google_apps_namespace())
                        namespace_manager.set_namespace(namespace_manager.google_apps_namespace())

                    else:
                        if isinstance(kwargs['namespace'], (str, unicode)) and namespace_manager.validate_namespace(kwargs['namespace']):

                            if _m_log: self.log.info('Setting request namespace to split domain "%s".' % kwargs['namespace'])
                            namespace_manager.set_namespace(kwargs['namespace'])
                            
                        else:
                            if kwargs['namespace'] is not None:
                                if _m_log: self.log.info('Given namespace "%s" failed to pass validation. Ignoring.' % str(kwargs['namespace']))

        if 'version' in kwargs:
            self.version = kwargs['version']
        else:
            self.version = version
        
        super(Platform, self).__init__(*args, **kwargs)