1 # vim: set fileencoding=utf-8 :
3 # (C) 2006, 2007, 2009, 2011 Guido Guenther <agx@sigxcpu.org>
4 # (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """Common functionality for import-orig scripts"""
23 import gbp.command_wrappers as gbpc
24 from gbp.pkg import parse_archive_filename
27 # Try to import readline, since that will cause raw_input to get fancy
28 # line editing and history capabilities. However, if readline is not
29 # available, input() will still work.
36 def cleanup_tmp_tree(tree):
37 """remove a tree of temporary files"""
39 gbpc.RemoveTree(tree)()
40 except gbpc.CommandExecFailed:
41 gbp.log.err("Removal of tmptree %s failed." % tree)
44 def ask_package_name(default, name_validator_func, err_msg):
46 Ask the user for the source package name.
47 @param default: The default package name to suggest to the user.
50 sourcepackage = input("What will be the source package name? [%s] " % default)
51 if not sourcepackage: # No input, use the default.
52 sourcepackage = default
53 # Valid package name, return it.
54 if name_validator_func(sourcepackage):
57 # Not a valid package name. Print an extra
58 # newline before the error to make the output a
60 gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, err_msg))
63 def ask_package_version(default, ver_validator_func, err_msg):
65 Ask the user for the upstream package version.
66 @param default: The default package version to suggest to the user.
69 version = input("What is the upstream version? [%s] " % default)
70 if not version: # No input, use the default.
72 # Valid version, return it.
73 if ver_validator_func(version):
76 # Not a valid upstream version. Print an extra
77 # newline before the error to make the output a
79 gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg))
81 def prepare_sources(source, pkg_name, pkg_version, pristine_commit_name,
82 filters, filter_pristine, prefix, tmpdir):
84 Prepare upstream sources for importing
86 Unpack, filter and repack sources for importing to git and to pristine-tar.
88 @param source: original upstream sources
89 @type source: C{UpstreamSource}
90 @param pkg_name: package name
91 @type pkg_name: C{str}
92 @param pkg_version: upstream version of the package
93 @type pkg_version: C{str}
94 @param pristine_commit_name: archive filename to commit to pristine-tar
95 @type pristine_commit_name: C{str} or C{None}
96 @param filters: filter to exclude files
97 @type filters: C{list} of C{str}
98 @param filter_pristine: filter pristine-tar, too
99 @type filter_pristine: C{bool}
100 @param prefix: prefix (i.e. leading directory of files) to use in
101 pristine-tar, set to C{None} to not mangle orig archive
102 @type prefix: C{str} or C{None}
103 @param tmpdir: temporary working dir (cleanup left to caller)
105 @return: path to prepared source tree and tarball to commit to pristine-tar
106 @rtype: C{tuple} of C{str}
109 # Determine parameters for pristine tar
110 pristine_filters = filters if filters and filter_pristine else None
111 pristine_prefix = None
112 if prefix is not None and prefix != 'auto':
113 prefix_subst = {'name': pkg_name,
114 'version': pkg_version,
115 'upstreamversion': pkg_version}
116 pristine_prefix = prefix % prefix_subst
117 # Handle unpacked sources, i.e. importing a directory
119 if pristine_commit_name:
120 gbp.log.warn('Preparing unpacked sources for pristine-tar')
121 pristine = prepare_pristine_tar(source, pkg_name, pkg_version,
122 pristine_commit_name,
123 pristine_filters, pristine_prefix,
126 # Re-use sources packed for pristine-tar, if available
130 packed_fn = tempfile.mkstemp(prefix="packed_", dir=tmpdir,
132 gbp.log.debug("Packing '%s' to '%s'" % (source.path, packed_fn))
133 packed = source.pack(packed_fn)
134 unpack_dir = tempfile.mkdtemp(prefix='filtered_', dir=tmpdir)
135 filtered = packed.unpack(unpack_dir, filters)
138 # Handle source archives
140 unpack_dir = tempfile.mkdtemp(prefix='filtered_', dir=tmpdir)
141 gbp.log.debug("Unpacking '%s' to '%s'" % (source.path, unpack_dir))
142 filtered = source.unpack(unpack_dir, filters)
143 if pristine_commit_name:
144 pristine = prepare_pristine_tar(source, pkg_name, pkg_version,
145 pristine_commit_name,
146 pristine_filters, pristine_prefix,
148 pristine_path = pristine.path if pristine else ''
149 return (filtered.unpacked, pristine_path)
151 def prepare_pristine_tar(source, pkg_name, pkg_version, pristine_commit_name,
152 filters=None, prefix=None, tmpdir=None):
154 Prepare the upstream sources for pristine-tar import
156 @param source: original upstream sources
157 @type source: C{UpstreamSource}
158 @param pkg_name: package name
159 @type pkg_name: C{str}
160 @param pkg_version: upstream version of the package
161 @type pkg_version: C{str}
162 @param pristine_commit_name: archive filename to commit to pristine-tar
163 @type pristine_commit_name: C{str} or C{None}
164 @param filters: filter to exclude files
165 @type filters: C{list} of C{str} or C{None}
166 @param prefix: prefix (i.e. leading directory of files) to use in
167 pristine-tar, set to C{None} to not mangle orig archive
168 @type prefix: C{str} or C{None}
169 @param tmpdir: temporary working dir (cleanup left to caller)
171 @return: prepared source archive
172 @rtype: C{UpstreamSource}
177 prefix = '%s-%s' % (pkg_name, pkg_version)
178 gbp.log.info("Using guessed prefix '%s/' for pristine-tar" % prefix)
181 if prefix is not None and prefix == source.prefix:
183 comp = parse_archive_filename(pristine_commit_name)[2]
184 if filters or prefix is not None or source.compression != comp:
185 if not source.unpacked:
186 unpack_dir = tempfile.mkdtemp(prefix='pristine_unpack_',
188 source.unpack(unpack_dir)
190 pristine_path = os.path.join(tmpdir, pristine_commit_name)
192 gbp.log.debug("Packing '%s' from '%s' for pristine-tar" %
193 (pristine_path, source.unpacked))
194 pristine = source.pack(pristine_path, filters, prefix)
196 # Just create symlink for mangling the pristine tarball name
197 os.symlink(source.path, pristine_path)
198 pristine = source.__class__(pristine_path)