summaryrefslogtreecommitdiff
path: root/gitubuntu/importppa.py
blob: 1f3283c07e1f94d4d9f472beafed1250d5cad27f (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import argparse
import atexit
import getpass
import logging
import os
import re
import sys
from gitubuntu.cache import CACHE_PATH
from gitubuntu.git_repository import (
    GitUbuntuRepository,
    GitUbuntuRepositoryFetchError,
)
from gitubuntu.importer import (
    cleanup,
    parse_parentfile,
    _PARENT_OVERRIDES,
    import_orig,
    import_dsc,
    get_changelog_for_commit,
    import_unapplied_spi,
    import_applied_spi,
    import_patches_unapplied_tree,
    import_patches_applied_tree,
)
from gitubuntu.source_information import (
    GitUbuntuSourceInformation,
    NoPublicationHistoryException,
    SourceExtractionException,
    launchpad_login_auth,
    GitUbuntuSourceInformation,
)
from gitubuntu.version import VERSION
try:
    pkg = 'python3-pygit2'
    import pygit2
    pkg = 'python3-ubuntutools'
    from ubuntutools.archive import DownloadError
except ImportError:
    logging.error('Is %s installed?', pkg)
    sys.exit(1)


def main(
    ppa,
    pkgname,
    directory,
    user,
    pullfile,
    parentfile,
    dl_cache,
    proto,
    retries,
    retry_backoffs,
    skip_orig,
    allow_applied_failures,
):
    if re.match(r'ppa:\w+', ppa) is None:
        logging.error(
            "Specified PPA (%s) is not in the format ppa:<name>",
            ppa,
        )
        return 1
    # what other transformations?
    namespace = ppa.replace(':', '_')

    logging.info('Ubuntu Server Team importer v%s' % VERSION)

    repo = GitUbuntuRepository(directory, user, proto)
    if not directory:
        logging.info('Using git repository at %s', repo.local_dir)
    repo.ensure_importer_branches_exist(namespace)

    source_information = GitUbuntuSourceInformation(
        ppa,
        pkgname,
        os.path.abspath(pullfile),
        retries,
        retry_backoffs,
    )

    ubuntu_head_versions = repo.get_heads_and_versions(
        'ubuntu',
        namespace,
    )
    for head_name in sorted(ubuntu_head_versions):
        _, _, pretty_head_name = head_name.partition('%s/', namespace)
        logging.debug('Last imported %s version is %s',
            pretty_head_name,
            ubuntu_head_versions[head_name]['version']
        )

    applied_ubuntu_head_versions = repo.get_heads_and_versions(
        'applied/ubuntu',
        namespace,
    )
    for head_name in sorted(applied_ubuntu_head_versions):
        _, _, pretty_head_name = head_name.partition('%s/', namespace)
        logging.debug('Last applied %s version is %s',
            pretty_head_name,
            applied_ubuntu_head_versions[head_name]['version']
        )

    oldcwd = os.getcwd()
    os.chdir(repo.local_dir)

    if dl_cache is None:
        workdir = os.path.join(repo.git_dir, CACHE_PATH)
    else:
        workdir = dl_cache

    os.makedirs(workdir, exist_ok=True)

    parse_parentfile(pkgname, parentfile)

    history_found = False
    try:
        for srcpkg_information in \
            source_information.launchpad_versions_published_after(
                ubuntu_head_versions,
                namespace,
                workdir=workdir
        ):
            try:
                import_unapplied_spi(
                    repo=repo,
                    spi=srcpkg_information,
                    namespace=namespace,
                    ubuntu_sinfo=source_information,
                    skip_orig=skip_orig,
                )
            except DownloadError:
                # it is non-fatal for a PPA to not have files for older
                # publishes
                pass
        history_found = True
    except NoPublicationHistoryException:
        logging.warning("No publication history found for %s in %s.",
            pkgname,
            ppa,
        )

    if not history_found:
        logging.error(
            "No publication history for '%s' in %s. Wrong source package name?",
            pkgname,
            ppa,
        )
        return 1

    try:
        for srcpkg_information in \
                source_information.launchpad_versions_published_after(
                    applied_ubuntu_head_versions,
                    '%s/applied' % namespace,
                    workdir=workdir
        ):
            try:
                import_applied_spi(
                    repo=repo,
                    spi=srcpkg_information,
                    namespace=namespace,
                    ubuntu_sinfo=source_information,
                    allow_applied_failures=allow_applied_failures,
                )
            except DownloadError:
                # it is non-fatal for a PPA to not have files for older
                # publishes
                pass
    except NoPublicationHistoryException:
        logging.warning("No publication history found for %s in %s.",
            pkgname, ppa
        )
    except:
        logging.error("Unable to import patches-applied to %s", ppa)

    os.chdir(oldcwd)

    repo.garbage_collect()

    return 0


def parse_args(subparsers=None, base_subparsers=None):
    kwargs = dict(description='Update a launchpad git tree based upon '
                              'the state of a PPA archive',
                  epilog='PPA must be specified as ppa:<name>',
                  formatter_class=argparse.ArgumentDefaultsHelpFormatter
                 )
    if base_subparsers:
        kwargs['parents'] = base_subparsers
    if subparsers:
        parser = subparsers.add_parser('import-ppa', **kwargs)
        parser.set_defaults(func=cli_main)
    else:
        parser = argparse.ArgumentParser(**kwargs)
    parser.add_argument('ppa', type=str,
                        help='Name of PPA to update in the git tree')
    parser.add_argument('package', type=str,
                        help='Which package to update in the git tree')
    parser.add_argument('-l', '--lp-user', type=str,
                        help='Specify the Launchpad user to use',
                        default=getpass.getuser())
    parser.add_argument('--dl-cache', type=str,
                        help=('Cache directory for downloads. Default is to '
                              'put downloads in <directory>/.git/%s' %
                              CACHE_PATH
                             ),
                        default=argparse.SUPPRESS)
    parser.add_argument('-d', '--directory', type=str,
                        help='Use git repository at specified location rather '
                             'than a temporary directory. '
                             'Will create the local repository if needed.',
                        default=argparse.SUPPRESS
                       )
    parser.add_argument(
        '--skip-orig',
        action='store_true',
        help=argparse.SUPPRESS,
    )
    parser.add_argument(
        '--allow-applied-failures',
        action='store_true',
        help=argparse.SUPPRESS,
    )
    if not subparsers:
        return parser.parse_args()
    return 'import-ppa - %s' % kwargs['description']

def cli_main(args):
    try:
        directory = args.directory
    except AttributeError:
        directory = None
    try:
        dl_cache = args.dl_cache
    except AttributeError:
        dl_cache = None

    return main(
        args.ppa,
        args.package,
        directory,
        args.lp_user,
        args.pullfile,
        args.parentfile,
        dl_cache,
        args.proto,
        args.retries,
        args.retry_backoffs,
        args.skip_orig,
        args.allow_applied_failures,
    )