<s> import ftplib <EOL> import logging <EOL> import ntpath <EOL> import os <EOL> import shlex <EOL> import shutil <EOL> import socket <EOL> import subprocess <EOL> import tempfile <EOL> import uuid <EOL> import zipfile <EOL> from io import BytesIO <EOL> from simple_idml . decorators import simple_decorator <EOL> from suds . client import Client <EOL> from tempfile import mkdtemp <EOL> CURRENT_DIR = os . path . abspath ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> SCRIPTS_DIR = os . path . join ( CURRENT_DIR , "<STR_LIT>" ) <EOL> JS_SAVE_AS_SCRIPT = "<STR_LIT>" <EOL> JS_EXPORT_SCRIPT = "<STR_LIT>" <EOL> JS_PACKAGE_SCRIPT = "<STR_LIT>" <EOL> JS_CLOSE_ALL_SCRIPT = "<STR_LIT>" <EOL> JS_SAVE_AS_SCRIPTS = [ JS_SAVE_AS_SCRIPT , JS_EXPORT_SCRIPT , JS_PACKAGE_SCRIPT ] <EOL> def close_all_documents ( indesign_server_url , indesign_client_workdir , indesign_server_workdir , <EOL> indesign_server_path_style = "<STR_LIT>" , ftp_params = None ) : <EOL> server_path_mod = os . path <EOL> if indesign_server_path_style == "<STR_LIT>" : <EOL> server_path_mod = ntpath <EOL> javascript_basename = JS_CLOSE_ALL_SCRIPT <EOL> javascript_master_filename = os . path . join ( SCRIPTS_DIR , javascript_basename ) <EOL> javascript_client_copy_filename = os . path . join ( indesign_client_workdir , javascript_basename ) <EOL> javascript_server_copy_filename = server_path_mod . join ( indesign_server_workdir , javascript_basename ) <EOL> _copy ( javascript_master_filename , javascript_client_copy_filename , ftp_params , src_open_mode = "<STR_LIT:r>" ) <EOL> cl = Client ( "<STR_LIT>" % indesign_server_url ) <EOL> cl . set_options ( location = indesign_server_url ) <EOL> params = cl . factory . create ( "<STR_LIT>" ) <EOL> params . scriptLanguage = '<STR_LIT>' <EOL> params . scriptFile = javascript_server_copy_filename <EOL> cl . service . RunScript ( params ) <EOL> @ simple_decorator <EOL> def use_dedicated_working_directory ( view_func ) : <EOL> def new_func ( src_filename , dst_formats_params , indesign_server_url , indesign_client_workdir , <EOL> indesign_server_workdir , indesign_server_path_style = "<STR_LIT>" , <EOL> clean_workdir = True , ftp_params = None , logger = None , logger_extra = None ) : <EOL> server_path_mod = os . path <EOL> if indesign_server_path_style == "<STR_LIT>" : <EOL> server_path_mod = ntpath <EOL> working_dir = _mkdir_unique ( indesign_client_workdir , ftp_params ) <EOL> indesign_client_workdir = working_dir <EOL> indesign_server_workdir = server_path_mod . join ( indesign_server_workdir , os . path . basename ( working_dir ) ) <EOL> response = view_func ( src_filename , dst_formats_params , indesign_server_url , indesign_client_workdir , <EOL> indesign_server_workdir , indesign_server_path_style , clean_workdir , ftp_params , <EOL> logger , logger_extra ) <EOL> if clean_workdir : <EOL> _rmtree ( working_dir , ftp_params ) <EOL> return response <EOL> return new_func <EOL> @ use_dedicated_working_directory <EOL> def save_as ( src_filename , dst_formats_params , indesign_server_url , indesign_client_workdir , <EOL> indesign_server_workdir , indesign_server_path_style = "<STR_LIT>" , <EOL> clean_workdir = True , ftp_params = None , logger = None , logger_extra = None ) : <EOL> """<STR_LIT>""" <EOL> if not logger : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> logger . addHandler ( logging . NullHandler ( ) ) <EOL> logger_extra = logger_extra or { } <EOL> server_path_mod = os . path <EOL> if indesign_server_path_style == "<STR_LIT>" : <EOL> server_path_mod = ntpath <EOL> def _save_as ( dst_format_params ) : <EOL> """<STR_LIT>""" <EOL> dst_format = dst_format_params [ "<STR_LIT>" ] <EOL> js_params = dst_format_params . get ( "<STR_LIT>" , { } ) <EOL> src_rootname = os . path . splitext ( src_basename ) [ <NUM_LIT:0> ] <EOL> dst_basename = "<STR_LIT>" % ( src_rootname , dst_format ) <EOL> javascript_basename = JS_SAVE_AS_SCRIPT <EOL> if dst_format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> javascript_basename = JS_EXPORT_SCRIPT <EOL> elif dst_format == '<STR_LIT>' : <EOL> javascript_basename = JS_PACKAGE_SCRIPT <EOL> dst_basename = src_rootname <EOL> javascript_master_filename = os . path . join ( SCRIPTS_DIR , javascript_basename ) <EOL> javascript_client_copy_filename = os . path . join ( indesign_client_workdir , javascript_basename ) <EOL> response_client_copy_filename = os . path . join ( indesign_client_workdir , dst_basename ) <EOL> javascript_server_copy_filename = server_path_mod . join ( indesign_server_workdir , javascript_basename ) <EOL> response_server_copy_filename = server_path_mod . join ( indesign_server_workdir , dst_basename ) <EOL> _copy ( javascript_master_filename , javascript_client_copy_filename , ftp_params , src_open_mode = "<STR_LIT:r>" ) <EOL> params = cl . factory . create ( "<STR_LIT>" ) <EOL> params . scriptLanguage = '<STR_LIT>' <EOL> params . scriptFile = javascript_server_copy_filename <EOL> src = cl . factory . create ( "<STR_LIT>" ) <EOL> src . name = "<STR_LIT:source>" <EOL> src . value = src_server_copy_filename <EOL> dst = cl . factory . create ( "<STR_LIT>" ) <EOL> dst . name = "<STR_LIT>" <EOL> dst . value = response_server_copy_filename <EOL> params . scriptArgs = [ src , dst ] <EOL> extra_params = [ ] <EOL> for k , v in js_params . items ( ) : <EOL> param = cl . factory . create ( "<STR_LIT>" ) <EOL> param . name = k <EOL> param . value = v <EOL> extra_params . append ( param ) <EOL> params . scriptArgs . extend ( extra_params ) <EOL> if dst_format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> fmt = cl . factory . create ( "<STR_LIT>" ) <EOL> fmt . name = "<STR_LIT>" <EOL> fmt . value = dst_format <EOL> params . scriptArgs . append ( fmt ) <EOL> logger . debug ( '<STR_LIT>' % params , extra = logger_extra ) <EOL> response = cl . service . RunScript ( params ) <EOL> logger . debug ( '<STR_LIT>' % response , extra = logger_extra ) <EOL> if dst_format == '<STR_LIT>' : <EOL> zip_filename = "<STR_LIT>" % response_client_copy_filename <EOL> _zip_dir ( response_client_copy_filename , zip_filename , ftp_params ) <EOL> response_client_copy_filename = zip_filename <EOL> logger . debug ( '<STR_LIT>' ) <EOL> response = _read ( response_client_copy_filename , ftp_params ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> if clean_workdir : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> _unlink ( response_client_copy_filename , ftp_params ) <EOL> _unlink ( javascript_client_copy_filename , ftp_params ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> return response <EOL> src_basename = os . path . basename ( src_filename ) <EOL> src_client_copy_filename = os . path . join ( indesign_client_workdir , src_basename ) <EOL> src_server_copy_filename = server_path_mod . join ( indesign_server_workdir , src_basename ) <EOL> _copy ( src_filename , src_client_copy_filename , ftp_params ) <EOL> cl = Client ( "<STR_LIT>" % indesign_server_url ) <EOL> cl . set_options ( location = indesign_server_url , timeout = <NUM_LIT> ) <EOL> responses = map ( lambda fmt : _save_as ( fmt ) , dst_formats_params ) <EOL> if clean_workdir : <EOL> _unlink ( src_client_copy_filename , ftp_params ) <EOL> return responses <EOL> def _copy ( src_filename , dst_filename , ftp_params = None , src_open_mode = "<STR_LIT:rb>" ) : <EOL> if not ftp_params : <EOL> shutil . copy ( src_filename , dst_filename ) <EOL> return <EOL> with open ( src_filename , src_open_mode ) as f : <EOL> ftp = get_ftp ( ftp_params ) <EOL> command = '<STR_LIT>' % dst_filename <EOL> try : <EOL> if "<STR_LIT:b>" in src_open_mode : <EOL> ftp . storbinary ( command , f ) <EOL> else : <EOL> ftp . storlines ( command , f ) <EOL> except BaseException , e : <EOL> print "<STR_LIT>" % dst_filename <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> raise e <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> def _unlink ( filename , ftp_params = None ) : <EOL> if not ftp_params : <EOL> os . unlink ( filename ) <EOL> return <EOL> ftp = get_ftp ( ftp_params ) <EOL> ftp . delete ( filename ) <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> def _rmtree ( tree , ftp_params = None ) : <EOL> if not ftp_params : <EOL> shutil . rmtree ( tree ) <EOL> return <EOL> ftp = get_ftp ( ftp_params ) <EOL> rmtree_ftp ( ftp , tree ) <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> def _read ( filename , ftp_params = None ) : <EOL> response = "<STR_LIT>" <EOL> if not ftp_params : <EOL> with open ( filename , "<STR_LIT:rb>" ) as f : <EOL> response = f . read ( ) <EOL> else : <EOL> with BytesIO ( ) as r : <EOL> ftp = get_ftp ( ftp_params ) <EOL> ftp . retrbinary ( '<STR_LIT>' % filename , r . write ) <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> r . seek ( <NUM_LIT:0> ) <EOL> response = r . read ( ) <EOL> return response <EOL> def _zip_dir ( dirname , zip_filename , ftp_params = None ) : <EOL> if ftp_params : <EOL> tmp_dirname = mkdtemp ( ) <EOL> tmp_zip_filename = os . path . join ( tmp_dirname , os . path . basename ( zip_filename ) ) <EOL> cmd = '<STR_LIT>' % { <EOL> '<STR_LIT:user>' : ftp_params [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : ftp_params [ '<STR_LIT>' ] [ <NUM_LIT:2> ] , <EOL> '<STR_LIT:url>' : ftp_params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : ftp_params [ '<STR_LIT>' ] and "<STR_LIT>" or "<STR_LIT>" , <EOL> '<STR_LIT>' : dirname , <EOL> '<STR_LIT>' : tmp_dirname <EOL> } <EOL> p = subprocess . Popen ( shlex . split ( cmd ) ) <EOL> p . wait ( ) <EOL> zip_tree ( os . path . join ( tmp_dirname , dirname ) , tmp_zip_filename ) <EOL> _copy ( tmp_zip_filename , zip_filename , ftp_params ) <EOL> shutil . rmtree ( tmp_dirname ) <EOL> _rmtree ( dirname , ftp_params ) <EOL> else : <EOL> zip_tree ( dirname , zip_filename ) <EOL> shutil . rmtree ( dirname ) <EOL> def zip_tree ( tree , destination ) : <EOL> relroot = os . path . abspath ( os . path . join ( tree , os . pardir ) ) <EOL> with zipfile . ZipFile ( destination , "<STR_LIT:w>" , zipfile . ZIP_DEFLATED ) as zip : <EOL> for root , dirs , files in os . walk ( tree ) : <EOL> zip . write ( root , os . path . relpath ( root , relroot ) ) <EOL> for file in files : <EOL> filename = os . path . join ( root , file ) <EOL> if os . path . isfile ( filename ) : <EOL> arcname = os . path . join ( os . path . relpath ( root , relroot ) , file ) <EOL> zip . write ( filename , arcname ) <EOL> def rmtree_ftp ( ftp , path ) : <EOL> """<STR_LIT>""" <EOL> wd = ftp . pwd ( ) <EOL> try : <EOL> names = ftp . nlst ( path ) <EOL> except ftplib . all_errors as e : <EOL> return <EOL> for name in names : <EOL> if os . path . split ( name ) [ <NUM_LIT:1> ] in ( '<STR_LIT:.>' , '<STR_LIT:..>' ) : <EOL> continue <EOL> try : <EOL> ftp . cwd ( name ) <EOL> ftp . cwd ( wd ) <EOL> rmtree_ftp ( ftp , name ) <EOL> except ftplib . all_errors : <EOL> ftp . delete ( name ) <EOL> try : <EOL> ftp . rmd ( path ) <EOL> except ftplib . all_errors as e : <EOL> raise e <EOL> def _mkdir_unique ( dir , ftp_params = None ) : <EOL> if not ftp_params : <EOL> unique_path = tempfile . mkdtemp ( dir = dir ) <EOL> else : <EOL> unique_path = os . path . join ( dir , uuid . uuid1 ( ) . hex ) <EOL> ftp = get_ftp ( ftp_params ) <EOL> ftp . mkd ( unique_path ) <EOL> close_ftp_conn ( ftp , ftp_params ) <EOL> return unique_path <EOL> def close_ftp_conn ( ftp , ftp_params = None ) : <EOL> """<STR_LIT>""" <EOL> if ftp_params and ( ftp_params . get ( '<STR_LIT>' , True ) is False ) : <EOL> ftp . close ( ) <EOL> else : <EOL> ftp . quit ( ) <EOL> def get_ftp ( ftp_params ) : <EOL> ftp = ftplib . FTP ( * ftp_params [ "<STR_LIT>" ] ) <EOL> ftp . set_pasv ( ftp_params [ "<STR_LIT>" ] ) <EOL> if ftp_params . get ( '<STR_LIT>' , False ) is True : <EOL> ftp . sock . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , <NUM_LIT:1> ) <EOL> if '<STR_LIT>' in ftp_params and hasattr ( socket , "<STR_LIT>" ) : <EOL> ftp . sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPINTVL , <EOL> ftp_params [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in ftp_params and hasattr ( socket , "<STR_LIT>" ) : <EOL> ftp . sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_KEEPIDLE , <EOL> ftp_params [ '<STR_LIT>' ] ) <EOL> return ftp </s>
