From 18658a879cbd1cdf981fda40c7516808fa265fb9 Mon Sep 17 00:00:00 2001
From: Giorgos Korfiatis <gkorf@grnet.gr>
Date: Wed, 6 May 2015 14:21:23 +0300
Subject: [PATCH] Better handling of agkyra dir

---
 agkyra/agkyra/config.py       | 15 +++++++++++++--
 agkyra/agkyra/protocol.py     |  3 ++-
 agkyra/agkyra/syncer/setup.py |  8 +++++---
 agkyra/launch                 |  6 ++++--
 4 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/agkyra/agkyra/config.py b/agkyra/agkyra/config.py
index d44e2ea..9c9f956 100644
--- a/agkyra/agkyra/config.py
+++ b/agkyra/agkyra/config.py
@@ -38,8 +38,19 @@ from kamaki.cli.utils import escape_ctrl_chars
 
 CLOUD_PREFIX = config.CLOUD_PREFIX
 config.HEADER = '# Agkyra configuration file version XXX\n'
-AGKYRA_DIR = os.environ.get('AGKYRA_DIR', os.path.expanduser('~/.agkyra'))
-CONFIG_PATH = '%s%sconfig.rc' % (AGKYRA_DIR, os.path.sep)
+
+HOME_DIR = os.path.expanduser('~')
+DEFAULT_AGKYRA_DIR = os.path.join(HOME_DIR, ".agkyra")
+AGKYRA_DIR = os.environ.get('AGKYRA_DIR', DEFAULT_AGKYRA_DIR)
+AGKYRA_DIR = os.path.abspath(AGKYRA_DIR)
+
+if os.path.exists(AGKYRA_DIR):
+    if not os.path.isdir(AGKYRA_DIR):
+        raise Exception("Cannot create dir '%s'; file exists" % AGKYRA_DIR)
+else:
+    os.mkdir(AGKYRA_DIR)
+
+CONFIG_PATH = os.path.join(AGKYRA_DIR, 'config.rc')
 config.CONFIG_PATH = CONFIG_PATH
 # neutralize kamaki CONFIG_ENV for this session
 config.CONFIG_ENV = ''
diff --git a/agkyra/agkyra/protocol.py b/agkyra/agkyra/protocol.py
index a6a8273..7c579fb 100644
--- a/agkyra/agkyra/protocol.py
+++ b/agkyra/agkyra/protocol.py
@@ -19,7 +19,7 @@ import logging
 from os.path import abspath
 from agkyra.syncer import (
     syncer, setup, pithos_client, localfs_client, messaging)
-from agkyra.config import AgkyraConfig
+from agkyra.config import AgkyraConfig, AGKYRA_DIR
 
 
 LOG = logging.getLogger(__name__)
@@ -209,6 +209,7 @@ class WebSocketProtocol(WebSocket):
             sync,
             self.settings['url'], self.settings['token'],
             self.settings['container'], self.settings['directory'],
+            agkyra_path=AGKYRA_DIR,
             ignore_ssl=True)
         master = pithos_client.PithosFileClient(syncer_settings)
         slave = localfs_client.LocalfsFileClient(syncer_settings)
diff --git a/agkyra/agkyra/syncer/setup.py b/agkyra/agkyra/syncer/setup.py
index e5b9300..743c78c 100644
--- a/agkyra/agkyra/syncer/setup.py
+++ b/agkyra/agkyra/syncer/setup.py
@@ -56,9 +56,11 @@ class SyncerSettings():
         self.endpoint = self._get_pithos_client(
             auth_url, auth_token, container)
 
-        self.home_dir = os.path.expanduser('~')
-        self.settings_path = join_path(self.home_dir, GLOBAL_SETTINGS_NAME)
+        home_dir = os.path.expanduser('~')
+        default_settings_path = join_path(home_dir, GLOBAL_SETTINGS_NAME)
+        self.settings_path = kwargs.get("agkyra_path", default_settings_path)
         self.create_dir(self.settings_path)
+
         self.instance_path = join_path(self.settings_path, instance)
         self.create_dir(self.instance_path)
 
@@ -69,7 +71,7 @@ class SyncerSettings():
         self.local_root_path = local_root_path
         self.create_dir(self.local_root_path)
 
-        self.cache_name = kwargs.get("cache_path", DEFAULT_CACHE_NAME)
+        self.cache_name = kwargs.get("cache_name", DEFAULT_CACHE_NAME)
         self.cache_path = join_path(self.local_root_path, self.cache_name)
         self.create_dir(self.cache_path)
 
diff --git a/agkyra/launch b/agkyra/launch
index f9813a2..a2e3940 100755
--- a/agkyra/launch
+++ b/agkyra/launch
@@ -16,10 +16,12 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import os
-CURPATH = os.path.dirname(os.path.abspath(__file__))
+
+from agkyra import config
+AGKYRA_DIR = config.AGKYRA_DIR
 
 import logging
-logfile = os.path.join(CURPATH, 'agkyra.log')
+logfile = os.path.join(AGKYRA_DIR, 'agkyra.log')
 logger = logging.getLogger('agkyra')
 handler = logging.FileHandler(logfile)
 formatter = logging.Formatter("%(levelname)s:%(asctime)s:%(message)s")
-- 
GitLab