CMake package_archive target to create a .zip/.tar.bz2 package on mac/unix,
[blender.git] / build_files / package_spec / build_archive.py
1 #!/usr/bin/python
2
3 import os
4 import shutil
5 import subprocess
6 import sys
7
8 # todo:
9 # strip executables
10
11 # get parameters
12 if len(sys.argv) < 5:
13         sys.stderr.write('Excepted arguments: ./build_archive.py name extension install_dir output_dir')
14         sys.exit(1)
15
16 package_name = sys.argv[1]
17 extension = sys.argv[2]
18 install_dir = sys.argv[3]
19 output_dir = sys.argv[4]
20
21 package_archive = os.path.join(output_dir, package_name + '.' + extension)
22 package_dir = package_name
23
24 # remove existing package with the same name
25 try:
26         if os.path.exists(package_archive):
27                 os.remove(package_archive)
28         if os.path.exists(package_dir):
29                 shutil.rmtree(package_dir)
30 except:
31         sys.stderr.write('Failed to clean up old package files: ' + sys.exc_info()[0] + '\n')
32         sys.exit(1)
33
34 # create temporary package dir
35 try:
36         shutil.copytree(install_dir, package_dir)
37
38         for f in os.listdir(package_dir):
39                 if f.startswith('makes'):
40                         os.remove(os.path.join(package_dir, f))
41 except:
42         sys.stderr.write('Failed to copy install directory: ' + sys.exc_info()[0] + '\n')
43         sys.exit(1)
44
45 # create archive
46 try:
47         if not os.path.exists(output_dir):
48                 os.mkdir(output_dir)
49
50         if extension == 'zip':
51                 archive_cmd = ['zip', '-9', '-r', package_archive, package_dir]
52         elif extension == 'tar.bz2':
53                 archive_cmd = ['tar', 'cjf', package_archive, package_dir]
54         else:
55                 sys.stderr.write('Unknown archive extension: ' + extension)
56                 sys.exit(-1)
57
58         subprocess.call(archive_cmd)
59 except:
60         sys.stderr.write('Failed to create package archive: ' + sys.exc_info()[0] + '\n')
61         sys.exit(1)
62
63 # empty temporary package dir
64 try:
65         shutil.rmtree(package_dir)
66 except:
67         sys.stderr.write('Failed to clean up package directory: ' + sys.exc_info()[0] + '\n')
68         sys.exit(1)
69