summaryrefslogtreecommitdiff
path: root/lib/spack/spack/cmd/create.py
blob: 8653fafa5f2eb8fe94c881abc4877d4bb5f399cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
##############################################################################
# Copyright (c) 2013, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Written by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://scalability-llnl.github.io/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (as published by
# the Free Software Foundation) version 2.1 dated February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import string
import os
import hashlib
import re
from contextlib import closing

import llnl.util.tty as tty
from llnl.util.filesystem import mkdirp

import spack
import spack.cmd
import spack.cmd.checksum
import spack.package
import spack.url
import spack.packages as packages
import spack.util.crypto as crypto

from spack.util.executable import which
from spack.stage import Stage


description = "Create a new package file from an archive URL"

package_template = string.Template("""\
# FIXME:
# This is a template package file for Spack.  We've conveniently
# put "FIXME" labels next to all the things you'll want to change.
#
# Once you've edited all the FIXME's, delete this whole message,
# save this file, and test out your package like this:
#
#     spack install ${name}
#
# You can always get back here to change things with:
#
#     spack edit ${name}
#
# See the spack documentation for more information on building
# packages.
#
from spack import *

class ${class_name}(Package):
    ""\"FIXME: put a proper description of your package here.""\"
    # FIXME: add a proper url for your package's homepage here.
    homepage = "http://www.example.com"
    url      = "${url}"

    versions = ${versions}

    def install(self, spec, prefix):
        # FIXME: Modify the configure line to suit your build system here.
        ${configure}

        # FIXME: Add logic to build and install here
        make()
        make("install")
""")


def setup_parser(subparser):
    subparser.add_argument('url', nargs='?', help="url of package archive")
    subparser.add_argument(
        '--keep-stage', action='store_true', dest='keep_stage',
        help="Don't clean up staging area when command completes.")
    subparser.add_argument(
        '-f', '--force', action='store_true', dest='force',
        help="Overwrite any existing package file with the same name.")


class ConfigureGuesser(object):
    def __call__(self, stage):
        """Try to guess the type of build system used by the project, and return
           an appropriate configure line.
        """
        tar = which('tar')
        output = tar(
            "--exclude=*/*/*", "-tf", stage.archive_file, return_output=True)

        autotools = 'configure("--prefix=%s" % prefix)'
        cmake     = 'cmake(".", *std_cmake_args)'
        lines = output.split('\n')

        if any(re.search(r'/configure$', l) for l in lines):
            self.configure = autotools
        elif  any(re.search(r'/CMakeLists.txt$', l) for l in lines):
            self.configure = cmake
        else:
            # Both, with cmake commented out
            self.configure = '%s\n        # %s' % (autotools, cmake)


def make_version_dict(ver_hash_tuples):
    max_len = max(len(str(v)) for v,hfg in ver_hash_tuples)
    width = max_len + 2
    format = "%-" + str(width) + "s : '%s',"
    sep = '\n                 '
    return '{ ' + sep.join(format % ("'%s'" % v, h)
                           for v, h in ver_hash_tuples) + ' }'


def create(parser, args):
    url = args.url

    # Try to deduce name and version of the new package from the URL
    name, version = spack.url.parse_name_and_version(url)
    if not name:
        tty.msg("Couldn't guess a name for this package.")
        while not name:
            new_name = raw_input("Name: ")
            if spack.db.valid_name(name):
                name = new_name
            else:
                print "Package name can only contain A-Z, a-z, 0-9, '_' and '-'"

    if not version:
        tty.die("Couldn't guess a version string from %s." % url)

    tty.msg("Creating template for package %s" % name)

    pkg_path = spack.db.filename_for_package_name(name)
    if os.path.exists(pkg_path) and not args.force:
        tty.die("%s already exists." % pkg_path)
    else:
        mkdirp(os.path.dirname(pkg_path))

    class_name = packages.class_name_for_package_name(name)
    versions = list(reversed(spack.package.find_versions_of_archive(url)))

    archives_to_fetch = 1
    if not versions:
        # If the fetch failed for some reason, revert to what the user provided
        versions = [version]
        urls = [url]
    else:
        urls = [spack.url.substitute_version(url, v) for v in versions]
        if len(urls) > 1:
            tty.msg("Found %s versions of %s:" % (len(urls), name),
                    *spack.cmd.elide_list(
                    ["%-10s%s" % (v,u) for v, u in zip(versions, urls)]))
            print
            archives_to_fetch = tty.get_number(
                "Include how many checksums in the package file?",
                default=5, abort='q')

            if not archives_to_fetch:
                tty.msg("Aborted.")
                return

    guesser = ConfigureGuesser()
    ver_hash_tuples = spack.cmd.checksum.get_checksums(
        versions[:archives_to_fetch], urls[:archives_to_fetch],
        first_stage_function=guesser, keep_stage=args.keep_stage)

    if not ver_hash_tuples:
        tty.die("Could not fetch any tarballs for %s." % name)

    # Write out a template for the file
    with closing(open(pkg_path, "w")) as pkg_file:
        pkg_file.write(
            package_template.substitute(
                name=name,
                configure=guesser.configure,
                class_name=class_name,
                url=url,
                versions=make_version_dict(ver_hash_tuples)))

    # If everything checks out, go ahead and edit.
    spack.editor(pkg_path)
    tty.msg("Created package %s." % pkg_path)