diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2015-04-08 00:22:07 -0700 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2015-04-08 00:22:07 -0700 |
commit | ab40d0b1a48478d6ba6af140de3d695ee8750749 (patch) | |
tree | a76a6fbcdf37126d930aa50bd4c98b7e7f18592a /lib | |
parent | c4a6d0413161c519ed3a0ddb8a9cf3cc5a422f20 (diff) | |
download | spack-ab40d0b1a48478d6ba6af140de3d695ee8750749.tar.gz spack-ab40d0b1a48478d6ba6af140de3d695ee8750749.tar.bz2 spack-ab40d0b1a48478d6ba6af140de3d695ee8750749.tar.xz spack-ab40d0b1a48478d6ba6af140de3d695ee8750749.zip |
Factor edit_package function out of edit command.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/cmd/edit.py | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/lib/spack/spack/cmd/edit.py b/lib/spack/spack/cmd/edit.py index 842dcd7faf..c4225a7dd3 100644 --- a/lib/spack/spack/cmd/edit.py +++ b/lib/spack/spack/cmd/edit.py @@ -24,7 +24,6 @@ ############################################################################## import os import string -from contextlib import closing import llnl.util.tty as tty from llnl.util.filesystem import mkdirp, join_path @@ -54,6 +53,27 @@ class ${class_name}(Package): """) +def edit_package(name, force=False): + path = spack.db.filename_for_package_name(name) + + if os.path.exists(path): + if not os.path.isfile(path): + tty.die("Something's wrong. '%s' is not a file!" % path) + if not os.access(path, os.R_OK|os.W_OK): + tty.die("Insufficient permissions on '%s'!" % path) + elif not force: + tty.die("No package '%s'. Use spack create, or supply -f/--force " + "to edit a new file." % name) + else: + mkdirp(os.path.dirname(path)) + with open(path, "w") as pkg_file: + pkg_file.write( + package_template.substitute( + name=name, class_name=mod_to_class(name))) + + spack.editor(path) + + def setup_parser(subparser): subparser.add_argument( '-f', '--force', dest='force', action='store_true', @@ -80,22 +100,7 @@ def edit(parser, args): # By default open the directory where packages or commands live. if not name: path = spack.packages_path + spack.editor(path) else: - path = spack.db.filename_for_package_name(name) - - if os.path.exists(path): - if not os.path.isfile(path): - tty.die("Something's wrong. '%s' is not a file!" % path) - if not os.access(path, os.R_OK|os.W_OK): - tty.die("Insufficient permissions on '%s'!" % path) - elif not args.force: - tty.die("No package '%s'. Use spack create, or supply -f/--force " - "to edit a new file." % name) - else: - mkdirp(os.path.dirname(path)) - with closing(open(path, "w")) as pkg_file: - pkg_file.write( - package_template.substitute(name=name, class_name=mod_to_class(name))) - - # If everything checks out, go ahead and edit. - spack.editor(path) + edit_package(name, args.force) + |