Implemented initialization of directory structure on empty server

Change-Id: I8bc5b039fe419821ef902a4d72b5d67321cf4241
diff --git a/rsync_client.py b/rsync_client.py
index 944b74b..dac460a 100644
--- a/rsync_client.py
+++ b/rsync_client.py
@@ -8,6 +8,7 @@
 import re
 import subprocess
 import tempfile
+import shutil
 
 
 logging.basicConfig(level=logging.INFO)
@@ -19,7 +20,7 @@
 staging_snapshot_stamp = now.strftime(staging_snapshot_stamp_format)
 
 
-class RemoteRsyncStaging(object):
+class RsyncHost(object):
     def __init__(self,
                  mirror_name,
                  host,
@@ -28,7 +29,8 @@
                  files_dir='files',
                  save_last_days=61,
                  rsync_extra_params='-v',
-                 staging_postfix='staging'):
+                 staging_postfix='staging',
+                 init_directory_structure=True):
         self.mirror_name = mirror_name
         self.host = host
         self.module = module
@@ -51,6 +53,9 @@
                                       staging_snapshot_stamp_regexp)
                                )
 
+        if init_directory_structure is True:
+            self.init_directory_structure()
+
     @property
     def url(self):
         return '{}::{}'.format(self.host, self.module)
@@ -97,6 +102,29 @@
     def staging_link_url(self):
         return '{}/{}'.format(self.url, self.staging_link_path)
 
+    def init_directory_structure(self):
+        root_dir_present = self.rsync_ls_dirs(
+            '/',
+            pattern=r'^{}$'.format(self.root_path)
+        )[1]
+        root_dir_present = True if len(root_dir_present) > 0 else False
+        if root_dir_present is True:
+            files_dir_present = self.rsync_ls_dirs(
+                '{}/'.format(self.root_path),
+                pattern=r'^{}$'.format(self.files_dir)
+            )[1]
+            files_dir_present = True if len(files_dir_present) > 0 else False
+
+        if not root_dir_present or not files_dir_present:
+            dir_to_sync = tempfile.mkdtemp()
+            os.makedirs('{}/{}'.format(dir_to_sync, self.files_path))
+            self._do_rsync(
+                source='{}/'.format(dir_to_sync),
+                dest='{}/'.format(self.url),
+                opts='-a'
+            )
+            shutil.rmtree(dir_to_sync)
+
     @property
     def empty_dir(self):
         if self.__dict__.get('_empty_dir') is None: