1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 import os
, subprocess
, tarfile
6 import urlparse
, ftplib
, httplib
7 from xml
.dom
import minidom
9 from zeroinstall
import SafeException
10 from zeroinstall
.injector
import model
, qdom
, namespaces
11 from zeroinstall
.support
import ro_rmtree
12 from logging
import info
14 release_status_file
= os
.path
.abspath('release-status')
16 def check_call(*args
, **kwargs
):
17 exitstatus
= subprocess
.call(*args
, **kwargs
)
19 if type(args
[0]) in (str, unicode):
22 cmd
= ' '.join(args
[0])
23 raise SafeException("Command failed with exit code %d:\n%s" % (exitstatus
, cmd
))
25 def show_and_run(cmd
, args
):
26 print "Executing: %s %s" % (cmd
, ' '.join("[%s]" % x
for x
in args
))
27 check_call(['sh', '-c', cmd
, '-'] + args
)
29 def suggest_release_version(snapshot_version
):
30 """Given a snapshot version, suggest a suitable release version.
31 >>> suggest_release_version('1.0-pre')
33 >>> suggest_release_version('0.9-post')
35 >>> suggest_release_version('3')
36 Traceback (most recent call last):
38 SafeException: Version '3' is not a snapshot version (should end in -pre or -post)
40 version
= model
.parse_version(snapshot_version
)
43 raise SafeException("Version '%s' is not a snapshot version (should end in -pre or -post)" % snapshot_version
)
45 # -post, so increment the number
47 version
[-1] = 0 # Remove the modifier
48 return model
.format_version(version
)
50 def publish(feed_path
, **kwargs
):
51 args
= [os
.environ
['0PUBLISH']]
55 args
+= ['--' + k
.replace('_', '-')]
56 elif value
is not None:
57 args
+= ['--' + k
.replace('_', '-'), value
]
58 args
.append(feed_path
)
59 info("Executing %s", args
)
62 def get_singleton_impl(feed
):
63 impls
= feed
.implementations
65 raise SafeException("Local feed '%s' contains %d versions! I need exactly one!" % (feed
.url
, len(impls
)))
66 return impls
.values()[0]
68 def backup_if_exists(name
):
69 if not os
.path
.exists(name
):
72 if os
.path
.exists(backup
):
73 print "(deleting old backup %s)" % backup
74 if os
.path
.isdir(backup
):
78 os
.rename(name
, backup
)
79 print "(renamed old %s as %s; will delete on next run)" % (name
, backup
)
81 def get_choice(options
):
83 choice
= raw_input('/'.join(options
) + ': ').lower()
84 if not choice
: continue
86 if o
.lower().startswith(choice
):
89 def make_archive_name(feed_name
, version
):
90 return feed_name
.lower().replace(' ', '-') + '-' + version
93 for x
in os
.environ
['PATH'].split(':'):
94 if os
.path
.isfile(os
.path
.join(x
, prog
)):
98 def show_diff(from_dir
, to_dir
):
99 for cmd
in [['meld'], ['xxdiff'], ['diff', '-ur']]:
101 code
= os
.spawnvp(os
.P_WAIT
, cmd
[0], cmd
+ [from_dir
, to_dir
])
103 print "WARNING: command %s failed with exit code %d" % (cmd
, code
)
106 class Status(object):
107 __slots__
= ['old_snapshot_version', 'release_version', 'head_before_release', 'new_snapshot_version',
108 'head_at_release', 'created_archive', 'src_tests_passed', 'tagged', 'verified_uploads', 'updated_master_feed']
110 for name
in self
.__slots
__:
111 setattr(self
, name
, None)
113 if os
.path
.isfile(release_status_file
):
114 for line
in file(release_status_file
):
115 assert line
.endswith('\n')
117 name
, value
= line
.split('=')
118 setattr(self
, name
, value
)
119 info("Loaded status %s=%s", name
, value
)
122 tmp_name
= release_status_file
+ '.new'
123 tmp
= file(tmp_name
, 'w')
125 lines
= ["%s=%s\n" % (name
, getattr(self
, name
)) for name
in self
.__slots
__ if getattr(self
, name
)]
126 tmp
.write(''.join(lines
))
128 os
.rename(tmp_name
, release_status_file
)
129 info("Wrote status to %s", release_status_file
)
135 if hasattr(address
, 'hostname'):
136 return address
.hostname
138 return address
[1].split(':', 1)[0]
141 if hasattr(address
, 'port'):
144 port
= address
[1].split(':', 1)[1:]
150 def get_http_size(url
, ttl
= 1):
151 assert url
.lower().startswith('http://')
153 address
= urlparse
.urlparse(url
)
154 http
= httplib
.HTTPConnection(host(address
), port(address
) or 80)
156 parts
= url
.split('/', 3)
162 http
.request('HEAD', '/' + path
, headers
= {'Host': host(address
)})
163 response
= http
.getresponse()
165 if response
.status
== 200:
166 return response
.getheader('Content-Length')
167 elif response
.status
in (301, 302):
168 new_url_rel
= response
.getheader('Location') or response
.getheader('URI')
169 new_url
= urlparse
.urljoin(url
, new_url_rel
)
171 raise SafeException("HTTP error: got status code %s" % response
.status
)
176 info("Resource moved! Checking new URL %s" % new_url
)
178 return get_http_size(new_url
, ttl
- 1)
180 raise SafeException('Too many redirections.')
182 def get_ftp_size(url
):
183 address
= urlparse
.urlparse(url
)
184 ftp
= ftplib
.FTP(host(address
))
187 return ftp
.size(url
.split('/', 3)[3])
192 scheme
= urlparse
.urlparse(url
)[0].lower()
193 if scheme
.startswith('http'):
194 return get_http_size(url
)
195 elif scheme
.startswith('ftp'):
196 return get_ftp_size(url
)
198 raise SafeException("Unknown scheme '%s' in '%s'" % (scheme
, url
))
200 def unpack_tarball(archive_file
):
201 tar
= tarfile
.open(archive_file
, 'r:bz2')
202 members
= [m
for m
in tar
.getmembers() if m
.name
!= 'pax_global_header']
203 #tar.extractall('.', members = members) # Python >= 2.5 only
204 for tarinfo
in members
:
205 tarinfo
= copy
.copy(tarinfo
)
208 tar
.extract(tarinfo
, '.')
211 with
open(path
, 'rb') as stream
:
212 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'
229 def make_readonly_recursive(path
):
230 for root
, dirs
, files
in os
.walk(path
):
231 for d
in dirs
+ files
:
232 full
= os
.path
.join(root
, d
)
233 mode
= os
.stat(full
).st_mode
234 os
.chmod(full
, mode
& 0o555)
236 def get_archive_url(options
, release_version
, archive
):
237 if not options
.archive_dir_public_url
:
238 return archive
# Not needed with 0repo
240 archive_dir_public_url
= options
.archive_dir_public_url
.replace('$RELEASE_VERSION', release_version
)
241 if not archive_dir_public_url
.endswith('/'):
242 archive_dir_public_url
+= '/'
243 return archive_dir_public_url
+ archive
245 def make_archives_relative(feed
):
246 with
open(feed
, 'rb') as stream
:
247 doc
= minidom
.parse(stream
)
248 for elem
in doc
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'archive') + doc
.getElementsByTagNameNS(namespaces
.XMLNS_IFACE
, 'file'):
249 href
= elem
.getAttribute('href')
250 assert href
, 'Missing href on %r' % elem
252 elem
.setAttribute('href', href
.rsplit('/', 1)[1])
253 with
open(feed
, 'wb') as stream
: