Ejemplo n.º 1
0
def install_defaults(opts):
    configurator = Configurator('canari.resources.templates:install_plume', '.',
                                {'non_interactive': True, 'remember_answers': False})

    configurator.variables['plume.venv'] = os.environ.get('VIRTUAL_ENV')
    if configurator.variables['plume.venv']:
        print 'Will use the virtual environment in %r to run Plume...' % configurator.variables['plume.venv']
    configurator.variables['plume.enable_ssl'] = 'n'
    print 'Installing init script to /etc/init.d...'
    configurator.variables['plume.init'] = check_init_script(configurator, '', '/etc/init.d')
    print 'Creating Plume root directory at /var/plume...'
    configurator.variables['plume.dir'] = check_mkdir(configurator, '', '/var/plume')
    print 'The PID file will be at /var/run/plume.pid...'
    configurator.variables['plume.run_dir'] = '/var/run'
    print 'The log files will be at /var/log/plume.log...'
    configurator.variables['plume.log_dir'] = '/var/log'
    configurator.variables['plume.user'] = check_uid(configurator, '', 'nobody')
    configurator.variables['plume.group'] = check_gid(configurator, '', 'nobody')
    print 'The Plume server will under UID/GID=%s/%s...' % (
        configurator.variables['plume.user'], configurator.variables['plume.group'])
    print 'TLS will be disabled by default...'
    configurator.variables['plume.certificate'] = ''
    configurator.variables['plume.private_key'] = ''

    configurator.ask_questions()
    configurator.render()
    finish(configurator)
Ejemplo n.º 2
0
def dockerize_package(args):
    project = CanariProject()

    print('Dockerizing %s transform package...' % project.name)

    configurator = Configurator(
            'canari.resources.templates:dockerize_package',
            project.root_dir,
            {'non_interactive': True},
            variables={'project.name': project.name, 'canari.version': version}
    )

    print('Creating Dockerfile for %s...' % project.name)
    configurator.render()
    print('done!')

    if not find_executable('docker'):
        print """Could not find 'docker' in your system path. Please download and install Docker from http://docker.com
        and rerun this command again.
        """

        exit(-1)

    docker_hosts = [j for sublist in [('-H', i) for i in args.host] for j in sublist]
    container = '%s/%s:%s' % (project.name, project.name, args.os)

    if not args.host:
        if not find_executable('docker-machine'):
            print """Could not find 'docker-machine' in your system path. Please download and install Docker Machine from
            http://docker.com and rerun this command again or manually specify a Docker host using the '-H' parameter,
            instead.
            """
            exit(-1)

        print 'Attempting to discover available Docker machines.'
        machines = run_command(['docker-machine', 'ls', '-q'], stdout=subprocess.PIPE).communicate()[0].split('\n')
        machines.remove('')

        machine = question.parse_int('More than one Docker machine was detected. Which one would you like to use to'
                                     'build and run this container?', machines) if len(machines) != 1 else 0

        print 'Setting up environment for Docker machine %s' % machines[machine]

        # Inject docker environment variables
        env = run_command(['docker-machine', 'env', machines[machine]], stdout=subprocess.PIPE).communicate()[0]
        os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env))

    with PushDir(project.root_dir):
        p = run_command(['docker'] + docker_hosts + ['build', '-t', container, '-f', 'Dockerfile-%s' % args.os, '.'])
        p.communicate()
        if p.returncode:
            print 'An error occurred while building the Docker container.'
            exit(-1)

    if question.parse_bool('Would you like to run this container now?'):
        port = question.parse_int_range('Which port would you like Plume to listen on externally?', 0, 65535, 8080)
        print 'Plume will be listening on http://%s:%s' % (re.findall('://([^:]+)', os.environ['DOCKER_HOST'])[0], port)
        run_command(['docker'] + docker_hosts + ['run', '-it', '-p', '8080:%s' % port, container]).communicate()

    print 'done!'
Ejemplo n.º 3
0
def create_package(opts):

    package_name = opts.package
    capitalized_package_name = package_name.capitalize()

    variables = {
        "project.name": package_name,
        "entity.example_name": "My%sEntity" % capitalized_package_name,
        "entity.base_name": "%sEntity" % capitalized_package_name,
        "created.year": datetime.now().year,
        "canari.version": canari.__version__,
    }

    defaults = {"project.create_example": True, "author.name": getuser()}

    if not path.exists(package_name):
        print("creating skeleton in %s" % package_name)
        configurator = Configurator(
            "canari.resources.templates:create_package",
            package_name,
            {"non_interactive": False, "remember_answers": True},
            variables=variables,
            defaults=defaults,
        )
        configurator.ask_questions()
        configurator.render()
    else:
        print("A directory with the name %s already exists... exiting" % package_name)
        exit(-1)

    print("done!")
Ejemplo n.º 4
0
def install_wizard(opts):
    configurator = Configurator('canari.resources.templates:install_plume', '.',
                                {'non_interactive': False, 'remember_answers': False})
    configurator.ask_questions()

    if os.environ.get('VIRTUAL_ENV'):
        run_venv = parse_bool(
            "--> Canari has detected that you're running this install script from within a virtualenv.\n"
            "--> Would you like to run Plume from this virtualenv (%r) as well?" % os.environ['VIRTUAL_ENV'], True)
        configurator.variables['plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False

    configurator.render()
    finish(configurator)
Ejemplo n.º 5
0
def generate_entities_doc(args):
    args = parse_args(args)

    transform_package = TransformDistribution(args.package)

    jinja2_env.filters['entity_properties'] = \
        lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and
                            not hasattr(Entity, p)])

    def get_property_type(v):
        if isinstance(v, IntegerEntityField):
            return 'int'
        elif isinstance(v, FloatEntityField):
            return 'float'
        elif isinstance(v, BooleanEntityField):
            return 'bool'
        elif isinstance(v, TimeSpanEntityField):
            return 'timedelta'
        elif isinstance(v, DateTimeEntityField):
            return 'datetime'
        elif isinstance(v, DateEntityField):
            return 'date'
        elif isinstance(v, LongEntityField):
            return 'long'
        else:
            return 'str'

    jinja2_env.filters['get_property_type'] = get_property_type

    entity_module = 'canari.maltego.entities' if transform_package.name == 'canari' \
        else '%s.transforms.common.entities' % transform_package.name

    variables = {
        'transform.module': entity_module,
        'transform.entities': transform_package.entities,
        'transform.author': '%s <%s>' % (transform_package.author, transform_package.author_email)
    }

    configurator = Configurator(
            'canari.resources.templates:generate_entities_doc',
            args.out_path,
            {'non_interactive': True},
            variables=variables
    )

    configurator.ask_questions()

    print('Creating entities.rst documentation for %r...' % args.package)
    configurator.render()

    print('done!')
Ejemplo n.º 6
0
def create_transform(args):

    opts = parse_args(args)
    project = CanariProject()

    transform_module = (opts.transform if not opts.transform.endswith('.py') else opts.transform[:-3])
    transform_name = ''.join([i[0].upper()+i[1:] for i in transform_module.split('_')])
    transform_module = transform_module.lower()

    if '.' in transform_module:
        print("Transform name (%r) cannot have a dot ('.')." % transform_name)
        exit(-1)
    elif not transform_module:
        print("You must specify a valid transform name.")
        exit(-1)

    target = project.root_dir
    transform_directory = project.transforms_dir

    if os.path.exists(os.path.join(transform_directory, '%s.py' % transform_module)):
        print('Transform %r already exists... quitting' % transform_module)
        exit(-1)

    variables = parse_config(os.path.join(target, '.mrbob.ini'))['variables']

    variables.update({'transform.module': transform_module, 'transform.name': transform_name})

    configurator = Configurator(
        'canari.resources.templates:create_transform',
        target,
        {'non_interactive': True},
        variables=variables
    )

    configurator.ask_questions()

    print('Creating transform %r...' % transform_module)
    configurator.render()

    print('done!')
Ejemplo n.º 7
0
def test_update_viewlets_configure_zcml_without_template(tmpdir):
    """Test configure changes when changes are already in place."""
    target_path = tmpdir.strpath + '/collective.sample'
    package_path = target_path + '/src/collective/sample'
    viewlets_path = package_path + '/viewlets/'
    os.makedirs(target_path)
    os.makedirs(package_path)
    os.makedirs(viewlets_path)
    template = """<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="collective.sample">

  -*- extra stuff goes here -*-
  <browser:viewlet
     name="viewlet"
     for="*"
     manager="plone.app.layout.viewlets.interfaces.IBelowContentTitle"
     layer="collective.sample.interfaces.ICollectiveTodoLayer"
     class=".py_viewlet.MyViewlet"
     permission="zope2.View"
     />


</configure>
"""
    with open(os.path.join(viewlets_path + 'configure.zcml'), 'w') as f:
        f.write(template)
    configurator = Configurator(
        template='bobtemplates.plone:viewlet',
        target_directory='collective.sample',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'viewlet_name': 'viewlet',
            'viewlet_python_class_name': 'MyViewlet',
            'viewlet_python_file_name': 'py_viewlet',
            'viewlet_template': False,
            'package_folder': package_path,
            'package.dottedname': 'collective.sample',
            'browser_layer': 'ICollectiveSampleLayer',
        },
    )
    viewlet._update_viewlets_configure_zcml(configurator)

    with open(os.path.join(viewlets_path + 'configure.zcml'), 'r') as f:
        content = f.read()
        if content != template:
            pytest.raises(ValidationError)
Ejemplo n.º 8
0
def test_prepare_renderer(tmpdir):
    package_root = tmpdir.strpath + "/collective.todo"
    package_path = init_package_base_structure(package_root)

    configurator = Configurator(
        template="bobtemplates.plone:theme",
        target_directory=package_path,
        variables={
            "theme.name": "test.theme",
        },
    )
    theme.prepare_renderer(configurator)

    assert configurator.variables['template_id'] == 'theme'
    assert configurator.variables['theme.normalized_name'] == 'test.theme'
    assert configurator.target_directory.endswith(
        '/collective.todo/src/collective/todo')  # NOQA: E501

    # nested namespace package
    package_root = os.path.join(
        tmpdir.strpath,
        'collective.foo.bar',
    )
    package_path = init_package_base_structure(package_root)
    configurator = Configurator(
        template='bobtemplates.plone:theme',
        target_directory=package_path,
        variables={
            'theme.name': 'test.theme',
            'package.root_folder': package_root,
        },
    )
    theme.prepare_renderer(configurator)

    assert configurator.variables['template_id'] == 'theme'
    assert configurator.variables['theme.normalized_name'] == 'test.theme'
    assert configurator.target_directory.endswith(
        '/collective.foo.bar/src/collective/foo/bar')  # NOQA: E501
def test_post_theme_name(tmpdir):
    target_path = tmpdir.strpath + '/collective.theme'
    configurator = Configurator(
        template='bobtemplates.plone:theme_barceloneta',
        target_directory=target_path,
    )

    theme_barceloneta.post_theme_name(configurator, None, 'collective.theme')
    with pytest.raises(ValidationError):
        theme_barceloneta.post_theme_name(
            configurator,
            None,
            'collective.$SPAM',
        )
Ejemplo n.º 10
0
def test_update_views_configure_zcml(tmpdir):
    """Test configure changes when changes are already in place."""
    target_path = tmpdir.strpath + '/collective.sample'
    package_path = target_path + '/src/collective/sample'
    views_path = package_path + '/views/'
    os.makedirs(target_path)
    os.makedirs(package_path)
    os.makedirs(views_path)
    template = """<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="{{{ package.dottedname }}}">

  -*- extra stuff goes here -*-
  <browser:page
     name="py_view"
     for="*"
     class=".py_view.MyView"
     template="pt_view.pt"
     permission="zope2.View"
     />


</configure>
"""
    with open(os.path.join(views_path + 'configure.zcml'), 'w') as f:
        f.write(template)
    configurator = Configurator(
        template='bobtemplates.plone:view',
        target_directory='collective.sample',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'view_python_class': True,
            'view_python_class_name': 'MyView',
            'view_python_file_name': 'py_view',
            'view_name': 'py-view',
            'view_template': True,
            'view_template_name': 'pt_view',
            'package_folder': package_path,
        },
    )
    view._update_views_configure_zcml(configurator)

    with open(os.path.join(views_path + 'configure.zcml'), 'r') as f:
        content = f.read()
        if content != template:
            pytest.raises(ValidationError)
Ejemplo n.º 11
0
def test_read_setup_cfg(tmpdir):
    configurator = Configurator(
        template='bobtemplates.plone:addon',
        target_directory='collective.todo',
    )
    base.read_setup_cfg(configurator)

    template = """[check-manifest]
check=True

[tool:bobtemplates.plone]
version=5.1
"""
    target_dir = tmpdir.strpath + '/collective.foo'
    os.mkdir(target_dir)
    with open(os.path.join(target_dir + '/setup.cfg'), 'w') as f:
        f.write(template)

    configurator = Configurator(
        template='bobtemplates.plone:addon',
        target_directory=target_dir,
    )
    base.read_setup_cfg(configurator)
def test_check_global_allow_true():
    """Test global_allow set to True."""
    configurator = Configurator(
        template='bobtemplates.plone:content_type',
        target_directory='collective.foo.bar',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'dexterity_type_global_allow': 'y',
        },
    )
    with pytest.raises(SkipQuestion):
        content_type.check_global_allow(configurator, None)
Ejemplo n.º 13
0
def test_get_view_name():
    question = Question(name='view_name', question='', default=None)
    configurator = Configurator(
        template='bobtemplates.plone:view',
        target_directory='collective.foo.bar',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'view_python_class': True,
            'view_python_class_name': 'DemoView',
        },
    )
    view.get_view_name_from_python_class(configurator, question)
Ejemplo n.º 14
0
def test_update_portlets_configure_zcml(tmpdir):
    """Test configure changes when changes are already in place."""
    target_path = tmpdir.strpath + '/collective.sample'
    package_path = target_path + '/src/collective/sample'
    portlets_path = package_path + '/portlets/'
    os.makedirs(target_path)
    os.makedirs(package_path)
    os.makedirs(portlets_path)
    template = """<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:browser="http://namespaces.zope.org/browser"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="collective.sample">

  '-*- extra stuff goes here -*-'

  <plone:portlet
    name="collective.sample.portlets.MyWeather"
    interface=".my_weather.IMyWeatherPortlet"
    assignment=".my_weather.Assignment"
    renderer=".my_weather.Renderer"
    addview=".my_weather.AddForm"
    editview=".my_weather.EditForm" />

</configure>
"""
    with open(os.path.join(portlets_path + 'configure.zcml'), 'w') as f:
        f.write(template)
    configurator = Configurator(
        template='bobtemplates.plone:portlet',
        target_directory='collective.sample',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'portlet_name': 'MyWeather',
            'portlet_name_normalized': 'my_weather',
            'portlet_configuration_name':
            'collective.sample.portlets.MyWeather',  # NOQA: E501
            'data_provider_class_name': 'IMyWeatherPortlet',
            'package_folder': package_path,
            'package.dottedname': 'collective.sample',
        },
    )
    portlet._update_portlets_configure_zcml(configurator)

    with open(os.path.join(portlets_path + 'configure.zcml'), 'r') as f:
        content = f.read()
        if content != template:
            pytest.raises(ValidationError)
Ejemplo n.º 15
0
def test_get_template_name():
    question = Question(name='view_name', question='', default=None)
    configurator = Configurator(
        template='bobtemplates.plone:view',
        target_directory='collective.foo.bar',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'view_template': True,
            'view_name': 'my-view',
        },
    )
    view.get_template_name_default(configurator, question)
Ejemplo n.º 16
0
def test_view_template_and_python_class_false():
    configurator = Configurator(
        template='bobtemplates.plone:view',
        target_directory='collective.foo.bar',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'view_python_class': False,
            'view_template': False,
        },
    )
    with pytest.raises(ValidationError):
        view.check_view_template_answer(configurator, None)
Ejemplo n.º 17
0
def generate_entities_doc(project, out_path, transform_package):

    if not out_path:
        if project.is_valid:
            out_path = project.root_dir
        else:
            out_path = os.getcwd()

    jinja2_env.filters['entity_properties'] = \
        lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and
                            not hasattr(Entity, p)])

    def get_property_type(v):
        if isinstance(v, IntegerEntityField):
            return 'int'
        elif isinstance(v, FloatEntityField):
            return 'float'
        elif isinstance(v, BooleanEntityField):
            return 'bool'
        elif isinstance(v, TimeSpanEntityField):
            return 'timedelta'
        elif isinstance(v, DateTimeEntityField):
            return 'datetime'
        elif isinstance(v, DateEntityField):
            return 'date'
        elif isinstance(v, LongEntityField):
            return 'long'
        else:
            return 'str'

    jinja2_env.filters['get_property_type'] = get_property_type

    entity_module = 'canari.maltego.entities' if transform_package.name == 'canari' \
        else '%s.transforms.common.entities' % transform_package.name

    variables = {
        'transform.module':
        entity_module,
        'transform.entities':
        transform_package.entities,
        'transform.author':
        '%s <%s>' % (transform_package.author, transform_package.author_email)
    }

    configurator = Configurator(
        'canari.resources.templates:generate_entities_doc',
        out_path, {'non_interactive': True},
        variables=variables)

    configurator.ask_questions()

    click.echo('Creating entities.rst documentation for %r...' %
               transform_package.name,
               err=True)
    configurator.render()

    click.echo('done!', err=True)
Ejemplo n.º 18
0
def test_post_renderer(tmpdir):
    base_path = tmpdir.strpath
    package_root_folder = os.path.join(
        base_path,
        'collective.foo',
    )
    package_path = init_package_base_structure(package_root_folder)
    configurator = Configurator(
        template='bobtemplates.plone:indexer',
        bobconfig={"non_interactive": True},
        target_directory=package_path,
        variables={
            'package.root_folder': package_root_folder,
            "indexer_name": "my_cool_index",
            "package_folder": package_path,
        },
    )
    # os.makedirs(target_path)

    template = """
    <configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:plone="http://namespaces.plone.org/plone">

    <!-- -*- extra stuff goes here -*- -->

    </configure>
"""
    with open(os.path.join(package_path + '/configure.zcml'), 'w') as f:
        f.write(template)

    os.chdir(package_path)
    base.set_global_vars(configurator)
    configurator.render()
Ejemplo n.º 19
0
    def configure(self, install_prefix, load=True, remote=False, defaults=False, **kwargs):
        if load:
            dst = os.path.join(install_prefix, 'canari.conf')
            if os.path.lexists(dst) and not defaults and \
                    parse_bool('%s already exists. Would you like to overwrite it?' % dst, default=False):
                print 'Writing fresh copy of canari.conf to %r...' % dst
                variables = {
                    'canari.command': ' '.join(sys.argv),
                    'profile.config': self.config_file if self.name != 'canari' else '',
                    'profile.path': '${PATH},/usr/local/bin,/opt/local/bin' if os.name == 'posix' else ''
                }

                configurator = Configurator('canari.resources.templates:create_profile',
                                            install_prefix,
                                            {'non_interactive': True, 'remember_answers': True},
                                            variables=variables)
                configurator.render()
                return

        if self._package_name != 'canari':
            if load:
                package_config = resource_filename(self.get_resource_module('etc'), self.config_file)
                self._write_config(package_config, os.path.join(install_prefix, self.config_file))
            self._update_config(os.path.join(install_prefix, 'canari.conf'), load, remote)
Ejemplo n.º 20
0
def test_post_renderer(tmpdir):
    """Test post rendering."""
    package_root = tmpdir.strpath + "/collective.todo"
    package_path = init_package_base_structure(package_root)

    configurator = Configurator(
        template="bobtemplates.plone:view",
        target_directory=package_path,
        bobconfig={"non_interactive": True},
        variables={
            "view_name": "my-new-view",
            "view_python_class": True,
            "view_python_class_name": "NewView",
            "view_base_class": "BrowserView",
            "view_template": True,
            "view_template_name": "new_view",
            "plone.version": "5.1",
        },
    )

    assert configurator
    os.chdir(package_path)
    base.set_global_vars(configurator)
    configurator.render()  # pre/render/post
Ejemplo n.º 21
0
def test_pre_theme_name(tmpdir):
    base_path = tmpdir.strpath
    configurator = Configurator(
        template='bobtemplates.plone:theme',
        target_directory=os.path.join(
            base_path,
            'collective.foo',
        ),
    )
    question = Question(
        'package',
        'type',
    )
    theme.pre_theme_name(configurator, question)
    theme.pre_theme_name(configurator, question)
def test_check_git_disabled_true(tmpdir):
    configurator = Configurator(
        template="plonecli:configure_mrbob",
        target_directory=tmpdir.strpath,
        bobconfig={"non_interactive": True},
        variables={
            "configure_mrbob.author.name": "The Plone Collective",
            "configure_mrbob.author.email": "*****@*****.**",
            "configure_mrbob.author.github.user": "******",
            "configure_mrbob.package.venv.disabled": False,
            "configure_mrbob.package.git.disabled": True,
        },
    )
    with pytest.raises(SkipQuestion):
        check_git_disabled(configurator, None)
def test_plonecli_config_with_config_file(tmpdir):
    template = """[mr.bob]
verbose = False

[variables]
# all variables answered here, will not being ask again:

author.name = The Plone Collective
author.email = [email protected]
author.github.user = collective
package.venv.disabled = n

[defaults]
# set your default values for questions here, they questions are still being ask
# but with your defaults:

plone.version = 5.1
#dexterity_type_global_allow = n
#dexterity_type_filter_content_types = y
#dexterity_type_activate_default_behaviors = n
#dexterity_type_supermodel = y
python.version = python3
"""
    with open(os.path.join(tmpdir.strpath, ".mrbob"), "w") as f:
        f.write(template)

    configurator = Configurator(
        template="plonecli:configure_mrbob",
        target_directory=tmpdir.strpath,
        bobconfig={"non_interactive": True},
        variables={
            "configure_mrbob.author.name": "XYZ",
            "configure_mrbob.author.email": "*****@*****.**",
            "configure_mrbob.author.github.user": "******",
            "configure_mrbob.package.venv.disabled": "n",
            "configure_mrbob.package.git.init": "y",
            "configure_mrbob.package.git.autocommit": "y",
            "configure_mrbob.package.git.disabled": "y",
            "configure_mrbob.plone.version": "5.1",
        },
    )
    post_render(configurator, target_directory=tmpdir.strpath)
    with open(os.path.join(tmpdir.strpath, ".mrbob"), "r") as f:
        content = f.read()
        assert "XYZ" in content
        assert "author.email = [email protected]" in content
        assert ("""plone.version = 5.1
#dexterity_type_global_allow = n""" in content)
Ejemplo n.º 24
0
def test_check_git_disabled_true(tmpdir):
    configurator = Configurator(
        template='plonecli:configure_mrbob',
        target_directory=tmpdir.strpath,
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'configure_mrbob.author.name': 'The Plone Collective',
            'configure_mrbob.author.email': '*****@*****.**',
            'configure_mrbob.author.github.user': '******',
            'configure_mrbob.package.git.disabled': True,
        },
    )
    with pytest.raises(SkipQuestion):
        check_git_disabled(configurator, None)
def test_prepare_renderer(tmpdir):
    package_root = tmpdir.strpath + "/collective.todo"
    package_path = init_package_base_structure(package_root)

    configurator = Configurator(
        template="bobtemplates.plone:theme_barceloneta",
        target_directory=package_path,
        variables={
            "theme.name": "test.theme",
        },
    )
    theme_barceloneta.prepare_renderer(configurator)

    assert configurator.variables["template_id"] == "theme_barceloneta"
    assert configurator.variables["theme.normalized_name"] == "test.theme"
    assert configurator.target_directory.endswith(
        "collective.todo")  # NOQA: E501
def test_check_git_disabled_false(tmpdir):
    configurator = Configurator(
        template="plonecli:configure_mrbob",
        target_directory=tmpdir.strpath,
        bobconfig={"non_interactive": True},
        variables={
            "configure_mrbob.author.name": "The Plone Collective",
            "configure_mrbob.author.email": "*****@*****.**",
            "configure_mrbob.author.github.user": "******",
            "configure_mrbob.package.venv.disabled": False,
            "configure_mrbob.package.git.disabled": False,
            "configure_mrbob.package.git.init": True,
            "configure_mrbob.package.git.autocommit": True,
            "configure_mrbob.plone.version": "5.1",
        },
    )
    check_git_disabled(configurator, None)
Ejemplo n.º 27
0
def test_check_git_disabled_false(tmpdir):
    configurator = Configurator(
        template='plonecli:configure_mrbob',
        target_directory=tmpdir.strpath,
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'configure_mrbob.author.name': 'The Plone Collective',
            'configure_mrbob.author.email': '*****@*****.**',
            'configure_mrbob.author.github.user': '******',
            'configure_mrbob.package.git.disabled': False,
            'configure_mrbob.package.git.init': True,
            'configure_mrbob.package.git.autocommit': True,
            'configure_mrbob.plone.version': '5.1',
        },
    )
    check_git_disabled(configurator, None)
Ejemplo n.º 28
0
    def config_dir(self):
        if not os.path.lexists(self._config_dir):
            click.echo("Initializing Canari configuration: %s" %
                       self._config_dir,
                       err=True)

            configurator = Configurator(
                'canari.resources.templates:init_canari', self._config_dir,
                {'non_interactive': True})

            configurator.ask_questions()
            configurator.render()
        return self._config_dir
Ejemplo n.º 29
0
def test_validate_packagename():
    # step 1: test None
    with pytest.raises(AttributeError):
        base.validate_packagename(None)

    # step 2: test base namespace (level 2)
    configurator = Configurator(
        template='bobtemplates.plone:addon',
        target_directory='collective.foo',
    )
    base.validate_packagename(configurator)

    # step 3: test without namespace (level 1)
    with pytest.raises(SystemExit):
        configurator = Configurator(
            template='bobtemplates.plone:addon',
            target_directory='foo',
        )
        base.validate_packagename(configurator)

    # step 4: test deep nested namespace (level 4)
    with pytest.raises(SystemExit):
        configurator = Configurator(
            template='bobtemplates.plone:addon',
            target_directory='collective.foo.bar.spam',
        )
        base.validate_packagename(configurator)

    # step 5: test leading dot
    with pytest.raises(SystemExit):
        configurator = Configurator(
            template='bobtemplates.plone:addon',
            target_directory='.collective.foo',
        )
        base.validate_packagename(configurator)

    # step 6: test ending dot
    with pytest.raises(SystemExit):
        configurator = Configurator(
            template='bobtemplates.plone:addon',
            target_directory='collective.foo.',
        )
        base.validate_packagename(configurator)

    # step 7: test invalid char
    with pytest.raises(SystemExit):
        configurator = Configurator(
            template='bobtemplates.plone:addon',
            target_directory='collective.$PAM',
        )
        base.validate_packagename(configurator)
def test_prepare_renderer(tmpdir):
    base_path = tmpdir.strpath
    package_root_folder = os.path.join(base_path, "collective.theme")
    configurator = Configurator(
        template="bobtemplates.plone:theme_barceloneta",
        target_directory=os.path.join(package_root_folder,
                                      "src/collective/theme"),
        variables={
            "theme.name": "test.theme",
            "package.root_folder": package_root_folder,
        },
    )
    init_package_base_files(configurator)
    theme_barceloneta.prepare_renderer(configurator)

    assert configurator.variables["template_id"] == "theme_barceloneta"
    assert configurator.variables["theme.normalized_name"] == "test.theme"
    assert configurator.target_directory.endswith(
        "collective.theme")  # NOQA: E501
Ejemplo n.º 31
0
def test_update_configure_zcml_with_changes(tmpdir):
    """Test configure changes when changes are already in place."""
    target_path = tmpdir.strpath + '/collective.demo'
    package_path = target_path + '/src/collective/demo'
    os.makedirs(target_path)
    os.makedirs(package_path)
    template = """<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="collective.demo">

  <i18n:registerTranslations directory="locales" />

  <!--<includeDependencies package="." />-->

  <include package=".portlets" />

  <include package=".browser" />

</configure>
"""
    with open(os.path.join(package_path + '/configure.zcml'), 'w') as f:
        f.write(template)
    configurator = Configurator(
        template='bobtemplates.plone:portlet',
        target_directory='collective.demo',
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'package_folder': package_path,
        },
    )
    portlet._update_configure_zcml(configurator)

    with open(os.path.join(package_path + '/configure.zcml'), 'r') as f:
        content = f.read()
        if content != template:
            pytest.raises(ValidationError)
Ejemplo n.º 32
0
def test_update_subscribers_configure_zcml(tmpdir):
    """Test configure changes when changes are already in place."""
    target_path = tmpdir.strpath + "/collective.sample"
    package_path = target_path + "/src/collective/sample"
    subscribers_path = package_path + "/subscribers/"
    os.makedirs(target_path)
    os.makedirs(package_path)
    os.makedirs(subscribers_path)
    template = """<configure
    xmlns="http://namespaces.zope.org/zope"
    i18n_domain="{{{ package.dottedname }}}">

  -*- extra stuff goes here -*-

  <subscriber for="plone.dexterity.interfaces.IDexterityContent
                   zope.lifecycleevent.interfaces.IObjectModifiedEvent"
              handler=".obj_mod_do_something.handler"
              />

</configure>
"""
    with open(os.path.join(subscribers_path + "configure.zcml"), "w") as f:
        f.write(template)
    configurator = Configurator(
        template="bobtemplates.plone:subscriber",
        target_directory="collective.sample",
        bobconfig={"non_interactive": True},
        variables={
            "subscriber_handler_name": "obj_mod_do_something",
            "subscriber_handler_file_name": "obj_mod_do_something",
            "package_folder": package_path,
        },
    )
    subscriber._update_subscribers_configure_zcml(configurator)

    with open(os.path.join(subscribers_path + "configure.zcml"), "r") as f:
        content = f.read()
        if content != template:
            pytest.raises(ValidationError)
Ejemplo n.º 33
0
def install_wizard():
    configurator = Configurator('canari.resources.templates:install_plume', '.',
                                {'non_interactive': False, 'remember_answers': False})
    configurator.ask_questions()

    if os.environ.get('VIRTUAL_ENV'):
        run_venv = click.prompt(
            "--> Canari has detected that you're running this install script from within a virtualenv.\n"
            "--> Would you like to run Plume from this virtualenv (%r) as well?" % os.environ['VIRTUAL_ENV'],
            default=True
        )
        configurator.variables['plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False
    else:
        configurator.variables['plume.venv'] = None

    configurator.render()
    finish(configurator)
Ejemplo n.º 34
0
def test_write_dest_version(tmpdir):
    package_root = tmpdir.strpath + "/collective.todo"
    package_path = init_package_base_structure(package_root)
    target_path = os.path.join(package_path, 'profiles/default')

    template = """<?xml version='1.0' encoding='UTF-8'?>
<metadata>
  <version>1004</version>
  <dependencies>
    <dependency>profile-plone.app.dexterity:default</dependency>
  </dependencies>
</metadata>
"""
    with open(os.path.join(target_path, 'metadata.xml'), 'w') as f:
        f.write(template)
    configurator = Configurator(
        template='bobtemplates.plone:upgrade_step',
        target_directory=package_path,
        bobconfig={
            'non_interactive': True,
        },
        variables={
            'plone.version':
            '5.1',
            'package_folder':
            package_path,
            'upgrade_step_title':
            'Add cool index and reindex it',
            'upgrade_step_dest_version':
            1005,
            'upgrade_step_description':
            'We add an index and reindex it with existing content.',
        },
    )
    assert configurator
    upgrade_step._write_dest_version(configurator)
    assert upgrade_step._read_source_version(configurator) == 1005
def test_prepare_renderer(tmpdir):
    base_path = tmpdir.strpath
    package_root_folder = os.path.join(
        base_path,
        'collective.foo',
    )
    configurator = Configurator(
        template='bobtemplates.plone:theme_barceloneta',
        target_directory=os.path.join(
            package_root_folder,
            'src/collective/foo',
        ),
        variables={
            'theme.name': 'test.theme',
            'package.root_folder': package_root_folder,
        },
    )
    init_package_base_files(configurator)
    theme_barceloneta.prepare_renderer(configurator)

    assert configurator.variables['template_id'] == 'theme_barceloneta'
    assert configurator.variables['theme.normalized_name'] == 'test.theme'
    assert configurator.target_directory.endswith(
        '/collective.foo/src/collective/foo')  # NOQA: E501
Ejemplo n.º 36
0
def test_cleanup_package(tmpdir):
    target_path = tmpdir.strpath + '/collective.theme'
    configurator = Configurator(
        template='bobtemplates.plone:theme_package',
        target_directory=target_path,
        variables={
            'package.nested': False,
            'package.namespace': 'collective',
            'package.namespace2': '',
            'package.name': 'theme',
            'package.type': 'theme',
            'year': 1970,
            'description': 'Test',
            'author.name': 'The Plone Collective',
            'author.email': '*****@*****.**',
            'author.github.user': '******',
            'author.irc': 'irc.freenode.org#plone',
            'plone.version': '5.0.1',
            'plone.is_plone5': True,
        },
    )
    # configurator.render()
    # hooks.cleanup_package(configurator)
    assert configurator
Ejemplo n.º 37
0
def generate_entities(args):
    opts = parse_args(args)

    mtz = MtzDistribution(opts.mtz_file)
    target = opts.out_path

    variables = opts.project.configuration['variables']

    entity_definitions = {}

    matcher = re.compile('(.+)\.([^\.]+)$')

    for entity_file in mtz.entities:
        entity = MaltegoEntity.parse(mtz.read_file(entity_file))
        namespace, name = matcher.match(entity.id).groups()
        if namespace in opts.exclude_namespace:
            continue
        elif not opts.namespace or namespace in opts.namespace:
            entity_definitions[(namespace, name)] = entity

    entity_classes = []

    if opts.append:
        module = opts.project.entities_module
        for entity_class in dir(module):
            entity_class = getattr(module, entity_class)
            if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \
                    and (entity_class._namespace_, entity_class.__name__) not in entity_definitions:
                entity_classes.append(entity_class)

    def get_entity_field_class(v):
        if v == 'int':
            return 'IntegerEntityField'
        elif v == 'float':
            return 'FloatEntityField'
        elif v == 'boolean':
            return 'BooleanEntityField'
        elif v == 'timespan':
            return 'TimeSpanEntityField'
        elif v == 'datetime':
            return 'DateTimeEntityField'
        elif v == 'date':
            return 'DateEntityField'
        elif v == 'long':
            return 'LongEntityField'
        else:
            return 'StringEntityField'

    def get_property_name(v):
        v = v.replace('.', '_').replace('-', '_')
        return '%s_' % v if keyword.iskeyword(v) else v

    jinja2_env.filters['entity_properties'] = \
        lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and
                            not hasattr(Entity, p)])

    jinja2_env.filters['get_entity_field_class'] = get_entity_field_class

    jinja2_env.filters['get_property_name'] = get_property_name

    variables.update({
        'entity.definitions': entity_definitions,
        'entity.classes': entity_classes
    })

    configurator = Configurator(
            'canari.resources.templates:generate_entities',
            target,
            {'non_interactive': True},
            variables=variables
    )

    configurator.ask_questions()

    print('Generating entities for %r...' % variables['project.name'])
    configurator.render()

    print('done!')