forked from emesene/emesene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installergtk3.py
184 lines (153 loc) · 6.07 KB
/
installergtk3.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import os
import sys
import re
import zipfile
import shutil
TARGET_DIR = "C://emesene//bundle"
URL_BUNDLE = "http://optionexplicit.be/projects/gnome-windows/GTK+3/gtk+/git/"
#FIXME: move to git repo
PATCHED_FILE = "http://ubuntuone.com/0rnj0GhbbYN3pI0dtOQazm"
def main():
ensure_bundle()
def ensure_bundle(force=False):
print "searching for gtk bundle"
bundle_dir = os.path.join(get_current_dir(), TARGET_DIR)
if not has_cached_bundle(bundle_dir):
print "gtk bundle not found. Starting download"
f = urllib.urlopen(URL_BUNDLE)
s = f.read()
f.close()
url_re = re.compile("(gtk\+-bundle-.*-win32.zip)\"")
m = url_re.search(s)
full_url = URL_BUNDLE + m.group(1)
#FIXME: implement download
download_bundle(full_url)
else:
print "cached gtk bundle found"
name = get_bundle_name(bundle_dir)
print "extracting gtk bundle files"
sourceZip = zipfile.ZipFile(os.path.join(bundle_dir, name), 'r')
for f in sourceZip.namelist():
#avoid unneeded content
if (not f.endswith('/')
and not f.startswith('include')
and not f.startswith('man')
and not f.startswith('manifest')
and not f.startswith('src')
and not f.startswith('lib/gdbus-2.0')
and not f.startswith('lib/gettext')
and not f.startswith('lib/pkgconfig')
and not f.startswith('lib/glib-2.0/include')
and not f.startswith('lib/gobject-introspection')
and not f.startswith('share/doc')
and not f.startswith('share/info')
and not f.startswith('share/icons')
and not f.startswith('share/emacs')
and not f.startswith('share/gdb')
and not f.startswith('share/glib-2.0/gdb')
and not f.startswith('share/man')
and not f.startswith('share/bash-completion')
and not f.startswith('share/gobject-introspection-1.0')
and not f.startswith('share/gtk-3.0/demo')
and not f.startswith('share/gtk-doc')
and not f.startswith('share/glib-2.0/gettext')
and not f.startswith('share/gettext')
and not f.startswith('share/aclocal')
and not f.startswith('share/themes/Emacs')
and not f.startswith('Lib/pkgconfig')
and not f.endswith('.la')
and not f.endswith('.exe')
and not f.endswith('.def')):
#skip non .dll files
if f.startswith('bin/') and not f.endswith('.dll'):
continue
if f.startswith('Lib/'):
dest = os.path.join(bundle_dir, 'lib2')
else:
dest = bundle_dir
sourceZip.extract(f, dest)
sourceZip.close()
reorder_bundle_tree(bundle_dir)
def reorder_bundle_tree(bundle_dir):
#create new gtk3 folder
print "reordering tree. this will take some time"
gtk_dir = os.path.join(bundle_dir, "lib2/Lib/site-packages/gtk3")
try:
shutil.rmtree(gtk_dir)
except:
pass
os.mkdir(gtk_dir)
shutil.move(os.path.join(bundle_dir, "lib2/Lib/libpyglib-gi-2.0-python.dll.a"),
os.path.join(bundle_dir, "lib2/Lib/site-packages/libpyglib-gi-2.0-python.dll.a"))
shutil.move(os.path.join(bundle_dir, "etc"), os.path.join(gtk_dir, "etc"))
shutil.move(os.path.join(bundle_dir, "share"), os.path.join(gtk_dir, "share"))
shutil.move(os.path.join(bundle_dir, "lib"), os.path.join(gtk_dir, "lib"))
shutil.move(os.path.join(bundle_dir, "bin"), os.path.join(gtk_dir, "bin"))
print "patching tree"
# create pth file
inp = file(os.path.join(bundle_dir, "lib2/Lib/site-packages/pygi.pth"), 'w')
inp.write("import distutils.sysconfig, os; os.putenv('PATH', os.path.join(distutils.sysconfig.get_python_lib(),'gtk3/bin;') + os.getenv('PATH'))")
inp.close()
#download pached pygtkcompat file
f = urllib.urlopen(PATCHED_FILE)
dest_url = os.path.join(bundle_dir, "lib2/Lib/site-packages/gi/pygtkcompat.py")
dest = file(dest_url, 'w')
dest.write(f.read())
dest.close()
f.close()
dest_dir = get_python_dir()
print "bundle will be copied into %s" % dest_dir
#FIXME: activate this copy into python folder
# copy_replace(os.path.join(bundle_dir, "lib2//Lib/site-packages"), dest_dir)
print "you can now use gtk3 on your python instalation"
def silent_remove(path):
try:
shutil.rmtree(path)
except:
pass
#http://stackoverflow.com/questions/7419665/python-move-and-overwrite-files-and-folders
def copy_replace(root_src_dir, root_dst_dir):
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir, root_dst_dir)
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.move(src_file, dst_dir)
def download_bundle(url):
print "download bundle from %s" % url
raise RuntimeError
def has_cached_bundle(bundle_dir):
'''return true if bundle has to be downloaded'''
if not os.path.exists(bundle_dir):
return False
if get_bundle_name(bundle_dir) is not None:
return True
return False
def get_bundle_name(bundle_dir):
'''return the bundle name'''
for dirname, dirnames, files in os.walk(bundle_dir):
for f in files:
if f.startswith("gtk+-bundle-"):
return f
return None
def get_current_dir():
'''get current path'''
this_module_path = os.path.dirname(unicode(__file__,
sys.getfilesystemencoding()))
return os.path.abspath(this_module_path)
def get_python_dir():
# gtk sources will be in a path like this:
#"C:\\Python27\\Lib\\site-packages\\gtk3"
for p in sys.path:
if p.endswith("site-packages"):
return p
return None
if __name__ == "__main__":
main()