def create_package(package_name, author, email, description, create_example): capitalized_package_name = package_name.capitalize() variables = { 'project.name': package_name, 'project.description': description, 'project.create_example': create_example, 'entity.example_name': 'My%sEntity' % capitalized_package_name, 'entity.base_name': '%sEntity' % capitalized_package_name, 'created.year': datetime.now().year, 'canari.version': canari.version, 'canari.major_version': StrictVersion(canari.version).version[0], 'author.name': author, 'author.email': email } if not path.exists(package_name): click.echo('creating skeleton in %s' % package_name, err=True) configurator = Configurator( 'canari.resources.templates:create_package%s' % ('' if create_example else '_no_example'), package_name, {'non_interactive': True, 'remember_answers': True}, variables=variables ) configurator.ask_questions() configurator.render() else: click.echo('A directory with the name %s already exists... exiting' % package_name, err=True) exit(-1) click.echo('done!', err=True)
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)
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( u'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!')
def create_transform(project, module_name): transform_name = stringcase.pascalcase(module_name) module_name = module_name.lower() target = project.root_dir transform_directory = project.transforms_dir if os.path.exists(os.path.join(transform_directory, '%s.py' % module_name)): click.echo('Transform %r already exists... quitting' % module_name, err=True) exit(-1) variables = parse_config(os.path.join(target, '.mrbob.ini'))['variables'] variables.update({ 'transform.module': module_name, 'transform.name': transform_name }) configurator = Configurator('canari.resources.templates:create_transform', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() click.echo('Creating transform %r...' % module_name, err=True) configurator.render() click.echo('done!', err=True)
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!'
def test_delete_unwanted_files_python(tmpdir): package_root = tmpdir.strpath + "/collective.todo" package_path = init_package_base_structure(package_root) views_path = os.path.join(package_path + "/views/") 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_template": False, "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 # as the post_rederer also calls delete_unwanted_files. we don't need to call here python_file_name = configurator.variables.get( 'view_python_file_name') + '.py' template_file_name = configurator.variables.get( 'view_template_name') + '.pt' python_file_path = os.path.join(views_path + python_file_name) template_file_path = os.path.join(views_path + template_file_name) assert not os.path.isfile(template_file_path) assert os.path.isfile(python_file_path)
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!")
def test_post_renderer(tmpdir): target_path = tmpdir.strpath + '/collective.foo' package_path = target_path + '/src/collective/foo' os.makedirs(target_path) os.makedirs(package_path) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) 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) configurator = Configurator( template='bobtemplates.cs:cs_migration', target_directory=package_path, bobconfig={'non_interactive': True}, ) migration.prepare_renderer(configurator) configurator.render() migration.post_renderer(configurator)
def install_defaults(): 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']: click.echo( 'Will use the virtual environment in %r to run Plume...' % configurator.variables['plume.venv'], err=True ) configurator.variables['plume.enable_ssl'] = 'n' click.echo('Installing init script to /etc/init.d...', err=True) configurator.variables['plume.init'] = check_init_script(configurator, '', '/etc/init.d') click.echo('Creating Plume root directory at /var/plume...', err=True) configurator.variables['plume.dir'] = check_mkdir(configurator, '', '/var/plume') click.echo('The PID file will be at /var/run/plume.pid...', err=True) configurator.variables['plume.run_dir'] = '/var/run' click.echo('The log files will be at /var/log/plume.log...', err=True) configurator.variables['plume.log_dir'] = '/var/log' configurator.variables['plume.user'] = check_uid(configurator, '', 'nobody') configurator.variables['plume.group'] = check_gid(configurator, '', 'nobody') click.echo('The Plume server will under UID/GID=%s/%s...' % ( configurator.variables['plume.user'], configurator.variables['plume.group']), err=True) click.echo('TLS will be disabled by default...', err=True) configurator.variables['plume.certificate'] = '' configurator.variables['plume.private_key'] = '' configurator.ask_questions() configurator.render() finish(configurator)
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)
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)
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
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!')
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)
def test_post_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, bobconfig={"non_interactive": True}, variables={ "plone.version": "5.1", "theme.name": "My Theme" }, ) assert configurator os.chdir(package_path) base.set_global_vars(configurator) configurator.render() # pre/render/post
def test_cleanup_package(tmpdir): target_path = tmpdir.strpath + '/collective.foo.bar' package_path = target_path + '/src/collective/foo/bar' profiles_path = package_path + '/profiles/default' os.makedirs(target_path) os.makedirs(package_path) os.makedirs(profiles_path) template = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <version>1000</version> <dependencies> </dependencies> </metadata> """ with open(os.path.join(profiles_path + '/metadata.xml'), 'w') as f: f.write(template) configurator = Configurator( template='bobtemplates.plone:addon', target_directory=target_path, bobconfig={ 'non_interactive': True, }, variables={ 'package.nested': True, 'package.namespace': 'collective', 'package.namespace2': 'foo', 'package.name': 'bar', 'year': 1970, 'package.git.init': True, 'package.description': 'Test', 'author.name': 'The Plone Collective', 'author.email': '*****@*****.**', 'author.github.user': '******', 'plone.version': '5.1', 'python.version': 'python2.7', 'plone.is_plone5': True, 'vscode_support': True, }, ) assert configurator base.set_global_vars(configurator) configurator.render()
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(u'canari.resources.templates:create_transform', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() print('Creating transform %r...' % transform_module) configurator.render() print('done!')
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( u'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)
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!')
def configure(self, install_prefix, load=True, remote=False, defaults=False, **kwargs): dst_canari_conf = os.path.join(install_prefix, 'canari.conf') if load and self._check_file_exists(dst_canari_conf, defaults): print('Writing fresh copy of canari.conf to %r...' % dst_canari_conf, file=sys.stderr) 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() if self._package_name != 'canari': dst_package_conf = os.path.join(install_prefix, self.config_file) if load: print('Copying %r to %r...' % (self.config_file, dst_package_conf)) package_config = resource_filename( self.get_resource_module('etc'), self.config_file) self._write_config(package_config, dst_package_conf, defaults) self._update_config(dst_canari_conf, load, remote, **kwargs)
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
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()
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)
def test_post_renderer(tmpdir): target_path = tmpdir.strpath + '/collective.todo' package_path = target_path + '/src/collective/todo' profiles_path = package_path + '/profiles/default' os.makedirs(target_path) os.makedirs(package_path) os.makedirs(profiles_path) template = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <version>1000</version> <dependencies> </dependencies> </metadata> """ with open(os.path.join(profiles_path + '/metadata.xml'), 'w') as f: f.write(template) template = """ [main] version=5.1 """ with open(os.path.join(target_path + '/bobtemplate.cfg'), 'w') as f: f.write(template) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) 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) configurator = Configurator( template='bobtemplates.plone:restapi_service', target_directory=package_path, bobconfig={ 'non_interactive': True, }, variables={ 'package_folder': package_path, 'plone.version': '5.1', 'service_class_name': 'SomeRelatedThings', 'service_name': 'some-related-things', }, ) assert configurator os.chdir(package_path) base.set_global_vars(configurator) restapi_service.pre_renderer(configurator) configurator.render() restapi_service.post_renderer(configurator)
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) if not args.host and os.path.exists('/var/run/docker.sock'): args.host = ['unix:///var/run/docker.sock'] 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.get('DOCKER_HOST', 'http://0.0.0.0'))[0], port) run_command(['docker'] + docker_hosts + ['run', '-it', '-p', '8080:%s' % port, container]).communicate() print 'done!'
def dockerize_package(project, os_, host): if sys.version_info[0] > 2: os_ += '-py3' click.echo('Dockerizing %s transform package...' % project.name, err=True) configurator = Configurator('canari.resources.templates:dockerize_package', project.root_dir, {'non_interactive': True}, variables={ 'project.name': project.name, 'canari.version': version }) click.echo('Creating Dockerfile for %s...' % project.name, err=True) configurator.render() click.echo('done!', err=True) if not find_executable('docker'): click.echo( "Could not find 'docker' in your system path. Please download and install Docker from " "http://docker.com and rerun this command again.", err=True) exit(-1) if not host: if os.name == 'nt': host = [''] elif os.path.exists('/var/run/docker.sock'): host = ['unix:///var/run/docker.sock'] docker_hosts = [ j for sublist in [('-H', i) for i in host] for j in sublist ] container = '%s/%s:%s' % (project.name, project.name, os_) if not host: if not find_executable('docker-machine'): click.echo( "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.", err=True) exit(-1) click.echo('Attempting to discover available Docker machines.', err=True) machines = get_output( run_command(['docker-machine', 'ls', '-q'], stdout=subprocess.PIPE)).split('\n') machines.remove('') if not machines: click.echo('No machines found :(\nExiting...', err=True) exit(-1) machine = prompt_menu( 'More than one Docker machine was detected. Which one would you like to use to' 'build and run this container?', machines) click.echo('Setting up environment for Docker machine %s' % machines[machine], err=True) # Inject docker environment variables env = get_output( run_command(['docker-machine', 'env', machines[machine]], stdout=subprocess.PIPE)) 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' % os_, '.']) p.communicate() if p.returncode: click.echo( 'An error occurred while building the Docker container.', err=True) exit(-1) if click.confirm('Would you like to run this container now?', default=False): port = click.prompt( 'Which port would you like Plume to listen on externally?', default=8080, type=click.IntRange(8080, 65535)) click.echo('Plume will be listening on http://%s:%s' % (re.findall( '://([^:]+)', os.environ.get('DOCKER_HOST', 'http://0.0.0.0'))[0], port), err=True) run_command(['docker'] + docker_hosts + ['run', '-it', '-p', '8080:%s' % port, container]).communicate() click.echo('done!', err=True)
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'], file=sys.stderr) configurator.render() print('done!', file=sys.stderr)
def generate_entities(project, output_path, mtz_file, exclude_namespace, namespace, maltego_entities, append, entity): if not output_path: if project.is_valid: output_path = project.common_dir else: output_path = os.getcwd() entities_py = os.path.join(output_path, 'entities.py') if os.path.exists(entities_py) and not append: click.confirm( '{!r} already exists. Are you sure you want to overwrite it?'. format(entities_py), default=False, abort=True) if maltego_entities: namespace.extend(exclude_namespace) exclude_namespace = [] mtz = MtzDistribution(mtz_file) target = output_path variables = 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 exclude_namespace: continue elif not namespace or namespace in namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if append: module = 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() click.echo('Generating entities for %r...' % variables['project.name'], err=True) configurator.render() click.echo('done!', err=True)
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!')
def test_post_renderer(tmpdir): base_path = tmpdir.strpath target_path = os.path.join( base_path, 'collective.theme', ) package_path = os.path.join( target_path, u'src/collective/theme', ) profiles_path = os.path.join( package_path, u'profiles/default', ) theme_path = os.path.join( package_path, u'theme', ) os.makedirs(package_path) os.makedirs(profiles_path) os.makedirs(theme_path) template = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <version>1000</version> <dependencies> </dependencies> </metadata> """ with open(os.path.join(profiles_path + '/metadata.xml'), 'w') as f: f.write(template) template = """ [main] version=5.1 """ with open(os.path.join(target_path + '/bobtemplate.cfg'), 'w') as f: f.write(template) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) 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) configurator = Configurator( template='bobtemplates.plone:theme_barceloneta', target_directory=package_path, bobconfig={ 'non_interactive': True, }, variables={ 'plone.version': '5.1', 'theme.name': 'My Theme', }, ) assert configurator os.chdir(package_path) base.set_global_vars(configurator) theme_barceloneta.prepare_renderer(configurator) configurator.render() theme_barceloneta.post_renderer(configurator)
def test_post_renderer(tmpdir): target_path = tmpdir.strpath + '/collective.todo' package_path = target_path + '/src/collective/todo' profiles_path = package_path + '/profiles/default' os.makedirs(target_path) os.makedirs(package_path) os.makedirs(profiles_path) template = """<?xml version="1.0" encoding="UTF-8"?> <metadata> <version>1000</version> <dependencies> </dependencies> </metadata> """ with open(os.path.join(profiles_path + '/metadata.xml'), 'w') as f: f.write(template) template = """ [main] version=5.1 """ with open(os.path.join(target_path + '/bobtemplate.cfg'), 'w') as f: f.write(template) template = """ dummy '-*- Extra requirements: -*-' """ with open(os.path.join(target_path + '/setup.py'), 'w') as f: f.write(template) 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) configurator = Configurator( template='bobtemplates.plone:upgrade_step', target_directory=package_path, bobconfig={ 'non_interactive': True, }, variables={ 'plone.version': '5.1', 'upgrade_step_title': 'Add cool index and reindex it', 'upgrade_step_description': 'We add an index and reindex it with existing content.', }, ) assert configurator os.chdir(package_path) base.set_global_vars(configurator) configurator.render()