Release 0.32
[0compile.git] / publish.py
blobd59ecd26d05b9581a60d07b7da7ba0c2e4559832
1 # Copyright (C) 2006, Thomas Leonard
2 # See http://0install.net/0compile.html
4 import os, sys, __main__
5 from logging import info
6 from optparse import OptionParser
7 import shutil
9 from zeroinstall import SafeException
11 from support import BuildEnv, spawn_and_check
13 pubish_command = os.environ["0COMPILE_0PUBLISH"]
15 def do_publish(args):
16 """publish [ DOWNLOAD-BASE-URL ]"""
18 parser = OptionParser(usage="usage: %prog publish [options] [ DOWNLOAD-BASE-URL ]")
20 parser.add_option('', "--target-feed", help="name of output feed file to create", metavar='FILE')
21 (options, args2) = parser.parse_args(args)
23 buildenv = BuildEnv()
24 if len(args2) == 0:
25 if not buildenv.download_base_url:
26 raise SafeException("No download base set. Give the URL for a remote directory.")
27 elif len(args2) == 1:
28 buildenv.config.set('compile', 'download-base-url', args2[0])
29 buildenv.save()
31 info("Using download base URL: %s", buildenv.download_base_url)
33 if not os.path.isdir(buildenv.distdir):
34 raise SafeException("Directory '%s' does not exist. Try 'compile build'." % buildenv.distdir)
36 distdir = os.path.basename(buildenv.distdir)
37 archive_name = buildenv.archive_stem + '.tar.bz2'
39 # Make all directories in the archive user writable
40 for main, dirs, files in os.walk(distdir):
41 os.chmod(main, os.stat(main).st_mode | 0200)
43 import tarfile
44 archive = tarfile.open(archive_name, mode = 'w:bz2')
45 archive.add(distdir, buildenv.archive_stem)
46 archive.close()
48 target_feed = options.target_feed or buildenv.local_download_iface
50 download_url = os.path.join(buildenv.download_base_url, archive_name)
51 shutil.copyfile(buildenv.local_iface_file, target_feed)
53 # XXX: we're assuming that 0publish requires the same version of Python as
54 # 0compile. This is currently needed for Arch Linux, but long-term we need to
55 # use the <runner>.
56 spawn_and_check(sys.executable, [
57 pubish_command,
58 target_feed,
59 '--archive-url', download_url,
60 '--archive-extract', buildenv.archive_stem])
62 if options.target_feed is None:
63 # If --target-feed is used this is probably a script, so don't print
64 # out hints.
65 print "Now upload '%s' as:\n%s\n" % (archive_name, download_url)
67 print "Once uploaded, you can download and run with:"
68 print "$ 0launch %s" % target_feed
70 __main__.commands.append(do_publish)