porting code to python3.x with os patch
[tools/git-buildpackage.git] / gbp / scripts / common / import_orig.py
1 # vim: set fileencoding=utf-8 :
2 #
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.
9 #
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.
14 #
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
18 #
19 """Common functionality for import-orig scripts"""
20 import os
21 import tempfile
22
23 import gbp.command_wrappers as gbpc
24 from gbp.pkg import parse_archive_filename
25 import gbp.log
26
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.
30 try:
31     import readline
32 except ImportError:
33     pass
34
35
36 def cleanup_tmp_tree(tree):
37     """remove a tree of temporary files"""
38     try:
39         gbpc.RemoveTree(tree)()
40     except gbpc.CommandExecFailed:
41         gbp.log.err("Removal of tmptree %s failed." % tree)
42
43
44 def ask_package_name(default, name_validator_func, err_msg):
45     """
46     Ask the user for the source package name.
47     @param default: The default package name to suggest to the user.
48     """
49     while True:
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):
55             return sourcepackage
56
57         # Not a valid package name. Print an extra
58         # newline before the error to make the output a
59         # bit clearer.
60         gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, err_msg))
61
62
63 def ask_package_version(default, ver_validator_func, err_msg):
64     """
65     Ask the user for the upstream package version.
66     @param default: The default package version to suggest to the user.
67     """
68     while True:
69         version = input("What is the upstream version? [%s] " % default)
70         if not version:  # No input, use the default.
71             version = default
72         # Valid version, return it.
73         if ver_validator_func(version):
74             return version
75
76         # Not a valid upstream version. Print an extra
77         # newline before the error to make the output a
78         # bit clearer.
79         gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, err_msg))
80
81 def prepare_sources(source, pkg_name, pkg_version, pristine_commit_name,
82                     filters, filter_pristine, prefix, tmpdir):
83     """
84     Prepare upstream sources for importing
85
86     Unpack, filter and repack sources for importing to git and to pristine-tar.
87
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)
104     @type tmpdir: C{str}
105     @return: path to prepared source tree and tarball to commit to pristine-tar
106     @rtype: C{tuple} of C{str}
107     """
108     pristine = None
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
118     if source.is_dir():
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,
124                                             tmpdir)
125         if filters:
126             # Re-use sources packed for pristine-tar, if available
127             if pristine:
128                 packed = pristine
129             else:
130                 packed_fn = tempfile.mkstemp(prefix="packed_", dir=tmpdir,
131                                              suffix='.tar')[1]
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)
136         else:
137             filtered = source
138     # Handle source archives
139     else:
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,
147                                             tmpdir)
148     pristine_path = pristine.path if pristine else ''
149     return (filtered.unpacked, pristine_path)
150
151 def prepare_pristine_tar(source, pkg_name, pkg_version, pristine_commit_name,
152                          filters=None, prefix=None, tmpdir=None):
153     """
154     Prepare the upstream sources for pristine-tar import
155
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)
170     @type tmpdir: C{str}
171     @return: prepared source archive
172     @rtype: C{UpstreamSource}
173     """
174     need_repack = False
175     if source.is_dir():
176         if prefix is None:
177             prefix = '%s-%s' % (pkg_name, pkg_version)
178             gbp.log.info("Using guessed prefix '%s/' for pristine-tar" % prefix)
179         need_repack = True
180     else:
181         if prefix is not None and prefix == source.prefix:
182             prefix = None
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_',
187                                               dir=tmpdir)
188                 source.unpack(unpack_dir)
189             need_repack = True
190     pristine_path = os.path.join(tmpdir, pristine_commit_name)
191     if need_repack:
192         gbp.log.debug("Packing '%s' from '%s' for pristine-tar" %
193                         (pristine_path, source.unpacked))
194         pristine = source.pack(pristine_path, filters, prefix)
195     else:
196         # Just create symlink for mangling the pristine tarball name
197         os.symlink(source.path, pristine_path)
198         pristine = source.__class__(pristine_path)
199
200     return pristine
201
202