appy.shared: improved deployment of a Appy app (creation of a Zope instance is no more required; corresponding folders are created in standard unix locations: /etc for the config file, /var/log for logs, /var/lib for the database, /usr/bin for scripts that start and stop the instance). appy.gen: first draft of a migration script that allows to migrate data from Plone-dependent Appy apps (<= 0.7.1) to Ploneless Appy 0.8.0.
This commit is contained in:
		
							parent
							
								
									95a899f3de
								
							
						
					
					
						commit
						1275df5753
					
				
					 13 changed files with 351 additions and 161 deletions
				
			
		| 
						 | 
				
			
			@ -80,8 +80,9 @@ class GeneratorScript:
 | 
			
		|||
            if options.debian:
 | 
			
		||||
                app = args[0]
 | 
			
		||||
                appDir = os.path.dirname(app)
 | 
			
		||||
                appName = os.path.basename(app)
 | 
			
		||||
                # Get the app version from zope/version.txt
 | 
			
		||||
                f = file(os.path.join(app, 'zope', 'version.txt'))
 | 
			
		||||
                f = file(os.path.join(app, 'zope', appName, 'version.txt'))
 | 
			
		||||
                version = f.read()
 | 
			
		||||
                f.close()
 | 
			
		||||
                version = version[:version.find('build')-1]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										47
									
								
								bin/new.py
									
										
									
									
									
								
							
							
						
						
									
										47
									
								
								bin/new.py
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -6,6 +6,7 @@
 | 
			
		|||
import os, os.path, sys, shutil, re
 | 
			
		||||
from optparse import OptionParser
 | 
			
		||||
from appy.shared.utils import cleanFolder, copyFolder
 | 
			
		||||
from appy.shared.packaging import ooStart, zopeConf
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
class NewError(Exception): pass
 | 
			
		||||
| 
						 | 
				
			
			@ -42,46 +43,6 @@ export INSTANCE_HOME
 | 
			
		|||
exec "$ZOPE_RUN" -C "$CONFIG_FILE" "$@"
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
# zope.conf template file for a pure Zope instance -----------------------------
 | 
			
		||||
zopeConf = '''# Zope configuration.
 | 
			
		||||
%%define INSTANCE %s
 | 
			
		||||
%%define HTTPPORT 8080
 | 
			
		||||
%%define ZOPE_USER zope
 | 
			
		||||
 | 
			
		||||
instancehome $INSTANCE
 | 
			
		||||
effective-user $ZOPE_USER
 | 
			
		||||
<eventlog>
 | 
			
		||||
  level info
 | 
			
		||||
  <logfile>
 | 
			
		||||
    path $INSTANCE/log/event.log
 | 
			
		||||
    level info
 | 
			
		||||
  </logfile>
 | 
			
		||||
</eventlog>
 | 
			
		||||
<logger access>
 | 
			
		||||
  level WARN
 | 
			
		||||
  <logfile>
 | 
			
		||||
    path $INSTANCE/log/Z2.log
 | 
			
		||||
    format %%(message)s
 | 
			
		||||
  </logfile>
 | 
			
		||||
</logger>
 | 
			
		||||
<http-server>
 | 
			
		||||
  address $HTTPPORT
 | 
			
		||||
</http-server>
 | 
			
		||||
<zodb_db main>
 | 
			
		||||
  <filestorage>
 | 
			
		||||
    path $INSTANCE/var/Data.fs
 | 
			
		||||
  </filestorage>
 | 
			
		||||
  mount-point /
 | 
			
		||||
</zodb_db>
 | 
			
		||||
<zodb_db temporary>
 | 
			
		||||
  <temporarystorage>
 | 
			
		||||
   name temporary storage for sessioning
 | 
			
		||||
  </temporarystorage>
 | 
			
		||||
  mount-point /temp_folder
 | 
			
		||||
  container-class Products.TemporaryFolder.TemporaryContainer
 | 
			
		||||
</zodb_db>
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
# zopectl template for a Plone (4) Zope instance -------------------------------
 | 
			
		||||
zopeCtlPlone = '''#!/bin/sh
 | 
			
		||||
PYTHON="%s"
 | 
			
		||||
| 
						 | 
				
			
			@ -153,14 +114,14 @@ class ZopeInstanceCreator:
 | 
			
		|||
        os.chmod('bin/runzope', 0744) # Make it executable by owner.
 | 
			
		||||
        # Create bin/startoo
 | 
			
		||||
        f = file('bin/startoo', 'w')
 | 
			
		||||
        f.write('#!/bin/sh\nsoffice -invisible -headless -nofirststartwizard '\
 | 
			
		||||
                '"-accept=socket,host=localhost,port=2002;urp;"&\n')
 | 
			
		||||
        f.write(ooStart)
 | 
			
		||||
        f.close()
 | 
			
		||||
        os.chmod('bin/startoo', 0744) # Make it executable by owner.
 | 
			
		||||
        # Create etc/zope.conf
 | 
			
		||||
        os.mkdir('etc')
 | 
			
		||||
        f = file('etc/zope.conf', 'w')
 | 
			
		||||
        f.write(zopeConf % self.instancePath)
 | 
			
		||||
        f.write(zopeConf % (self.instancePath, '%s/var' % self.instancePath,
 | 
			
		||||
                            '%s/log' % self.instancePath, ''))
 | 
			
		||||
        f.close()
 | 
			
		||||
        # Create other folders
 | 
			
		||||
        for name in ('Extensions', 'log', 'Products', 'var'): os.mkdir(name)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -424,7 +424,7 @@ class Publisher:
 | 
			
		|||
        f.write(toc)
 | 
			
		||||
        f.close()
 | 
			
		||||
 | 
			
		||||
    privateScripts = ('publish.py', 'zip.py', 'runOpenOffice.sh')
 | 
			
		||||
    privateScripts = ('publish.py', 'zip.py', 'startoo.sh')
 | 
			
		||||
    def prepareGenFolder(self, minimalist=False):
 | 
			
		||||
        '''Creates the basic structure of the temp folder where the appy
 | 
			
		||||
           website will be generated.'''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										23
									
								
								bin/zopectl.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								bin/zopectl.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
# ------------------------------------------------------------------------------
 | 
			
		||||
import sys, os, os.path
 | 
			
		||||
import Zope2.Startup.zopectl as zctl
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
class ZopeRunner:
 | 
			
		||||
    '''This class allows to run a Appy/Zope instance.'''
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        # Check that an arg has been given (start, stop, fg, run)
 | 
			
		||||
        if not sys.argv[3].strip():
 | 
			
		||||
            print 'Argument required.'
 | 
			
		||||
            sys.exit(-1)
 | 
			
		||||
        # Identify the name of the application for which Zope must run.
 | 
			
		||||
        app = os.path.splitext(os.path.basename(sys.argv[2]))[0].lower()
 | 
			
		||||
        # Launch Zope.
 | 
			
		||||
        options = zctl.ZopeCtlOptions()
 | 
			
		||||
        options.realize(None)
 | 
			
		||||
        options.program = ['/usr/bin/%srun' % app]
 | 
			
		||||
        c = zctl.ZopeCmd(options)
 | 
			
		||||
        c.onecmd(" ".join(options.args))
 | 
			
		||||
        return min(c._exitstatus, 1)
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
| 
						 | 
				
			
			@ -120,7 +120,8 @@ class Generator:
 | 
			
		|||
        # Determine application name
 | 
			
		||||
        self.applicationName = os.path.basename(application)
 | 
			
		||||
        # Determine output folder (where to store the generated product)
 | 
			
		||||
        self.outputFolder = os.path.join(application, 'zope')
 | 
			
		||||
        self.outputFolder = os.path.join(application, 'zope',
 | 
			
		||||
                                         self.applicationName)
 | 
			
		||||
        self.options = options
 | 
			
		||||
        # Determine templates folder
 | 
			
		||||
        genFolder = os.path.dirname(__file__)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,6 +8,7 @@ import appy.version
 | 
			
		|||
import appy.gen as gen
 | 
			
		||||
from appy.gen.po import PoParser
 | 
			
		||||
from appy.gen.utils import updateRolesForPermission, createObject
 | 
			
		||||
from appy.gen.migrator import Migrator
 | 
			
		||||
from appy.shared.data import languages
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
| 
						 | 
				
			
			@ -87,14 +88,16 @@ class ZopeInstaller:
 | 
			
		|||
 | 
			
		||||
    def installUi(self):
 | 
			
		||||
        '''Installs the user interface.'''
 | 
			
		||||
        # Delete the existing folder if it existed.
 | 
			
		||||
        zopeContent = self.app.objectIds()
 | 
			
		||||
        if 'ui' in zopeContent: self.app.manage_delObjects(['ui'])
 | 
			
		||||
        self.app.manage_addFolder('ui')
 | 
			
		||||
        # Some useful imports
 | 
			
		||||
        from OFS.Folder import manage_addFolder
 | 
			
		||||
        from OFS.Image import manage_addImage, manage_addFile
 | 
			
		||||
        from Products.PythonScripts.PythonScript import PythonScript
 | 
			
		||||
        from Products.PageTemplates.ZopePageTemplate import \
 | 
			
		||||
             manage_addPageTemplate
 | 
			
		||||
        # Delete the existing folder if it existed.
 | 
			
		||||
        zopeContent = self.app.objectIds()
 | 
			
		||||
        if 'ui' in zopeContent: self.app.manage_delObjects(['ui'])
 | 
			
		||||
        manage_addFolder(self.app, 'ui')
 | 
			
		||||
        # Browse the physical folder and re-create it in the Zope folder
 | 
			
		||||
        j = os.path.join
 | 
			
		||||
        ui = j(j(appy.getPath(), 'gen'), 'ui')
 | 
			
		||||
| 
						 | 
				
			
			@ -106,13 +109,13 @@ class ZopeInstaller:
 | 
			
		|||
                for name in folderName.strip(os.sep).split(os.sep):
 | 
			
		||||
                    zopeFolder = zopeFolder._getOb(name)
 | 
			
		||||
            # Create sub-folders at this level
 | 
			
		||||
            for name in dirs: zopeFolder.manage_addFolder(name)
 | 
			
		||||
            for name in dirs: manage_addFolder(zopeFolder, name)
 | 
			
		||||
            # Create files at this level
 | 
			
		||||
            for name in files:
 | 
			
		||||
                baseName, ext = os.path.splitext(name)
 | 
			
		||||
                f = file(j(root, name))
 | 
			
		||||
                if ext in gen.File.imageExts:
 | 
			
		||||
                    zopeFolder.manage_addImage(name, f)
 | 
			
		||||
                    manage_addImage(zopeFolder, name, f)
 | 
			
		||||
                elif ext == '.pt':
 | 
			
		||||
                    manage_addPageTemplate(zopeFolder, baseName, '', f.read())
 | 
			
		||||
                elif ext == '.py':
 | 
			
		||||
| 
						 | 
				
			
			@ -120,7 +123,7 @@ class ZopeInstaller:
 | 
			
		|||
                    zopeFolder._setObject(baseName, obj)
 | 
			
		||||
                    zopeFolder._getOb(baseName).write(f.read())
 | 
			
		||||
                else:
 | 
			
		||||
                    zopeFolder.manage_addFile(name, f)
 | 
			
		||||
                    manage_addFile(zopeFolder, name, f)
 | 
			
		||||
                f.close()
 | 
			
		||||
        # Update the home page
 | 
			
		||||
        if 'index_html' in zopeContent:
 | 
			
		||||
| 
						 | 
				
			
			@ -199,9 +202,10 @@ class ZopeInstaller:
 | 
			
		|||
        '''Creates the tool and the root data folder if they do not exist.'''
 | 
			
		||||
        # Create or update the base folder for storing data
 | 
			
		||||
        zopeContent = self.app.objectIds()
 | 
			
		||||
        from OFS.Folder import manage_addFolder
 | 
			
		||||
 | 
			
		||||
        if 'data' not in zopeContent:
 | 
			
		||||
            self.app.manage_addFolder('data')
 | 
			
		||||
            manage_addFolder(self.app, 'data')
 | 
			
		||||
            data = self.app.data
 | 
			
		||||
            # Manager has been granted Add permissions for all root classes.
 | 
			
		||||
            # This may not be desired, so remove this.
 | 
			
		||||
| 
						 | 
				
			
			@ -240,7 +244,13 @@ class ZopeInstaller:
 | 
			
		|||
        appyTool.log('Appy version is "%s".' % appy.version.short)
 | 
			
		||||
 | 
			
		||||
        # Create the admin user if no user exists.
 | 
			
		||||
        if not self.app.acl_users.getUsers():
 | 
			
		||||
        try:
 | 
			
		||||
            users = self.app.acl_users.getUsers()
 | 
			
		||||
        except:
 | 
			
		||||
            # When Plone has installed PAS in acl_users this may fail. Plone
 | 
			
		||||
            # may still be in the way for migration purposes.
 | 
			
		||||
            users = ('admin') # We suppose there is at least a user.
 | 
			
		||||
        if not users:
 | 
			
		||||
            self.app.acl_users._doAddUser('admin', 'admin', ['Manager'], ())
 | 
			
		||||
            appyTool.log('Admin user "admin" created.')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -399,6 +409,10 @@ class ZopeInstaller:
 | 
			
		|||
        self.installCatalog()
 | 
			
		||||
        self.installTool()
 | 
			
		||||
        self.installUi()
 | 
			
		||||
        # Perform migrations if required
 | 
			
		||||
        Migrator(self).run()
 | 
			
		||||
        # Update Appy version in the database
 | 
			
		||||
        self.app.config.appy().appyVersion = appy.version.short
 | 
			
		||||
        # Empty the fake REQUEST object, only used at Zope startup.
 | 
			
		||||
        del self.app.config.getProductConfig().fakeRequest.wrappers
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										159
									
								
								gen/migrator.py
									
										
									
									
									
								
							
							
						
						
									
										159
									
								
								gen/migrator.py
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -7,56 +7,125 @@ class Migrator:
 | 
			
		|||
       installation, we've detected a new Appy version.'''
 | 
			
		||||
    def __init__(self, installer):
 | 
			
		||||
        self.installer = installer
 | 
			
		||||
        self.logger = installer.logger
 | 
			
		||||
        self.app = installer.app
 | 
			
		||||
 | 
			
		||||
    def migrateTo_0_7_1(self):
 | 
			
		||||
        '''Appy 0.7.1 has its own management of Ref fields. So we must
 | 
			
		||||
           update data structures that store Ref info on instances.'''
 | 
			
		||||
        ins = self.installer
 | 
			
		||||
        ins.info('Migrating to Appy 0.7.1...')
 | 
			
		||||
        allClassNames = [ins.tool.__class__.__name__] + ins.config.allClassNames
 | 
			
		||||
        for className in allClassNames:
 | 
			
		||||
            i = -1
 | 
			
		||||
            updated = 0
 | 
			
		||||
            ins.info('Analysing class "%s"...' % className)
 | 
			
		||||
            refFields = None
 | 
			
		||||
            for obj in ins.tool.executeQuery(className,\
 | 
			
		||||
                                             noSecurity=True)['objects']:
 | 
			
		||||
                i += 1
 | 
			
		||||
                if i == 0:
 | 
			
		||||
                    # Get the Ref fields for objects of this class
 | 
			
		||||
                    refFields = [f for f in obj.getAllAppyTypes() \
 | 
			
		||||
                                 if (f.type == 'Ref') and not f.isBack]
 | 
			
		||||
                    if refFields:
 | 
			
		||||
                        refNames = ', '.join([rf.name for rf in refFields])
 | 
			
		||||
                        ins.info('  Ref fields found: %s' % refNames)
 | 
			
		||||
    bypassRoles = ('Authenticated', 'Member')
 | 
			
		||||
    bypassGroups = ('Administrators', 'Reviewers')
 | 
			
		||||
    def migrateUsers(self, ploneSite):
 | 
			
		||||
        '''Migrate users from Plone's acl_users to Zope acl_users with
 | 
			
		||||
           corresponding Appy objects.'''
 | 
			
		||||
        # First of all, remove the Plone-patched root acl_users by a standard
 | 
			
		||||
        # (hum, Appy-patched) Zope UserFolder.
 | 
			
		||||
        tool = self.app.config.appy()
 | 
			
		||||
        from AccessControl.User import manage_addUserFolder
 | 
			
		||||
        self.app.manage_delObjects(ids=['acl_users'])
 | 
			
		||||
        manage_addUserFolder(self.app)
 | 
			
		||||
        # Put an admin user into it
 | 
			
		||||
        newUsersDb = self.app.acl_users
 | 
			
		||||
        newUsersDb._doAddUser('admin', 'admin', ['Manager'], ())
 | 
			
		||||
        # Copy users from Plone acl_users to Zope acl_users
 | 
			
		||||
        for user in ploneSite.acl_users.getUsers():
 | 
			
		||||
            id = user.getId()
 | 
			
		||||
            userRoles = user.getRoles()
 | 
			
		||||
            for br in self.bypassRoles:
 | 
			
		||||
                if br in userRoles: userRoles.remove(br)
 | 
			
		||||
            userInfo = ploneSite.portal_membership.getMemberById(id)
 | 
			
		||||
            userName = userInfo.getProperty('fullname') or id
 | 
			
		||||
            userEmail = userInfo.getProperty('email') or ''
 | 
			
		||||
            appyUser = tool.create('users', login=id,
 | 
			
		||||
                password1='fake', password2='fake', roles=userRoles,
 | 
			
		||||
                name=userName, firstName=' ', email=userEmail)
 | 
			
		||||
            appyUser.title = appyUser.title.strip()
 | 
			
		||||
            # Set the correct password
 | 
			
		||||
            password = ploneSite.acl_users.source_users._user_passwords[id]
 | 
			
		||||
            newUsersDb.data[id].__ = password
 | 
			
		||||
            # Manage groups. Exclude not-used default Plone groups.
 | 
			
		||||
            for groupId in user.getGroups():
 | 
			
		||||
                if groupId in self.bypassGroups: continue
 | 
			
		||||
                if tool.count('Group', login=groupId):
 | 
			
		||||
                    # The Appy group already exists, get it
 | 
			
		||||
                    appyGroup = tool.search('Group', login=groupId)[0]
 | 
			
		||||
                else:
 | 
			
		||||
                        ins.info('  No Ref field found.')
 | 
			
		||||
                    # Create the group. Todo: get Plone group roles and title
 | 
			
		||||
                    appyGroup = tool.create('groups', login=groupId,
 | 
			
		||||
                                            title=groupId)
 | 
			
		||||
                appyGroup.addUser(appyUser)
 | 
			
		||||
 | 
			
		||||
    def reindexObject(self, obj):
 | 
			
		||||
        obj.reindex()
 | 
			
		||||
        i = 1
 | 
			
		||||
        for subObj in obj.objectValues():
 | 
			
		||||
            i += self.reindexObject(subObj)
 | 
			
		||||
        return i # The number of reindexed (sub-)object(s)
 | 
			
		||||
 | 
			
		||||
    def migrateTo_0_8_0(self):
 | 
			
		||||
        '''Migrates a Plone-based (<= 0.7.1) Appy app to a Ploneless (0.8.0)
 | 
			
		||||
           Appy app.'''
 | 
			
		||||
        self.logger.info('Migrating to Appy 0.8.0...')
 | 
			
		||||
        # Find the Plone site. It must be at the root of the Zope tree.
 | 
			
		||||
        ploneSite = None
 | 
			
		||||
        for obj in self.app.objectValues():
 | 
			
		||||
            if obj.__class__.__name__ == 'PloneSite':
 | 
			
		||||
                ploneSite = obj
 | 
			
		||||
                break
 | 
			
		||||
                isUpdated = False
 | 
			
		||||
                for field in refFields:
 | 
			
		||||
                    # Attr for storing UIDs of referred objects has moved
 | 
			
		||||
                    # from _appy_[fieldName] to [fieldName].
 | 
			
		||||
                    refs = getattr(obj, '_appy_%s' % field.name)
 | 
			
		||||
                    if refs:
 | 
			
		||||
                        isUpdated = True
 | 
			
		||||
                        setattr(obj, field.name, refs)
 | 
			
		||||
                        exec 'del obj._appy_%s' % field.name
 | 
			
		||||
                        # Set the back references
 | 
			
		||||
                        for refObject in field.getValue(obj):
 | 
			
		||||
                            refObject.link(field.back.name, obj, back=True)
 | 
			
		||||
                if isUpdated: updated += 1
 | 
			
		||||
            if updated:
 | 
			
		||||
                ins.info('  %d/%d object(s) updated.' % (updated, i+1))
 | 
			
		||||
        # As a preamble: delete translation objects from self.app.config: they
 | 
			
		||||
        # will be copied from the old tool.
 | 
			
		||||
        self.app.config.manage_delObjects(ids=self.app.config.objectIds())
 | 
			
		||||
        # Migrate data objects:
 | 
			
		||||
        # - from oldDataFolder to self.app.data
 | 
			
		||||
        # - from oldTool       to self.app.config (excepted translation
 | 
			
		||||
        #                         objects that were re-created from i18n files).
 | 
			
		||||
        appName = self.app.config.getAppName()
 | 
			
		||||
        for oldFolderName in (appName, 'portal_%s' % appName.lower()):
 | 
			
		||||
            oldFolder = getattr(ploneSite, oldFolderName)
 | 
			
		||||
            objectIds = [id for id in oldFolder.objectIds()]
 | 
			
		||||
            cutted = oldFolder.manage_cutObjects(ids=objectIds)
 | 
			
		||||
            if oldFolderName == appName:
 | 
			
		||||
                destFolder = self.app.data
 | 
			
		||||
            else:
 | 
			
		||||
                destFolder = self.app.config
 | 
			
		||||
            destFolder.manage_pasteObjects(cutted)
 | 
			
		||||
            i = 0
 | 
			
		||||
            for obj in destFolder.objectValues():
 | 
			
		||||
                i += self.reindexObject(obj)
 | 
			
		||||
            self.logger.info('%d objects imported into %s.' % \
 | 
			
		||||
                             (i, destFolder.getId()))
 | 
			
		||||
            if oldFolderName != appName:
 | 
			
		||||
                # Re-link objects copied into the self.app.config with the Tool
 | 
			
		||||
                # through Ref fields.
 | 
			
		||||
                tool = self.app.config.appy()
 | 
			
		||||
                pList = tool.o.getProductConfig().PersistentList
 | 
			
		||||
                for field in tool.fields:
 | 
			
		||||
                    if field.type != 'Ref': continue
 | 
			
		||||
                    n = field.name
 | 
			
		||||
                    if n in ('users', 'groups'): continue
 | 
			
		||||
                    uids = getattr(oldFolder, n)
 | 
			
		||||
                    if uids:
 | 
			
		||||
                        # Update the forward reference
 | 
			
		||||
                        setattr(tool.o, n, pList(uids))
 | 
			
		||||
                        # Update the back reference
 | 
			
		||||
                        for obj in getattr(tool, n):
 | 
			
		||||
                            backList = getattr(obj.o, field.back.name)
 | 
			
		||||
                            backList.remove(oldFolder._at_uid)
 | 
			
		||||
                            backList.append(tool.uid)
 | 
			
		||||
                        self.logger.info('config.%s: linked %d object(s)' % \
 | 
			
		||||
                                         (n, len(uids)))
 | 
			
		||||
                    else:
 | 
			
		||||
                        self.logger.info('config.%s: no object to link.' % n)
 | 
			
		||||
        self.migrateUsers(ploneSite)
 | 
			
		||||
        self.logger.info('Migration done.')
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        i = self.installer
 | 
			
		||||
        installedVersion = i.appyTool.appyVersion
 | 
			
		||||
        if self.app.acl_users.__class__.__name__ == 'UserFolder':
 | 
			
		||||
            return # Already Ploneless
 | 
			
		||||
        tool = self.app.config.appy()
 | 
			
		||||
        appyVersion = tool.appyVersion
 | 
			
		||||
        if not appyVersion or (appyVersion < '0.8.0'):
 | 
			
		||||
            # Migration is required.
 | 
			
		||||
            startTime = time.time()
 | 
			
		||||
        migrationRequired = False
 | 
			
		||||
        if not installedVersion or (installedVersion <= '0.7.0'):
 | 
			
		||||
            migrationRequired = True
 | 
			
		||||
            self.migrateTo_0_7_1()
 | 
			
		||||
            self.migrateTo_0_8_0()
 | 
			
		||||
            stopTime = time.time()
 | 
			
		||||
        if migrationRequired:
 | 
			
		||||
            i.info('Migration done in %d minute(s).'% ((stopTime-startTime)/60))
 | 
			
		||||
            elapsed = (stopTime-startTime) / 60.0
 | 
			
		||||
            self.logger.info('Migration done in %d minute(s).' % elapsed)
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -129,7 +129,7 @@ class ModelClass:
 | 
			
		|||
class User(ModelClass):
 | 
			
		||||
    # In a ModelClass we need to declare attributes in the following list.
 | 
			
		||||
    _appy_attributes = ['title', 'name', 'firstName', 'login', 'password1',
 | 
			
		||||
                        'password2', 'roles']
 | 
			
		||||
                        'password2', 'email', 'roles']
 | 
			
		||||
    # All methods defined below are fake. Real versions are in the wrapper.
 | 
			
		||||
    title = gen.String(show=False, indexed=True)
 | 
			
		||||
    gm = {'group': 'main', 'multiplicity': (1,1), 'width': 25}
 | 
			
		||||
| 
						 | 
				
			
			@ -144,6 +144,7 @@ class User(ModelClass):
 | 
			
		|||
    password1 = gen.String(format=gen.String.PASSWORD, show=showPassword,
 | 
			
		||||
                           validator=validatePassword, **gm)
 | 
			
		||||
    password2 = gen.String(format=gen.String.PASSWORD, show=showPassword, **gm)
 | 
			
		||||
    email = gen.String(group='main', width=25)
 | 
			
		||||
    gm['multiplicity'] = (0, None)
 | 
			
		||||
    roles = gen.String(validator=gen.Selection('getGrantableRoles'),
 | 
			
		||||
                       indexed=True, **gm)
 | 
			
		||||
| 
						 | 
				
			
			@ -177,7 +178,7 @@ class Translation(ModelClass):
 | 
			
		|||
    def show(self, name): pass
 | 
			
		||||
 | 
			
		||||
# The Tool class ---------------------------------------------------------------
 | 
			
		||||
# Here are the prefixes of the fields generated on the Tool.
 | 
			
		||||
# Prefixes of the fields generated on the Tool.
 | 
			
		||||
toolFieldPrefixes = ('defaultValue', 'podTemplate', 'formats', 'resultColumns',
 | 
			
		||||
                     'enableAdvancedSearch', 'numberOfSearchColumns',
 | 
			
		||||
                     'searchFields', 'optionalFields', 'showWorkflow',
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -100,7 +100,7 @@
 | 
			
		|||
    </tal:operator>
 | 
			
		||||
    <tal:comment replace="nothing">The list of values</tal:comment>
 | 
			
		||||
    <select tal:attributes="name widgetName; size widget/height" multiple="multiple">
 | 
			
		||||
      <option tal:repeat="v python:tool.getPossibleValues(name, withTranslations=True, withBlankValue=False, className=contentType)"
 | 
			
		||||
      <option tal:repeat="v python:tool.getPossibleValues(name, withTranslations=True, withBlankValue=False, className=className)"
 | 
			
		||||
              tal:attributes="value python:v[0]; title python: v[1]"
 | 
			
		||||
              tal:content="python: tool.truncateValue(v[1], widget)">
 | 
			
		||||
      </option>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -110,6 +110,7 @@ class ZopeUserPatches:
 | 
			
		|||
    def getRolesInContext(self, object):
 | 
			
		||||
        '''Return the list of global and local (to p_object) roles granted to
 | 
			
		||||
           this user (or to any of its groups).'''
 | 
			
		||||
        if isinstance(object, AbstractWrapper): object = object.o
 | 
			
		||||
        object = getattr(object, 'aq_inner', object)
 | 
			
		||||
        # Start with user global roles
 | 
			
		||||
        res = self.getRoles()
 | 
			
		||||
| 
						 | 
				
			
			@ -120,7 +121,8 @@ class ZopeUserPatches:
 | 
			
		|||
        groups = getattr(self, 'groups', ())
 | 
			
		||||
        for id, roles in localRoles.iteritems():
 | 
			
		||||
            if (id != userId) and (id not in groups): continue
 | 
			
		||||
            for role in roles: res.add(role)
 | 
			
		||||
            for role in roles:
 | 
			
		||||
                if role not in res: res.append(role)
 | 
			
		||||
        return res
 | 
			
		||||
 | 
			
		||||
    def allowed(self, object, object_roles=None):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
# ------------------------------------------------------------------------------
 | 
			
		||||
import os, os.path, subprocess, md5, shutil
 | 
			
		||||
from appy.shared.utils import getOsTempFolder, FolderDeleter
 | 
			
		||||
from appy.shared.utils import getOsTempFolder, FolderDeleter, cleanFolder
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
debianInfo = '''Package: python-appy%s
 | 
			
		||||
| 
						 | 
				
			
			@ -8,23 +8,77 @@ Version: %s
 | 
			
		|||
Architecture: all
 | 
			
		||||
Maintainer: Gaetan Delannay <gaetan.delannay@geezteem.com>
 | 
			
		||||
Installed-Size: %d
 | 
			
		||||
Depends: python (>= %s), python (<= %s)%s
 | 
			
		||||
Depends: python (>= %s)%s
 | 
			
		||||
Section: python
 | 
			
		||||
Priority: optional
 | 
			
		||||
Homepage: http://appyframework.org
 | 
			
		||||
Description: Appy builds simple but complex web Python apps.
 | 
			
		||||
'''
 | 
			
		||||
appCtl = '''#!/usr/lib/zope2.12/bin/python
 | 
			
		||||
import sys
 | 
			
		||||
from appy.bin.zopectl import ZopeRunner
 | 
			
		||||
args = ' '.join(sys.argv[1:])
 | 
			
		||||
sys.argv = [sys.argv[0], '-C', '/etc/%s.conf', args]
 | 
			
		||||
ZopeRunner().run()
 | 
			
		||||
'''
 | 
			
		||||
appRun = '''#!/bin/sh
 | 
			
		||||
exec "/usr/lib/zope2.12/bin/runzope" -C "/etc/%s.conf" "$@"
 | 
			
		||||
'''
 | 
			
		||||
ooStart = '#!/bin/sh\nsoffice -invisible -headless -nofirststartwizard ' \
 | 
			
		||||
          '"-accept=socket,host=localhost,port=2002;urp;"&\n'
 | 
			
		||||
zopeConf = '''# Zope configuration.
 | 
			
		||||
%%define INSTANCE %s
 | 
			
		||||
%%define DATA %s
 | 
			
		||||
%%define LOG %s
 | 
			
		||||
%%define HTTPPORT 8080
 | 
			
		||||
%%define ZOPE_USER zope
 | 
			
		||||
 | 
			
		||||
instancehome $INSTANCE
 | 
			
		||||
effective-user $ZOPE_USER
 | 
			
		||||
%s
 | 
			
		||||
<eventlog>
 | 
			
		||||
  level info
 | 
			
		||||
  <logfile>
 | 
			
		||||
    path $LOG/event.log
 | 
			
		||||
    level info
 | 
			
		||||
  </logfile>
 | 
			
		||||
</eventlog>
 | 
			
		||||
<logger access>
 | 
			
		||||
  level WARN
 | 
			
		||||
  <logfile>
 | 
			
		||||
    path $LOG/Z2.log
 | 
			
		||||
    format %%(message)s
 | 
			
		||||
  </logfile>
 | 
			
		||||
</logger>
 | 
			
		||||
<http-server>
 | 
			
		||||
  address $HTTPPORT
 | 
			
		||||
</http-server>
 | 
			
		||||
<zodb_db main>
 | 
			
		||||
  <filestorage>
 | 
			
		||||
    path $DATA/Data.fs
 | 
			
		||||
  </filestorage>
 | 
			
		||||
  mount-point /
 | 
			
		||||
</zodb_db>
 | 
			
		||||
<zodb_db temporary>
 | 
			
		||||
  <temporarystorage>
 | 
			
		||||
   name temporary storage for sessioning
 | 
			
		||||
  </temporarystorage>
 | 
			
		||||
  mount-point /temp_folder
 | 
			
		||||
  container-class Products.TemporaryFolder.TemporaryContainer
 | 
			
		||||
</zodb_db>
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
class Debianizer:
 | 
			
		||||
    '''This class allows to produce a Debian package from a Python (Appy)
 | 
			
		||||
       package.'''
 | 
			
		||||
 | 
			
		||||
    def __init__(self, app, out, appVersion='0.1.0',
 | 
			
		||||
                 pythonVersions=('2.6', '2.7'),
 | 
			
		||||
                 pythonVersions=('2.6',),
 | 
			
		||||
                 depends=('zope2.12', 'openoffice.org', 'imagemagick')):
 | 
			
		||||
        # app is the path to the Python package to Debianize.
 | 
			
		||||
        self.app = app
 | 
			
		||||
        self.appName = os.path.basename(app)
 | 
			
		||||
        self.appNameLower = self.appName.lower()
 | 
			
		||||
        # out is the folder where the Debian package will be generated.
 | 
			
		||||
        self.out = out
 | 
			
		||||
        # What is the version number for this app ?
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +87,8 @@ class Debianizer:
 | 
			
		|||
        self.pythonVersions = pythonVersions
 | 
			
		||||
        # Debian package dependencies
 | 
			
		||||
        self.depends = depends
 | 
			
		||||
        # Zope 2.12 requires Python 2.6
 | 
			
		||||
        if 'zope2.12' in depends: self.pythonVersions = ('2.6',)
 | 
			
		||||
 | 
			
		||||
    def run(self):
 | 
			
		||||
        '''Generates the Debian package.'''
 | 
			
		||||
| 
						 | 
				
			
			@ -49,19 +105,74 @@ class Debianizer:
 | 
			
		|||
        for version in self.pythonVersions:
 | 
			
		||||
            libFolder = j(srcFolder, 'python%s' % version)
 | 
			
		||||
            os.makedirs(libFolder)
 | 
			
		||||
            shutil.copytree(self.app, j(libFolder, self.appName))
 | 
			
		||||
        # Create data.tar.gz based on it.
 | 
			
		||||
        os.chdir(debFolder)
 | 
			
		||||
        os.system('tar czvf data.tar.gz ./usr')
 | 
			
		||||
            destFolder = j(libFolder, self.appName)
 | 
			
		||||
            shutil.copytree(self.app, destFolder)
 | 
			
		||||
            # Clean dest folder (.svn/.bzr files)
 | 
			
		||||
            cleanFolder(destFolder, folders=('.svn', '.bzr'))
 | 
			
		||||
        # When packaging Appy itself, everything is in /usr/lib/pythonX. When
 | 
			
		||||
        # packaging an Appy app, we will generate more files for creating a
 | 
			
		||||
        # running instance.
 | 
			
		||||
        if self.appName != 'appy':
 | 
			
		||||
            # Create the folders that will collectively represent the deployed
 | 
			
		||||
            # Zope instance.
 | 
			
		||||
            binFolder = j(debFolder, 'usr', 'bin')
 | 
			
		||||
            os.makedirs(binFolder)
 | 
			
		||||
            # <app>ctl
 | 
			
		||||
            name = '%s/%sctl' % (binFolder, self.appNameLower)
 | 
			
		||||
            f = file(name, 'w')
 | 
			
		||||
            f.write(appCtl % self.appNameLower)
 | 
			
		||||
            os.chmod(name, 0744) # Make it executable by owner.
 | 
			
		||||
            f.close()
 | 
			
		||||
            # <app>run
 | 
			
		||||
            name = '%s/%srun' % (binFolder, self.appNameLower)
 | 
			
		||||
            f = file(name, 'w')
 | 
			
		||||
            f.write(appRun % self.appNameLower)
 | 
			
		||||
            os.chmod(name, 0744) # Make it executable by owner.
 | 
			
		||||
            f.close()
 | 
			
		||||
            # startoo
 | 
			
		||||
            name = '%s/startoo' % binFolder
 | 
			
		||||
            f = file(name, 'w')
 | 
			
		||||
            f.write(ooStart)
 | 
			
		||||
            f.close()
 | 
			
		||||
            os.chmod(name, 0744) # Make it executable by owner.
 | 
			
		||||
            # /var/lib/<app> (will store Data.fs, lock files, etc)
 | 
			
		||||
            varLibFolder = j(debFolder, 'var', 'lib', self.appNameLower)
 | 
			
		||||
            os.makedirs(varLibFolder)
 | 
			
		||||
            f = file('%s/README' % varLibFolder, 'w')
 | 
			
		||||
            f.write('This folder stores the %s database.\n' % self.appName)
 | 
			
		||||
            f.close()
 | 
			
		||||
            # /var/log/<app> (will store event.log and Z2.log)
 | 
			
		||||
            varLogFolder = j(debFolder, 'var', 'log', self.appNameLower)
 | 
			
		||||
            os.makedirs(varLogFolder)
 | 
			
		||||
            f = file('%s/README' % varLogFolder, 'w')
 | 
			
		||||
            f.write('This folder stores the log files for %s.\n' % self.appName)
 | 
			
		||||
            f.close()
 | 
			
		||||
            # /etc/<app>.conf (Zope configuration file)
 | 
			
		||||
            etcFolder = j(debFolder, 'etc')
 | 
			
		||||
            os.makedirs(etcFolder)
 | 
			
		||||
            name = '%s/%s.conf' % (etcFolder, self.appNameLower)
 | 
			
		||||
            n = self.appNameLower
 | 
			
		||||
            f = file(name, 'w')
 | 
			
		||||
            productsFolder = '/usr/lib/python%s/%s/zope' % \
 | 
			
		||||
                             (self.pythonVersions[0], self.appName)
 | 
			
		||||
            f.write(zopeConf % ('/var/lib/%s' % n, '/var/lib/%s' % n,
 | 
			
		||||
                                '/var/log/%s' % n,
 | 
			
		||||
                                'products %s\n' % productsFolder))
 | 
			
		||||
            f.close()
 | 
			
		||||
        # Get the size of the app, in Kb.
 | 
			
		||||
        cmd = subprocess.Popen(['du', '-b', '-s', 'usr'],stdout=subprocess.PIPE)
 | 
			
		||||
        os.chdir(tempFolder)
 | 
			
		||||
        cmd = subprocess.Popen(['du', '-b', '-s', 'debian'],
 | 
			
		||||
                               stdout=subprocess.PIPE)
 | 
			
		||||
        size = int(int(cmd.stdout.read().split()[0])/1024.0)
 | 
			
		||||
        os.chdir(debFolder)
 | 
			
		||||
        # Create data.tar.gz based on it.
 | 
			
		||||
        os.system('tar czvf data.tar.gz *')
 | 
			
		||||
        # Create the control file
 | 
			
		||||
        f = file('control', 'w')
 | 
			
		||||
        nameSuffix = ''
 | 
			
		||||
        dependencies = []
 | 
			
		||||
        if self.appName != 'appy':
 | 
			
		||||
            nameSuffix = '-%s' % self.appName.lower()
 | 
			
		||||
            nameSuffix = '-%s' % self.appNameLower
 | 
			
		||||
            dependencies.append('python-appy')
 | 
			
		||||
        if self.depends:
 | 
			
		||||
            for d in self.depends: dependencies.append(d)
 | 
			
		||||
| 
						 | 
				
			
			@ -69,12 +180,15 @@ class Debianizer:
 | 
			
		|||
        if dependencies:
 | 
			
		||||
            depends = ', ' + ', '.join(dependencies)
 | 
			
		||||
        f.write(debianInfo % (nameSuffix, self.appVersion, size,
 | 
			
		||||
                              self.pythonVersions[0], self.pythonVersions[1],
 | 
			
		||||
                              depends))
 | 
			
		||||
                              self.pythonVersions[0], depends))
 | 
			
		||||
        f.close()
 | 
			
		||||
        # Create md5sum file
 | 
			
		||||
        f = file('md5sums', 'w')
 | 
			
		||||
        for dir, dirnames, filenames in os.walk('usr'):
 | 
			
		||||
        toWalk = ['usr']
 | 
			
		||||
        if self.appName != 'appy':
 | 
			
		||||
            toWalk += ['etc', 'var']
 | 
			
		||||
        for folderToWalk in toWalk:
 | 
			
		||||
            for dir, dirnames, filenames in os.walk(folderToWalk):
 | 
			
		||||
                for name in filenames:
 | 
			
		||||
                    m = md5.new()
 | 
			
		||||
                    pathName = j(dir, name)
 | 
			
		||||
| 
						 | 
				
			
			@ -90,7 +204,7 @@ class Debianizer:
 | 
			
		|||
        f.close()
 | 
			
		||||
        # Create postinst, a script that will:
 | 
			
		||||
        # - bytecompile Python files after the Debian install
 | 
			
		||||
        # - create a Zope instance (excepted if we are installing Appy itself).
 | 
			
		||||
        # - change ownership of some files if required
 | 
			
		||||
        f = file('postinst', 'w')
 | 
			
		||||
        content = '#!/bin/sh\nset -e\n'
 | 
			
		||||
        for version in self.pythonVersions:
 | 
			
		||||
| 
						 | 
				
			
			@ -98,22 +212,16 @@ class Debianizer:
 | 
			
		|||
            lib = '/usr/lib/python%s' % version
 | 
			
		||||
            cmds = ' %s -m compileall -q %s/%s 2> /dev/null\n' % (bin, lib,
 | 
			
		||||
                                                                  self.appName)
 | 
			
		||||
            if self.appName != 'appy':
 | 
			
		||||
                inst = '/home/zope/%sInstance' % self.appName
 | 
			
		||||
                cmds += '  if [ -e %s ]\n  then\n' % inst
 | 
			
		||||
                # If the Zope instance already exists, simply restart it.
 | 
			
		||||
                cmds += '    %s/bin/zopectl restart\n  else\n' % inst
 | 
			
		||||
                # Else, create a Zope instance in the home of user "zope".
 | 
			
		||||
                cmds += '    %s %s/appy/bin/new.py zope /usr/lib/zope2.12 ' \
 | 
			
		||||
                        '%s\n' % (bin, lib, inst)
 | 
			
		||||
                # Within this instance, create a symlink to the Zope product
 | 
			
		||||
                cmds += '    ln -s %s/%s/zope %s/Products/%s\n' % \
 | 
			
		||||
                        (lib, self.appName, inst, self.appName)
 | 
			
		||||
                # Launch the instance
 | 
			
		||||
                cmds += '    %s/bin/zopectl start\n' % inst
 | 
			
		||||
                # Launch OpenOffice in server mode
 | 
			
		||||
                cmds += '    %s/bin/startoo\n  fi\n' % inst
 | 
			
		||||
            content += 'if [ -e %s ]\nthen\n%sfi\n' % (bin, cmds)
 | 
			
		||||
        if self.appName != 'appy':
 | 
			
		||||
            # Allow user "zope", that runs the Zope instance, to write the
 | 
			
		||||
            # database and log files.
 | 
			
		||||
            content += 'chown -R zope:root /var/lib/%s\n' % self.appNameLower
 | 
			
		||||
            content += 'chown -R zope:root /var/log/%s\n' % self.appNameLower
 | 
			
		||||
            # (re-)start the app
 | 
			
		||||
            content += '%sctl restart\n' % self.appNameLower
 | 
			
		||||
            # (re-)start oo
 | 
			
		||||
            content += 'startoo\n'
 | 
			
		||||
        f.write(content)
 | 
			
		||||
        f.close()
 | 
			
		||||
        # Create prerm, a script that will remove all pyc files before removing
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -36,18 +36,28 @@ class FolderDeleter:
 | 
			
		|||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
extsToClean = ('.pyc', '.pyo', '.fsz', '.deltafsz', '.dat', '.log')
 | 
			
		||||
def cleanFolder(folder, exts=extsToClean, verbose=False):
 | 
			
		||||
def cleanFolder(folder, exts=extsToClean, folders=(), verbose=False):
 | 
			
		||||
    '''This function allows to remove, in p_folder and subfolders, any file
 | 
			
		||||
       whose extension is in p_exts.'''
 | 
			
		||||
       whose extension is in p_exts, and any folder whose name is in
 | 
			
		||||
       p_folders.'''
 | 
			
		||||
    if verbose: print 'Cleaning folder', folder, '...'
 | 
			
		||||
    # Remove files with an extension listed in exts
 | 
			
		||||
    # Remove files with an extension listed in p_exts
 | 
			
		||||
    if exts:
 | 
			
		||||
        for root, dirs, files in os.walk(folder):
 | 
			
		||||
            for fileName in files:
 | 
			
		||||
                ext = os.path.splitext(fileName)[1]
 | 
			
		||||
                if (ext in exts) or ext.endswith('~'):
 | 
			
		||||
                    fileToRemove = os.path.join(root, fileName)
 | 
			
		||||
                if verbose: print 'Removing %s...' % fileToRemove
 | 
			
		||||
                    if verbose: print 'Removing file %s...' % fileToRemove
 | 
			
		||||
                    os.remove(fileToRemove)
 | 
			
		||||
    # Remove folders whose names are in p_folders.
 | 
			
		||||
    if folders:
 | 
			
		||||
        for root, dirs, files in os.walk(folder):
 | 
			
		||||
            for folderName in dirs:
 | 
			
		||||
                if folderName in folders:
 | 
			
		||||
                    toDelete = os.path.join(root, folderName)
 | 
			
		||||
                    if verbose: print 'Removing folder %s...' % toDelete
 | 
			
		||||
                    FolderDeleter.delete(toDelete)
 | 
			
		||||
 | 
			
		||||
# ------------------------------------------------------------------------------
 | 
			
		||||
def copyFolder(source, dest, cleanDest=False):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue