1 # Copyright (C) 2007, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
4 import os
, subprocess
, shutil
, tarfile
5 import urlparse
, ftplib
, httplib
6 from zeroinstall
import SafeException
7 from zeroinstall
.injector
import model
, qdom
8 from logging
import info
10 release_status_file
= os
.path
.abspath('release-status')
12 def check_call(*args
, **kwargs
):
13 exitstatus
= subprocess
.call(*args
, **kwargs
)
15 if type(args
[0]) in (str, unicode):
18 cmd
= ' '.join(args
[0])
19 raise SafeException("Command failed with exit code %d:\n%s" % (exitstatus
, cmd
))
21 def show_and_run(cmd
, args
):
22 print "Executing: %s %s" % (cmd
, ' '.join("[%s]" % x
for x
in args
))
23 check_call(['sh', '-c', cmd
, '-'] + args
)
25 def suggest_release_version(snapshot_version
):
26 """Given a snapshot version, suggest a suitable release version.
27 >>> suggest_release_version('1.0-pre')
29 >>> suggest_release_version('0.9-post')
31 >>> suggest_release_version('3')
32 Traceback (most recent call last):
34 SafeException: Version '3' is not a snapshot version (should end in -pre or -post)
36 version
= model
.parse_version(snapshot_version
)
39 raise SafeException("Version '%s' is not a snapshot version (should end in -pre or -post)" % snapshot_version
)
41 # -post, so increment the number
43 version
[-1] = 0 # Remove the modifier
44 return model
.format_version(version
)
46 def publish(iface
, **kwargs
):
47 args
= [os
.environ
['0PUBLISH']]
51 args
+= ['--' + k
.replace('_', '-')]
52 elif value
is not None:
53 args
+= ['--' + k
.replace('_', '-'), value
]
55 info("Executing %s", args
)
58 def get_singleton_impl(iface
):
59 impls
= iface
.implementations
61 raise SafeException("Local feed '%s' contains %d versions! I need exactly one!" % (iface
.uri
, len(impls
)))
62 return impls
.values()[0]
64 def backup_if_exists(name
):
65 if not os
.path
.exists(name
):
68 if os
.path
.exists(backup
):
69 print "(deleting old backup %s)" % backup
70 if os
.path
.isdir(backup
):
74 os
.rename(name
, backup
)
75 print "(renamed old %s as %s; will delete on next run)" % (name
, backup
)
77 def get_choice(options
):
79 choice
= raw_input('/'.join(options
) + ': ').lower()
80 if not choice
: continue
82 if o
.lower().startswith(choice
):
85 def make_archive_name(feed_name
, version
):
86 return feed_name
.lower().replace(' ', '-') + '-' + version
89 for x
in os
.environ
['PATH'].split(':'):
90 if os
.path
.isfile(os
.path
.join(x
, prog
)):
94 def show_diff(from_dir
, to_dir
):
95 for cmd
in [['meld'], ['xxdiff'], ['diff', '-ur']]:
97 code
= os
.spawnvp(os
.P_WAIT
, cmd
[0], cmd
+ [from_dir
, to_dir
])
99 print "WARNING: command %s failed with exit code %d" % (cmd
, code
)
102 class Status(object):
103 __slots__
= ['old_snapshot_version', 'release_version', 'head_before_release', 'new_snapshot_version',
104 'head_at_release', 'created_archive', 'tagged', 'uploaded_archive', 'updated_master_feed']
106 for name
in self
.__slots
__:
107 setattr(self
, name
, None)
109 if os
.path
.isfile(release_status_file
):
110 for line
in file(release_status_file
):
111 assert line
.endswith('\n')
113 name
, value
= line
.split('=')
114 setattr(self
, name
, value
)
115 info("Loaded status %s=%s", name
, value
)
118 tmp_name
= release_status_file
+ '.new'
119 tmp
= file(tmp_name
, 'w')
121 lines
= ["%s=%s\n" % (name
, getattr(self
, name
)) for name
in self
.__slots
__ if getattr(self
, name
)]
122 tmp
.write(''.join(lines
))
124 os
.rename(tmp_name
, release_status_file
)
125 info("Wrote status to %s", release_status_file
)
131 if hasattr(address
, 'hostname'):
132 return address
.hostname
134 return address
[1].split(':', 1)[0]
137 if hasattr(address
, 'port'):
140 port
= address
[1].split(':', 1)[1:]
146 def get_http_size(url
, ttl
= 1):
147 assert url
.lower().startswith('http://')
149 address
= urlparse
.urlparse(url
)
150 http
= httplib
.HTTPConnection(host(address
), port(address
) or 80)
152 parts
= url
.split('/', 3)
158 http
.request('HEAD', '/' + path
, headers
= {'Host': host(address
)})
159 response
= http
.getresponse()
161 if response
.status
== 200:
162 return response
.getheader('Content-Length')
163 elif response
.status
in (301, 302):
164 new_url_rel
= response
.getheader('Location') or response
.getheader('URI')
165 new_url
= urlparse
.urljoin(url
, new_url_rel
)
167 raise SafeException("HTTP error: got status code %s" % response
.status
)
172 info("Resource moved! Checking new URL %s" % new_url
)
174 return get_http_size(new_url
, ttl
- 1)
176 raise SafeException('Too many redirections.')
178 def get_ftp_size(url
):
179 address
= urlparse
.urlparse(url
)
180 ftp
= ftplib
.FTP(host(address
))
183 return ftp
.size(url
.split('/', 3)[3])
188 scheme
= urlparse
.urlparse(url
)[0].lower()
189 if scheme
.startswith('http'):
190 return get_http_size(url
)
191 elif scheme
.startswith('ftp'):
192 return get_ftp_size(url
)
194 raise SafeException("Unknown scheme '%s' in '%s'" % (scheme
, url
))
196 def unpack_tarball(archive_file
):
197 tar
= tarfile
.open(archive_file
, 'r:bz2')
198 members
= [m
for m
in tar
.getmembers() if m
.name
!= 'pax_global_header']
199 tar
.extractall('.', members
= members
)
204 return model
.ZeroInstallFeed(qdom
.parse(stream
), local_path
= path
)
208 def get_archive_basename(impl
):
209 return os
.path
.basename(urlparse
.urlparse(impl
.download_sources
[0].url
).path
)
211 def relative_path(ancestor
, dst
):
212 stem
= os
.path
.abspath(os
.path
.dirname(ancestor
))
213 dst
= os
.path
.abspath(dst
)
216 assert dst
.startswith(stem
)
217 return dst
[len(stem
):]
219 assert relative_path('/foo', '/foo') == 'foo'
220 assert relative_path('/foo', '/foo/bar') == 'foo/bar'