GITC: Pull GITC Manifest Dir from the config.

Updates the repo launcher and gitc_utils to pull the manifest
directory location out of the gitc config file.

Change-Id: Id08381b8a7d61962093d5cddcb3ff6afbb13004b
diff --git a/gitc_utils.py b/gitc_utils.py
index 4d8d536..d082c8d 100644
--- a/gitc_utils.py
+++ b/gitc_utils.py
@@ -20,13 +20,15 @@
 
 import git_command
 import git_config
+import wrapper
 
 
-# TODO (sbasi) - Remove this constant and fetch manifest dir from /gitc/.config
-GITC_MANIFEST_DIR = '/usr/local/google/gitc/'
 GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
 NUM_BATCH_RETRIEVE_REVISIONID = 300
 
+def get_gitc_manifest_dir():
+  return wrapper.Wrapper().get_gitc_manifest_dir()
+
 def parse_clientdir(gitc_fs_path):
   """Parse a path in the GITC FS and return its client name.
 
diff --git a/manifest_xml.py b/manifest_xml.py
index b33ec62..a7fe8dd 100644
--- a/manifest_xml.py
+++ b/manifest_xml.py
@@ -956,7 +956,7 @@
     super(GitcManifest, self).__init__(repodir)
     self.isGitcClient = True
     self.gitc_client_name = gitc_client_name
-    self.gitc_client_dir = os.path.join(gitc_utils.GITC_MANIFEST_DIR,
+    self.gitc_client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
                                         gitc_client_name)
     self.manifestFile = os.path.join(self.gitc_client_dir, '.manifest')
 
diff --git a/repo b/repo
index bf8fa3d..ff82159 100755
--- a/repo
+++ b/repo
@@ -108,7 +108,7 @@
 S_manifests = 'manifests'       # special manifest repository
 REPO_MAIN = S_repo + '/main.py' # main script
 MIN_PYTHON_VERSION = (2, 6)     # minimum supported python version
-GITC_MANIFEST_DIR = '/usr/local/google/gitc'
+GITC_CONFIG_FILE = '/gitc/.config'
 
 
 import errno
@@ -222,6 +222,20 @@
                dest='gitc_client',
                help='The name for the new gitc_client instance.')
 
+_gitc_manifest_dir = None
+def get_gitc_manifest_dir():
+  global _gitc_manifest_dir
+  if _gitc_manifest_dir is None:
+    try:
+      with open(GITC_CONFIG_FILE, 'r') as gitc_config:
+        for line in gitc_config:
+          match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line)
+          if match:
+            _gitc_manifest_dir = match.group('gitc_manifest_dir')
+    except IOError:
+      _gitc_manifest_dir = ''
+  return _gitc_manifest_dir
+
 class CloneFailure(Exception):
   """Indicate the remote clone of repo itself failed.
   """
@@ -255,7 +269,12 @@
 
   try:
     if gitc_init:
-      client_dir = os.path.join(GITC_MANIFEST_DIR, opt.gitc_client)
+      gitc_manifest_dir = get_gitc_manifest_dir()
+      if not gitc_manifest_dir:
+        _print('error: GITC filesystem is not running. Exiting...',
+               file=sys.stderr)
+        sys.exit(1)
+      client_dir = os.path.join(gitc_manifest_dir, opt.gitc_client)
       if not os.path.exists(client_dir):
         os.makedirs(client_dir)
       os.chdir(client_dir)
@@ -746,6 +765,12 @@
   wrapper_path = os.path.abspath(__file__)
   my_main, my_git = _RunSelf(wrapper_path)
 
+  cwd = os.getcwd()
+  if cwd.startswith(get_gitc_manifest_dir()):
+    _print('error: repo cannot be used in the GITC local manifest directory.'
+           '\nIf you want to work on this GITC client please rerun this '
+           'command from the corresponding client under /gitc/', file=sys.stderr)
+    sys.exit(1)
   if not repo_main:
     if opt.help:
       _Usage()
diff --git a/subcmds/gitc_init.py b/subcmds/gitc_init.py
index 92b34f7..c0568ca 100644
--- a/subcmds/gitc_init.py
+++ b/subcmds/gitc_init.py
@@ -59,10 +59,10 @@
     if not opt.gitc_client:
       print('fatal: gitc client (-c) is required', file=sys.stderr)
       sys.exit(1)
-    self.client_dir = os.path.join(gitc_utils.GITC_MANIFEST_DIR,
+    self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
                                    opt.gitc_client)
-    if not os.path.exists(gitc_utils.GITC_MANIFEST_DIR):
-      os.makedirs(gitc_utils.GITC_MANIFEST_DIR)
+    if not os.path.exists(gitc_utils.get_gitc_manifest_dir()):
+      os.makedirs(gitc_utils.get_gitc_manifest_dir())
     if not os.path.exists(self.client_dir):
       os.mkdir(self.client_dir)
     super(GitcInit, self).Execute(opt, args)
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 934aaa8..2e9bbe7 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -194,9 +194,6 @@
                  help="overwrite an existing git directory if it needs to "
                       "point to a different object directory. WARNING: this "
                       "may cause loss of data")
-    p.add_option('--force-gitc',
-                 dest='force_gitc', action='store_true',
-                 help="actually sync sources in the gitc client directory.")
     p.add_option('-l', '--local-only',
                  dest='local_only', action='store_true',
                  help="only update working tree, don't fetch")
@@ -539,16 +536,6 @@
         print('error: both -u and -p must be given', file=sys.stderr)
         sys.exit(1)
 
-    cwd = os.getcwd()
-    if cwd.startswith(gitc_utils.GITC_MANIFEST_DIR) and not opt.force_gitc:
-      print('WARNING this will pull all the sources like a normal repo sync.\n'
-            '\nIf you want to update your GITC Client View please rerun this '
-            'command in \n%s%s.\nOr if you actually want to pull the sources, '
-            'rerun with --force-gitc.' %
-            (gitc_utils.GITC_FS_ROOT_DIR,
-             cwd.split(gitc_utils.GITC_MANIFEST_DIR)[1]))
-      sys.exit(1)
-
     if opt.manifest_name:
       self.manifest.Override(opt.manifest_name)