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