~abentley/juju-release-tools/stanzas-to-streams

« back to all changes in this revision

Viewing changes to upload_packages.py

  • Committer: Curtis Hovey
  • Date: 2015-08-26 21:55:52 UTC
  • mfrom: (216.2.10 upload-packges)
  • Revision ID: curtis@canonical.com-20150826215552-f85sbtq52flyq15r
Upload packges to Lp when they are new.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from __future__ import print_function
 
4
 
 
5
from argparse import ArgumentParser
 
6
import os
 
7
import subprocess
 
8
import sys
 
9
 
 
10
from launchpadlib.launchpad import Launchpad
 
11
 
 
12
 
 
13
def get_changes(package_dir):
 
14
    """Return source_name, version, file_name of the changes in the dir."""
 
15
    file_names = [
 
16
        f for f in os.listdir(package_dir) if f.endswith('_source.changes')]
 
17
    file_name = file_names[0]
 
18
    for file_name in os.listdir(package_dir):
 
19
        if file_name.endswith('_source.changes'):
 
20
            break
 
21
    else:
 
22
        raise ValueError('*.changes file not found in {}'.format(package_dir))
 
23
    with open(os.path.join(package_dir, file_name)) as changes_file:
 
24
        changes = changes_file.read()
 
25
    version = source_name = None
 
26
    for line in changes.splitlines():
 
27
        if line.startswith('Version:'):
 
28
            ignore, version = line.split(' ')
 
29
        if line.startswith('Source:'):
 
30
            ignore, source_name = line.split(' ')
 
31
        if version and source_name:
 
32
            break
 
33
    else:
 
34
        raise AssertionError(
 
35
            'Version: and Source: not found in {}'.format(file_name))
 
36
    return source_name, version, file_name
 
37
 
 
38
 
 
39
def upload_package(ppa, archive, package_dir, dry_run=False):
 
40
    """Upload a source package found in the directory to the PPA.
 
41
 
 
42
    Return True when the upload is performed, or False when the archive already
 
43
    has the source package.
 
44
    """
 
45
    source_name, version, file_name = get_changes(package_dir)
 
46
    package_histories = archive.getPublishedSources(
 
47
        source_name=source_name, version=version)
 
48
    if package_histories:
 
49
        print('{} {} is uploaded'.format(source_name, version))
 
50
        return False
 
51
    print('uploading {} {}'.format(source_name, version))
 
52
    if not dry_run:
 
53
        subprocess.check_call(['dput', ppa, file_name], cwd=package_dir)
 
54
    return True
 
55
 
 
56
 
 
57
def upload_packages(lp, ppa, package_dirs, dry_run=False):
 
58
    """Upload new source packages to the PPA."""
 
59
    ignore, team_archive = ppa.split(':')
 
60
    team_name, archive_name = team_archive.split('/')
 
61
    team = lp.people[team_name]
 
62
    archive = team.getPPAByName(name=archive_name)
 
63
    for package_dir in package_dirs:
 
64
        upload_package(ppa, archive, package_dir, dry_run=dry_run)
 
65
 
 
66
 
 
67
def get_args(argv=None):
 
68
    """Return the option parser for this program."""
 
69
    parser = ArgumentParser('Upload new source packages to Launchpad.')
 
70
    parser.add_argument(
 
71
        '-d', '--dry-run', action="store_true", default=False,
 
72
        help='Explain what will happen without making changes')
 
73
    parser.add_argument(
 
74
        "-c", "--credentials", default=None, type=os.path.expanduser,
 
75
        help="Launchpad credentials file.")
 
76
    parser.add_argument(
 
77
        'ppa', help='The ppa to upload to: ppa:<person>/<archive>.')
 
78
    parser.add_argument(
 
79
        'package_dirs', nargs='+', type=os.path.expanduser,
 
80
        help='One or more source package directories.')
 
81
    return parser.parse_args(argv)
 
82
 
 
83
 
 
84
def main(argv=None):
 
85
    """Upload new source packages to a PPA."""
 
86
    args = get_args(argv)
 
87
    lp = Launchpad.login_with(
 
88
        'upload-packages', service_root='https://api.launchpad.net',
 
89
        version='devel', credentials_file=args.credentials)
 
90
    upload_packages(
 
91
        lp, args.ppa, args.package_dirs, dry_run=args.dry_run)
 
92
    return 0
 
93
 
 
94
 
 
95
if __name__ == '__main__':
 
96
    sys.exit(main(sys.argv[1:]))