11#!/usr/bin/python
22
3+ """ Wrapper module for ascp usage. """
4+
35import os
46import re
57import subprocess
68import logging
79
8- # Test wrapper code for ascp
9-
1010# download example command(s):
1111#
1212# only getting ~ 10Mb/s (on 20-30Mb connection): with defaults:
2929
3030# compare version numbers
3131def version_cmp (v1 , v2 ):
32+ """
33+ Compare version/release numbers.
34+ """
3235 logger .debug ("In version_cmp." )
3336 def normalize (v ):
34- return [int (x ) for x in re .sub (r'(\.0+)*$' ,'' , v ).split ("." )]
37+ """ Normalize a dotted version string. """
38+ return [int (x ) for x in re .sub (r'(\.0+)*$' , '' , v ).split ("." )]
3539 return cmp (normalize (v1 ), normalize (v2 ))
3640
37- # Return version number of ascp executable referenced by ascp_command.
38- # May raise an exception if the path is invalid.
39- def get_ascp_version (ascp_command ):
41+ def get_ascp_version ():
42+ """
43+ Return version number of ascp executable referenced by ascp_command.
44+ May raise an exception if the path is invalid.
45+ """
4046 logger .debug ("In get_ascp_version." )
4147
4248 version = None
43- output = subprocess .check_output ([ascp_command , "--version" ],
44- universal_newlines = True )
49+ output = subprocess .check_output ([ASCP_COMMAND , "--version" ],
50+ universal_newlines = True )
51+
4552 cre = re .compile (r"^.+version (\d[\d\.]+)" , re .MULTILINE )
4653 for match in cre .finditer (output ):
4754 version = match .groups ()[0 ]
4855
4956 if version is None :
50- raise Exception ("Output from ascp command ('" + ascp_command + \
57+ raise Exception ("Output from ascp command ('" + ASCP_COMMAND + \
5158 " --version') did not contain a recognizable " + \
5259 "version number." )
5360 return version
5461
55- def check_ascp_version (ascp_command = "ascp" ):
62+ def check_ascp_version ():
63+ """
64+ Check that the ascp utility is installed and that its version
65+ is within an acceptable range. If the utility is not present,
66+ or the version is unacceptable, an exception is raised.
67+ """
5668 logger .debug ("In check_ascp_version." )
5769
5870 # check ascp version, raise error if too low
5971 try :
60- ascp_ver = get_ascp_version (ascp_command )
72+ ascp_ver = get_ascp_version ()
6173 except :
6274 raise Exception ("Unable to determine ascp version. Is it installed?" )
6375
@@ -67,40 +79,49 @@ def check_ascp_version(ascp_command="ascp"):
6779 return True
6880
6981def get_ascp_env (password ):
82+ """
83+ Get the environment dictionary after adding the ASPERA_SCP_PASS variable
84+ (and value) to it.
85+ """
7086 logger .debug ("In get_ascp_env." )
7187
72- e = os .environ .copy ()
73- if 'ASPERA_SCP_PASS' in e :
88+ environment = os .environ .copy ()
89+ if 'ASPERA_SCP_PASS' in environment :
7490 logger .info ("Honoring previously set ASPERA_SCP_PASS environment variable." )
7591 else :
7692 if password != None :
7793 logger .info ("Setting ASPERA_SCP_PASS environment variable." )
78- e ['ASPERA_SCP_PASS' ] = password
94+ environment ['ASPERA_SCP_PASS' ] = password
7995
80- return e
96+ return environment
8197
82- # run ascp command, returning True for success or False for failure
8398def run_ascp (ascp_cmd , password , keyfile = None ):
99+ """
100+ Run the ascp command, returning True for success or False for failure.
101+ """
84102 logger .debug ("In run_ascp." )
85103
86104 if keyfile :
87105 if not os .path .exists (keyfile ):
88106 raise IOError (
89- "Can't use private key. No such file or directory: " + keyfile )
107+ "Can't use private key. No such file or directory: " + keyfile )
90108 ascp_cmd = [ascp_cmd [0 ], "-i" , keyfile ] + ascp_cmd [1 :]
91109
92110 try :
93- logger .debug ("Command: " + " " .join (ascp_cmd ))
94- process = subprocess .Popen (ascp_cmd , stdout = subprocess .PIPE ,
95- stdin = subprocess .PIPE ,
96- stderr = subprocess .PIPE ,
97- universal_newlines = True ,
98- env = get_ascp_env (password ))
111+ logger .debug ("Command: %s" , " " .join (ascp_cmd ))
112+ process = subprocess .Popen (
113+ ascp_cmd ,
114+ stdout = subprocess .PIPE ,
115+ stdin = subprocess .PIPE ,
116+ stderr = subprocess .PIPE ,
117+ universal_newlines = True ,
118+ env = get_ascp_env (password )
119+ )
99120
100121 logger .info ("Beginning transfer." )
101122 (s_out , s_err ) = process .communicate ()
102123 rc = process .returncode
103- logger .info ("Invocation of ascp complete. Return code: %s." % str (rc ))
124+ logger .info ("Invocation of ascp complete. Return code: %s." , str (rc ))
104125
105126 success = False
106127
@@ -112,36 +133,46 @@ def run_ascp(ascp_cmd, password, keyfile=None):
112133 logger .error ("Aspera authentication failure." )
113134 else :
114135 if s_err != None :
115- logger .error ("Unexpected STDERR from ascp: %s" % s_err )
136+ logger .error ("Unexpected STDERR from ascp: %s" , s_err )
116137 if s_out != None :
117- logger .error ("Unexpected STDOUT from ascp: %s" % s_out )
138+ logger .error ("Unexpected STDOUT from ascp: %s" , s_out )
118139 except subprocess .CalledProcessError as cpe :
119- logger .error ("Encountered an error when running ascp: " , cpe )
140+ logger .error ("Encountered an error when running ascp: %s " , cpe )
120141
121142 return success
122143
123- # download a single file via Aspera. return True if successful, False if not
124144def download_file (server , username , password , remote_path , local_path ,
125145 keyfile = None ):
146+ """
147+ Download a single remote file using the aspera ascp utility.
148+ Returns True if successful, False if not.
149+ """
126150 logger .debug ("In download_file." )
127151
128- check_ascp_version (ASCP_COMMAND )
129- ascp_cmd = [ ASCP_COMMAND , "-T" , "-v" , "-l" , "300M" , username + "@" +
130- server + ":" + remote_path , local_path ]
152+ check_ascp_version ()
153+ ascp_cmd = [
154+ ASCP_COMMAND , "-T" , "-v" , "-l" , "300M" ,
155+ username + "@" + server + ":" + remote_path ,
156+ local_path
157+ ]
158+
131159 return run_ascp (ascp_cmd , password , keyfile )
132160
133- # upload a single file via Aspera. return True if successful, False if not
134161def upload_file (server , username , password , local_file , remote_path ,
135162 keyfile = None ):
163+ """
164+ Upload a single file with the Aspera ascp utility.
165+ Return True if successful, False if not.
166+ """
136167 logger .debug ("In upload_file." )
137- check_ascp_version (ASCP_COMMAND )
168+ check_ascp_version ()
138169
139170 # check that local file exists
140171 if not os .path .isfile (local_file ):
141172 logger .warn ("local file " + local_file + " does not exist" )
142173 return False
143174
144175 remote_clause = username + "@" + server + ":" + remote_path
145- ascp_cmd = [ ASCP_COMMAND , "-T" , "-v" , "-l" , "300M" , local_file , remote_clause ]
176+ ascp_cmd = [ASCP_COMMAND , "-T" , "-v" , "-l" , "300M" , local_file , remote_clause ]
146177
147178 return run_ascp (ascp_cmd , password , keyfile )
0 commit comments