source: products/qPloneSkinDump/trunk/exportingObjects.py @ 1591

Last change on this file since 1591 was 74, checked in by chervol, 18 years ago

initial plone2.0.5 branch import

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1from App.config import getConfiguration
2from Products.CMFCore.utils import getToolByName
3from zExceptions import BadRequest
4from zLOG import LOG, INFO
5from time import gmtime, strftime
6import os, os.path
7osp = os.path
8from config import PRODUCTS_PATH
9
10def copyFile(src_dir, dst_dir, f_name):
11    # Copy file from src_dir to dst_dir under original name
12    try:
13        src_file = open(osp.join(src_dir, f_name),"rb")
14        dst_file = open(osp.join(dst_dir, f_name),"wb")
15        dst_file.write(src_file.read())
16        dst_file.close()
17        src_file.close()
18    except Exception, e:
19        msg = "!!! In copying files from <%s> dir to <%s> dir exception occur. Details: %s" % (src_dir,dst_dir)
20        LOG('performImportToPortal',INFO,'copyFile', msg)
21       
22def moveToDir(file_list, src_dir_path, temp_dir_path):
23    # Move files from Instanse's dir to created temp dir
24    if not osp.exists(temp_dir_path):
25        os.mkdir(temp_dir_path)
26    try:
27        [copyFile(src_dir_path, temp_dir_path, f_name) for f_name in file_list]
28        [os.remove(osp.join(src_dir_path, f_name)) for f_name in file_list]
29    except Exception, e:
30        msg = "!!! Exception occur during moving files from Instance's dir to temp dir. Detaile:%s" % str(e)
31        LOG('performImportToPortal',777,'moveToDir', msg)
32
33###############################################################
34##                         EXPORTING                         ##
35###############################################################
36def exportObjects(context, doesExportObjects, exporting_objects,
37                  fs_dest_directory, product_name):
38    # Check whether should perform exporting
39    if not doesExportObjects:
40        return None
41    # Get Instance's exported and Product's imoprt pathes
42    instance_epath, product_epath = getImportedPathes(fs_dest_directory, product_name)
43    # Move same named files from Instance export dir to Temp dir
44    temp_dir_path, product_elist = moveSameFilesToTemp(instance_epath, product_epath, exporting_objects)
45    # Export objects
46    fail_export = []
47    portal = getToolByName(context, 'portal_url').getPortalObject()
48    for odject_id in exporting_objects:
49        try:
50            portal.manage_exportObject(id=odject_id)
51        except:
52            fail_export.append(odject_id)
53    # Move exported portal *zexp objects to SkinProduct's import dir
54    for f_name in os.listdir(instance_epath):
55        f_path = osp.join(instance_epath, f_name)
56        if f_name.endswith('.zexp') and osp.isfile(f_path) and f_name in product_elist:
57            copyFile(instance_epath, product_epath, f_name)
58            os.remove(f_path)
59    # Clean: Move same files from temd dir to var dir
60    if osp.exists(temp_dir_path) and osp.isdir(temp_dir_path):
61        f_names = os.listdir(temp_dir_path)
62        moveToDir(f_names, temp_dir_path, instance_epath)
63        os.rmdir(temp_dir_path)
64    return fail_export
65
66def getImportedPathes(fs_dest_directory, product_name):
67    # Based on instance path, construct import pathes
68    cfg = getConfiguration()
69    instance_epath = cfg.clienthome
70    product_epath = osp.join(fs_dest_directory, product_name, "import")
71    # Check presence of Product import directory
72    if not osp.isdir(product_epath):       
73        raise BadRequest, "Skin Product's import directory '%s' - does not exist or isn't direcory" % product_epath
74    # Check presence of Instance import directory
75    if not osp.isdir(instance_epath):
76        raise BadRequest, "Instance import directory '%s' - does not exist or isn't direcory" % instance_epath
77    return [instance_epath, product_epath]
78
79def moveSameFilesToTemp(instance_epath, product_epath, exporting_objects):
80    # Get list of Instance's export directory and exported objects
81    instance_elist = [i for i in os.listdir(instance_epath) \
82                      if osp.isfile(osp.join(instance_epath,i)) and i.endswith('.zexp')]
83    product_elist = ["%s.zexp" % i for i in exporting_objects]
84    # Compose temp dir back_[date] dir path in Instance import directory
85    temp_dir_id = "back_%s" % strftime("%Y%m%d%H%M%S", gmtime())
86    temp_dir_path = osp.join(instance_epath, temp_dir_id)
87    # Check for presence samenamed files in Instance export dir and among exporting_objects.
88    # If so - move samenamed files from Instance export dir to temp back_[date] dir.
89    same_instance_files = [f_name for f_name in instance_elist if f_name in product_elist]
90    if same_instance_files:
91        moveToDir(same_instance_files, instance_epath, temp_dir_path)
92    return [temp_dir_path, product_elist]
Note: See TracBrowser for help on using the repository browser.