summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2016-03-02 00:04:46 -0800
committerTodd Gamblin <tgamblin@llnl.gov>2016-03-02 00:04:46 -0800
commitbe306d09e99a69732afc3f44724222ab6c6d71cc (patch)
treed380d93c657c5f849352c776c1ad15f8ed340e27
parentc488f7c4d8e2ff240d561820df11a51518199a53 (diff)
downloadspack-be306d09e99a69732afc3f44724222ab6c6d71cc.tar.gz
spack-be306d09e99a69732afc3f44724222ab6c6d71cc.tar.bz2
spack-be306d09e99a69732afc3f44724222ab6c6d71cc.tar.xz
spack-be306d09e99a69732afc3f44724222ab6c6d71cc.zip
Move repo creation code into repository.py
-rw-r--r--lib/spack/spack/cmd/repo.py46
-rw-r--r--lib/spack/spack/repository.py58
2 files changed, 58 insertions, 46 deletions
diff --git a/lib/spack/spack/cmd/repo.py b/lib/spack/spack/cmd/repo.py
index 908f5400ab..c2e352786d 100644
--- a/lib/spack/spack/cmd/repo.py
+++ b/lib/spack/spack/cmd/repo.py
@@ -74,51 +74,7 @@ def setup_parser(subparser):
def repo_create(args):
"""Create a new package repository."""
- root = canonicalize_path(args.directory)
- namespace = args.namespace
-
- if not args.namespace:
- namespace = os.path.basename(root)
-
- if not re.match(r'\w[\.\w-]*', namespace):
- tty.die("'%s' is not a valid namespace." % namespace)
-
- existed = False
- if os.path.exists(root):
- if os.path.isfile(root):
- tty.die('File %s already exists and is not a directory' % root)
- elif os.path.isdir(root):
- if not os.access(root, os.R_OK | os.W_OK):
- tty.die('Cannot create new repo in %s: cannot access directory.' % root)
- if os.listdir(root):
- tty.die('Cannot create new repo in %s: directory is not empty.' % root)
- existed = True
-
- full_path = os.path.realpath(root)
- parent = os.path.dirname(full_path)
- if not os.access(parent, os.R_OK | os.W_OK):
- tty.die("Cannot create repository in %s: can't access parent!" % root)
-
- try:
- config_path = os.path.join(root, repo_config_name)
- packages_path = os.path.join(root, packages_dir_name)
-
- mkdirp(packages_path)
- with open(config_path, 'w') as config:
- config.write("repo:\n")
- config.write(" namespace: '%s'\n" % namespace)
-
- except (IOError, OSError) as e:
- tty.die('Failed to create new repository in %s.' % root,
- "Caused by %s: %s" % (type(e), e))
-
- # try to clean up.
- if existed:
- shutil.rmtree(config_path, ignore_errors=True)
- shutil.rmtree(packages_path, ignore_errors=True)
- else:
- shutil.rmtree(root, ignore_errors=True)
-
+ full_path, namespace = create_repo(args.directory, args.namespace)
tty.msg("Created repo with namespace '%s'." % namespace)
tty.msg("To register it with spack, run this command:",
'spack repo add %s' % full_path)
diff --git a/lib/spack/spack/repository.py b/lib/spack/spack/repository.py
index e8d0cc09ec..6aa75cb43e 100644
--- a/lib/spack/spack/repository.py
+++ b/lib/spack/spack/repository.py
@@ -33,7 +33,7 @@ from bisect import bisect_left
from external import yaml
import llnl.util.tty as tty
-from llnl.util.filesystem import join_path
+from llnl.util.filesystem import *
import spack.error
import spack.config
@@ -705,6 +705,58 @@ class Repo(object):
return self.exists(pkg_name)
+def create_repo(root, namespace=None):
+ """Create a new repository in root with the specified namespace.
+
+ If the namespace is not provided, use basename of root.
+ Return the canonicalized path and the namespace of the created repository.
+ """
+ root = canonicalize_path(root)
+ if not namespace:
+ namespace = os.path.basename(root)
+
+ if not re.match(r'\w[\.\w-]*', namespace):
+ raise InvalidNamespaceError("'%s' is not a valid namespace." % namespace)
+
+ existed = False
+ if os.path.exists(root):
+ if os.path.isfile(root):
+ raise BadRepoError('File %s already exists and is not a directory' % root)
+ elif os.path.isdir(root):
+ if not os.access(root, os.R_OK | os.W_OK):
+ raise BadRepoError('Cannot create new repo in %s: cannot access directory.' % root)
+ if os.listdir(root):
+ raise BadRepoError('Cannot create new repo in %s: directory is not empty.' % root)
+ existed = True
+
+ full_path = os.path.realpath(root)
+ parent = os.path.dirname(full_path)
+ if not os.access(parent, os.R_OK | os.W_OK):
+ raise BadRepoError("Cannot create repository in %s: can't access parent!" % root)
+
+ try:
+ config_path = os.path.join(root, repo_config_name)
+ packages_path = os.path.join(root, packages_dir_name)
+
+ mkdirp(packages_path)
+ with open(config_path, 'w') as config:
+ config.write("repo:\n")
+ config.write(" namespace: '%s'\n" % namespace)
+
+ except (IOError, OSError) as e:
+ raise BadRepoError('Failed to create new repository in %s.' % root,
+ "Caused by %s: %s" % (type(e), e))
+
+ # try to clean up.
+ if existed:
+ shutil.rmtree(config_path, ignore_errors=True)
+ shutil.rmtree(packages_path, ignore_errors=True)
+ else:
+ shutil.rmtree(root, ignore_errors=True)
+
+ return full_path, namespace
+
+
class RepoError(spack.error.SpackError):
"""Superclass for repository-related errors."""
@@ -713,6 +765,10 @@ class NoRepoConfiguredError(RepoError):
"""Raised when there are no repositories configured."""
+class InvalidNamespaceError(RepoError):
+ """Raised when an invalid namespace is encountered."""
+
+
class BadRepoError(RepoError):
"""Raised when repo layout is invalid."""