[Erp5-report] r36080 luke - in /erp5/trunk/utils/erp5.timmy/src/erp5/timmy: ./ examples/ te...

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Jun 8 11:41:31 CEST 2010


Author: luke
Date: Tue Jun  8 11:41:30 2010
New Revision: 36080

URL: http://svn.erp5.org?rev=36080&view=rev
Log:
 - move templates from examples to template directory inside of package
 - use pkg_resources to access package provided data
 - make -t parameter optional and use package distributed templates as fallback
 - try to continue even in case if no templates are found
 - remove not used missing_template_list
 - use %r to present pathnames in log

Added:
    erp5/trunk/utils/erp5.timmy/src/erp5/timmy/template/
      - copied from r36075, erp5/trunk/utils/erp5.timmy/src/erp5/timmy/examples/
Removed:
    erp5/trunk/utils/erp5.timmy/src/erp5/timmy/examples/
Modified:
    erp5/trunk/utils/erp5.timmy/src/erp5/timmy/timmy.py

Modified: erp5/trunk/utils/erp5.timmy/src/erp5/timmy/timmy.py
URL: http://svn.erp5.org/erp5/trunk/utils/erp5.timmy/src/erp5/timmy/timmy.py?rev=36080&r1=36079&r2=36080&view=diff
==============================================================================
--- erp5/trunk/utils/erp5.timmy/src/erp5/timmy/timmy.py [utf8] (original)
+++ erp5/trunk/utils/erp5.timmy/src/erp5/timmy/timmy.py [utf8] Tue Jun  8 11:41:30 2010
@@ -1,4 +1,5 @@
 import uuid, os, xmlrpclib, subprocess, logging
+import pkg_resources
 from string import Template
 from optparse import OptionParser
 import sys
@@ -77,7 +78,6 @@
       'key_file',
       'main_output',
       'server_url',
-      'template_directory',
       'pid_file'
     ]
   missing_required_option_list = [o for o in required_option_list \
@@ -94,7 +94,6 @@
   elif not os.path.isdir(options.instances_directory):
     raise ValueError('File %s is not a directory' %
         os.path.abspath(options.instances_directory))
-  missing_template_list = []
   return options, args
 
 def setRunning(value, pid_file):
@@ -123,12 +122,20 @@
     logging.critical('Timmy could not write pidfile %s' % pid_file)
     raise
 
-def updateBaseProfile(template_directory, file_output, base_profile,
+def findTemplate(template_directory_list, template_name):
+  for template_directory in template_directory_list:
+    path = os.path.join(template_directory, template_name)
+    if os.path.isfile(path):
+      logging.debug('Found template %r' % path)
+      return path
+  return None
+
+def updateBaseProfile(template_directory_list, file_output, base_profile,
     instances_directory, instance_dict_list):
   # TODO:
   #  * cleanup in case of problem
   #  * use safe update of output file
-  template_data = ''.join(file(os.path.join(template_directory,
+  template_data = ''.join(file(findTemplate(template_directory_list,
     'main-template.cfg')).readlines())
   template = PercentTemplate(template_data)
   replacement_dict = {
@@ -140,7 +147,7 @@
     profile_path = os.path.join(instances_directory,
         '%s.cfg' % instance['ID'])
     if not os.path.exists(profile_path):
-      logging.warning('Profile %s not generated, ignoring' % profile_path)
+      logging.warning('Profile %r not generated, ignoring' % profile_path)
       continue
     replacement_dict['INSTANCE_PROFILE_LIST'].append('%s/%s.cfg' % (
       instances_directory, instance['ID']))
@@ -150,7 +157,7 @@
     profile_path = os.path.join(instances_directory,
         '%s.cfg' % instance['ID'])
     if not os.path.exists(profile_path):
-      logging.warning('Profile %s not generated' % profile_path)
+      logging.warning('Profile %r not generated' % profile_path)
       continue
     replacement_dict['INSTANCE_PROFILE_LIST'].append('%s/%s.cfg' % (
       instances_directory, instance['ID']))
@@ -164,14 +171,15 @@
   out.write(template.substitute(replacement_dict))
   out.close()
 
-def updateInstanceProfiles(template_directory, output_directory,
+def updateInstanceProfiles(template_directory_list, output_directory,
     instance_dict_list):
   for instance in instance_dict_list:
     template_name = '%s-template.cfg' % instance['TYPE'].lower()\
       .replace(' ','-')
-    template_path = os.path.join(template_directory, template_name)
-    if not os.path.exists(template_path):
-      logging.warning('Template for %s not found, ignoring' % instance['TYPE'])
+    template_path = findTemplate(template_directory_list, template_name)
+    if template_path is None:
+      logging.warning('Template %r for %s not found, ignoring' % (template_name,
+        instance['TYPE']))
       continue
     template_data = ''.join(file(template_path).readlines())
 
@@ -572,6 +580,29 @@
   print 'Server key located at: %s' % os.path.abspath(options.key_file)
   print 'Key: %s' % key
 
+def getTemplateDirectoryList(user_template_directory):
+  """Returns list of directories, in which there might be templates"""
+  template_directory_list = []
+  if pkg_resources.resource_isdir(__name__, 'template'):
+    template_directory = pkg_resources.resource_filename(__name__, "template")
+    template_directory_list.append(template_directory)
+    logging.info('Using %r as fallback directory with templates' %
+        template_directory)
+
+  if user_template_directory is not None:
+    if os.path.isdir(user_template_directory):
+      template_directory = os.path.abspath(user_template_directory)
+      template_directory_list.append(template_directory)
+      logging.info('Using %r as default directory with templates' %
+          template_directory)
+    else:
+      logging.warning('User template %r is not a directory' %
+          template_directory)
+  if len(template_directory_list) == 0:
+    logging.warning('No templates were found, some functionality will be not available')
+  template_directory_list.reverse()
+  return template_directory_list
+
 def run():
   socket.setdefaulttimeout(30.0)
   (options, args) = parseOptions()
@@ -582,6 +613,7 @@
   logging.basicConfig(**logging_kw)
   time_begin = time.time()
   logging.info('[%s] Timmy started' % os.getpid())
+  options.template_directory_list = getTemplateDirectoryList(options.template_directory)
   try:
     setRunning(True, options.pid_file)
   except:
@@ -643,7 +675,7 @@
 
       # 1a pass - instance profiles
       try:
-        updateInstanceProfiles(options.template_directory,
+        updateInstanceProfiles(options.template_directory_list,
           options.instances_directory, partition_dict_list)
       except:
         server.call('updatePartitionState', computer_id, 'reportError',
@@ -652,7 +684,7 @@
         raise
       # 1b pass - main profile
       try:
-        updateBaseProfile(options.template_directory, options.main_output,
+        updateBaseProfile(options.template_directory_list, options.main_output,
           options.base_profile, options.instances_directory,
           partition_dict_list)
       except:




More information about the Erp5-report mailing list