def run(parser, options, args): """launch an object In: - ``parser`` -- the ``optparse.OptParser`` object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines The unique argument is the path of the object to launch. The path syntax is described into the module ``nagare.admin.reference``. For example, ``/tmp/counter.py:Counter`` is the path to the class ``Counter`` of the module ``tmp.counter.py`` """ if len(args) != 2: parser.error('Bad number of arguments') if 'nagare_reloaded' not in os.environ: return reloader.restart_with_monitor() # With the ``serve-module`` command, the automatic reloader is always activated reloader.install(excluded_directories=(pkg_resources.get_default_cache(),)) # Load the object if os.path.sep in args[0]: path = 'file ' + args[0] else: path = 'python ' + args[0] app = reference.load_object(path)[0] # Wrap it into a WSGIApp app = wsgi.create_WSGIApp(app) # Always use the standalone publisher (Python HTTP server) publishers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.publishers')]) publisher = publishers['standalone'].load()() wsgi_pipe = debugged_app(app) if options.debug else app publisher.register_application(args[0], args[1], app, wsgi_pipe) app.set_config('', {'application': {'redirect_after_post': False, 'name': args[1], 'always_html': True}}, None) app.set_publisher(publisher) # Always use the standalone session manager (in memory sessions) sessions_managers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.sessions')]) sessions_manager = sessions_managers['standalone'].load()() app.set_sessions_manager(sessions_manager) # Set the application logger level to DEBUG log.configure({'logger': {'level': 'DEBUG'}}, args[1]) log.activate() log.set_logger('nagare.application.' + args[1]) # The static contents of the framework are served by the standalone server publisher.register_static('nagare', lambda path, r=pkg_resources.Requirement.parse('nagare'): get_file_from_package(r, path)) # Launch the object publisher.serve(None, dict(host=options.host, port=options.port), None)
def load_default(): #return load_siz_file(os.path.join(path, default_file)) return load_siz_file( os.path.join( pkg_resources.get_default_cache(), pkg_resources.resource_filename( "mmstructlib.radii.data", default_file ) ) )
def resource_filename(self, strFallBackFolder, strResourcePath): #Try to get the resource from an egg. Fallback to getting from the file system strFileName = "" pathDefault = pkg_resources.get_default_cache() #Ensure that folder exists. If not make it if not os.path.exists(pathDefault): os.makedirs(pathDefault, 0744) #print "Created pathDefault %s" % pathDefault #Check if resource already has been extracted to temp folder. #Use the extracted resource if it exists. bolFound = False for (path, dirs, files) in os.walk(pathDefault): for name in files: if name == strResourcePath and bolFound != True: strFileName = os.path.join(path, name) bolFound = True #Extract resource (from egg) if bolFound != True: try: msgu = pkg_resources.Requirement.parse(self.__package_name) entries = pkg_resources.resource_listdir(msgu, strFallBackFolder) for entry in entries: #print "__egg_resource_filename resource entry %s" % entry if entry == strResourcePath: #resource found. Extract to pathDefault strFileName = pkg_resources.resource_filename(msgu, os.path.join(strFallBackFolder, entry) ) bolFound = True except pkg_resources.ExtractionError: pass else: pass if not os.path.exists(strFileName): #get from file system #print "__egg_resource_filename (from file system) %s %s" % (strFallBackFolder, strResourcePath) strPackageDir = self.determine_path() strFileName = os.path.join (strPackageDir, strFallBackFolder, strResourcePath) return strFileName
def run(parser, options, args): """Launch one or more applications In: - ``parser`` -- the ``optparse.OptParser`` object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines The arguments are a list of names of registered applications or paths to application configuration files. """ # If no argument are given, display the list of the registered applications if not args: print 'Available applications:' # The applications are registered under the ``nagare.applications`` entry point applications = pkg_resources.iter_entry_points('nagare.applications') for name in sorted(application.name for application in applications): print ' -', name return # Read the (optional) publisher configuration file pconf = read_publisher_options(parser, options) if pconf['reloader']['activated']: if 'nagare_reloaded' not in os.environ: return reloader.restart_with_monitor() filenames = pconf['reloader'].get('files', []) if isinstance(filenames, basestring): filenames = [filenames] filenames = filter(os.path.isfile, filenames) if options.conf: filenames.append(options.conf) watcher = reloader.install(pconf['reloader']['interval'], filenames, (pkg_resources.get_default_cache(),)) else: watcher = None # Load the publisher publishers = {entry.name: entry for entry in pkg_resources.iter_entry_points('nagare.publishers')} t = pconf['publisher']['type'] publisher = publishers[t].load()() # If no port is given, set the port number according to the used publisher if pconf['publisher']['port'] == -1: pconf['publisher']['port'] = publisher.default_port configs = [] # Merge all the ``[logging]`` section of all the applications for cfgfile in args: # Read the configuration file of the application conffile, app, project_name, aconf = util.read_application(cfgfile, parser.error) if conffile is None: parser.error('Configuration file not found for application "%s"' % cfgfile) configs.append((conffile, app, project_name, aconf)) log.configure(aconf['logging'].dict(), aconf['application']['app']) # Configure the logging service log.activate() # Configure each application and register it to the publisher for cfgfile, app, project_name, aconf in configs: # log.set_logger('nagare.application.'+aconf['application']['name']) if watcher: watcher.watch_file(aconf.filename) app_url = aconf['application']['name'] static_path = aconf['application']['static'] if os.path.isdir(static_path): # Register the function to serve the static contents of the application static_url = publisher.register_static( app_url, lambda path, static_path=static_path: get_file_from_root(static_path, path) ) else: static_path = static_url = None data_path = aconf['application']['data'] if not os.path.isdir(data_path): data_path = None # Load the sessions manager factory sessions_managers = {entry.name: entry for entry in pkg_resources.iter_entry_points('nagare.sessions')} conf = pconf['sessions'].dict() conf.update(aconf['sessions'].dict()) t = conf.pop('type') sessions_manager = sessions_managers[t].load()() sessions_manager.set_config(options.conf, conf, parser.error) app, metadatas = util.activate_WSGIApp( app, cfgfile, aconf, parser.error, project_name, static_path, static_url, data_path, publisher, sessions_manager ) # Register the application to the publisher publisher.register_application( aconf['application']['app'], app_url, app, create_wsgi_pipe(app, options, cfgfile, aconf, parser.error) ) # Register the function to serve the static contents of the framework nagare = pkg_resources.Requirement.parse('nagare') nagare_location = pkg_resources.resource_filename(nagare, 'nagare') publisher.register_static( 'nagare', lambda path, root=os.path.join(nagare_location, 'static'): get_file_from_root(root, path) ) # Launch all the applications publisher.serve(options.conf, pconf['publisher'], parser.error)
def cleanup(): if _pmap: raise RuntimeError packdir = os.path.join(get_default_cache(), __package_name__) remove(packdir, ignore_errors=True)
def get_dotted_filename(self, template_name, template_extension='.html'): """this helper function is designed to search a template or any other file by python module name. Given a string containing the file/template name passed to the @expose decorator we will return a resource useable as a filename even if the file is in fact inside a zipped egg or in a frozen library. The actual implementation is a revamp of the Genshi buffet support plugin, but could be used with any kind a file inside a python package. :param template_name: the string representation of the template name as it has been given by the user on his @expose decorator. Basically this will be a string in the form of: `"myapp.templates.somename"` :type template_name: str :param template_extension: the extension we excpect the template to have, this MUST be the full extension as returned by the os.path.splitext function. This means it should contain the dot. ie: '.html' This argument is optional and the default value if nothing is provided will be '.html' :type template_extension: str The ``template_name`` parameter also accepts a form with explicit extension ``myapp.templates.somename!xhtml`` that will override the ``template_exstesion`` argument and will always use ``.xhtml`` as the extension. This is usually convenient in extensions and libraries that expose a template and want to ensure they work even in the case the application using them has a different extension for templates on the same engine. """ cache_key = template_name try: return self.__cache[cache_key] except KeyError: # the template name was not found in our cache try: # Allow for the package.file!ext syntax template_name, template_extension = template_name.rsplit( '!', 1) template_extension = '.' + template_extension except ValueError: pass divider = template_name.rfind('.') if divider >= 0: package = template_name[:divider] basename = template_name[divider + 1:] resourcename = basename + template_extension try: result = resource_filename(package, resourcename) except ImportError as e: raise DottedFileLocatorError( "%s. Perhaps you have forgotten an __init__.py in that folder." % e) except NotImplementedError: # Cope with zipped files or py2exe apps if not hasattr(self, '__temp_dir'): self.__temp_dir = os.path.join( get_default_cache(), 'tgdf-%s' % uuid.uuid1()) result = os.path.join(self.__temp_dir, package, resourcename) if not os.path.isdir(os.path.dirname(result)): os.makedirs(os.path.dirname(result)) with contextlib.closing( resource_stream(package, resourcename)) as rd: with open(result, 'wb') as result_f: result_f.write(rd.read()) else: result = template_name result = os.path.abspath(result) self.__cache[cache_key] = result return result
def run(parser, options, args): """Launch one or more applications In: - ``parser`` -- the ``optparse.OptParser`` object used to parse the configuration file - ``options`` -- options in the command lines - ``args`` -- arguments in the command lines The arguments are a list of names of registered applications or paths to application configuration files. """ # If no argument are given, display the list of the registered applications if len(args) == 0: print 'Available applications:' # The applications are registered under the ``nagare.applications`` entry point applications = pkg_resources.iter_entry_points('nagare.applications') for name in sorted([application.name for application in applications]): print ' -', name return # Read the (optional) publisher configuration file pconf = read_publisher_options(parser, options) if pconf['reloader']['activated']: if 'nagare_reloaded' not in os.environ: return reloader.restart_with_monitor() filenames = pconf['reloader'].get('files', []) if isinstance(filenames, basestring): filenames = [filenames] filenames = filter(os.path.isfile, filenames) if options.conf: filenames.append(options.conf) watcher = reloader.install(pconf['reloader']['interval'], filenames, (pkg_resources.get_default_cache(),)) else: watcher = None # Load the publisher publishers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.publishers')]) t = pconf['publisher']['type'] publisher = publishers[t].load()() # If no port is given, set the port number according to the used publisher if pconf['publisher']['port'] == -1: pconf['publisher']['port'] = publisher.default_port configs = [] # Merge all the ``[logging]`` section of all the applications for cfgfile in args: # Read the configuration file of the application (cfgfile, app, dist, aconf) = util.read_application(cfgfile, parser.error) configs.append((cfgfile, app, dist, aconf)) log.configure(aconf['logging'].dict(), aconf['application']['name']) # Configure the logging service log.activate() # Configure each application and register it to the publisher for (cfgfile, app, dist, aconf) in configs: #log.set_logger('nagare.application.'+aconf['application']['name']) if watcher: watcher.watch_file(aconf.filename) requirement = None if not dist else pkg_resources.Requirement.parse(dist.project_name) data_path = None if not requirement else pkg_resources.resource_filename(requirement, '/data') # Create the function to get the static contents of the application static_path = aconf['application'].get('static') get_file = None if static_path is not None and os.path.isdir(static_path): # If a ``static`` parameter exists, under the ``[application]`` section, # serve the static contents from this root get_file = lambda path, static_path=static_path: get_file_from_root(static_path, path) else: # Else, serve the static from the ``static`` directory # of the application package if requirement: get_file = lambda path, requirement=requirement: get_file_from_package(requirement, path) static_path = pkg_resources.resource_filename(requirement, '/static') # Register the function to serve the static contents of the application static_url = publisher.register_static(aconf['application']['name'], get_file) # Load the sessions manager factory sessions_managers = dict([(entry.name, entry) for entry in pkg_resources.iter_entry_points('nagare.sessions')]) conf = pconf['sessions'].dict() conf.update(aconf['sessions'].dict()) t = conf.pop('type') sessions_manager = sessions_managers[t].load()() sessions_manager.set_config(options.conf, conf, parser.error) (app, metadatas) = util.activate_WSGIApp( app, cfgfile, aconf, parser.error, '' if not dist else dist.project_name, static_path, static_url, data_path, publisher, sessions_manager ) # Register the application to the publisher publisher.register_application( aconf['application']['path'], aconf['application']['name'], app, create_wsgi_pipe(app, options, cfgfile, aconf, parser.error) ) # Register the function to serve the static contents of the framework publisher.register_static( 'nagare', lambda path, r=pkg_resources.Requirement.parse('nagare'): get_file_from_package(r, path) ) # Launch all the applications publisher.serve(options.conf, pconf['publisher'], parser.error)
def test_pkg_resources_factory(self): # NOTE: this test fails if run standalone: # ImportError: No module named test_egg_save # Probably need Egg_TestModel.test_egg_save, or adjusted sys.path. if MODULE_NAME == '__main__': return logging.debug('') logging.debug('test_pkg_resources_factory') # Write to egg. egg_info = self.model.save_to_egg(self.model.name, next_egg(), py_dir=PY_DIR, child_objs=self.child_objs) self.egg_name = egg_info[0] # Create factory. factory = PkgResourcesFactory(['openmdao.component', 'openmdao.container'], [os.getcwd()]) # Create and move to test directory. orig_dir = os.getcwd() test_dir = 'EggTest' if os.path.exists(test_dir): shutil.rmtree(test_dir, onerror=onerror) os.mkdir(test_dir) os.chdir(test_dir) try: # Check multiple model instances. self.create_and_check_model(factory, 'test_model_1', 'Hello world!\n') self.create_and_check_model(factory, 'test_model_2', 'Hello world!\n') # Check that reloading doesn't clobber existing files. file_data = 'New and interesting stuff\n' path = os.path.join('test_model_2', 'Source', EXTERNAL_FILES[2]) out = open(path, 'w') out.write(file_data) out.close() logging.debug('updated %s', path) self.create_and_check_model(factory, 'test_model_2', file_data) # Check observations. global OBSERVATIONS OBSERVATIONS = [] model = factory.create('Egg_TestModel', name='observed', observer=observer) if model is None: self.fail("Create of 'observed' failed.") expected = [ ('copy', 'Source/xyzzy'), ('copy', 'sub/data2'), ('copy', 'Source/hello'), ('copy', 'sub/data4'), ('complete', 'observed'), ] self.assertEqual(len(OBSERVATIONS), len(expected)) for i, observation in enumerate(OBSERVATIONS): state, string, file_fraction, byte_fraction = observation self.assertEqual(state, expected[i][0]) self.assertEqual(string, expected[i][1]) self.assertEqual(file_fraction, float(i)/float(len(expected)-1)) # Create a component. comp = factory.create('Egg_TestModel.Oddball', name='test_comp', observer=observer) if comp is None: self.fail('Create of test_comp failed.') self.assertEqual(comp.executions, 0) comp.run() self.assertEqual(comp.executions, 3) self.assertEqual(comp.get_pathname(), 'test_comp') # Create a (sub)component. sub = factory.create('Egg_TestModel.Oddball.oddcomp', name='test_sub') if sub is None: self.fail('Create of test_sub failed.') self.assertEqual(sub.get_pathname(), 'test_sub') # Create a (sub)container. sub = factory.create('Egg_TestModel.Oddball.oddcont', name='test_sub') if sub is None: self.fail('Create of test_sub failed.') self.assertEqual(sub.get_pathname(), 'test_sub') # Try a non-existent entry point. obj = factory.create('no-such-entry', name='xyzzy') self.assertEqual(obj, None) finally: os.chdir(orig_dir) shutil.rmtree(test_dir, onerror=onerror) shutil.rmtree(os.path.join(pkg_resources.get_default_cache(), "{}-tmp".format(self.egg_name)))
def get_dotted_filename(self, template_name, template_extension='.html'): """this helper function is designed to search a template or any other file by python module name. Given a string containing the file/template name passed to the @expose decorator we will return a resource useable as a filename even if the file is in fact inside a zipped egg or in a frozen library. The actual implementation is a revamp of the Genshi buffet support plugin, but could be used with any kind a file inside a python package. :param template_name: the string representation of the template name as it has been given by the user on his @expose decorator. Basically this will be a string in the form of: `"myapp.templates.somename"` :type template_name: str :param template_extension: the extension we excpect the template to have, this MUST be the full extension as returned by the os.path.splitext function. This means it should contain the dot. ie: '.html' This argument is optional and the default value if nothing is provided will be '.html' :type template_extension: str The ``template_name`` parameter also accepts a form with explicit extension ``myapp.templates.somename!xhtml`` that will override the ``template_exstesion`` argument and will always use ``.xhtml`` as the extension. This is usually convenient in extensions and libraries that expose a template and want to ensure they work even in the case the application using them has a different extension for templates on the same engine. """ cache_key = template_name try: return self.__cache[cache_key] except KeyError: # the template name was not found in our cache try: # Allow for the package.file!ext syntax template_name, template_extension = template_name.rsplit('!', 1) template_extension = '.' + template_extension except ValueError: pass divider = template_name.rfind('.') if divider >= 0: package = template_name[:divider] basename = template_name[divider + 1:] resourcename = basename + template_extension try: result = resource_filename(package, resourcename) except ImportError as e: raise DottedFileLocatorError( "%s. Perhaps you have forgotten an __init__.py in that folder." % e ) except NotImplementedError: # Cope with zipped files or py2exe apps if not hasattr(self, '__temp_dir'): self.__temp_dir = os.path.join(get_default_cache(), 'tgdf-%s' % uuid.uuid1()) result = os.path.join(self.__temp_dir, package, resourcename) if not os.path.isdir(os.path.dirname(result)): os.makedirs(os.path.dirname(result)) with contextlib.closing(resource_stream(package, resourcename)) as rd: with open(result, 'wb') as result_f: result_f.write(rd.read()) else: result = template_name result = os.path.abspath(result) self.__cache[cache_key] = result return result
def cleanup(): if _pmap: raise RuntimeError packdir = os.path.join(get_default_cache(), __package_name__) cleanpy(packdir)
from snakeskin import __version__ import pkg_resources pkg_resources.declare_namespace(__name__) pkg_resources.get_default_cache("snakeskin_cli").version