-
Notifications
You must be signed in to change notification settings - Fork 0
/
genZips.py
71 lines (59 loc) · 1.98 KB
/
genZips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import os
from glob import glob
import zipfile
from zipfile import ZipFile
import zlib
import sys
import shutil
# Assumes that the stremas github samples repo has been cloned at static/samples in the current directory, or
# that path to the cloned repo is specified at run tim.
# Use the initialize.sh script to run this script instead of running it directly.
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
if "catalog.json" != file:
ziph.write(os.path.join(root, file))
def should_zip(folder):
#if a folder has a catalog.json entry then we should create af it.
return os.path.exists(folder+"/catalog.json")
def zip_single_dir(dir):
project_name = dir[:-1]
zip_name = project_name +".zip"
zipf = ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
#print "Creating " + zip_name + " in " + os.getcwd()
zipdir(dir, zipf)
zipf.close()
#move the zip file into the directory
if os.path.exists(dir+zip_name):
os.remove(dir+zip_name) #if already exists
shutil.move(zip_name, dir)
def zip_recursively(dir):
num_zipped = 0
if should_zip(dir):
#this is a single project
zip_single_dir(dir)
num_zipped = 1
else:
#check the sub directories for a project
os.chdir(dir)
for child_directory in glob('*/'):
num_zipped += zip_recursively(child_directory)
os.chdir("..")
return num_zipped
def zip_root_directory():
num_zipped = 0
sub_dirs = glob("*/")
for category in sub_dirs:
#every top level folder is a category
num_zipped += zip_recursively(category)
print "Done...Zipped " + str(num_zipped) + " Directories"
if __name__ == '__main__':
#Usage: genZips.py [samples directory]
if (len(sys.argv) == 1):
target_dir = os.getcwd()
else:
target_dir = sys.argv[1]
os.chdir(target_dir)
zip_root_directory()