CheckSkins.py 13.3 KB
Newer Older
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1 2 3 4
from Globals import get_request
import re
import os
import sys
Yoshinori Okuji's avatar
Yoshinori Okuji committed
5
import csv
Yoshinori Okuji's avatar
Yoshinori Okuji committed
6
from Products.CMFCore.utils import expandpath
Yoshinori Okuji's avatar
Yoshinori Okuji committed
7 8

from zLOG import LOG
Yoshinori Okuji's avatar
Yoshinori Okuji committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

try:
    from App.config import getConfiguration
except ImportError:
    getConfiguration = None

if getConfiguration is None:
  data_dir = '/var/lib/zope/data'
else:
  data_dir = getConfiguration().instancehome + '/data'

fs_skin_spec = ('ERP5 Filesystem Formulator Form',
                'ERP5 Filesystem PDF Template',
                'Filesystem Formulator Form',
                'Filesystem Page Template',
                'Filesystem Script (Python)',
                'Filesystem Z SQL Method')

zodb_skin_spec = ('ERP5 Form', 'ERP5 PDF Template', 'Page Template', 'Script', 'Script (Python)','Z SQL Method')

def getSkinPathList(self, spec=fs_skin_spec+zodb_skin_spec):
  path_list = self.portal_skins.getSkinPath(self.portal_skins.getDefaultSkin())
  path_list = path_list.split(',')
  skin_list = []
  for path in path_list:
    if path in ('content', 'content18', 'control', 'generic', 'mailin', 'pro', 'topic', 'zpt_content', 'zpt_control', 'zpt_generic', 'zpt_reporttool', 'zpt_topic'):
      continue
    for id in self.portal_skins[path].objectIds(spec):
      skin_list.append(path + '/' + id)
  return skin_list

def split(name):
  """
    Split the name using an underscore as a separator and
    using the change from lower case to upper case as a separator.

    Example: foo_barBaz -> foo, bar, Baz
  """
  part_list = []
  part = ''
  pc = None
  for c in name:
    if c == '_':
      if len(part) > 0:
        part_list.append(part)
      part = ''
      pc = None
    elif pc is not None and pc.islower() and c.isupper():
      if len(part) > 0:
        part_list.append(part)
      part = c
      pc = None
    else:
      pc = c
      part += c
  if pc is not None:
    part_list.append(part)
  return part_list

def suggestName(name, meta_type):
  """
    Suggest a good name for a given name.
  """
  # Determine the prefix. The default is "Base_".
  i = name.find('_')
  if i < 1:
    prefix = 'Base'
  else:
    # Special treatment for view, print, create and list, because they are used very often.
    # packing list is an exception, because it is confusing (i.e. sale_packing_list_list).
    view_index = name.find('_view')
    print_index = name.find('_print')
    create_index = name.find('_create')
    list_index = name.find('_list')
    packing_list_index = name.find('packing_list_')
    if view_index > 0:
      i = view_index
    elif print_index > 0:
      i = print_index
    elif create_index > 0:
      i = create_index
    elif list_index > 0:
      if packing_list_index > 0:
        i = packing_list_index + 12
      else:
        i = list_index
    part_list = split(name[:i])
    prefix = ''
    for part in part_list:
      prefix += part.capitalize()
    name = name[i+1:]
  if meta_type in ('Filesystem Z SQL Method', 'Z SQL Method'):
    new_name = 'z'
    if name[0] == 'z':
      name = name[1:]
    part_list = split(name)
    for part in part_list:
      new_name += part.capitalize()
  else:
    part_list = split(name)
    new_name = part_list[0].lower()
    for part in part_list[1:]:
      new_name += part.capitalize()
  return prefix + '_' + new_name


def checkSkinNames(self, REQUEST=None, csv=0, all=0):
  """
    Check if the name of each skin follows the naming convention.
  """
  if csv:
    msg = 'Folder,Name,New Name,Meta Type\n'
  else:
    msg = '<html><body>'
  rexp = re.compile('^[A-Z][a-zA-Z0-9]*_[a-z][a-zA-Z0-9]*$')
  rexp_zsql = re.compile('^[A-Z][a-zA-Z0-9]*_z[A-Z][a-zA-Z0-9]*$')
  path_list = getSkinPathList(self)
  bad_list = []
  for path in path_list:
    name = path.split('/')[-1]
    skin = self.portal_skins.restrictedTraverse(path)
    if skin.meta_type in ('Filesystem Z SQL Method', 'Z SQL Method'):
      r = rexp_zsql
    else:
      r = rexp
    if all or r.search(name) is None:
      bad_list.append((path, skin.meta_type))
  if len(bad_list) == 0:
    if not csv:
      msg += '<p>Everything is fine.</p>\n'
  else:
    bad_list.sort()
    if not csv:
      msg += '<p>These %d skins do not follow the naming convention:</p><table width="100%%">\n' % len(bad_list)
      msg += '<tr><td>Folder</td><td>Skin Name</td><td>Meta Type</td></tr>\n'
    i = 0
    for path,meta_type in bad_list:
      name = path.split('/')[-1]
      suggested_name = suggestName(name, meta_type)
      folder = path[:-len(name)-1]
      if (i % 2) == 0:
        c = '#88dddd'
      else:
        c = '#dddd88'
      i += 1
      if csv:
        msg += '%s,%s,%s,%s\n' % (folder, name, suggested_name, meta_type)
      else:
        msg += '<tr bgcolor="%s"><td>%s</td><td>%s</td><td>%s</td></tr>\n' % (c, folder, name, meta_type)
    if not csv:
      msg += '</table>\n'
  if not csv:
    msg += '</body></html>'
  return msg

Yoshinori Okuji's avatar
Yoshinori Okuji committed
164
def fixSkinNames(self, REQUEST=None, file=None, dry_run=0):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  """
    Fix bad skin names.

    This method does:

      - Check all the contents of all skins.

      - Check immediate_view, constructor_path and actions in all portal types.

      - Check skins of all business templates.

      - Check actbox_url in transitions and worklists and scripts of all workflows.

      - Rename skins.
  """
  if REQUEST is None:
    REQUEST = get_request()

  if file is None:
    msg = 'You must put a CSV file inside the data directory, and specify %s/ERP5Site_fixSkinNames?file=NAME \n\n' % self.absolute_url()
    msg += 'The template of a CSV file is available via %s/ERP5Site_checkSkinNames?csv=1 \n\n' % self.absolute_url()
    msg += 'This does not modify anything by default. If you really want to fix skin names, specify %s/ERP5Site_fixSkinNames?file=NAME&dry_run=0 \n\n' % self.absolute_url()
    return msg

  file = os.path.join(data_dir, file)
  file = open(file, 'r')
  class NamingInformation: pass
  info_list = []
  try:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
194 195 196 197 198 199 200 201
    reader = csv.reader(file)
    for row in reader:
      folder, name, new_name, meta_type = row[:4]
      if len(row) > 4 and len(row[4]) > 0:
        removed = 1
        new_name = row[4]
      else:
        removed = 0
Yoshinori Okuji's avatar
Yoshinori Okuji committed
202 203 204 205 206 207
      if meta_type == 'Meta Type': continue
      if name == new_name: continue
      # Check the existence of the skin and the meta type. Paranoid?
      #if self.portal_skins[folder][name].meta_type != meta_type:
      #  raise RuntimeError, '%s/%s has a different meta type' % (folder, name)
      info = NamingInformation()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
208
      info.meta_type = meta_type
Yoshinori Okuji's avatar
Yoshinori Okuji committed
209 210 211 212
      info.folder = folder
      info.name = name
      info.new_name = new_name
      info.regexp = re.compile('\\b' + re.escape(name) + '\\b') # This is used to search the name
Yoshinori Okuji's avatar
Yoshinori Okuji committed
213
      info.removed = removed
Yoshinori Okuji's avatar
Yoshinori Okuji committed
214 215 216 217 218 219 220 221 222 223 224 225 226
      info_list.append(info)
  finally:
    file.close()

  # Now we have information enough. Check the skins.
  msg = ''
  path_list = getSkinPathList(self)
  for path in path_list:
    skin = self.portal_skins.restrictedTraverse(path)
    try:
      text = skin.manage_FTPget()
    except:
      type, value, traceback = sys.exc_info()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
227 228 229
      line = 'WARNING: the skin %s could not be retrieved because of the exception %s: %s\n' % (path, str(type), str(value))
      LOG('fixSkinNames', 0, line)
      msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
230 231 232 233 234 235 236
    else:
      name_list = []
      for info in info_list:
        if info.regexp.search(text) is not None:
          text = info.regexp.sub(info.new_name, text)
          name_list.append(info.name)
      if len(name_list) > 0:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
237 238 239
        line = '%s is modified for %s' % ('portal_skins/' + path, ', '.join(name_list))
        LOG('fixSkinNames', 0, line)
        msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
240
        if not dry_run:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
241 242 243 244 245 246 247 248 249
          if skin.meta_type in fs_skin_spec:
            f = open(expandpath(skin.getObjectFSPath()), 'w')
            try:
              f.write(text)
            finally:
              f.close()
          else:
            REQUEST['BODY'] = text
            skin.manage_FTPput(REQUEST, REQUEST.RESPONSE)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
250 251 252 253 254 255 256

  # Check the portal types.
  for t in self.portal_types.objectValues():
    # Initial view name.
    text = t.immediate_view
    for info in info_list:
      if info.name == text:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
257 258 259
        line = 'Initial view name of %s is modified for %s' % ('portal_types/' + t.id, text)
        LOG('fixSkinNames', 0, line)
        msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
260 261 262 263 264 265 266 267
        if not dry_run:
          t.immediate_view = info.new_name
        break
    # Constructor path.
    text = getattr(t, 'constructor_path', None)
    if text is not None:
      for info in info_list:
        if info.name == text:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
268 269 270
          line = 'Constructor path of %s is modified for %s' % ('portal_types/' + t.id, text)
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
271 272 273 274 275 276 277 278 279
          if not dry_run:
            t.constructor_path = info.new_name
          break
    # Actions.
    for action in t.listActions():
      text = action.action.text
      for info in info_list:
        if info.regexp.search(text) is not None:
          text = info.regexp.sub(info.new_name, text)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
280 281 282
          line = 'Action %s of %s is modified for %s' % (action.getId(), 'portal_types/' + t.id, info.name)
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          if not dry_run:
            action.action.text = text
          break

  # Check the portal templates.
  template_tool = getattr(self, 'portal_templates', None)
  # Check the existence of template tool, because an older version of ERP5 does not have it.
  if template_tool is not None:
    for template in template_tool.contentValues(filter={'portal_type':'Business Template'}):
      # Skins.
      skin_id_list = []
      name_list = []
      for skin_id in template.getTemplateSkinIdList():
        for info in info_list:
          if info.name == skin_id:
            name_list.append(skin_id)
            skin_id = info.new_name
            break
        skin_id_list.append(skin_id)
      if len(name_list) > 0:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
303 304 305
        line = 'Skins of %s is modified for %s' % ('portal_templates/' + template.getId(), ', '.join(name_list))
        LOG('fixSkinNames', 0, line)
        msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
306 307 308 309 310 311 312 313 314 315 316 317 318
        if not dry_run:
          template.setTemplateSkinIdList(skin_id_list)
      # Paths.
      path_list = []
      name_list = []
      for path in template.getTemplatePathList():
        for info in info_list:
          if info.regexp.search(path):
            name_list.append(skin_id)
            path = info.regexp.sub(info.new_name, path)
            break
        path_list.append(path)
      if len(name_list) > 0:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
319 320 321
        line = 'Paths of %s is modified for %s' % ('portal_templates/' + template.getId(), ', '.join(name_list))
        LOG('fixSkinNames', 0, line)
        msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
322 323 324 325 326 327 328 329 330 331 332 333
        if not dry_run:
          template.setTemplatePathList(path_list)

  # Workflows.
  for wf in self.portal_workflow.objectValues():
    # Transitions.
    for id in wf.transitions.objectIds():
      transition = wf.transitions._getOb(id)
      text = transition.actbox_url
      for info in info_list:
        if info.regexp.search(text) is not None:
          text = info.regexp.sub(info.new_name, text)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
334 335 336
          line = 'Transition %s of %s is modified for %s' % (id, 'portal_workflow/' + wf.id, info.name)
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
337 338 339 340 341 342 343 344 345 346
          if not dry_run:
            transition.actbox_url = text
          break
    # Worklists.
    for id in wf.worklists.objectIds():
      worklist = wf.worklists._getOb(id)
      text = worklist.actbox_url
      for info in info_list:
        if info.regexp.search(text) is not None:
          text = info.regexp.sub(info.new_name, text)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
347 348 349
          line = 'Worklist %s of %s is modified for %s' % (id, 'portal_workflow/' + wf.id, info.name)
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
350 351 352 353 354 355 356 357 358 359 360 361 362
          if not dry_run:
            worklist.actbox_url = text
          break
    # Scripts.
    for id in wf.scripts.objectIds():
      script = wf.scripts._getOb(id)
      text = script.manage_FTPget()
      name_list = []
      for info in info_list:
        if info.regexp.search(text) is not None:
          text = info.regexp.sub(info.new_name, text)
          name_list.append(info.name)
      if len(name_list) > 0:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
363 364 365
        line = 'Script %s of %s is modified for %s' % (id, 'portal_workflow/' + wf.id, ', '.join(name_list))
        LOG('fixSkinNames', 0, line)
        msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
366 367 368 369 370 371 372 373
        if not dry_run:
          REQUEST['BODY'] = text
          script.manage_FTPput(REQUEST, REQUEST.RESPONSE)

  # Rename the skins.
  if not dry_run:
    for info in info_list:
      try:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
374 375 376 377 378 379 380 381
        if info.meta_type in fs_skin_spec:
          skin = self.portal_skins[info.folder][info.name]
          old_path = expandpath(skin.getObjectFSPath())
          new_path = info.regexp.sub(info.new_name, old_path)
          if info.removed:
            os.remove(old_path)
          else:
            os.rename(old_path, new_path)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
382
        else:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
383 384 385 386 387
          folder = self.portal_skins[info.folder]
          if info.removed:
            folder.manage_delObjects([info.name])
          else:
            folder.manage_renameObjects([info.name], [info.new_name])
Yoshinori Okuji's avatar
Yoshinori Okuji committed
388 389
      except:
        type, value, traceback = sys.exc_info()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
390
        if info.removed:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
391 392 393
          line = 'WARNING: the skin %s could not be removed because of the exception %s: %s\n' % (info.name, str(type), str(value))
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
394
        else:
Yoshinori Okuji's avatar
Yoshinori Okuji committed
395 396 397
          line = 'WARNING: the skin %s could not be renamed to %s because of the exception %s: %s\n' % (info.name, info.new_name, str(type), str(value))
          LOG('fixSkinNames', 0, line)
          msg += '%s\n' % line
Yoshinori Okuji's avatar
Yoshinori Okuji committed
398 399

  return msg