def _update_template_styles(pod_template, style_template_filename):
    """
    Update template pod_template by templateStyle.
    """
    # save in temporary file, the template
    temp_file = create_temporary_file(pod_template.odt_file,
                                      'pod_template.odt')
    new_template = open(temp_file.name, 'w')
    new_template.write(pod_template.odt_file.data)
    new_template.close()

    # merge style from templateStyle in template
    cmd = '{path} {script} {tmp_file} {extension} -p{port} -t{style_template}'.format(
        path=config.get_uno_path(),
        script=CONVSCRIPT,
        tmp_file=temp_file.name,
        extension='odt',
        port=config.get_oo_port(),
        style_template=style_template_filename)
    executeCommand(cmd.split())

    # read the merged file
    resTempFileName = '.res.'.join(temp_file.name.rsplit('.', 1))
    if os.path.isfile(resTempFileName):
        resTemplate = open(resTempFileName, 'rb')
        # update template
        result = NamedBlobFile(data=resTemplate.read(),
                               contentType='applications/odt')
        pod_template.odt_file = result
        os.remove(resTempFileName)

    os.remove(temp_file.name)
Ejemplo n.º 2
0
 def installPlone25or30Stuff(self, linksForProducts):
     '''Here, we will copy all Plone2-related stuff in the Zope instance
        we've created, to get a full Plone-ready Zope instance. If
        p_linksForProducts is True, we do not perform a real copy: we will
        create symlinks to products lying within Plone installer files.'''
     j = os.path.join
     if self.ploneVersion == 'plone25':
         sourceFolders = ('zeocluster/Products',)
     else:
         sourceFolders = ('zinstance/Products', 'zinstance/lib/python')
     for sourceFolder in sourceFolders:
         sourceBase = j(self.plonePath, sourceFolder)
         destBase = j(self.instancePath,
                      sourceFolder[sourceFolder.find('/')+1:])
         for name in os.listdir(sourceBase):
             folderName = j(sourceBase, name)
             if os.path.isdir(folderName):
                 destFolder = j(destBase, name)
                 # This is a Plone product. Copy it to the instance.
                 if linksForProducts:
                     # Create a symlink to this product in the instance
                     executeCommand(['ln', '-s', folderName, destFolder])
                 else:
                     # Copy thre product into the instance
                     copyFolder(folderName, destFolder)
def _update_template_styles(pod_template, style_template_filename):
    """
    Update template pod_template by templateStyle.
    """
    # save in temporary file, the template
    temp_file = create_temporary_file(pod_template.odt_file, 'pod_template.odt')
    new_template = open(temp_file.name, 'w')
    new_template.write(pod_template.odt_file.data)
    new_template.close()

    # merge style from templateStyle in template
    cmd = '{path} {script} {tmp_file} {extension} -p{port} -t{style_template}'.format(
        path=config.get_uno_path(),
        script=CONVSCRIPT,
        tmp_file=temp_file.name,
        extension='odt',
        port=config.get_oo_port(),
        style_template=style_template_filename
    )
    executeCommand(cmd.split())

    # read the merged file
    resTempFileName = '.res.'.join(temp_file.name.rsplit('.', 1))
    if os.path.isfile(resTempFileName):
        resTemplate = open(resTempFileName, 'rb')
        # update template
        result = NamedBlobFile(data=resTemplate.read(), contentType='applications/odt')
        pod_template.odt_file = result
        os.remove(resTempFileName)

    os.remove(temp_file.name)
Ejemplo n.º 4
0
 def packZodb(self):
     '''Packs the ZODB and keeps one week history'''
     storage = ZODB.FileStorage.FileStorage(self.storageLocation)
     #storage.pack(time.time()-(7*24*60*60), ZODB.serialize.referencesf)
     storage.pack(time.time()-self.keepSeconds, ZODB.serialize.referencesf)
     for fileSuffix in ('', '.index'):
         fileName = self.storageLocation + fileSuffix
         executeCommand(['chown', self.zopeUser, fileName])
Ejemplo n.º 5
0
 def run(self):
     startTime = time.time()
     # Build the command
     cmd = [self.python, self.repozo, '-Rv', '-r', self.backupFolder]
     if self.restoreDate: cmd += ['-D', self.restoreDate]
     cmd += ['-o', self.storageLocation]
     print('Executing %s...' % str(cmd))
     executeCommand(cmd)
     stopTime = time.time()
     print('Done in %d minute(s).' % ((stopTime-startTime)/60))
Ejemplo n.º 6
0
 def run(self):
     # Call LO in server mode to convert self.odtFile to PDF
     converter = os.path.join(self.appyFolder, 'pod', 'converter.py')
     cmd = ['python', converter, self.odtFile, 'pdf', '-p%d' % self.port]
     print(cmd)
     executeCommand(cmd)
     # Check if the PDF was generated
     pdfFile = '%s.pdf' % os.path.splitext(self.odtFile)[0]
     if not os.path.exists(pdfFile):
         print('PDF was not generated.')
     else:
         os.remove(pdfFile)
         print('Check successfull.')
Ejemplo n.º 7
0
 def run(self):
     # Create the instance folder hierarchy
     if not os.path.exists(self.instancePath):
         os.makedirs(self.instancePath)
     curdir = os.getcwd()
     # Create bin/zopectl
     os.chdir(self.instancePath)
     os.mkdir('bin')
     f = file('bin/zopectl', 'w')
     f.write(zopeCtl % self.instancePath)
     f.close()
     os.chmod('bin/zopectl', 0744) # Make it executable by owner.
     # Create bin/runzope
     f = file('bin/runzope', 'w')
     f.write(runZope % self.instancePath)
     f.close()
     os.chmod('bin/runzope', 0744) # Make it executable by owner.
     # Create bin/startlo
     f = file('bin/startlo', 'w')
     f.write(loStart)
     f.close()
     os.chmod('bin/startlo', 0744) # Make it executable by owner.
     # Create etc/zope.conf
     os.mkdir('etc')
     f = file('etc/zope.conf', 'w')
     f.write(zopeConf % (self.instancePath, '%s/var' % self.instancePath,
                         '%s/log' % self.instancePath, '8080', ''))
     f.close()
     # Create other folders
     for name in ('Extensions', 'log', 'Products', 'var'): os.mkdir(name)
     f = file('Products/__init__.py', 'w')
     f.write('#Makes me a Python package.\n')
     f.close()
     # Create 'inituser' file with admin password
     import binascii
     try:
         from hashlib import sha1 as sha
     except:
         from sha import new as sha
     f = open('inituser', 'w')
     password = binascii.b2a_base64(sha('admin').digest())[:-1]
     f.write('admin:{SHA}%s\n' % password)
     f.close()
     os.chmod('inituser', 0644)
     # User "zope" must own this instance
     executeCommand('chown', '-R', 'zope', self.instancePath)
     print('Zope instance created in %s.' % self.instancePath)
     os.chdir(curdir)
Ejemplo n.º 8
0
 def executeCommand(self, cmd):
     '''Executes command p_cmd'''
     w = self.log
     w('Executing "%s"...' % cmd)
     out, err = executeCommand(cmd)
     if out: w(out)
     if err: w(err)
     w('Done.')
Ejemplo n.º 9
0
 def convert(self, fileName, format):
     """Launches a UNO-enabled Python interpreter as defined in the self for
        converting, using OpenOffice in server mode, a file named p_fileName
        into an output p_format."""
     convScript = "%s/pod/converter.py" % os.path.dirname(appy.__file__)
     cmd = '%s %s "%s" %s -p%d' % (self.unoEnabledPython, convScript, fileName, format, self.openOfficePort)
     self.log("Executing %s..." % cmd)
     return executeCommand(cmd)  # The result can contain an error message
Ejemplo n.º 10
0
 def convert(self, fileName, format):
     '''Launches a UNO-enabled Python interpreter as defined in the self for
        converting, using OpenOffice in server mode, a file named p_fileName
        into an output p_format.'''
     convScript = '%s/pod/converter.py' % os.path.dirname(appy.__file__)
     cmd = '%s %s "%s" %s -p%d' % (self.unoEnabledPython, convScript,
                                   fileName, format, self.openOfficePort)
     self.log('executing %s...' % cmd)
     return executeCommand(cmd) # The result can contain an error message
Ejemplo n.º 11
0
 def convert(self, fileName, format):
     '''Launches a UNO-enabled Python interpreter as defined in the tool for
        converting, using LibreOffice in server mode, a file named p_fileName
        into an output p_format.'''
     convScript = '%s/pod/converter.py' % os.path.dirname(appy.__file__)
     cfg = self.o.getProductConfig(True)
     cmd = [cfg.unoEnabledPython, convScript, fileName, format,
            '-p%d' % cfg.libreOfficePort]
     self.log('executing %s...' % str(cmd))
     return executeCommand(cmd) # The result is a tuple (s_out, s_err)
def _update_template_styles(pod_template, style_template_filename):
    """
    Update template pod_template by templateStyle.
    """
    # we check if the pod_template has been modified except by style only
    style_changes_only = pod_template.style_modification_md5 and \
        pod_template.current_md5 == pod_template.style_modification_md5
    # save in temporary file, the template
    temp_file = create_temporary_file(pod_template.odt_file, 'pod_template.odt')
    new_template = open(temp_file.name, 'w')
    new_template.write(pod_template.odt_file.data)
    new_template.close()

    # merge style from templateStyle in template
    cmd = '{path} {script} {tmp_file} {extension} -p{port} -t{style_template}'.format(
        path=config.get_uno_path(),
        script=CONVSCRIPT,
        tmp_file=temp_file.name,
        extension='odt',
        port=config.get_oo_port(),
        style_template=style_template_filename
    )
    (stdout, stderr) = executeCommand(cmd.split())
    if stderr:
        logger.error("Error during command '%s'" % cmd)
        logger.error("Error is '%s'" % stderr)
        portal = api.portal.get()
        request = portal.REQUEST
        try:
            api.portal.show_message(message=_(u"Problem during styles update on template '${tmpl}': ${err}",
                                    mapping={'tmpl': safe_unicode(pod_template.absolute_url_path()),
                                             'err': safe_unicode(stderr)}),
                                    request=request, type='error')
        except:
            pass
        raise Redirect(request.get('ACTUAL_URL'), _(u"Problem during styles update on template '${tmpl}': ${err}",
                                                    mapping={'tmpl': safe_unicode(pod_template.absolute_url_path()),
                                                             'err': safe_unicode(stderr)}))

    # read the merged file
    resTempFileName = '.res.'.join(temp_file.name.rsplit('.', 1))
    if os.path.isfile(resTempFileName):
        resTemplate = open(resTempFileName, 'rb')
        # update template
        result = NamedBlobFile(data=resTemplate.read(),
                               contentType='application/vnd.oasis.opendocument.text',
                               filename=pod_template.odt_file.filename)
        pod_template.odt_file = result
        remove_tmp_file(resTempFileName)
        # if only styles were modified: update the style_modification_md5 attribute
        if style_changes_only:
            pod_template.style_modification_md5 = pod_template.current_md5

    remove_tmp_file(temp_file.name)
Ejemplo n.º 13
0
 def callOpenOffice(self, resultOdtName, resultType):
     '''Call Open Office in server mode to convert or update the ODT
        result.'''
     ooOutput = ''
     try:
         if (not isinstance(self.ooPort, int)) and \
         (not isinstance(self.ooPort, long)):
             raise PodError(BAD_OO_PORT % str(self.ooPort))
         try:
             from appy.pod.converter import Converter, ConverterError
             try:
                 print 'Calling converter on %s %s' % (resultOdtName, resultType)
                 Converter(resultOdtName, resultType,
                             self.ooPort).run()
             except ConverterError, ce:
                 print 'oops: %s' % format_exc()
                 raise PodError(CONVERT_ERROR % str(ce))
         except ImportError:
             # I do not have UNO. So try to launch a UNO-enabled Python
             # interpreter which should be in self.pyPath.
             print 'oops: %s' % format_exc()
             if not self.pyPath:
                 raise PodError(NO_PY_PATH % resultType)
             if self.pyPath.find(' ') != -1:
                 raise PodError(BLANKS_IN_PATH % self.pyPath)
             if not os.path.isfile(self.pyPath):
                 raise PodError(PY_PATH_NOT_FILE % self.pyPath)
             if resultOdtName.find(' ') != -1:
                 qResultOdtName = '"%s"' % resultOdtName
             else:
                 qResultOdtName = resultOdtName
             convScript = '%s/converter.py' % \
                         os.path.dirname(appy.pod.__file__)
             if convScript.find(' ') != -1:
                 convScript = '"%s"' % convScript
             cmd = '%s %s %s %s -p%d' % \
                 (self.pyPath, convScript, qResultOdtName, resultType,
                 self.ooPort)
             ooOutput = executeCommand(cmd)
     except PodError, pe:
         # When trying to call OO in server mode for producing
         # ODT (=forceOoCall=True), if an error occurs we still
         # have an ODT to return to the user. So we produce a
         # warning instead of raising an error.
         if (resultType == 'odt') and self.forceOoCall:
             print WARNING_INCOMPLETE_ODT % str(pe)
         else:
             raise pe
Ejemplo n.º 14
0
 def callLibreOffice(self, resultName, resultType):
     '''Call LibreOffice in server mode to convert or update the result.'''
     loOutput = ''
     try:
         if (not isinstance(self.ooPort, int)) and \
            (not isinstance(self.ooPort, int)):
             raise PodError(BAD_OO_PORT % str(self.ooPort))
         try:
             from appy.pod.converter import Converter, ConverterError
             try:
                 Converter(resultName, resultType, self.ooPort,
                           self.stylesTemplate).run()
             except ConverterError as ce:
                 raise PodError(CONVERT_ERROR % str(ce))
         except ImportError:
             # I do not have UNO. So try to launch a UNO-enabled Python
             # interpreter which should be in self.pyPath.
             if not self.pyPath:
                 raise PodError(NO_PY_PATH % resultType)
             if self.pyPath.find(' ') != -1:
                 raise PodError(BLANKS_IN_PATH % self.pyPath)
             if not os.path.isfile(self.pyPath):
                 raise PodError(PY_PATH_NOT_FILE % self.pyPath)
             if resultName.find(' ') != -1:
                 qResultName = '"%s"' % resultName
             else:
                 qResultName = resultName
             convScript = '%s/converter.py' % \
                         os.path.dirname(appy.pod.__file__)
             if convScript.find(' ') != -1:
                 convScript = '"%s"' % convScript
             cmd = '%s %s %s %s -p%d' % \
                 (self.pyPath, convScript, qResultName, resultType,
                 self.ooPort)
             if self.stylesTemplate: cmd += ' -t%s' % self.stylesTemplate
             loOutput = executeCommand(cmd)
     except PodError as pe:
         # When trying to call LO in server mode for producing ODT or ODS
         # (=forceOoCall=True), if an error occurs we have nevertheless
         # an ODT or ODS to return to the user. So we produce a warning
         # instead of raising an error.
         if (resultType in self.templateTypes) and self.forceOoCall:
             print((WARNING_INCOMPLETE_OD % str(pe)))
         else:
             raise pe
     return loOutput
Ejemplo n.º 15
0
 def callLibreOffice(self, resultName, resultType):
     '''Call LibreOffice in server mode to convert or update the result.'''
     loOutput = ''
     try:
         if (not isinstance(self.ooPort, int)) and \
            (not isinstance(self.ooPort, int)):
             raise PodError(BAD_OO_PORT % str(self.ooPort))
         try:
             from appy.pod.converter import Converter, ConverterError
             try:
                 Converter(resultName, resultType, self.ooPort,
                           self.stylesTemplate).run()
             except ConverterError as ce:
                 raise PodError(CONVERT_ERROR % str(ce))
         except ImportError:
             # I do not have UNO. So try to launch a UNO-enabled Python
             # interpreter which should be in self.pyPath.
             if not self.pyPath:
                 raise PodError(NO_PY_PATH % resultType)
             if self.pyPath.find(' ') != -1:
                 raise PodError(BLANKS_IN_PATH % self.pyPath)
             if not os.path.isfile(self.pyPath):
                 raise PodError(PY_PATH_NOT_FILE % self.pyPath)
             if resultName.find(' ') != -1:
                 qResultName = '"%s"' % resultName
             else:
                 qResultName = resultName
             convScript = '%s/converter.py' % \
                         os.path.dirname(appy.pod.__file__)
             if convScript.find(' ') != -1:
                 convScript = '"%s"' % convScript
             cmd = '%s %s %s %s -p%d' % \
                 (self.pyPath, convScript, qResultName, resultType,
                 self.ooPort)
             if self.stylesTemplate: cmd += ' -t%s' % self.stylesTemplate
             loOutput = executeCommand(cmd)
     except PodError as pe:
         # When trying to call LO in server mode for producing ODT or ODS
         # (=forceOoCall=True), if an error occurs we have nevertheless
         # an ODT or ODS to return to the user. So we produce a warning
         # instead of raising an error.
         if (resultType in self.templateTypes) and self.forceOoCall:
             print((WARNING_INCOMPLETE_OD % str(pe)))
         else:
             raise pe
     return loOutput
Ejemplo n.º 16
0
 def callOpenOffice(self, resultOdtName, resultType):
     '''Call Open Office in server mode to convert or update the ODT
        result.'''
     ooOutput = ''
     try:
         if (not isinstance(self.ooPort, int)) and \
         (not isinstance(self.ooPort, long)):
             raise PodError(BAD_OO_PORT % str(self.ooPort))
         try:
             from appy.pod.converter import Converter, ConverterError
             try:
                 Converter(resultOdtName, resultType,
                             self.ooPort).run()
             except ConverterError, ce:
                 raise PodError(CONVERT_ERROR % str(ce))
         except ImportError:
             # I do not have UNO. So try to launch a UNO-enabled Python
             # interpreter which should be in self.pyPath.
             if not self.pyPath:
                 raise PodError(NO_PY_PATH % resultType)
             if self.pyPath.find(' ') != -1:
                 raise PodError(BLANKS_IN_PATH % self.pyPath)
             if not os.path.isfile(self.pyPath):
                 raise PodError(PY_PATH_NOT_FILE % self.pyPath)
             if resultOdtName.find(' ') != -1:
                 qResultOdtName = '"%s"' % resultOdtName
             else:
                 qResultOdtName = resultOdtName
             convScript = '%s/converter.py' % \
                         os.path.dirname(appy.pod.__file__)
             if convScript.find(' ') != -1:
                 convScript = '"%s"' % convScript
             cmd = '%s %s %s %s -p%d' % \
                 (self.pyPath, convScript, qResultOdtName, resultType,
                 self.ooPort)
             ooOutput = executeCommand(cmd)
     except PodError, pe:
         # When trying to call OO in server mode for producing
         # ODT (=forceOoCall=True), if an error occurs we still
         # have an ODT to return to the user. So we produce a
         # warning instead of raising an error.
         if (resultType == 'odt') and self.forceOoCall:
             print WARNING_INCOMPLETE_ODT % str(pe)
         else:
             raise pe
Ejemplo n.º 17
0
 def walkFile(self, fileName):
     '''Unzip p_fileName in a temp folder, call self.script, and then re-zip
        the result.'''
     print 'Walking %s...' % fileName
     # Create a temp folder
     name = 'f%f' % time.time()
     tempFolder = os.path.join(self.tempFolder, name)
     os.mkdir(tempFolder)
     # Unzip the file in it
     unzip(fileName, tempFolder)
     # Call self.script
     py = sys.executable or 'python'
     cmd = [py, self.script, tempFolder]
     print '  Running %s...' % cmd,
     out, err = executeCommand(cmd)
     # Re-zip the result
     zip(fileName, tempFolder, odf=True)
     FolderDeleter.delete(tempFolder)
     print 'done.'
Ejemplo n.º 18
0
def _update_template_styles(pod_template, style_template_filename):
    """
    Update template pod_template by templateStyle.
    """
    # we check if the pod_template has been modified except by style only
    style_changes_only = \
        pod_template.style_modification_md5 and pod_template.current_md5 == pod_template.style_modification_md5
    # save in temporary file, the template
    temp_file = create_temporary_file(pod_template.odt_file,
                                      'pod_template.odt')
    new_template = open(temp_file.name, 'w')
    new_template.write(pod_template.odt_file.data)
    new_template.close()

    # merge style from templateStyle in template
    cmd = '{path} {script} {tmp_file} {extension} -e ' \
          '{libreoffice_host} -p {port} ' \
          '-t {style_template} -v -a {stream}'.format(path=config.get_uno_path(),
                                                      script=CONVSCRIPT,
                                                      tmp_file=temp_file.name,
                                                      extension='odt',
                                                      libreoffice_host=config.get_oo_server(),
                                                      port=config.get_oo_port(),
                                                      style_template=style_template_filename,
                                                      stream=config.get_use_stream())
    (stdout, stderr) = executeCommand(cmd.split())
    if stderr:
        logger.error("Error during command '%s'" % cmd)
        logger.error("Error is '%s'" % stderr)
        portal = api.portal.get()
        request = portal.REQUEST
        api.portal.show_message(message=_(
            u"Problem during styles update on template '${tmpl}': ${err}",
            mapping={
                'tmpl': safe_unicode(pod_template.absolute_url_path()),
                'err': safe_unicode(stderr)
            }),
                                request=request,
                                type='error')
        raise Redirect(
            request.get('ACTUAL_URL'),
            translate(
                _(u"Problem during styles update on template '${tmpl}': ${err}",
                  mapping={
                      'tmpl': safe_unicode(pod_template.absolute_url_path()),
                      'err': safe_unicode(stderr)
                  })))

    # read the merged file
    resTempFileName = '.res.'.join(temp_file.name.rsplit('.', 1))
    if os.path.isfile(resTempFileName):
        resTemplate = open(resTempFileName, 'rb')
        # update template
        result = NamedBlobFile(
            data=resTemplate.read(),
            contentType='application/vnd.oasis.opendocument.text',
            filename=pod_template.odt_file.filename)
        pod_template.odt_file = result
        remove_tmp_file(resTempFileName)
        # if only styles were modified: update the style_modification_md5 attribute
        if style_changes_only:
            pod_template.style_modification_md5 = pod_template.current_md5

    remove_tmp_file(temp_file.name)
Ejemplo n.º 19
0
 def run(self):
     '''Generates the Debian package.'''
     curdir = os.getcwd()
     j = os.path.join
     tempFolder = getOsTempFolder()
     # Create, in the temp folder, the required sub-structure for the Debian
     # package.
     debFolder = j(tempFolder, 'debian')
     if os.path.exists(debFolder):
         FolderDeleter.delete(debFolder)
     # Copy the Python package into it
     srcFolder = j(debFolder, 'usr', 'lib')
     for version in self.pythonVersions:
         libFolder = j(srcFolder, 'python%s' % version)
         os.makedirs(libFolder)
         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()
         # startlo
         name = '%s/startlo' % binFolder
         f = file(name, 'w')
         f.write(loStart)
         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, str(self.zopePort),
                             'products %s\n' % productsFolder))
         f.close()
         # /etc/init.d/<app> (start the app at boot time)
         initdFolder = j(etcFolder, 'init.d')
         os.makedirs(initdFolder)
         name = '%s/%s' % (initdFolder, self.appNameLower)
         f = file(name, 'w')
         n = self.appNameLower
         f.write(initScript % (n, n, 'Start Zope with the Appy-based %s ' \
                               'application.' % n, '%sctl start' % n,
                               '%sctl restart' % n, '%sctl stop' % n))
         f.close()
         os.chmod(name, 0744) # Make it executable by owner
         # /etc/init.d/lo (start LibreOffice at boot time)
         name = '%s/lo' % initdFolder
         f = file(name, 'w')
         f.write(initScript % ('lo','lo', 'Start LibreOffice in server mode',
                               'startlo', 'startlo', "#Can't stop LO."))
         f.write('\n')
         f.close()
         os.chmod(name, 0744) # Make it executable by owner.
     # Get the size of the app, in Kb.
     os.chdir(tempFolder)
     out, err = executeCommand(['du', '-b', '-s', 'debian'])
     size = int(int(out.split()[0])/1024.0)
     os.chdir(debFolder)
     # Create data.tar.gz based on it
     executeCommand(['tar', 'czvf', 'data.tar.gz', '*'])
     # Create the control file
     f = file('control', 'w')
     nameSuffix = ''
     dependencies = []
     if self.appName != 'appy':
         nameSuffix = '-%s' % self.appNameLower
         dependencies.append('python-appy')
     if self.depends:
         for d in self.depends: dependencies.append(d)
     depends = ''
     if dependencies:
         depends = ', ' + ', '.join(dependencies)
     f.write(debianInfo % (nameSuffix, self.appVersion, size,
                           self.pythonVersions[0], depends))
     f.close()
     # Create md5sum file
     f = file('md5sums', 'w')
     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)
                 currentFile = file(pathName, 'rb')
                 while True:
                     data = currentFile.read(8096)
                     if not data:
                         break
                     m.update(data)
                 currentFile.close()
                 # Add the md5 sum to the file
                 f.write('%s  %s\n' % (m.hexdigest(), pathName))
     f.close()
     # Create postinst, a script that will:
     # - bytecompile Python files after the Debian install
     # - change ownership of some files if required
     # - [in the case of an app-package] call update-rc.d for starting it at
     #   boot time.
     f = file('postinst', 'w')
     content = '#!/bin/sh\nset -e\n'
     for version in self.pythonVersions:
         bin = '/usr/bin/python%s' % version
         lib = '/usr/lib/python%s' % version
         cmds = ' %s -m compileall -q %s/%s 2> /dev/null\n' % (bin, lib,
                                                               self.appName)
         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
         # Call update-rc.d for starting the app at boot time
         content += 'update-rc.d %s defaults\n' % self.appNameLower
         content += 'update-rc.d lo defaults\n'
         # (re-)start the app
         content += '%sctl restart\n' % self.appNameLower
         # (re-)start lo
         content += 'startlo\n'
     f.write(content)
     f.close()
     # Create prerm, a script that will remove all pyc files before removing
     # the Debian package.
     f = file('prerm', 'w')
     content = '#!/bin/sh\nset -e\n'
     for version in self.pythonVersions:
         content += 'find /usr/lib/python%s/%s -name "*.pyc" -delete\n' % \
                    (version, self.appName)
     f.write(content)
     f.close()
     # Create control.tar.gz
     executeCommand(['tar', 'czvf', 'control.tar.gz', './control',
                     './md5sums', './postinst', './prerm'])
     # Create debian-binary
     f = file('debian-binary', 'w')
     f.write('2.0\n')
     f.close()
     # Create the signature if required
     if self.sign:
         # Create the concatenated version of all files within the deb
         out, err = executeCommand(['cat', 'debian-binary', 'control.tar.gz',
                                    'data.tar.gz'])
         f = file('/tmp/combined-contents', 'wb')
         f.write(out)
         f.close()
         executeCommand(['gpg', '-abs', '-o', '_gpgorigin',
                         '/tmp/combined-contents'])
         signFile = '_gpgorigin'
         os.remove('/tmp/combined-contents')
         # Export the public key and name it according to its ID as found by
         # analyzing the result of command "gpg --fingerprint".
         out, err = executeCommand(['gpg', '--fingerprint'])
         fingerprint = out.split('\n')
         id = 'pubkey'
         for line in fingerprint:
             if '=' not in line: continue
             id = line.split('=')[1].strip()
             id = ''.join(id.split()[-4:])
             break
         out, err = executeCommand(['gpg', '--export', '-a'])
         f = file('%s/%s.asc' % (self.out, id), 'w')
         f.write(out)
         f.close()
     else:
         signFile = None
     # Create the .deb package
     debName = 'python-appy%s-%s.deb' % (nameSuffix, self.appVersion)
     cmd = ['ar', '-r', debName]
     if signFile: cmd.append(signFile)
     cmd += ['debian-binary', 'control.tar.gz', 'data.tar.gz']
     out, err = executeCommand(cmd)
     # Move it to self.out
     os.rename(j(debFolder, debName), j(self.out, debName))
     # Clean temp files
     FolderDeleter.delete(debFolder)
     os.chdir(curdir)
Ejemplo n.º 20
0
from appy.shared.utils import executeCommand

# Packages apache2 and dpkg-dev must be installed on the machine for enabling
# the Debian repository.
repoFolder = '/var/www/debianrepo'

# Create the repo folder if it does not exist
binaryFolder = os.path.join(repoFolder, 'binary')
if not os.path.exists(binaryFolder):
    os.makedirs(binaryFolder)

# Create the script that will allow to recompute indexes when packages are
# added or updated into the repository.
refreshScript = '''#!/bin/bash
cd %s
echo "(Re-)building indexes for binary packages..."
dpkg-scanpackages binary /dev/null | gzip -9c > binary/Packages.gz
echo "Done."
''' % repoFolder

curdir = os.getcwd()
os.chdir(repoFolder)
scriptName = os.path.join(repoFolder, 'refresh.sh')
if not os.path.exists(scriptName):
    f = file(scriptName, 'w')
    f.write(refreshScript)
    f.close()
executeCommand(['chmod', '-R', '755', repoFolder])
os.chdir(curdir)
print('Repository created.')
Ejemplo n.º 21
0
 def callGrep(self, folder):
     '''Performs a "grep" with self.keyword in p_folder.'''
     cmd = ['grep', '-irn', self.keyword, folder]
     out, err = executeCommand(cmd)
     return bool(out)
Ejemplo n.º 22
0
 def createInstance(self, linksForProducts):
     '''Calls the Zope script that allows to create a Zope instance and copy
        into it all the Plone packages and products.'''
     j = os.path.join
     # Find the Python interpreter running Zope
     for elem in os.listdir(self.plonePath):
         pythonPath = None
         elemPath = j(self.plonePath, elem)
         if elem.startswith('Python-') and os.path.isdir(elemPath):
             pythonPath = elemPath + '/bin/python'
             if not os.path.exists(pythonPath):
                 raise NewError(PYTHON_EXE_NOT_FOUND % pythonPath)
             break
     if not pythonPath:
         raise NewError(PYTHON_NOT_FOUND % self.plonePath)
     self.pythonPath = pythonPath
     # Find the Zope script mkzopeinstance.py and Zope itself
     makeInstancePath = None
     self.zopePath = None
     for dirname, dirs, files in os.walk(self.plonePath):
         # Find Zope
         for folderName in dirs:
             if folderName.startswith('Zope2-'):
                 self.zopePath = j(dirname, folderName)
         # Find mkzopeinstance
         for fileName in files:
             if fileName == 'mkzopeinstance.py':
                 if self.ploneVersion == 'plone4':
                     makeInstancePath = j(dirname, fileName)
                 else:
                     if ('/buildout-cache/' not in dirname):
                         makeInstancePath = j(dirname, fileName)
     if not makeInstancePath:
         raise NewError(MKZOPE_NOT_FOUND % self.plonePath)
     # Execute mkzopeinstance.py with the right Python interpreter.
     # For Plone4, we will call it later.
     cmd = [pythonPath, makeInstancePath, '-d', self.instancePath]
     if self.ploneVersion != 'plone4':
         print(cmd)
         executeCommand(cmd)
     # Now, make the instance Plone-ready
     action = 'Copying'
     if linksForProducts:
         action = 'Symlinking'
     print('%s Plone stuff in the Zope instance...' % action)
     if self.ploneVersion in ('plone25', 'plone30'):
         self.installPlone25or30Stuff(linksForProducts)
     elif self.ploneVersion in ('plone3x', 'plone4'):
         versions = self.copyEggs()
         if self.ploneVersion == 'plone3x':
             self.patchPlone3x()
         elif self.ploneVersion == 'plone4':
             # Create the Zope instance
             os.environ['PYTHONPATH'] = '%s:%s' % \
                 (j(self.instancePath,'Products'),
                  j(self.instancePath, 'lib/python'))
             print(cmd)
             executeCommand(cmd)
             self.patchPlone4(versions)
     # Remove .bat files under Linux
     if os.name == 'posix':
         cleanFolder(j(self.instancePath, 'bin'), exts=('.bat',))