1 | from App.config import getConfiguration |
---|
2 | from Products.CMFCore.utils import getToolByName |
---|
3 | from zExceptions import BadRequest |
---|
4 | from zLOG import LOG, INFO |
---|
5 | from time import gmtime, strftime |
---|
6 | import os, os.path |
---|
7 | osp = os.path |
---|
8 | from config import PRODUCTS_PATH |
---|
9 | |
---|
10 | def 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 | |
---|
22 | def 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 | ############################################################### |
---|
36 | def exportObjects(context, doesExportObjects, exporting_objects, product_name): |
---|
37 | # Check whether should perform exporting |
---|
38 | if not doesExportObjects: |
---|
39 | return None |
---|
40 | # Get Instance's exported and Product's imoprt pathes |
---|
41 | instance_epath, product_epath = getImportedPathes(product_name) |
---|
42 | # Move same named files from Instance export dir to Temp dir |
---|
43 | temp_dir_path, product_elist = moveSameFilesToTemp(instance_epath, product_epath, exporting_objects) |
---|
44 | # Export objects |
---|
45 | fail_export = [] |
---|
46 | portal = getToolByName(context, 'portal_url').getPortalObject() |
---|
47 | for odject_id in exporting_objects: |
---|
48 | try: |
---|
49 | portal.manage_exportObject(id=odject_id) |
---|
50 | except: |
---|
51 | fail_export.append(odject_id) |
---|
52 | # Move exported portal *zexp objects to SkinProduct's import dir |
---|
53 | for f_name in os.listdir(instance_epath): |
---|
54 | f_path = osp.join(instance_epath, f_name) |
---|
55 | if f_name.endswith('.zexp') and osp.isfile(f_path) and f_name in product_elist: |
---|
56 | copyFile(instance_epath, product_epath, f_name) |
---|
57 | os.remove(f_path) |
---|
58 | # Clean: Move same files from temd dir to var dir |
---|
59 | if osp.exists(temp_dir_path) and osp.isdir(temp_dir_path): |
---|
60 | f_names = os.listdir(temp_dir_path) |
---|
61 | moveToDir(f_names, temp_dir_path, instance_epath) |
---|
62 | os.rmdir(temp_dir_path) |
---|
63 | return fail_export |
---|
64 | |
---|
65 | def getImportedPathes(product_name): |
---|
66 | # Based on instance path, construct import pathes |
---|
67 | cfg = getConfiguration() |
---|
68 | instance_epath = cfg.clienthome |
---|
69 | product_epath = osp.join(PRODUCTS_PATH, product_name, "import") |
---|
70 | # Check presence of Product import directory |
---|
71 | if not osp.isdir(product_epath): |
---|
72 | raise BadRequest, "Skin Product's import directory '%s' - does not exist or isn't direcory" % product_epath |
---|
73 | # Check presence of Instance import directory |
---|
74 | if not osp.isdir(instance_epath): |
---|
75 | raise BadRequest, "Instance import directory '%s' - does not exist or isn't direcory" % instance_epath |
---|
76 | return [instance_epath, product_epath] |
---|
77 | |
---|
78 | def moveSameFilesToTemp(instance_epath, product_epath, exporting_objects): |
---|
79 | # Get list of Instance's export directory and exported objects |
---|
80 | instance_elist = [i for i in os.listdir(instance_epath) \ |
---|
81 | if osp.isfile(osp.join(instance_epath,i)) and i.endswith('.zexp')] |
---|
82 | product_elist = ["%s.zexp" % i for i in exporting_objects] |
---|
83 | # Compose temp dir back_[date] dir path in Instance import directory |
---|
84 | temp_dir_id = "back_%s" % strftime("%Y%m%d%H%M%S", gmtime()) |
---|
85 | temp_dir_path = osp.join(instance_epath, temp_dir_id) |
---|
86 | # Check for presence samenamed files in Instance export dir and among exporting_objects. |
---|
87 | # If so - move samenamed files from Instance export dir to temp back_[date] dir. |
---|
88 | same_instance_files = [f_name for f_name in instance_elist if f_name in product_elist] |
---|
89 | if same_instance_files: |
---|
90 | moveToDir(same_instance_files, instance_epath, temp_dir_path) |
---|
91 | return [temp_dir_path, product_elist] |
---|