Beispiel #1
0
Datei: main.py Projekt: rusi/mcdp
def get_nopicture_jpg():
    package = dir_from_package_name('mcdp_web')
    fn = os.path.join(package, 'static/nopicture.jpg')
    if not os.path.exists(fn):  # pragma: no cover
        raise ValueError(fn)
    contents = open(fn).read()
    return contents
Beispiel #2
0
    def get_icon(self, options, raise_if_not_found=False):
        imagepaths = []
        imagepaths.append(self._get_default_imagepath())

        libraries = os.path.join(dir_from_package_name('mcdp_data'),
                                 'libraries')
        imagepaths.append(
            os.path.join(libraries, 'FDM.' + MCDPConstants.shelf_extension,
                         'fdm.' + MCDPConstants.library_extension))
        imagepaths.append(
            os.path.join(libraries, 'FDM.' + MCDPConstants.shelf_extension,
                         'mechanisms.' + MCDPConstants.library_extension))
        sources = []
        sources.append(self.image_source)
        sources.append(ImagesFromPaths(imagepaths))
        image_source = TryMany(sources)

        try:
            best = choose_best_icon(options, image_source)
        except NoImageFound:
            if raise_if_not_found:
                raise
            return None
        resized = resize_icon(best, MCDPConstants.gdc_image_size_pixels)
        return resized
Beispiel #3
0
def get_test_db():
    ''' Returns the db_view for the test data '''
    from mcdp_hdb_mcdp.main_db_schema import DB
    from mcdp_hdb.pipes import mount_directory
    #     from mcdp_hdb.disk_struct import ProxyDirectory
    disk_map = DB.dm
    # first generate test db
    db_schema = DB.db
    db_data = db_schema.generate_empty()
    db_view = DB.view_manager.create_view_instance(db_schema, db_data)

    package = dir_from_package_name('mcdp_data')
    dirname = os.path.join(package, 'bundled.mcdp_repo')
    # read repo from dirname
    #     d = ProxyDirectory.from_disk(dirname)
    #     bundled = disk_map.interpret_hierarchy_(DB.repo, d)

    #     shelf_unittests = bundled
    repo_name = 'bundled'
    # create one

    mount_directory(view0=db_view.repos,
                    child_name=repo_name,
                    disk_map=disk_map,
                    dirname=dirname)
    return db_view
Beispiel #4
0
def get_template(name):
    ''' returns the jinja file  with base in mcdp_web '''
    d = dir_from_package_name('mcdp_web')
    fn = os.path.join(d, name)
    if not os.path.exists(fn):
        msg = 'The template %r does not exist.' % name
        raise ValueError(msg)
    return fn
Beispiel #5
0
    def setUp(self):
        from webtest import TestApp

        with tmpdir(erase=False) as d:
            # create a db
            bname = 'master'
            userdb_remote = os.path.join(d, 'userdb_remote')
            repo0 = create_user_db_repo(userdb_remote, bname)

            mcdp_data = dir_from_package_name('mcdp_data')
            unittests = os.path.join(
                mcdp_data, 'bundled.mcdp_repo', 'shelves',
                'unittests.' + MCDPConstants.shelf_extension)
            assert os.path.exists(unittests), unittests
            dest = os.path.join(
                userdb_remote, another_name_for_unittests_shelf + '.' +
                MCDPConstants.shelf_extension)
            shutil.copytree(unittests, dest)
            repo_commit_all_changes(repo0)

            userdb = os.path.join(d, 'userdb')
            repo = Repo.init(userdb)
            origin = repo.create_remote('origin', url=userdb_remote)
            origin.fetch()

            head = repo.create_head(bname, origin.refs[bname])
            head.set_tracking_branch(
                origin.refs[bname]
            )  # set local "master" to track remote "master
            head.checkout()

            settings = {
                'users':
                userdb,
                'load_mcdp_data':
                '0',
                'repos_yaml':
                """{
    'local': {
#         'user_db': '../mcdp-user-db/users',
#         'users': '../mcdp-user-db/users.mcdp_repo'
    },
    'remote': {}
    }"""
            }
            options = parse_mcdpweb_params_from_dict(settings)
            wa = WebApp(options, settings)
            app = wa.get_app()

            all_shelves = set()
            db_view = wa.hi.db_view
            for repo in db_view.repos.values():
                for shelf in repo.shelves:
                    all_shelves.add(shelf)

            db_view.user_db['anonymous'].subscriptions = list(all_shelves)

            self.testapp = TestApp(app)
Beispiel #6
0
def get_test_librarian():
    package = dir_from_package_name('mcdp_data')
    folder = os.path.join(package, 'bundled.mcdp_repo')

    if not os.path.exists(folder):
        raise_desc(ValueError, 'Test folders not found.', folder=folder)

    librarian = Librarian()
    librarian.find_libraries(folder)

    libraries = librarian.libraries
    n = len(libraries)
    if n <= 1:
        msg = 'Expected more libraries.'
        raise_desc(ValueError, msg, folder, libraries=libraries)

    orig = list(libraries)
    vname = MCDPConstants.ENV_TEST_LIBRARIES

    if vname in os.environ:
        use = os.environ[vname].split(",")

        logger.debug('environment variable %s = %s' % (vname, use))

        logger.info('Because %s is set, I will use only %s instead of %s.' %
                    (vname, use, orig))

        for _ in orig:
            if not _ in use:
                del libraries[_]
    else:
        pass
        #logger.debug('environment variable %s is unset' % vname)

    vname2 = MCDPConstants.ENV_TEST_LIBRARIES_EXCLUDE
    if vname2 in os.environ:
        exclude = os.environ[vname2].split(',')
        logger.debug('environment variable %s = %s' % (vname2, exclude))
    else:
        exclude = []
        # logger.debug('environment variable %s is unset' % vname2)

    if exclude:
        for a in exclude:
            if not a in libraries:
                msg = '%s = %s but %r is not a library.' % (vname2, exclude, a)
                logger.error(msg)
            else:
                logger.info('Excluding %s' % vname2)
                del libraries[a]

    return librarian
Beispiel #7
0
def get_mathjax_preamble():
    package = dir_from_package_name('mcdp_docs')
    fn = os.path.join(package, 'symbols.tex')
    if not os.path.exists(fn):  # pragma: no cover
        raise ValueError(fn)
    tex = open(fn).read()
    f = '$$' + tex + '$$'
    f += """
<script type="text/x-mathjax-config">
    console.log('here!');

   MathJax.Hub.Config({
       TeX: { extensions: ["color.js"] },
       SVG: {font:'STIX-Web'}
   });
</script>"""

    return f
Beispiel #8
0
def define_tests(context):
    """ Define other mcdp_lang_tests """
    filenames = []
    folder = dir_from_package_name('mcdp_lang_tests.ok')

    filenames.extend(locate_files(folder, '*.mcdp'))

    context = context.child('examples')
    found = set()

    def get_unique_basename(f):
        orig = os.path.splitext(os.path.basename(f))[0]
        if orig in found:
            for i in range(10):
                bn = '%s_%d' % (orig, i + 1)
                if not bn in found:
                    return bn
            assert False, (orig, found)
        else:
            found.add(orig)
            return orig

#     if False:
#         folder = os.path.join(dir_from_package_name('mocdp'), '../../libraries')
#         examples2 = list(locate_files(folder, '*.mcdp'))
#         print('Other files found: %s' % examples2)
#         filenames.extend(examples2)
#     else:

    examples2 = []

    for f in filenames:

        basename = get_unique_basename(f)
        print('defining %s - %s' % (basename, f))
        if f in examples2:
            contents = open(f).read()
            if '...' in contents:
                tests = [assert_semantic_error_fn]
            else:
                tests = [assert_parsable_to_connected_ndp_fn]
        else:
            tests = get_marked_tests(f)
        define_test_for(context, f, basename, tests)
Beispiel #9
0
def define_test_for(context, filename, basename, tests):

    known_failure = False

    mocdp_base = os.path.join(dir_from_package_name('mocdp'), '..', '..')
    outdir = os.path.join(mocdp_base, 'out', 'mcdp_lang_tests')

    for test in tests:
        n = test.__name__.replace('assert_', '').replace('_ndp', '')
        p_job_id = '%s-%s' % (basename, n)

        if not known_failure:
            p_job = context.comp_config(test_one,
                                        test,
                                        filename,
                                        job_id=p_job_id)
        else:
            context.comp_config(known_fail,
                                test_one,
                                test,
                                filename,
                                job_id=p_job_id)

    if not known_failure:
        if (assert_parsable_to_connected_ndp_fn in tests
                or assert_parsable_to_unconnected_ndp_fn in tests):
            job_id = '%s-%s' % (basename, 'report_ndp1')
            r = context.comp_config(test_report_ndp1,
                                    filename,
                                    outdir,
                                    basename,
                                    job_id=job_id,
                                    extra_dep=[p_job])
            context.add_report(r, 'examples_report_ndp1', file=basename)

        if assert_parsable_to_connected_ndp_fn in tests:
            job_id = '%s-%s' % (basename, 'report_dp1')
            r = context.comp_config(test_report_dp1,
                                    filename,
                                    outdir,
                                    basename,
                                    job_id=job_id,
                                    extra_dep=[p_job])
            context.add_report(r, 'examples_report_dp1', file=basename)
Beispiel #10
0
    def f2():
        d = dir_from_package_name('mcdp_web_tests')
        ini = os.path.join(d, 'test1.ini')
        with bootstrap(ini) as env:
            app = WebApp.singleton
            db_view0 = app.hi.db_view
            # create a copy of the DB
            db_data0 = db_view0._data
            db_view1 = DB.view_manager.create_view_instance(DB.db, db_data0)
            db_view1.set_root()
            logger.info('creating copies of the repositories')
            for r in db_view0.repos:
                db_view1.repos[r] = db_view0.repos[r]
            logger.info('creating copies of the repositories [done]')

            app.hi.db_view = db_view1
            db0 = testdb1()
            db_view1.user_db.users['anonymous'] = db_view0.user_db.users[
                'anonymous']
            db_view1.user_db.users[USER1] = db0['user_db']['users']['andrea']
            f(env)
Beispiel #11
0
    def find_libraries(self, dirname):
        if is_python_module_name(dirname):
            package = dir_from_package_name(dirname)
            logger.info('%s -> %s' % (dirname, package))
            dirname = package

        if dirname.endswith('.' + MCDPConstants.library_extension):
            libraries = [dirname]
        else:
            libraries = locate_files(dirname,
                                     "*." + MCDPConstants.library_extension,
                                     followlinks=False,
                                     include_directories=True,
                                     include_files=False)
            if not libraries:
                # use dirname as library path
                libraries = [dirname]

        for path in libraries:
            self.add_lib_by_path(path)

        # get all the images
        allimages = {}  # base.ext -> same struct as l.file_to_contents
        for _short, data in self.libraries.items():
            l = data['library']
            for ext in MCDPConstants.exts_images:
                basenames = l._list_with_extension(ext)
                for b in basenames:
                    b_ext = b + '.' + ext
                    allimages[b_ext] = l.file_to_contents[b_ext]

        for _short, data in self.libraries.items():
            l = data['library']
            for basename, d in allimages.items():
                if not basename in l.file_to_contents:
                    l.file_to_contents[basename] = d
Beispiel #12
0
Datei: main.py Projekt: rusi/mcdp
    def __init__(self, options, settings):
        self.options = options
        self.settings = settings

        # display options
        for k in sorted(self.options._values):
            v = self.options._values[k]
            logger.debug('option %20s = %r ' % (k, v))
        # display settings
        for k in sorted(self.settings):
            v = self.settings[k]
            logger.debug('setting %20s = %r ' % (k, v))

        WebApp.singleton = self

        dirname = options.libraries
        if dirname is None:
            package = dir_from_package_name('mcdp_data')
            default_libraries = os.path.join(package, 'libraries')
            msg = (
                'Option "-d" not passed, so I will open the default libraries '
                'shipped with PyMCDP. These might not be writable depending on your setup.'
            )
            logger.info(msg)
            dirname = default_libraries

        self.dirname = dirname

        AppVisualization.__init__(self)
        AppQR.__init__(self)
        AppSolver.__init__(self)
        AppInteractive.__init__(self)
        AppSolver2.__init__(self)
        AppEditorFancyGeneric.__init__(self)
        WebAppImages.__init__(self)

        # name -> dict(desc: )
        self.views = {}
        self.exceptions = []

        self.add_model_view('syntax', 'Source code display')
        self.add_model_view('edit_fancy', 'Editor')
        # self.add_model_view('edit', 'Simple editor for IE')
        self.add_model_view('solver2', desc='Solver interface')
        self.add_model_view('ndp_graph', 'NDP Graph representation')
        self.add_model_view('ndp_repr', 'NDP Text representation')
        self.add_model_view('dp_graph', 'DP graph representation')
        self.add_model_view('dp_tree', 'DP tree representation')
        self.add_model_view('images', 'Other image representations')
        self.add_model_view('solver', 'Graphical solver [experimental]')

        # csfr_token -> Session
        self.sessions = OrderedDict()

        config_repos = yaml_load(self.options.repos_yaml)
        logger.info('Config:\n' + indent(self.options.repos_yaml, '>'))
        logger.info(config_repos)
        instance = self.options.instance
        #root= 'out/root'
        root = create_tmpdir('HostInstance_root')
        logger.debug('Tmp dir: %s' % root)

        if not 'local' in config_repos:
            config_repos['local'] = {}
        if not 'remote' in config_repos:
            config_repos['remote'] = {}

        # add the bundled repository
        bundled_repo_dir = os.path.join(dir_from_package_name('mcdp_data'),
                                        'bundled.mcdp_repo')
        config_repos['local']['bundled'] = bundled_repo_dir

        self.hi = HostInstance(instance=instance,
                               upstream='master',
                               root=root,
                               repo_git=config_repos['remote'],
                               repo_local=config_repos['local'])

        if self.options.allow_anonymous_write:
            logger.warning(
                'Note: because allow_anonymous_write anonymous users can admin the repos.'
            )
            self.hi.set_local_permission_mode()
            from pyramid.security import Allow, Everyone, ALL_PERMISSIONS
            MCDPResourceRoot.__acl__.append((Allow, Everyone, ALL_PERMISSIONS))
Beispiel #13
0
def get_prerender_js():
    package = dir_from_package_name('mcdp_docs')
    fn = os.path.join(package, 'prerender.js')
    assert os.path.exists(fn), fn
    return fn
Beispiel #14
0
 def _get_default_imagepath(self):
     base = dir_from_package_name('mcdp_report')
     imagepath = os.path.join(base, 'icons')
     if not os.path.exists(imagepath):
         raise ValueError('Icons path does not exist: %r' % imagepath)
     return imagepath
Beispiel #15
0
    def get_app(self):
        self.time_start = time.time()

        secret = 'itsasecreet'  # XXX

        self.my_session_factory = SignedCookieSessionFactory(secret + 'sign')
        self.root_factory = MCDPResourceRoot
        config = Configurator(
                root_factory=self.root_factory, settings=self.settings)
        config.set_session_factory(self.my_session_factory)

        # config.include('pyramid_debugtoolbar')

        authn_policy = AuthTktAuthenticationPolicy(
                secret + 'authn', hashalg='sha512', callback=groupfinder)
        authz_policy = ACLAuthorizationPolicy()
        config.set_authentication_policy(authn_policy)
        config.set_authorization_policy(authz_policy)
        config.set_default_permission(Privileges.ACCESS)

        config.add_renderer('jsonp', JSONP(param_name='callback'))

        package = dir_from_package_name('mcdp_web')
        static_path = os.path.join(package, 'static')
        msg = 'Using static path %r' % static_path
        logger.info(msg)

        config.add_static_view(name='static', path=static_path, cache_max_age=3600)
        config.include('pyramid_jinja2')
        config.add_jinja2_search_path('mcdp_web:.')

        AppStatus.config(self, config)
        AppVisualization.config(self, config)
        AppQR.config(self, config)
        AppSolver.config(self, config)
        AppInteractive.config(self, config)
        AppEditorFancyGeneric.config(self, config)
        WebAppImages.config(self, config)
        AppLogin.config(self, config)
        AppSolver2.config(self, config)

        config.add_view(
                self.view_dummy, context=ResourceAbout, renderer='about.jinja2')
        config.add_view(
                self.view_index, context=MCDPResourceRoot, renderer='index.jinja2')
        config.add_view(
                self.view_dummy, context=ResourceLibraries, renderer='list_libraries.jinja2')
        config.add_view(
                self.view_dummy, context=ResourceRepos, renderer='repos.jinja2')
        config.add_view(
                self.view_dummy, context=ResourceLibraryInteractive, renderer='empty.jinja2')

        config.add_view(self.view_dummy, context=ResourceLibrary,
                        renderer='library_index.jinja2', permission=Privileges.READ)
        config.add_view(self.view_dummy, context=ResourceThings,
                        renderer='library_index.jinja2', permission=Privileges.READ)  # same as above

        config.add_view(
                self.view_dummy, context=ResourceRepo, renderer='shelves_index.jinja2')
        config.add_view(self.view_dummy, context=ResourceShelves,
                        renderer='shelves_index.jinja2')  # same as above
        config.add_view(self.view_dummy, context=ResourceAllShelves,
                        renderer='shelves_index.jinja2')  # same as above
        config.add_view(
                self.view_changes, context=ResourceChanges, renderer='changes.jinja2')
        config.add_view(
                self.view_tree, context=ResourceTree, renderer='tree.jinja2')
        config.add_view(self.view_not_found_generic, context=ResourceNotFoundGeneric,
                        renderer='not_found_generic.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_shelf_library_new,
                        context=ResourceLibrariesNewLibname, permission=Privileges.WRITE)
        config.add_view(self.view_shelf, context=ResourceShelf,
                        renderer='shelf.jinja2', permission=Privileges.DISCOVER)
        config.add_view(self.view_shelves_subscribe,
                        context=ResourceShelvesShelfSubscribe, permission=Privileges.SUBSCRIBE)
        config.add_view(self.view_shelves_unsubscribe,
                        context=ResourceShelvesShelfUnsubscribe, permission=Privileges.SUBSCRIBE)
        config.add_view(self.view_library_doc, context=ResourceLibraryDocRender,
                        renderer='library_doc.jinja2', permission=Privileges.READ)
        config.add_view(self.view_library_doc_not_found, context=ResourceLibraryDocNotFound,
                        renderer='library_doc_not_found.jinja2', permission=Privileges.READ)
        config.add_view(self.view_library_asset_not_found, context=ResourceLibraryAssetNotFound,
                        renderer='asset_not_found.jinja2', permission=Privileges.READ)
        config.add_view(
                self.view_library_asset, context=ResourceLibraryAsset, permission=Privileges.READ)
        config.add_view(self.view_refresh_library,
                        context=ResourceLibraryRefresh, permission=Privileges.READ)
        config.add_view(self.view_refresh, context=ResourceRefresh)
        config.add_view(self.view_users, context=ResourceListUsers,
                        renderer='users.jinja2', permission=Privileges.VIEW_USER_LIST)
        config.add_view(self.view_users_user, context=ResourceListUsersUser, renderer='user_page.jinja2',
                        permission=Privileges.VIEW_USER_PROFILE_PUBLIC)

        config.add_view(self.view_impersonate, context=ResourceUserImpersonate,
                        permission=Privileges.IMPERSONATE_USER)

        config.add_view(
                self.view_exception, context=Exception, renderer='exception.jinja2')
        config.add_view(self.exit, context=ResourceExit,
                        renderer='json', permission=NO_PERMISSION_REQUIRED)

        config.add_view(self.view_exceptions_occurred_json, context=ResourceExceptionsJSON,
                        renderer='json', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_exceptions_occurred, context=ResourceExceptionsFormatted,
                        renderer='exceptions_formatted.jinja2', permission=NO_PERMISSION_REQUIRED)

        config.add_view(
                self.view_dummy, context=ResourceShelfNotFound, renderer='shelf_not_found.jinja2')
        config.add_view(
                self.view_dummy, context=ResourceShelfForbidden, renderer='shelf_forbidden.jinja2')
        config.add_view(
                self.view_dummy, context=ResourceShelfInactive, renderer='shelf_inactive.jinja2')
        config.add_view(self.view_resource_not_found,
                        context=ResourceRepoNotFound, renderer='repo_not_found.jinja2')
        config.add_view(self.view_thing_delete, context=ResourceThingDelete)
        config.add_view(self.view_thing_rename, context=ResourceThingRename)
        config.add_view(self.view_thing, context=ResourceThing)
        config.add_view(self.view_picture, context=ResourceUserPicture)

        config.add_view(self.view_search, http_cache=0,
                        context=ResourceSearchPage, renderer='search.jinja2')
        config.add_view(self.view_search_query, http_cache=0,
                        context=ResourceSearchPageQuery, renderer='json')

        config.add_view(self.view_confirm_bind, http_cache=0, context=ResourceConfirmBind,
                        renderer='confirm_bind.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_confirm_bind_bind, http_cache=0, context=ResourceConfirmBindBind,
                        renderer='confirm_bind_bind.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_confirm_creation_similar, http_cache=0, context=ResourceConfirmCreationSimilar,
                        renderer='confirm_creation_similar.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_confirm_creation, http_cache=0, context=ResourceConfirmCreation,
                        renderer='confirm_creation.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_confirm_creation_create, http_cache=0, context=ResourceConfirmCreationCreate,
                        renderer='confirm_creation_create.jinja2', permission=NO_PERMISSION_REQUIRED)
        config.add_view(self.view_db_view, http_cache=0,
                        context=ResourceDBView, renderer='db_view.jinja2')

        config.add_view(
                serve_robots, context=ResourceRobots, permission=NO_PERMISSION_REQUIRED)
        config.add_notfound_view(self.view_not_found, renderer='404.jinja2')
        config.scan()

        config.add_view(
                self.view_authomatic, context=ResourceAuthomaticProvider, permission=NO_PERMISSION_REQUIRED)
        self.get_authomatic_config()
        app = config.make_wsgi_app()
        return app