| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | """ |
|---|
| 3 | POSIX Installation |
|---|
| 4 | ~~~~~~~~~~~~~~~~~~ |
|---|
| 5 | |
|---|
| 6 | This script is invoked by the makefile to install Zine on a POSIX system. |
|---|
| 7 | |
|---|
| 8 | :copyright: (c) 2010 by the Zine Team, see AUTHORS for more details. |
|---|
| 9 | :license: BSD, see LICENSE for more details. |
|---|
| 10 | """ |
|---|
| 11 | import sys |
|---|
| 12 | import os |
|---|
| 13 | import shutil |
|---|
| 14 | from subprocess import call as run |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | join = os.path.join |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | PACKAGES = '_dynamic _ext importers upgrades utils views websetup docs'.split() |
|---|
| 21 | SCRIPTS = 'create-apache-config server shell'.split() |
|---|
| 22 | DESTDIR = os.environ.get('DESTDIR') |
|---|
| 23 | |
|---|
| 24 | if DESTDIR is not None: |
|---|
| 25 | DESTDIR = os.path.abspath(DESTDIR) |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | def silent(f, *args): |
|---|
| 29 | try: |
|---|
| 30 | return f(*args) |
|---|
| 31 | except: |
|---|
| 32 | pass |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | def rel_path(path, start): |
|---|
| 36 | start_list = os.path.abspath(start).split(os.path.sep) |
|---|
| 37 | path_list = os.path.abspath(path).split(os.path.sep) |
|---|
| 38 | offset = len(os.path.commonprefix([start_list, path_list])) |
|---|
| 39 | parts = [os.path.pardir] * (len(start_list) - offset) + path_list[offset:] |
|---|
| 40 | if not parts: |
|---|
| 41 | return os.path.curdir |
|---|
| 42 | return join(*parts) |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | def rel_symlink(src, dst): |
|---|
| 46 | os.symlink(rel_path(src, os.path.dirname(dst)), dst) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def copy_folder(src, dst, recurse=True, skip=(), delete_if_exists=False): |
|---|
| 50 | if delete_if_exists and os.path.exists(dst): |
|---|
| 51 | shutil.rmtree(dst) |
|---|
| 52 | silent(os.makedirs, dst) |
|---|
| 53 | for localname in os.listdir(src): |
|---|
| 54 | if localname in skip: |
|---|
| 55 | continue |
|---|
| 56 | filename = join(src, localname) |
|---|
| 57 | if os.path.isfile(filename): |
|---|
| 58 | shutil.copy2(filename, dst) |
|---|
| 59 | elif recurse: |
|---|
| 60 | dst_folder = join(dst, localname) |
|---|
| 61 | shutil.copytree(filename, dst_folder) |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | def copy_servers(source, destination, lib_dir, python): |
|---|
| 65 | silent(os.makedirs, destination) |
|---|
| 66 | for filename in os.listdir(source): |
|---|
| 67 | f = file(join(source, filename)) |
|---|
| 68 | try: |
|---|
| 69 | lines = list(f) |
|---|
| 70 | if lines[0].startswith('#!'): |
|---|
| 71 | lines[0] = '#!%s\n' % python |
|---|
| 72 | for idx, line in enumerate(lines): |
|---|
| 73 | if line.startswith('ZINE_LIB ='): |
|---|
| 74 | lines[idx] = 'ZINE_LIB = %r\n' % strip_destdir(lib_dir) |
|---|
| 75 | break |
|---|
| 76 | finally: |
|---|
| 77 | f.close() |
|---|
| 78 | f = file(join(destination, filename), 'w') |
|---|
| 79 | try: |
|---|
| 80 | f.write(''.join(lines)) |
|---|
| 81 | finally: |
|---|
| 82 | f.close() |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | def copy_core_translations(src_dir, lib_dir, share_dir): |
|---|
| 86 | copy_folder(src_dir, lib_dir, recurse=False, delete_if_exists=True) |
|---|
| 87 | if os.path.exists(share_dir): |
|---|
| 88 | shutil.rmtree(share_dir) |
|---|
| 89 | os.makedirs(share_dir) |
|---|
| 90 | for language in os.listdir(src_dir): |
|---|
| 91 | lang_src = join(src_dir, language, 'messages.mo') |
|---|
| 92 | if not os.path.isfile(lang_src): |
|---|
| 93 | continue |
|---|
| 94 | lang_share_dir = join(share_dir, language, 'LC_MESSAGES') |
|---|
| 95 | silent(os.makedirs, lang_share_dir) |
|---|
| 96 | shutil.copy2(lang_src, join(lang_share_dir, 'zine.mo')) |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | def copy_plugins(src_dir, lib_dir, share_dir): |
|---|
| 100 | for path in lib_dir, join(share_dir, 'plugins'): |
|---|
| 101 | if os.path.exists(path): |
|---|
| 102 | shutil.rmtree(path) |
|---|
| 103 | for plugin in os.listdir(src_dir): |
|---|
| 104 | plugin_src_dir = join(src_dir, plugin) |
|---|
| 105 | plugin_lib_dir = join(lib_dir, plugin) |
|---|
| 106 | |
|---|
| 107 | # plugin code |
|---|
| 108 | copy_folder(plugin_src_dir, plugin_lib_dir, |
|---|
| 109 | skip=frozenset(('shared', 'templates'))) |
|---|
| 110 | |
|---|
| 111 | # plugin web data and templates |
|---|
| 112 | for folder, new_folder in ('shared', 'htdocs'), \ |
|---|
| 113 | ('templates', 'templates'): |
|---|
| 114 | src_folder = join(plugin_src_dir, folder) |
|---|
| 115 | if os.path.exists(src_folder): |
|---|
| 116 | dst_folder = join(share_dir, new_folder, plugin) |
|---|
| 117 | copy_folder(src_folder, dst_folder, delete_if_exists=True) |
|---|
| 118 | lib_folder = join(plugin_lib_dir, folder) |
|---|
| 119 | rel_symlink(dst_folder, lib_folder) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | def copy_scripts(source, destination, lib_dir): |
|---|
| 123 | silent(os.makedirs, destination) |
|---|
| 124 | f = file(join(source, '_init_zine.py')) |
|---|
| 125 | try: |
|---|
| 126 | contents = f.read().replace('ZINE_LIB = None', |
|---|
| 127 | 'ZINE_LIB = %r' % strip_destdir(lib_dir)) |
|---|
| 128 | finally: |
|---|
| 129 | f.close() |
|---|
| 130 | f = file(join(destination, '_init_zine.py'), 'w') |
|---|
| 131 | try: |
|---|
| 132 | f.write(contents) |
|---|
| 133 | finally: |
|---|
| 134 | f.close() |
|---|
| 135 | for script in SCRIPTS: |
|---|
| 136 | shutil.copy2(join(source, script), destination) |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | def strip_destdir(path): |
|---|
| 140 | if DESTDIR is None or not path.startswith(DESTDIR): |
|---|
| 141 | return path |
|---|
| 142 | return path[len(DESTDIR):] |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | def main(prefix): |
|---|
| 146 | if DESTDIR is not None: |
|---|
| 147 | dest_dir = join(DESTDIR, prefix.lstrip('/')) |
|---|
| 148 | else: |
|---|
| 149 | dest_dir = prefix |
|---|
| 150 | |
|---|
| 151 | python = sys.executable |
|---|
| 152 | source = os.path.abspath('.') |
|---|
| 153 | zine_source = join(source, 'zine') |
|---|
| 154 | lib_dir = join(dest_dir, 'lib', 'zine') |
|---|
| 155 | share_dir = join(dest_dir, 'share', 'zine') |
|---|
| 156 | |
|---|
| 157 | print 'Installing to ' + dest_dir |
|---|
| 158 | print 'Using ' + python |
|---|
| 159 | |
|---|
| 160 | # create some folders for us |
|---|
| 161 | silent(os.makedirs, join(lib_dir, 'zine')) |
|---|
| 162 | silent(os.makedirs, share_dir) |
|---|
| 163 | |
|---|
| 164 | # copy the packages and modules into the zine package |
|---|
| 165 | copy_folder(zine_source, join(lib_dir, 'zine'), |
|---|
| 166 | recurse=False, delete_if_exists=True) |
|---|
| 167 | for package in PACKAGES: |
|---|
| 168 | copy_folder(join(zine_source, package), |
|---|
| 169 | join(lib_dir, 'zine', package)) |
|---|
| 170 | |
|---|
| 171 | # old zine installations had the translations at a different |
|---|
| 172 | # location. Delete them if we find them there. |
|---|
| 173 | old_translations = join(share_dir, 'i18n') |
|---|
| 174 | if os.path.isdir(old_translations): |
|---|
| 175 | shutil.rmtree(old_translations) |
|---|
| 176 | |
|---|
| 177 | # copy the core translations |
|---|
| 178 | copy_core_translations(join(zine_source, 'i18n'), |
|---|
| 179 | join(lib_dir, 'zine', 'i18n'), |
|---|
| 180 | join(dest_dir, 'share', 'locale')) |
|---|
| 181 | |
|---|
| 182 | # copy the plugins over |
|---|
| 183 | copy_plugins(join(zine_source, 'plugins'), |
|---|
| 184 | join(lib_dir, 'plugins'), |
|---|
| 185 | share_dir) |
|---|
| 186 | |
|---|
| 187 | # compile all files |
|---|
| 188 | run([sys.executable, '-O', '-mcompileall', '-qf', |
|---|
| 189 | join(lib_dir, 'zine'), join(lib_dir, 'plugins')]) |
|---|
| 190 | |
|---|
| 191 | # templates and shared data |
|---|
| 192 | copy_folder(join(zine_source, 'shared'), |
|---|
| 193 | join(share_dir, 'htdocs', 'core'), delete_if_exists=True) |
|---|
| 194 | copy_folder(join(zine_source, 'templates'), |
|---|
| 195 | join(share_dir, 'templates', 'core'), delete_if_exists=True) |
|---|
| 196 | |
|---|
| 197 | # copy the server files |
|---|
| 198 | copy_servers(join(source, 'servers'), |
|---|
| 199 | join(share_dir, 'servers'), lib_dir, python) |
|---|
| 200 | |
|---|
| 201 | # copy the scripts |
|---|
| 202 | copy_scripts(join(source, 'scripts'), |
|---|
| 203 | join(share_dir, 'scripts'), lib_dir) |
|---|
| 204 | print 'All done.' |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | if __name__ == '__main__': |
|---|
| 208 | if len(sys.argv) != 2: |
|---|
| 209 | print >> sys.stderr, 'error: install script only accepts a prefix' |
|---|
| 210 | sys.exit(1) |
|---|
| 211 | os.chdir(os.path.join(os.path.dirname(__file__), '..')) |
|---|
| 212 | main(sys.argv[1]) |
|---|