Allow "0compile publish" with no DOWNLOAD-BASE-URL
[0compile.git] / publish.py
blob94f9aa1285cbf0438463189554636bf71e581ed1
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) == 1:
25 buildenv.config.set('compile', 'download-base-url', args2[0])
26 buildenv.save()
27 elif len(args2) > 1:
28 parser.print_help()
29 sys.exit(1)
31 download_base_url = buildenv.download_base_url or None
33 if download_base_url:
34 info("Using download base URL: %s", download_base_url)
36 if not os.path.isdir(buildenv.distdir):
37 raise SafeException("Directory '%s' does not exist. Try 'compile build'." % buildenv.distdir)
39 distdir = os.path.basename(buildenv.distdir)
40 archive_name = buildenv.archive_stem + '.tar.bz2'
42 # Make all directories in the archive user writable
43 for main, dirs, files in os.walk(distdir):
44 os.chmod(main, os.stat(main).st_mode | 0200)
46 import tarfile
47 archive = tarfile.open(archive_name, mode = 'w:bz2')
48 archive.add(distdir, buildenv.archive_stem)
49 archive.close()
51 target_feed = options.target_feed or buildenv.local_download_iface
53 if download_base_url:
54 download_url = os.path.join(download_base_url, archive_name)
55 else:
56 download_url = archive_name
57 shutil.copyfile(buildenv.local_iface_file, target_feed)
59 # XXX: we're assuming that 0publish requires the same version of Python as
60 # 0compile. This is currently needed for Arch Linux, but long-term we need to
61 # use the <runner>.
62 spawn_and_check(sys.executable, [
63 pubish_command,
64 target_feed,
65 '--archive-url', download_url,
66 '--archive-extract', buildenv.archive_stem])
68 if options.target_feed is None:
69 # If --target-feed is used this is probably a script, so don't print
70 # out hints.
71 print "Now upload '%s' as:\n%s\n" % (archive_name, download_url)
73 print "Once uploaded, you can download and run with:"
74 print "$ 0launch %s" % target_feed
76 __main__.commands.append(do_publish)