4 import tarfile
, zipfile
, gzip
, bz2
5 from optparse
import OptionParser
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.
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!'
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.
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');
53 print "Generated tar file"
56 if make_gzip(tar_file_name
, destfile
):
57 print "Generated gzip file"
59 print "Could not generate gzip file"
62 if make_bz2(tar_file_name
, destfile
):
63 print "Generated bz2 file"
65 print "Could not generate bz2 file"
68 zip_recursive(destfile
+ '.zip', options
.source
, files
)
69 print "Generated zip file\n"
71 def make_gzip(tar_file
, destination
):
73 Takes a tar_file and destination. Compressess the tar file and creates
76 tar_contents
= open(tar_file
, 'rb')
77 gzipfile
= gzip
.open(destination
+ '.tar.gz', 'wb')
78 gzipfile
.writelines(tar_contents
)
83 def make_bz2(tar_file
, destination
):
85 Takes a tar_file and destination. Compressess the tar file and creates
88 tar_contents
= open(tar_file
, 'rb')
89 bz2file
= bz2
.BZ2File(destination
+ '.tar.bz2', 'wb')
90 bz2file
.writelines(tar_contents
)
95 def zip_recursive(destination
, source_dir
, rootfiles
):
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
102 zipped
= zipfile
.ZipFile(destination
, 'w', zipfile
.ZIP_DEFLATED
)
104 for root
, dirs
, files
in os
.walk(source_dir
):
106 if root
== source_dir
:
123 fullpath
= os
.path
.join(root
, f
)
124 zipped
.write(fullpath
)
129 if __name__
== '__main__':