~skss/live-usb-install/live-usb-install

« back to all changes in this revision

Viewing changes to tools/pylauncher/pack.py

  • Committer: Krasimir Stefanov
  • Date: 2010-12-08 13:56:49 UTC
  • Revision ID: lokiisyourmaster@gmail.com-20101208135649-qvtp2n4zw07oqu7h
2.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright (c) 2007, 2008 Agostino Russo
 
4
#
 
5
# Written by Agostino Russo <agostino.russo@gmail.com>
 
6
#
 
7
# pack.py is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as
 
9
# published by the Free Software Foundation; either version 2 of
 
10
# the License, or (at your option) any later version.
 
11
#
 
12
# pack.py is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
 
 
22
import sys
 
23
import os
 
24
import shutil
 
25
from os.path import abspath, join, basename, dirname, exists
 
26
 
 
27
SIGNATURE="@@@pylauncher@@@"
 
28
 
 
29
def ajoin(*args):
 
30
    return abspath(join(*args))
 
31
 
 
32
def compress(target_dir):
 
33
    #TBD the 7z compressor should be properly compiled
 
34
    cwd = os.getcwd()
 
35
    compressor = ajoin("C:", "Program Files", "7-Zip","7z.exe")
 
36
    cmd = '%s a -t7z -m0=lzma -mx=9 -mfb=256 -md=32m -ms=on ../archive.7z *'
 
37
    cmd = cmd % (compressor,)
 
38
    print cmd
 
39
    os.chdir(target_dir)
 
40
    os.system(cmd)
 
41
    os.chdir(cwd)
 
42
 
 
43
def cat(outfile, *infiles):
 
44
    fout = open(outfile, 'wb')
 
45
    for fname in infiles:
 
46
        fin = open(abspath(fname), 'rb')
 
47
        data = fin.read()
 
48
        fin.close()
 
49
        fout.write(data)
 
50
    fout.close()
 
51
 
 
52
def make_self_extracting_exe(target_dir):
 
53
    header = ajoin(dirname(__file__), 'header.exe')
 
54
    archive = ajoin(dirname(target_dir),'archive.7z')
 
55
    target = ajoin(dirname(target_dir), 'application.exe')
 
56
    signature = ajoin(dirname(target_dir), 'signature')
 
57
    f = open(signature, 'wb')
 
58
    f.write(SIGNATURE)
 
59
    f.close()
 
60
    print "Creating self extracting file %s" % target
 
61
    cat(target, header, signature, archive)
 
62
 
 
63
def add_python_interpreter(target_dir):
 
64
    #TBD detect the dll/lib of the current python instance
 
65
    for f in ('pylauncher.exe', 'python26.dll', 'pyrun.exe'):
 
66
        source = ajoin(dirname(__file__), f)
 
67
        shutil.copy(source, target_dir)
 
68
 
 
69
def main():
 
70
    target_dir = sys.argv[1]
 
71
    add_python_interpreter(target_dir)
 
72
    compress(target_dir)
 
73
    make_self_extracting_exe(target_dir)
 
74
 
 
75
if __name__ == "__main__":
 
76
    main()