Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: undo
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 261 lines (225 sloc) 7.352 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
# Copyright (C) 2007, Thomas Leonard
# See the README file for details, or visit http://0install.net.

import copy
import os, subprocess, shutil, tarfile
import urlparse, ftplib, httplib
from zeroinstall import SafeException
from zeroinstall.injector import model, qdom
from logging import info

release_status_file = os.path.abspath('release-status')

def check_call(*args, **kwargs):
exitstatus = subprocess.call(*args, **kwargs)
if exitstatus != 0:
if type(args[0]) in (str, unicode):
cmd = args[0]
else:
cmd = ' '.join(args[0])
raise SafeException("Command failed with exit code %d:\n%s" % (exitstatus, cmd))

def show_and_run(cmd, args):
print "Executing: %s %s" % (cmd, ' '.join("[%s]" % x for x in args))
check_call(['sh', '-c', cmd, '-'] + args)

def show_and_run_with_failure_prompt(prompt, *a, **k):
try:
show_and_run(*a, **k)
except SafeException:
print prompt
print "C) Continue"
print "Q) Quit"
if get_choice(['Continue','Quit']) == 'Quit': raise

def suggest_release_version(snapshot_version):
"""Given a snapshot version, suggest a suitable release version.
>>> suggest_release_version('1.0-pre')
'1.0'
>>> suggest_release_version('0.9-post')
'0.10'
>>> suggest_release_version('3')
Traceback (most recent call last):
...
SafeException: Version '3' is not a snapshot version (should end in -pre or -post)
"""
version = model.parse_version(snapshot_version)
mod = version[-1]
if mod == 0:
raise SafeException("Version '%s' is not a snapshot version (should end in -pre or -post)" % snapshot_version)
if mod > 0:
# -post, so increment the number
version[-2][-1] += 1
version[-1] = 0 # Remove the modifier
return model.format_version(version)

def publish(iface, **kwargs):
args = [os.environ['0PUBLISH']]
for k in kwargs:
value = kwargs[k]
if value is True:
args += ['--' + k.replace('_', '-')]
elif value is not None:
args += ['--' + k.replace('_', '-'), value]
args.append(iface)
info("Executing %s", args)
check_call(args)

def get_singleton_impl(iface):
impls = iface.implementations
if len(impls) != 1:
raise SafeException("Local feed '%s' contains %d versions! I need exactly one!" % (iface.uri, len(impls)))
return impls.values()[0]

def backup_name(name): return name + '~'

def backup_if_exists(name):
if not os.path.exists(name):
return False
backup = backup_name(name)
if os.path.exists(backup):
print "(deleting old backup %s)" % backup
remove_recursively(backup)
os.rename(name, backup)
print "(renamed old %s as %s; will delete on next run)" % (name, backup)
return True

def remove_recursively(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.unlink(path)

def revert_backup(name):
backup = backup_name(name)
if os.path.exists(backup):
if os.path.exists(name):
remove_recursively(name)
os.rename(backup, name)
print "(reverted backup %s to %s)" % (backup, name)
return True
else:
print "ERROR: no backup exists for file %s" % (name,)
return False

def get_choice(options):
while True:
choice = raw_input('/'.join(options) + ': ').lower()
if not choice: continue
for o in options:
if o.lower().startswith(choice):
return o

def make_archive_name(feed_name, version):
return feed_name.lower().replace(' ', '-') + '-' + version

def in_PATH(prog):
for x in os.environ['PATH'].split(':'):
if os.path.isfile(os.path.join(x, prog)):
return True
return False

def show_diff(from_dir, to_dir):
for cmd in [['meld'], ['xxdiff'], ['diff', '-ur']]:
if in_PATH(cmd[0]):
code = os.spawnvp(os.P_WAIT, cmd[0], cmd + [from_dir, to_dir])
if code:
print "WARNING: command %s failed with exit code %d" % (cmd, code)
return

class Status(object):
__slots__ = ['old_snapshot_version', 'release_version', 'head_before_release', 'new_snapshot_version',
'head_at_release', 'created_archive', 'src_tests_passed', 'tagged', 'verified_uploads', 'updated_master_feed']
def __init__(self):
for name in self.__slots__:
setattr(self, name, None)

if os.path.isfile(release_status_file):
for line in file(release_status_file):
assert line.endswith('\n')
line = line[:-1]
name, value = line.split('=')
setattr(self, name, value)
info("Loaded status %s=%s", name, value)

def save(self):
tmp_name = release_status_file + '.new'
tmp = file(tmp_name, 'w')
try:
lines = ["%s=%s\n" % (name, getattr(self, name) or '') for name in self.__slots__ if hasattr(self, name)]
tmp.write(''.join(lines))
tmp.close()
os.rename(tmp_name, release_status_file)
info("Wrote status to %s", release_status_file)
except:
os.unlink(tmp_name)
raise

def to_dict(self):
return dict([(k,getattr(self, k)) for k in self.__slots__])

def __repr__(self):
return repr(self.to_dict())

def host(address):
if hasattr(address, 'hostname'):
return address.hostname
else:
return address[1].split(':', 1)[0]

def port(address):
if hasattr(address, 'port'):
return address.port
else:
port = address[1].split(':', 1)[1:]
if port:
return int(port[0])
else:
return None

def get_http_size(url, ttl = 1):
assert url.lower().startswith('http://')

address = urlparse.urlparse(url)
http = httplib.HTTPConnection(host(address), port(address) or 80)

parts = url.split('/', 3)
if len(parts) == 4:
path = parts[3]
else:
path = ''

http.request('HEAD', '/' + path, headers = {'Host': host(address)})
response = http.getresponse()
try:
if response.status == 200:
return response.getheader('Content-Length')
elif response.status in (301, 302):
new_url_rel = response.getheader('Location') or response.getheader('URI')
new_url = urlparse.urljoin(url, new_url_rel)
else:
raise SafeException("HTTP error: got status code %s" % response.status)
finally:
response.close()

if ttl:
info("Resource moved! Checking new URL %s" % new_url)
assert new_url
return get_http_size(new_url, ttl - 1)
else:
raise SafeException('Too many redirections.')

def get_ftp_size(url):
address = urlparse.urlparse(url)
ftp = ftplib.FTP(host(address))
try:
ftp.login()
return ftp.size(url.split('/', 3)[3])
finally:
ftp.close()

def get_size(url):
scheme = urlparse.urlparse(url)[0].lower()
if scheme.startswith('http'):
return get_http_size(url)
elif scheme.startswith('ftp'):
return get_ftp_size(url)
else:
raise SafeException("Unknown scheme '%s' in '%s'" % (scheme, url))

def unpack_tarball(archive_file):
tar = tarfile.open(archive_file, 'r:bz2')
members = [m for m in tar.getmembers() if m.name != 'pax_global_header']
#tar.extractall('.', members = members) # Python >= 2.5 only
for tarinfo in members:
tarinfo = copy.copy(tarinfo)
tarinfo.mode |= 0600
tarinfo.mode &= 0755
tar.extract(tarinfo, '.')

def load_feed(path):
stream = open(path)
try:
return model.ZeroInstallFeed(qdom.parse(stream), local_path = path)
finally:
stream.close()

def get_archive_basename(impl):
# "2" means "path" (for Python 2.4)
return os.path.basename(urlparse.urlparse(impl.download_sources[0].url)[2])

def relative_path(ancestor, dst):
stem = os.path.abspath(os.path.dirname(ancestor))
dst = os.path.abspath(dst)
if stem != '/':
stem += '/'
assert dst.startswith(stem)
return dst[len(stem):]

assert relative_path('/foo', '/foo') == 'foo'
assert relative_path('/foo', '/foo/bar') == 'foo/bar'
Something went wrong with that request. Please try again.