~ubuntu-branches/ubuntu/maverick/ubuntu-dev-tools/maverick-proposed

« back to all changes in this revision

Viewing changes to lp-project-upload

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-11-28 18:48:35 UTC
  • mfrom: (18.1.66 lucid)
  • Revision ID: james.westby@ubuntu.com-20091128184835-0ot21cscixychr6b
Tags: 0.83debian1
* Merge from Ubuntu Lucid, local Debian changes:
  - Adjust Maintainer and Uploaders.
  - Depend on python-lazr.restfulclient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Copyright (c) 2009 Canonical Ltd.
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License as published by the
 
7
# Free Software Foundation; either version 2, or (at your option) any
 
8
# later version.
 
9
#
 
10
# lp-set-dup is distributed in the hope that it will be useful, but
 
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
# General Public License for more details.
 
14
 
 
15
# Authors:
 
16
#  Martin Pitt <martin.pitt@ubuntu.com>, based on
 
17
#  http://blog.launchpad.net/api/recipe-for-uploading-files-via-the-api
 
18
 
 
19
'''Upload a release tarball to a Launchpad project.'''
 
20
 
 
21
import sys, datetime, os.path, subprocess, tempfile, os
 
22
 
 
23
from ubuntutools.lp.libsupport import get_launchpad
 
24
from launchpadlib.errors import HTTPError
 
25
 
 
26
def create_release(project, version):
 
27
    '''Create new release and milestone for LP project.'''
 
28
 
 
29
    print 'Release %s could not be found for project. Create it? (Y/n)' % version
 
30
    answer = sys.stdin.readline().strip()
 
31
    if answer.startswith('n'):
 
32
        sys.exit(0)
 
33
    if len(proj.series) != 1:
 
34
        print >> sys.stderr, 'Does not support creating releases if more than one series exists.'
 
35
        sys.exit(3)
 
36
    release_date = datetime.date.today().strftime('%Y-%m-%d')
 
37
    series = proj.series[0]
 
38
    milestone = series.newMilestone(name=version,
 
39
            date_targeted=release_date)
 
40
    return milestone.createProductRelease(date_released=release_date)
 
41
 
 
42
def edit_file(prefix, description):
 
43
    (fd, f) = tempfile.mkstemp(prefix=prefix+'.')
 
44
    os.write(fd, '\n\n#------\n# Please enter the %s here. Lines which start with "#" are ignored.\n' % 
 
45
            description)
 
46
    os.close(fd)
 
47
    subprocess.call(['sensible-editor', f])
 
48
    content = ''
 
49
    for l in open(f):
 
50
        if l.startswith('#'):
 
51
            continue
 
52
        content += l
 
53
 
 
54
    return content.strip()
 
55
 
 
56
#
 
57
# main
 
58
#
 
59
 
 
60
if len(sys.argv) != 4:
 
61
    print >> sys.stderr, '''Upload a release tarball to a Launchpad project.
 
62
 
 
63
Usage: %s <project name> <version> <tarball>''' % sys.argv[0]
 
64
    sys.exit(1)
 
65
 
 
66
(project, version, tarball) = sys.argv[1:]
 
67
 
 
68
try:
 
69
    lp = get_launchpad('ubuntu-dev-tools')
 
70
except Exception, e:
 
71
    print >> sys.stderr, 'Could not connect to Launchpad:', str(e)
 
72
    sys.exit(2)
 
73
 
 
74
try:
 
75
    # Look up the project using the Launchpad instance.
 
76
    proj = lp.projects[project]
 
77
    # Find the release in the project's releases collection.
 
78
    release = None
 
79
    for rel in proj.releases:
 
80
        if rel.version == version:
 
81
            release = rel
 
82
            break
 
83
    if not release:
 
84
        release = create_release(proj, version)
 
85
 
 
86
    # Get the file contents.
 
87
    file_content = open(tarball, 'r').read()
 
88
    # Get the signature, if available.
 
89
    signature = tarball + '.asc'
 
90
    if not os.path.exists(signature):
 
91
        print 'Calling GPG to create tarball signature...'
 
92
        if subprocess.call(['gpg', '--armor', '--sign', '--detach-sig', tarball]) != 0:
 
93
            print >> sys.stderr, 'gpg failed, aborting'
 
94
 
 
95
    if os.path.exists(signature):
 
96
        signature_content = open(signature, 'r').read()
 
97
    else:
 
98
        signature_content = None
 
99
 
 
100
    # Create a new product release file.
 
101
    release.add_file(filename=tarball, description='release tarball',
 
102
            file_content=file_content, content_type='appplication/x-gzip',
 
103
            file_type='Code Release Tarball', signature_filename=signature,
 
104
            signature_content=signature_content)
 
105
 
 
106
    changelog = edit_file('changelog', 'changelog')
 
107
    if changelog:
 
108
        release.changelog = changelog
 
109
    release_notes = edit_file('releasenotes', 'release notes')
 
110
    if release_notes:
 
111
        release.release_notes = release_notes
 
112
 
 
113
    release.lp_save()
 
114
 
 
115
except HTTPError, e:
 
116
    print 'An error happened in the upload:', e.content
 
117
    sys.exit(1)
 
118