1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 import os
, subprocess
, shutil
, tarfile
6 import urlparse
, ftplib
, httplib
7 from zeroinstall
import SafeException
8 from zeroinstall
.injector
import model
, qdom
9 from logging
import info
11 release_status_file
= os
.path
.abspath('release-status')
13 def check_call(*args
, **kwargs
):
14 exitstatus
= subprocess
.call(*args
, **kwargs
)
16 if type(args
[0]) in (str, unicode):
19 cmd
= ' '.join(args
[0])
20 raise SafeException("Command failed with exit code %d:\n%s" % (exitstatus
, cmd
))
22 def show_and_run(cmd
, args
):
23 print "Executing: %s %s" % (cmd
, ' '.join("[%s]" % x
for x
in args
))
24 check_call(['sh', '-c', cmd
, '-'] + args
)
26 def suggest_release_version(snapshot_version
):
27 """Given a snapshot version, suggest a suitable release version.
28 >>> suggest_release_version('1.0-pre')
30 >>> suggest_release_version('0.9-post')
32 >>> suggest_release_version('3')
33 Traceback (most recent call last):
35 SafeException: Version '3' is not a snapshot version (should end in -pre or -post)
37 version
= model
.parse_version(snapshot_version
)
40 raise SafeException("Version '%s' is not a snapshot version (should end in -pre or -post)" % snapshot_version
)
42 # -post, so increment the number
44 version
[-1] = 0 # Remove the modifier
45 return model
.format_version(version
)
47 def publish(iface
, **kwargs
):
48 args
= [os
.environ
['0PUBLISH']]
52 args
+= ['--' + k
.replace('_', '-')]
53 elif value
is not None:
54 args
+= ['--' + k
.replace('_', '-'), value
]
56 info("Executing %s", args
)
59 def get_singleton_impl(iface
):
60 impls
= iface
.implementations
62 raise SafeException("Local feed '%s' contains %d versions! I need exactly one!" % (iface
.uri
, len(impls
)))
63 return impls
.values()[0]
65 def backup_if_exists(name
):
66 if not os
.path
.exists(name
):
69 if os
.path
.exists(backup
):
70 print "(deleting old backup %s)" % backup
71 if os
.path
.isdir(backup
):
75 os
.rename(name
, backup
)
76 print "(renamed old %s as %s; will delete on next run)" % (name
, backup
)
78 def get_choice(options
):
80 choice
= raw_input('/'.join(options
) + ': ').lower()
81 if not choice
: continue
83 if o
.lower().startswith(choice
):
86 def make_archive_name(feed_name
, version
):
87 return feed_name
.lower().replace(' ', '-') + '-' + version
90 for x
in os
.environ
['PATH'].split(':'):
91 if os
.path
.isfile(os
.path
.join(x
, prog
)):
95 def show_diff(from_dir
, to_dir
):
96 for cmd
in [['meld'], ['xxdiff'], ['diff', '-ur']]:
98 code
= os
.spawnvp(os
.P_WAIT
, cmd
[0], cmd
+ [from_dir
, to_dir
])
100 print "WARNING: command %s failed with exit code %d" % (cmd
, code
)
103 class Status(object):
104 __slots__
= ['old_snapshot_version', 'release_version', 'head_before_release', 'new_snapshot_version',
105 'head_at_release', 'created_archive', 'src_tests_passed', 'tagged', 'verified_uploads', 'updated_master_feed']
107 for name
in self
.__slots
__:
108 setattr(self
, name
, None)
110 if os
.path
.isfile(release_status_file
):
111 for line
in file(release_status_file
):
112 assert line
.endswith('\n')
114 name
, value
= line
.split('=')
115 setattr(self
, name
, value
)
116 info("Loaded status %s=%s", name
, value
)
119 tmp_name
= release_status_file
+ '.new'
120 tmp
= file(tmp_name
, 'w')
122 lines
= ["%s=%s\n" % (name
, getattr(self
, name
)) for name
in self
.__slots
__ if getattr(self
, name
)]
123 tmp
.write(''.join(lines
))
125 os
.rename(tmp_name
, release_status_file
)
126 info("Wrote status to %s", release_status_file
)
132 if hasattr(address
, 'hostname'):
133 return address
.hostname
135 return address
[1].split(':', 1)[0]
138 if hasattr(address
, 'port'):
141 port
= address
[1].split(':', 1)[1:]
147 def get_http_size(url
, ttl
= 1):
148 assert url
.lower().startswith('http://')
150 address
= urlparse
.urlparse(url
)
151 http
= httplib
.HTTPConnection(host(address
), port(address
) or 80)
153 parts
= url
.split('/', 3)
159 http
.request('HEAD', '/' + path
, headers
= {'Host': host(address
)})
160 response
= http
.getresponse()
162 if response
.status
== 200:
163 return response
.getheader('Content-Length')
164 elif response
.status
in (301, 302):
165 new_url_rel
= response
.getheader('Location') or response
.getheader('URI')
166 new_url
= urlparse
.urljoin(url
, new_url_rel
)
168 raise SafeException("HTTP error: got status code %s" % response
.status
)
173 info("Resource moved! Checking new URL %s" % new_url
)
175 return get_http_size(new_url
, ttl
- 1)
177 raise SafeException('Too many redirections.')
179 def get_ftp_size(url
):
180 address
= urlparse
.urlparse(url
)
181 ftp
= ftplib
.FTP(host(address
))
184 return ftp
.size(url
.split('/', 3)[3])
189 scheme
= urlparse
.urlparse(url
)[0].lower()
190 if scheme
.startswith('http'):
191 return get_http_size(url
)
192 elif scheme
.startswith('ftp'):
193 return get_ftp_size(url
)
195 raise SafeException("Unknown scheme '%s' in '%s'" % (scheme
, url
))
197 def unpack_tarball(archive_file
):
198 tar
= tarfile
.open(archive_file
, 'r:bz2')
199 members
= [m
for m
in tar
.getmembers() if m
.name
!= 'pax_global_header']
200 #tar.extractall('.', members = members) # Python >= 2.5 only
201 for tarinfo
in members
:
202 tarinfo
= copy
.copy(tarinfo
)
205 tar
.extract(tarinfo
, '.')
210 return model
.ZeroInstallFeed(qdom
.parse(stream
), local_path
= path
)
214 def get_archive_basename(impl
):
215 # "2" means "path" (for Python 2.4)
216 return os
.path
.basename(urlparse
.urlparse(impl
.download_sources
[0].url
)[2])
218 def relative_path(ancestor
, dst
):
219 stem
= os
.path
.abspath(os
.path
.dirname(ancestor
))
220 dst
= os
.path
.abspath(dst
)
223 assert dst
.startswith(stem
)
224 return dst
[len(stem
):]
226 assert relative_path('/foo', '/foo') == 'foo'
227 assert relative_path('/foo', '/foo/bar') == 'foo/bar'