-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzipper.py
146 lines (115 loc) · 3.06 KB
/
zipper.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Alan Baines
import os.path
import os
import threading
import hashlib
import shutil
import time
import glob
import re
import zipfile
import datetime
import tempfile
import sys
import traceback
import json
def get_mod_name():
with open('info.json') as info_json:
data = json.load(info_json)
name = data['name']
version = data['version']
n_v = name+'_'+version
return n_v
rootx = os.path.dirname(os.path.abspath(__file__))
print( 'rootx', rootx )
baseFolder = rootx[:rootx.rindex(os.sep)+1]
print( 'baseFolder', baseFolder )
rootName = get_mod_name()
print( 'rootName', rootName )
zipPath = os.path.join(baseFolder,rootName+".zip")
print( 'zipPath', zipPath )
if os.path.exists(zipPath):
os.remove(zipPath)
print("Zip Removed:",zipPath)
whitelistextensions=[
".cfg",
".lua",
]
whitelist=[
os.sep+"README.md",
os.sep+"changelog.txt",
os.sep+"info.json",
os.sep+"license.md",
os.sep+"thumbnail.png",
os.sep+"description.json",
]
whitelistextensionsinsidefolders=[
".png",
".ogg",
]
def getAllFiles(directory):
returns = []
for path, subdirs, files in os.walk(directory):
for name in files:
f = os.path.join(path, name)
returns.append(f)
return returns
def endsWithAny(text,collection):
for c in collection:
if text.endswith(c):
return c
return False
git_directory_flag = os.sep+'.git'+os.sep
def collectWhiteListFiles(root,whitelist,whitelistextensions,whitelistextensionsinsidefolders):
returns = []
ignored = []
for file in getAllFiles(root):
shortname = file[len(root):]
c = shortname.count(os.sep)
if endsWithAny(file,whitelist):
returns.append(shortname)
elif endsWithAny(file,whitelistextensions):
returns.append(shortname)
elif c >= 2 and endsWithAny(file,whitelistextensionsinsidefolders):
returns.append(shortname)
elif git_directory_flag in file:
pass
else:
ignored.append(shortname)
return returns, ignored
def setExtensions(listFiles):
s = set()
for f in listFiles:
e = f[f.rindex('.')+1:]
s.add(e)
return s
def printWhiteListFiles(root):
print("")
print('printWhiteListFiles','root',root)
r,i = collectWhiteListFiles(root,whitelist,whitelistextensions,whitelistextensionsinsidefolders)
if len(i)>0:
print ('{:-^80}'.format(' ignored '))
for f in i:
print(f)
print(setExtensions(i))
print ('{:=^80}'.format(' white '))
for f in r:
print(f)
print(setExtensions(r))
print("")
printWhiteListFiles(rootx)
print ('{:+^80}'.format(' zip '))
r,i = collectWhiteListFiles(rootx,whitelist,whitelistextensions,whitelistextensionsinsidefolders)
with zipfile.ZipFile(zipPath, 'w') as zout:
for f in r:
arcname=rootName+f
filename="."+f
print(filename,arcname)
zout.write(filename,arcname)
# check os.name to determine interactive mode
if os.name == 'nt':
input("Press Enter to continue...")
elif os.name == 'posix':
print( os.listdir(os.pardir) )
else:
raise Exception("unknown os.name",os.name)