[1] | 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 |
---|
[74] | 8 | from config import PRODUCTS_PATH |
---|
[1] | 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 | ############################################################### |
---|
[74] | 36 | def exportObjects(context, doesExportObjects, exporting_objects, |
---|
| 37 | fs_dest_directory, product_name): |
---|
[1] | 38 | # Check whether should perform exporting |
---|
| 39 | if not doesExportObjects: |
---|
| 40 | return None |
---|
| 41 | # Get Instance's exported and Product's imoprt pathes |
---|
[74] | 42 | instance_epath, product_epath = getImportedPathes(fs_dest_directory, product_name) |
---|
[1] | 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 | |
---|
[74] | 66 | def getImportedPathes(fs_dest_directory, product_name): |
---|
[1] | 67 | # Based on instance path, construct import pathes |
---|
| 68 | cfg = getConfiguration() |
---|
| 69 | instance_epath = cfg.clienthome |
---|
[74] | 70 | product_epath = osp.join(fs_dest_directory, product_name, "import") |
---|
[1] | 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 | |
---|
| 79 | def 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] |
---|