-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdist.py
145 lines (118 loc) · 3.88 KB
/
dist.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
####################################################
#
# dist.py - Create PyGUI distribution tar file
#
####################################################
source_indent = 4
text_indent = 2
text_suffixes = {
".py": source_indent,
".pyx": source_indent,
".txt": text_indent
}
ignore_suffixes = [
"~", ".pyc", ".o", ".so", ".core", "]",
".DS_Store"
]
ignore_names = [
"build", "dist", "core", "Icon\r", ".DS_Store"
]
tar_name_prefix = "PyGUI"
dist_items = [
"Demos", "Doc", "GUI", "Tests",
"CHANGES.txt", "README.txt", "TODO.txt",
"setup.cfg", "setup.py", "distutils_extensions.py"
]
#testing_items = [
#]
#----------------------------------------------------
import os, re, sys, tarfile
from cStringIO import StringIO
from glob import glob
tabs = re.compile(r"^\t+", re.MULTILINE)
def endswith_any(path, suffixes):
for suffix in suffixes:
if path.endswith(suffix):
return 1
return 0
def expand_indentation(indent):
def f(match):
n = len(match.group(0))
return (n * indent) * " "
return f
def join_path(*args):
return os.path.normpath(os.path.join(*args))
def add_text_file(tar, arcname, path, indent):
f = open(path, "r")
info = tar.gettarinfo(path, arcname, f)
data = f.read()
f.close()
data = tabs.sub(expand_indentation(indent), data)
info.size = len(data)
h = StringIO(data)
tar.addfile(info, h)
#def add_text_file(zip, arcname, path, indent):
# f = open(path, "r")
# data = f.read()
# f.close()
# data = tabs.sub(expand_indentation(indent), data)
# zip.writestr(arcname, data)
def add_file(tar, arcname, path):
tar.add(path, arcname)
#def add_file(zip, arcname, path):
# zip.write(path, arcname)
def add_directory(tar, arc_dir, root_dir, dir, exclude = []):
dir_path = join_path(root_dir, dir)
for name in os.listdir(dir_path):
if name not in ignore_names \
and not endswith_any(name, ignore_suffixes):
item = join_path(dir, name)
add_item(tar, arc_dir, root_dir, item, exclude)
def add_item(tar, arc_dir, root_dir, item, exclude = []):
if item not in exclude:
arc_path = join_path(arc_dir, item)
item_path = join_path(root_dir, item)
arrow = "<-"
if os.path.isdir(item_path):
add_directory(tar, arc_dir, root_dir, item, exclude)
else:
suffix = os.path.splitext(item_path)[1]
if suffix in text_suffixes:
add_text_file(tar, arc_path, item_path, indent = text_suffixes[suffix])
arrow = "<="
else:
add_file(tar, arc_path, item_path)
print arc_path, arrow, repr(item_path)
def add_items(tar, arc_dir, root_dir, items, exclude = []):
for item in items:
add_item(tar, arc_dir, root_dir, item, exclude)
def open_archive(tar_dir, prefix, version):
tarpath = os.path.join(tar_dir, "%s-%s.tar.gz" % (prefix, version))
print "=====", tarpath, "====="
tar = tarfile.open(tarpath, "w:gz")
return tar
#def open_archive(tar_dir, prefix, version):
# zippath = os.path.join(tar_dir, "%s-%s.zip" % (prefix, version))
# print "=====", zippath, "====="
# zip = zipfile.ZipFile(zippath, "w", zipfile.ZIP_DEFLATED)
# return zip
def create_source_archive(tar_dir, arc_dir, root_dir, version):
tar = open_archive(tar_dir, tar_name_prefix, version)
#add_directory(tar, arc_dir, root_dir, os.curdir, exclude = testing_items)
add_items(tar, arc_dir, root_dir, dist_items)
tar.close()
#def create_source_archive(zip_dir, arc_dir, root_dir, version):
# zip = open_zipfile(zip_dir, tar_name_prefix, version)
# #add_directory(tar, arc_dir, root_dir, os.curdir, exclude = testing_items)
# add_items(zip, arc_dir, root_dir, dist_items)
# zip.close()
#def create_test_tarball(tar_dir, arc_dir, root_dir, version):
# tar = open_tarfile("%s-Tests" % tar_name_prefix, version)
# add_items(tar, arc_dir, root_dir, testing_items)
# tar.close()
def main(tar_dir, root_dir, version):
arc_dir = "PyGUI-%s" % version
create_source_archive(tar_dir, arc_dir, root_dir, version)
#create_test_tarball(tar_dir, arc_dir, root_dir, version)
if __name__ == "__main__":
main(*sys.argv[1:])