"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/init.py" (1 Jan 2020, 6101 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "init.py": 1.6.1_vs_2.0.0.

    1 #
    2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
    3 # This module is free software, and you may redistribute it and/or modify
    4 # under the same terms as Python, so long as this copyright message and
    5 # disclaimer are retained in their original form.
    6 #
    7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
    8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
    9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
   10 # POSSIBILITY OF SUCH DAMAGE.
   11 #
   12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
   13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
   15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
   16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
   17 #
   18 """Init (create) a roundup instance.
   19 """
   20 from __future__ import print_function
   21 __docformat__ = 'restructuredtext'
   22 
   23 import os, errno, email.parser
   24 
   25 
   26 from roundup import install_util
   27 from roundup.configuration import CoreConfig
   28 from roundup.i18n import _
   29 
   30 
   31 def copytree(src, dst, symlinks=0):
   32     """Recursively copy a directory tree using copyDigestedFile().
   33 
   34     The destination directory is allowed to exist.
   35 
   36     If the optional symlinks flag is true, symbolic links in the
   37     source tree result in symbolic links in the destination tree; if
   38     it is false, the contents of the files pointed to by symbolic
   39     links are copied.
   40 
   41     This was copied from shutil.py in std lib.
   42     """
   43 
   44     # Prevent 'hidden' files (those starting with '.') from being considered.
   45     names = [f for f in os.listdir(src) if not f.startswith('.')]
   46     try:
   47         os.mkdir(dst)
   48     except OSError as error:
   49         if error.errno != errno.EEXIST: raise   # noqa
   50     for name in names:
   51         srcname = os.path.join(src, name)
   52         dstname = os.path.join(dst, name)
   53         if symlinks and os.path.islink(srcname):
   54             linkto = os.readlink(srcname)
   55             os.symlink(linkto, dstname)
   56         elif os.path.isdir(srcname):
   57             copytree(srcname, dstname, symlinks)
   58         else:
   59             install_util.copyDigestedFile(srcname, dstname)
   60 
   61 
   62 def install(instance_home, template, settings={}):
   63     '''Install an instance using the named template and backend.
   64 
   65     'instance_home'
   66        the directory to place the instance data in
   67     'template'
   68        the directory holding the template to use in creating the instance data
   69     'settings'
   70        config.ini setting overrides (dictionary)
   71 
   72     The instance_home directory will be created using the files found in
   73     the named template (roundup.templates.<name>). A usual instance_home
   74     contains:
   75 
   76     config.ini
   77       tracker configuration file
   78     schema.py
   79       database schema definition
   80     initial_data.py
   81       database initialization script, used to populate the database
   82       with 'roundup-admin init' command
   83     interfaces.py
   84       (optional, not installed from standard templates) defines
   85       the CGI Client and mail gateway MailGW classes that are
   86       used by roundup.cgi, roundup-server and roundup-mailgw.
   87     db/
   88       the actual database that stores the instance's data
   89     html/
   90       the html templates that are used by the CGI Client
   91     detectors/
   92       the auditor and reactor modules for this instance
   93     extensions/
   94       code extensions to Roundup
   95     '''
   96     # At the moment, it's just a copy
   97     copytree(template, instance_home)
   98 
   99     # rename the tempate in the TEMPLATE-INFO.txt file
  100     ti = loadTemplateInfo(instance_home)
  101     ti['name'] = ti['name'] + '-' + os.path.split(instance_home)[1]
  102     saveTemplateInfo(instance_home, ti)
  103 
  104     # if there is no config.ini or old-style config.py
  105     # installed from the template, write default config text
  106     config_ini_file = os.path.join(instance_home, CoreConfig.INI_FILE)
  107     if not os.path.isfile(config_ini_file):
  108         config = CoreConfig(settings=settings)
  109         config.save(config_ini_file)
  110 
  111 
  112 def listTemplates(dir):
  113     ''' List all the Roundup template directories in a given directory.
  114 
  115         Find all the dirs that contain a TEMPLATE-INFO.txt and parse it.
  116 
  117         Return a list of dicts of info about the templates.
  118     '''
  119     ret = {}
  120     for idir in os.listdir(dir):
  121         idir = os.path.join(dir, idir)
  122         ti = loadTemplateInfo(idir)
  123         if ti:
  124             ret[ti['name']] = ti
  125     return ret
  126 
  127 
  128 def loadTemplateInfo(path):
  129     ''' Attempt to load a Roundup template from the indicated directory.
  130 
  131         Return None if there's no template, otherwise a template info
  132         dictionary.
  133     '''
  134     tif = os.path.join(path, 'TEMPLATE-INFO.txt')
  135     if not os.path.exists(tif):
  136         return None
  137 
  138     if os.path.exists(os.path.join(path, 'config.py')):
  139         print(_("WARNING: directory '%s'\n"
  140                 "\tcontains old-style template - ignored")
  141               % os.path.abspath(path))
  142         return None
  143 
  144     # load up the template's information
  145     try:
  146         f = open(tif)
  147         m = email.parser.Parser().parse(f, True)
  148         ti = {}
  149         ti['name'] = m['name']
  150         ti['description'] = m['description']
  151         ti['intended-for'] = m['intended-for']
  152         ti['path'] = path
  153     finally:
  154         f.close()
  155     return ti
  156 
  157 
  158 def writeHeader(name, value):
  159     ''' Write an rfc822-compatible header line, making it wrap reasonably
  160     '''
  161     out = [name.capitalize() + ':']
  162     n = len(out[0])
  163     for word in value.split():
  164         if len(word) + n > 74:
  165             out.append('\n')
  166             n = 0
  167         out.append(' ' + word)
  168         n += len(out[-1])
  169     return ''.join(out) + '\n'
  170 
  171 
  172 def saveTemplateInfo(dir, info):
  173     ''' Save the template info (dict of values) to the TEMPLATE-INFO.txt
  174         file in the indicated directory.
  175     '''
  176     ti = os.path.join(dir, 'TEMPLATE-INFO.txt')
  177     f = open(ti, 'w')
  178     try:
  179         for name in 'name description intended-for path'.split():
  180             f.write(writeHeader(name, info[name]))
  181     finally:
  182         f.close()
  183 
  184 # vim: set filetype=python sts=4 sw=4 et si :