From 734dcad47b37483c0bb068fe0d05bf637cf7eace Mon Sep 17 00:00:00 2001
From: "Mads M. Pedersen" <mmpe@dtu.dk>
Date: Fri, 28 Jul 2017 08:49:32 +0200
Subject: [PATCH] use counter instead of time for name of tmp zip file in
 ssh_client upload/download files

---
 wetb/utils/cluster_tools/ssh_client.py | 10 ++++++++--
 wetb/utils/tests/test_ssh_client.py    |  8 ++++----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/wetb/utils/cluster_tools/ssh_client.py b/wetb/utils/cluster_tools/ssh_client.py
index 66886fe3..7492b1a8 100644
--- a/wetb/utils/cluster_tools/ssh_client.py
+++ b/wetb/utils/cluster_tools/ssh_client.py
@@ -116,6 +116,8 @@ class SSHClient(object):
         self.ssh_lock = threading.RLock()
         #self.sftp = None
         self.transport = None
+        self.counter_lock = threading.RLock()
+        self.counter=0
         if key is not None:
             self.key = paramiko.RSAKey.from_private_key(StringIO(key), password=passphrase)
 
@@ -254,7 +256,9 @@ class SSHClient(object):
         files = set([os.path.abspath(f) for f in files])
 
         compression_levels = {0:zipfile.ZIP_STORED, 1:zipfile.ZIP_DEFLATED, 2:zipfile.ZIP_BZIP2, 3:zipfile.ZIP_LZMA}
-        zn =  'tmp_%s_%s.zip'%(id(self),time.time())
+        with self.counter_lock:
+            self.counter+=1
+            zn =  'tmp_%s_%04d.zip'%(id(self),self.counter)
         zipf = zipfile.ZipFile(zn, 'w', compression_levels[compression_level])
         try:
             for f in files:
@@ -276,7 +280,9 @@ class SSHClient(object):
         if not isinstance(file_lst, (tuple, list)):
             file_lst = [file_lst]
         file_lst = [f.replace("\\","/") for f in file_lst]
-        zn =  'tmp_%s_%s.zip'%(id(self),time.time())
+        with self.counter_lock:
+            self.counter+=1
+            zn =  'tmp_%s_%04d.zip'%(id(self),self.counter)
         
         remote_zip = os.path.join(remote_path, zn).replace("\\","/")
         self.execute("cd %s && zip -r %s %s"%(remote_path, zn, " ".join(file_lst)))
diff --git a/wetb/utils/tests/test_ssh_client.py b/wetb/utils/tests/test_ssh_client.py
index 022caddb..f1f294fe 100644
--- a/wetb/utils/tests/test_ssh_client.py
+++ b/wetb/utils/tests/test_ssh_client.py
@@ -34,7 +34,7 @@ class sshrisoe_interactive_auth_handler(object):
         return []
     
 tfp = os.path.join(os.path.dirname(__file__), 'test_files/')
-all = 0
+all = 1
 class TestSSHClient(unittest.TestCase):
 
     def setUp(self):
@@ -87,7 +87,7 @@ class TestSSHClient(unittest.TestCase):
                  
         
     def test_folder_transfer_specific_files_uppercase(self):
-        if 0 or all:
+        if 1 or all:
             if x:
                 p = tfp
                 files = [os.path.join(tfp, "TEST.txt")]
@@ -99,7 +99,7 @@ class TestSSHClient(unittest.TestCase):
             
             
     def test_folder_transfer_specific_files(self):
-        if 0 or all:
+        if 1 or all:
             if x:
                 p = r"C:\mmpe\HAWC2\models\version_12.3beta/"
                 p = r'C:\mmpe\programming\python\WindEnergyToolbox\wetb\hawc2\tests\test_files\simulation_setup\DTU10MWRef6.0_IOS/'
@@ -132,7 +132,7 @@ class TestSSHClient(unittest.TestCase):
                 self.assertEqual(out.strip(), "ssh-03.risoe.dk")
 
     def test_ssh_risoe_gorm(self):
-        if 1 or all:
+        if 0 or all:
             if x:
             
                 gateway = SSHClient('ssh.risoe.dk', 'mmpe', password="xxx", interactive_auth_handler = sshrisoe_interactive_auth_handler(x.mmpe))
-- 
GitLab