Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / webtt / plugins / debug_kit / build.py
blobba75dcf629af2cb2aaab6bfb2e90664a9ad73535
1 #! /usr/bin/env python
3 import sys, os
4 import tarfile, zipfile, gzip, bz2
5 from optparse import OptionParser
7 """
8 Builds packaged releases of DebugKit so I don't have to do things manually.
10 Excludes itself (build.py), .gitignore, .DS_Store and the .git folder from the archives.
11 """
12 def main():
13 parser = OptionParser();
14 parser.add_option('-o', '--output-dir', dest="output_dir",
15 help="write the packages to DIR", metavar="DIR")
16 parser.add_option('-p', '--prefix-name', dest="prefix",
17 help="prefix used for the generated files")
18 parser.add_option('-k', '--skip', dest="skip", default="",
19 help="A comma separated list of files to skip")
20 parser.add_option('-s', '--source-dir', dest="source", default=".",
21 help="The source directory for the build process")
23 (options, args) = parser.parse_args()
25 if options.output_dir == '' or options.output_dir == options.source:
26 print 'Requires an output dir, and that output dir cannot be the same as the source one!'
27 exit()
29 # append .git and build.py to the skip files
30 skip = options.skip.split(',')
31 skip.extend(['.git', '.gitignore', '.DS_Store', 'build.py'])
33 # get list of files in top level dir.
34 files = os.listdir(options.source)
36 os.chdir(options.source)
38 # filter the files, I couldn't figure out how to do it in a more concise way.
39 for f in files[:]:
40 try:
41 skip.index(f)
42 files.remove(f)
43 except ValueError:
44 pass
46 # make a boring tar file
47 destfile = ''.join([options.output_dir, options.prefix])
48 tar_file_name = destfile + '.tar'
49 tar = tarfile.open(tar_file_name, 'w');
50 for f in files:
51 tar.add(f)
52 tar.close()
53 print "Generated tar file"
55 # make the gzip
56 if make_gzip(tar_file_name, destfile):
57 print "Generated gzip file"
58 else:
59 print "Could not generate gzip file"
61 # make the bz2
62 if make_bz2(tar_file_name, destfile):
63 print "Generated bz2 file"
64 else:
65 print "Could not generate bz2 file"
67 # make the zip file
68 zip_recursive(destfile + '.zip', options.source, files)
69 print "Generated zip file\n"
71 def make_gzip(tar_file, destination):
72 """
73 Takes a tar_file and destination. Compressess the tar file and creates
74 a .tar.gzip
75 """
76 tar_contents = open(tar_file, 'rb')
77 gzipfile = gzip.open(destination + '.tar.gz', 'wb')
78 gzipfile.writelines(tar_contents)
79 gzipfile.close()
80 tar_contents.close()
81 return True
83 def make_bz2(tar_file, destination):
84 """
85 Takes a tar_file and destination. Compressess the tar file and creates
86 a .tar.bz2
87 """
88 tar_contents = open(tar_file, 'rb')
89 bz2file = bz2.BZ2File(destination + '.tar.bz2', 'wb')
90 bz2file.writelines(tar_contents)
91 bz2file.close()
92 tar_contents.close()
93 return True
95 def zip_recursive(destination, source_dir, rootfiles):
96 """
97 Recursively zips source_dir into destination.
98 rootfiles should contain a list of files in the top level directory that
99 are to be included. Any top level files not in rootfiles will be omitted
100 from the zip file.
102 zipped = zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED)
104 for root, dirs, files in os.walk(source_dir):
105 inRoot = False
106 if root == source_dir:
107 inRoot = True
109 if inRoot:
110 for d in dirs:
111 try:
112 rootfiles.index(d)
113 except ValueError:
114 dirs.remove(d)
116 for f in files[:]:
117 if inRoot:
118 try:
119 rootfiles.index(f)
120 except ValueError:
121 continue
123 fullpath = os.path.join(root, f)
124 zipped.write(fullpath)
125 zipped.close()
126 return destination
129 if __name__ == '__main__':
130 main()