def buildRelease(self): """Prepare a release by extracting files from the repository. You can specify a tag name on the command line, like > bin/ReleaseHelper.py tag=1.1.2 This will extract the files tagged with 1.1.2 and build the tarball Webware-1.1.2.tar.gz in your parent directory. This means the release will match exactly what is in the repository, and reduces the risk of local changes, modified files, or new files which are not in the repository from showing up in the release. """ tag = self._args.get('tag') pkg = self._args.get('pkg') pkgType = 'zip archive' if pkg == 'zip' else 'tarball' if tag: print "Creating %s from tag %s ..." % (pkgType, tag) else: print "Creating %s from current workspace..." % pkgType # the location of this script: progPath = os.path.join(os.getcwd(), sys.argv[0]) # we assume this script is located in Webware/bin/ webwarePath = os.path.dirname(os.path.dirname(progPath)) # make the Webware directory our current directory self.chdir(webwarePath) # the tarball will land in its parent directory: tarDir = os.pardir if webwarePath not in sys.path: sys.path.insert(1, webwarePath) from MiscUtils.PropertiesObject import PropertiesObject target = 'Release' try: os.mkdir(target) except OSError: print "Staging directory already exists:", target print "Please remove this directory." return cleanup = [target] source = 'tags/%s' % tag if tag else 'HEAD' try: self.run('git archive %s | tar -x -C %s' % (source, target)) if not os.path.exists(target): print "Unable to extract from %r" % source if tag: print "Perhaps the tag %r does not exist." % tag self.error() propertiesFile = os.path.join(target, 'Properties.py') if not os.path.exists(propertiesFile): self.error('Properties.py not found.') props = PropertiesObject(propertiesFile) if props.get('name') != 'Webware for Python': self.error('This is not a Webware package.') ver = props['versionString'] print "Webware version is:", ver if not tag: # timestamp for time of release used to in versioning the file year, month, day = time.localtime(time.time())[:3] datestamp = "%04d%02d%02d" % (year, month, day) # drop leading 2 digits from year. (Ok, itn's not Y2K but it # is short and unique in a narrow time range of 100 years.) datestamp = datestamp[2:] ver += "-" + datestamp print "Packaged release will be:", ver pkgDir = "Webware-%s" % ver if os.path.exists(pkgDir): self.error("%s is in the way, please remove it." % pkgDir) # rename the target to the pkgDir so the extracted parent # directory from the tarball will be unique to this package. self.run("mv %s %s" % (target, pkgDir)) cleanup.append(pkgDir) pkgExt = '.zip' if pkg == 'zip' else '.tar.gz' pkgName = os.path.join(pkgDir + pkgExt) # cleanup .git files self.run("find %s -name '.git*' -exec rm {} \;" % pkgDir) # We could cleanup any other files not part of this release here. # (For instance, we could create releases without documentation). # We could also create additional files to be part of this release # without being part of the repository, for instance documentation # that is automatically created from markup. pkgPath = os.path.join(tarDir, pkgName) if os.path.exists(pkgPath): self.error("%s is in the way, please remove it." % pkgPath) tarCmd = 'zip -qr' if pkg == 'zip' else 'tar -czf' self.run('%s %s %s' % (tarCmd, pkgPath, pkgDir)) if not os.path.exists(pkgPath): self.error('Could not create %s.' % pkgType) finally: # Clean up for path in cleanup: if os.path.exists(path): self.run('rm -rf ' + path) print print "file:", pkgName print "dir:", os.path.abspath(tarDir) print "size:", os.path.getsize(pkgPath), 'Bytes' print print 'Success.' print
class PlugIn: """Template for Webware Plug-ins. A plug-in is a software component that is loaded by Webware in order to provide additional Webware functionality without necessarily having to modify Webware's source. The most infamous plug-in is PSP (Python Server Pages) which ships with Webware. Plug-ins often provide additional servlet factories, servlet subclasses, examples and documentation. Ultimately, it is the plug-in author's choice as to what to provide and in what manner. Instances of this class represent plug-ins which are ultimately Python packages. A plug-in must also be a Webware component which means that it will have a Properties.py file advertising its name, version, requirements, etc. You can ask a plug-in for its properties(). The plug-in/package must have an __init__.py which must contain the following function:: def installInWebware(application): ... This function is invoked to take whatever actions are needed to plug the new component into Webware. See PSP for an example. If you ask an Application for its plugIns(), you will get a list of instances of this class. The path of the plug-in is added to sys.path, if it's not already there. This is convenient, but we may need a more sophisticated solution in the future to avoid name collisions between plug-ins. Note that this class is hardly ever subclassed. The software in the plug-in package is what provides new functionality and there is currently no way to tell the Application to use custom subclasses of this class on a case-by-case basis (and so far there is currently no need). Instructions for invoking:: # 'self' is typically Application. It gets passed to installInWebware() p = PlugIn(self, 'Foo', '../Foo') willNotLoadReason = plugIn.load() if willNotLoadReason: print(f'Plug-in {path} cannot be loaded because:') print(willNotLoadReason) return None p.install() # Note that load() and install() could raise exceptions. # You should expect this. """ # region Init, load and install def __init__(self, application, name, module): """Initializes the plug-in with basic information. This lightweight constructor does not access the file system. """ self._app = application self._name = name self._module = module self._path = module.__path__[0] try: self._builtin = module.__package__ == f'webware.{name}' except AttributeError: self._builtin = False self._dir = os.path.dirname(self._path) self._cacheDir = os.path.join(self._app._cacheDir, self._name) self._examplePages = self._examplePagesContext = None def load(self, verbose=True): """Loads the plug-in into memory, but does not yet install it. Will return None on success, otherwise a message (string) that says why the plug-in could not be loaded. """ if verbose: print(f'Loading plug-in: {self._name} at {self._path}') # Grab the Properties.py self._properties = PropertiesObject( self.serverSidePath('Properties.py')) if self._builtin and 'version' not in self._properties: self._properties['version'] = self._app._webwareVersion self._properties['versionString'] = self._app._webwareVersionString if not self._properties['willRun']: return self._properties['willNotRunReason'] # Update sys.path if self._dir not in sys.path: sys.path.append(self._dir) # Inspect it and verify some required conventions if not hasattr(self._module, 'installInWebware'): raise PlugInError(f"Plug-in '{self._name!r}' in {self._dir!r}" " has no installInWebware() function.") # Give the module a pointer back to us setattr(self._module, 'plugIn', self) # Make a subdirectory for it in the Cache directory: if not os.path.exists(self._cacheDir): os.mkdir(self._cacheDir) self.setUpExamplePages() def setUpExamplePages(self): """Add a context for the examples.""" if self._app.hasContext('Examples'): config = self._properties.get('webwareConfig', {}) self._examplePages = config.get('examplePages') or None if self.hasExamplePages(): examplesPath = self.serverSidePath('Examples') if not os.path.exists(examplesPath): raise PlugInError( f'Plug-in {self._name!r} says it has example pages, ' 'but there is no Examples/ subdir.') if os.path.exists(os.path.join(examplesPath, '__init__.py')): ctxName = self._name + '/Examples' if not self._app.hasContext(ctxName): self._app.addContext(ctxName, examplesPath) self._examplePagesContext = ctxName else: raise PlugInError( 'Cannot create Examples context for' f' plug-in {self._name!r} (no __init__.py found).') def examplePages(self): return self._examplePages def hasExamplePages(self): return self._examplePages is not None def examplePagesContext(self): return self._examplePagesContext def install(self): """Install plug-in by invoking its installInWebware() function.""" self._module.installInWebware(self._app) # endregion Init, load and install # region Access def name(self): """Return the name of the plug-in. Example: 'Foo'""" return self._name def directory(self): """Return the directory in which the plug-in resides. Example: '..'""" return self._dir def path(self): """Return the full path of the plug-in. Example: '../Foo'""" return self._path def serverSidePath(self, path=None): if path: return os.path.normpath(os.path.join(self._path, path)) return self._path def module(self): """Return the Python module object of the plug-in.""" return self._module def properties(self): """Return the properties. This is a dictionary-like object, of the plug-in which comes from its Properties.py file. See MiscUtils.PropertiesObject.py. """ return self._properties
class PlugIn(object): """Template for Webware Plug-ins. A plug-in is a software component that is loaded by WebKit in order to provide additional WebKit functionality without necessarily having to modify WebKit's source. The most infamous plug-in is PSP (Python Server Pages) which ships with Webware. Plug-ins often provide additional servlet factories, servlet subclasses, examples and documentation. Ultimately, it is the plug-in author's choice as to what to provide and in what manner. Instances of this class represent plug-ins which are ultimately Python packages (see the Python Tutorial, 6.4: "Packages" at http://docs.python.org/tut/node8.html#SECTION008400000000000000000). A plug-in must also be a Webware component which means that it will have a Properties.py file advertising its name, version, requirements, etc. You can ask a plug-in for its properties(). The plug-in/package must have an __init__.py while must contain a function: def InstallInWebKit(appServer): This function is invoked to take whatever actions are needed to plug the new component into WebKit. See PSP for an example. If you ask an AppServer for its plugIns(), you will get a list of instances of this class. The path of the plug-in is added to sys.path, if it's not already there. This is convenient, but we may need a more sophisticated solution in the future to avoid name collisions between plug-ins. Note that this class is hardly ever subclassed. The software in the plug-in package is what provides new functionality and there is currently no way to tell AppServer to use custom subclasses of this class on a case-by-case basis (and so far there is currently no need). Instructions for invoking: p = PlugIn(self, '../Foo') # 'self' is typically AppServer. It gets passed to InstallInWebKit() willNotLoadReason = plugIn.load() if willNotLoadReason: print ' Plug-in %s cannot be loaded because:\n %s' % (path, willNotLoadReason) return None p.install() # Note that load() and install() could raise exceptions. You should expect this. """ ## Init, load and install ## def __init__(self, appServer, path): """Initializes the plug-in with basic information. This lightweight constructor does not access the file system. """ self._appServer = appServer self._path = path self._dir, self._name = os.path.split(path) self._cacheDir = os.path.join( self._appServer.application()._cacheDir, self._name) self._ver = '(unknown)' self._docs = self._docContext = None self._examplePages = self._examplePagesContext = None def load(self): """Loads the plug-in into memory, but does not yet install it. Will return None on success, otherwise a message (string) that says why the plug-in could not be loaded. """ print 'Loading plug-in: %s at %s' % (self._name, self._path) assert os.path.exists(self._path) # Grab the Properties.py self._properties = PropertiesObject(self.serverSidePath('Properties.py')) if not self._properties['willRun']: return self._properties['willNotRunReason'] # Update sys.path if not self._dir in sys.path: sys.path.append(self._dir) # Import the package self._module = __import__(self._name, globals(), [], []) # Inspect it and verify some required conventions if not hasattr(self._module, 'InstallInWebKit'): raise PlugInError( "Plug-in '%s' in '%s' has no InstallInWebKit() function." % (self._name, self._dir)) # Give the module a pointer back to us setattr(self._module, 'plugIn', self) # Make a subdirectory for it in the Cache directory: if not os.path.exists(self._cacheDir): os.mkdir(self._cacheDir) self.setUpDocContext() self.setUpExamplePages() def setUpDocContext(self): """Add a context for the documentation.""" app = self._appServer.application() if app.hasContext('Docs'): self._docs = self._properties.get('docs') or None if self.hasDocs(): docsPath = self.serverSidePath('Docs') assert os.path.exists(docsPath), ( 'Plug-in %s says it has documentation, ' 'but there is no Docs/ subdir.' % self._name) if os.path.exists(os.path.join(docsPath, '__init__.py')): ctxName = self._name + '/Docs' if not app.hasContext(ctxName): app.addContext(ctxName, docsPath) self._docContext = ctxName else: print ('Cannot create Docs context for plug-in %s' ' (no __init__.py found).' % self._name) def setUpExamplePages(self): """Add a context for the examples.""" app = self._appServer.application() if app.hasContext('Examples'): config = self._properties.get('WebKitConfig', {}) self._examplePages = config.get('examplePages') or None if self.hasExamplePages(): examplesPath = self.serverSidePath('Examples') assert os.path.exists(examplesPath), ( 'Plug-in %s says it has example pages, ' 'but there is no Examples/ subdir.' % self._name) if os.path.exists(os.path.join(examplesPath, '__init__.py')): ctxName = self._name + '/Examples' if not app.hasContext(ctxName): app.addContext(ctxName, examplesPath) self._examplePagesContext = ctxName else: print ('Cannot create Examples context for plug-in %s' ' (no __init__.py found).' % self._name) def docs(self): return self._docs def hasDocs(self): return self._docs is not None def docsContext(self): return self._docsContext def examplePages(self): return self._examplePages def hasExamplePages(self): return self._examplePages is not None def examplePagesContext(self): return self._examplePagesContext def install(self): """Install plug-in by invoking its required InstallInWebKit function.""" self._module.InstallInWebKit(self._appServer) ## Access ## def name(self): """Return the name of the plug-in. Example: 'Foo'""" return self._name def directory(self): """Return the directory in which the plug-in resides. Example: '..'""" return self._dir def path(self): """Return the full path of the plug-in. Example: '../Foo'""" return self._path def serverSidePath(self, path=None): if path: return os.path.normpath(os.path.join(self._path, path)) else: return self._path def module(self): """Return the Python module object of the plug-in.""" return self._module def properties(self): """Return the properties. This is a dictionary-like object, of the plug-in which comes from its Properties.py file. See MiscUtils.PropertiesObject.py. """ return self._properties
def buildRelease(self): """Prepare a release by using the SVN export approach. This is used when a tag name is specified on the command line, like > bin/ReleaseHelper.py tag=Release-0.9b3 This will export the SVN files tagged with Release-0_9b3 and build the tarball Webware-0.9b3.tar.gz in your parent directory. This means the release will match exactly what is in the SVN, and reduces the risk of local changes, modified files, or new files which are not in SVN from showing up in the release. """ url = self._args.get('url', ' http://svn.w4py.org/Webware/tags') tag = self._args.get('tag', None) if tag: print "Creating tarball from tag %s ..." % tag else: print "Creating tarball from current workspace..." # the location of this script: progPath = os.path.join(os.getcwd(), sys.argv[0]) # we assume this script is located in Webware/bin/ webwarePath = os.path.dirname(os.path.dirname(progPath)) # make the Webware directory our current directory self.chdir(webwarePath) # the tarball will land in its parent directory: tarDir = os.pardir if webwarePath not in sys.path: sys.path.insert(1, webwarePath) from MiscUtils.PropertiesObject import PropertiesObject target = 'ReleaseHelper-Export' if os.path.exists(target): print "There is incomplete ReleaseHelper data in:", target print "Please remove this directory." return 1 cleanup = [target] if tag: source = '%s/%s' % (url, tag) else: source = '.' try: self.run('svn export -q %s %s' % (source, target)) if not os.path.exists(target): print "Unable to export from %r" % source if tag: print "Perhaps the tag %r does not exist." % tag self.error() propertiesFile = os.path.join(target, 'Properties.py') if not os.path.exists(propertiesFile): self.error('Properties.py not found.') props = PropertiesObject(propertiesFile) if props.get('name', None) != 'Webware for Python': self.error('This is not a Webware package.') ver = props['versionString'] print "Webware version is:", ver if not tag: # timestamp for time of release used to in versioning the file year, month, day = time.localtime(time.time())[:3] datestamp = "%04d%02d%02d" % (year, month, day) # drop leading 2 digits from year. (Ok, itn's not Y2K but it # is short and unique in a narrow time range of 100 years.) datestamp = datestamp[2:] ver += "-" + datestamp print "Packaged release will be:", ver pkgDir = "Webware-%s" % ver if os.path.exists(pkgDir): self.error("%s is in the way, please remove it." % pkgDir) # rename the target to the pkgDir so the extracted parent # directory from the tarball will be unique to this package. self.run("mv %s %s" % (target, pkgDir)) cleanup.append(pkgDir) pkgName = os.path.join(pkgDir + ".tar.gz") # cleanup .cvs files self.run("find %s -name '.cvs*' -exec rm {} \;" % pkgDir) # We could cleanup any other files not part of this release here. # (For instance, we could create releases without documentation). # We could also create additional files to be part of this release # without being part of the repository, for instance documentation # that is automatically created from markup. pkgPath = os.path.join(tarDir, pkgName) if os.path.exists(pkgPath): self.error("%s is in the way, please remove it." % pkgPath) self.run('tar -czf %s %s' % (pkgPath, pkgDir)) if not os.path.exists(pkgPath): self.error('Could not create tarball.') finally: # Clean up for path in cleanup: if os.path.exists(path): self.run('rm -rf ' + path) print print "file:", pkgName print "dir:", os.path.abspath(tarDir) print "size:", os.path.getsize(pkgPath), 'Bytes' print print 'Success.' print
class PlugIn(Object): """ A plug-in is a software component that is loaded by WebKit in order to provide additional WebKit functionality without necessarily having to modify WebKit's source. The most infamous plug-in is PSP (Python Server Pages) which ships with Webware. Plug-ins often provide additional servlet factories, servlet subclasses, examples and documentation. Ultimately, it is the plug-in author's choice as to what to provide and in what manner. Instances of this class represent plug-ins which are ultimately Python packages (see the Python Tutorial, 6.4: "Packages" at http://www.python.org/doc/current/tut/node8.html#SECTION008400000000000000000). A plug-in must also be a Webware component which at means that it will have a Properties.py file advertising its name, version, requirements, etc. You can ask a plug-in for its properties(). The plug-in/package must have an __init__.py while must contain a function: def InstallInWebKit(appServer): This function is invoked to take whatever actions are needed to plug the new component into WebKit. See PSP for an example. If you ask an AppServer for its plugIns(), you will get a list of instances of this class. The path of the plug-in is added to sys.path, if it's not already there. This is convenient, but we may need a more sophisticated solution in the future to avoid name collisions between plug-ins. Note that this class is hardly ever subclassed. The software in the plug-in package is what provides new functionality and there is currently no way to tell AppServer to use custom subclasses of this class on a case-by-case basis (and so far there is currently no need). Instructions for invoking: p = PlugIn(self, '../Foo') # 'self' is typically AppServer. It gets passed to InstallInWebKit() willNotLoadReason = plugIn.load() if willNotLoadReason: print ' Plug-in %s cannot be loaded because:\n %s' % (path, willNotLoadReason) return None p.install() # Note that load() and install() could raise exceptions. You should expect this. """ ## Init, load and install ## def __init__(self, appServer, path): """ Initializes the plug-in with basic information. This lightweight constructor does not access the file system. """ self._appServer = appServer self._path = path self._dir, self._name = os.path.split(path) self._ver = '(unknown)' self._examplePages = None def load(self): """ Loads the plug-in into memory, but does not yet install it. Will return None on success, otherwise a message (string) that says why the plug-in could not be loaded. """ print 'Loading plug-in: %s at %s' % (self._name, self._path) assert os.path.exists(self._path) # Grab the Properties.py self._properties = PropertiesObject( self.serverSidePath('Properties.py')) if not self._properties['willRun']: return self._properties['willNotRunReason'] # Update sys.path if not self._dir in sys.path: sys.path.append(self._dir) # Import the package self._module = __import__(self._name, globals(), [], []) # Inspect it and verify some required conventions if not hasattr(self._module, 'InstallInWebKit'): raise PlugInError, "Plug-in '%s' in '%s' has no InstallInWebKit() function." % ( self._name, self._dir) # Give the module a pointer back to us setattr(self._module, 'plugIn', self) # Make a directory for it in Cache/ cacheDir = os.path.join(self._appServer.serverSidePath(), 'Cache', self._name) if not os.path.exists(cacheDir): os.mkdir(cacheDir) self.setUpExamplePages() def setUpExamplePages(self): # Add a context for the examples app = self._appServer.application() if app.hasContext('Examples'): config = self._properties.get('WebKitConfig', {}) self._examplePages = config.get('examplePages', None) if self._examplePages is not None: examplesPath = self.serverSidePath('Examples') assert os.path.exists( examplesPath ), 'Plug-in %s says it has example pages, but there is no Examples/ subdir.' % self._name ctxName = self._name + 'Examples' if not app.hasContext(ctxName): app.addContext(ctxName, examplesPath) self._examplePagesContext = ctxName def hasExamplePages(self): return self._examplePages is not None def examplePagesContext(self): return self._examplePagesContext def examplePages(self): return self._examplePages def install(self): """ Installs the plug-in by invoking it's required InstallInWebKit() function. """ self._module.InstallInWebKit(self._appServer) ## Access ## def name(self): """ Returns the name of the plug-in. Example: 'Foo' """ return self._name def directory(self): """ Returns the directory in which the plug-in resides. Example: '..' """ return self._dir def path(self): """ Returns the full path of the plug-in. Example: '../Foo' """ return self._path def serverSidePath(self, path=None): if path: return os.path.normpath(os.path.join(self._path, path)) else: return self._path def module(self): """ Returns the Python module object of the plug-in. """ return self._module def properties(self): """ Returns the properties, a dictionary-like object, of the plug-in which comes from its Properties.py file. See MiscUtils.PropertiesObject.py. """ return self._properties ## Deprecated ## def version(self): """ DEPRECATED: PlugIn.version() on 1/25 in ver 0.5. Use self.properties()['versionString'] instead. @ Returns the version of the plug-in as reported in its Properties.py. Example: (0, 2, 0) """ self.deprecated(self.version) return self._properties['version']