forked from jbber/wordpress-creative-commons-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_release.py
189 lines (152 loc) · 4.99 KB
/
make_release.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
185
186
187
188
# Copyright George Notaras
REL_FILES = [
'AUTHORS',
'cc-configurator.php',
'bccl-settings.php',
'bccl-admin-panel.php',
'bccl-template-tags.php',
'bccl-utils.php',
'bccl-licenses.php',
'bccl-generators.php',
'bccl-deprecated.php',
'index.php',
'LICENSE',
'NOTICE',
#'README',
'readme.txt',
#'screenshot-1.png',
'uninstall.php',
]
REL_DIRS = [
'languages',
'css',
'js',
'media',
]
PLUGIN_METADATA_FILE = 'cc-configurator.php'
POT_HEADER = """# POT (Portable Object Template)
#
# This file is part of the Creative-Commons-Configurator plugin for WordPress.
#
# http://www.g-loaded.eu/2006/01/14/creative-commons-configurator-wordpress-plugin/
#
# Copyright (C) 2006-2013 George Notaras <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
# ==============================================================================
import sys
import os
import glob
import zipfile
import shutil
import subprocess
import polib
def get_name_release():
def get_data(cur_line):
return cur_line.split(':')[1].strip()
f = open(PLUGIN_METADATA_FILE)
name = ''
release = ''
for line in f:
if line.lower().startswith('plugin name:'):
name = get_data(line)
elif line.lower().startswith('version:'):
release = get_data(line)
if name and release:
break
f.close()
if not name:
raise Exception('Cannot determine plugin name')
elif not release:
raise Exception('Cannot determine plugin version')
else:
# Replace spaces in name and convert it to lowercase
name = name.replace(' ', '-')
name = name.lower()
return name, release
name, release = get_name_release()
print 'Generating POT file...'
# Translation
pot_domain = os.path.splitext(PLUGIN_METADATA_FILE)[0]
# Generate POT file
args = ['xgettext', '--default-domain=%s' % pot_domain, '--output=%s.pot' % pot_domain, '--language=PHP', '--from-code=UTF-8', '--keyword=__', '--keyword=_e', '--no-wrap', '--package-name=%s' % pot_domain, '--package-version=%s' % release, '--copyright-holder', 'George Notaras <[email protected]>']
# Add php files as arguments
for rf in REL_FILES:
if rf.endswith('.php'):
args.append(rf)
print (' ').join(args)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
# Replace POT Header
f = open('%s.pot' % pot_domain, 'r')
pot_lines = f.readlines()
f.close()
f = open('%s.pot' % pot_domain, 'w')
f.write(POT_HEADER)
for n, line in enumerate(pot_lines):
if n < 4:
continue
f.write(line)
f.close()
print 'Complete'
# Compile language .po files to .mo
print 'Compiling PO files to MO...'
for po_file in os.listdir('languages'):
if not po_file.endswith('.po'):
continue
po_path = os.path.join('languages', po_file)
print 'Converting', po_path
po = polib.pofile(po_path, encoding='utf-8')
mo_path = po_path[:-3] + '.mo'
po.save_as_mofile(mo_path)
print 'Complete'
print
# Because of the stupidity of the WordPress plugin registration system
# the plugin directory ends with ``-1``
name_release_dir = name + '-1'
# Delete the output directory if it exists.
if os.path.exists(name_release_dir):
shutil.rmtree(name_release_dir)
# Create release dir and move release files inside it
os.mkdir(name_release_dir)
# Copy files
for p_file in REL_FILES:
shutil.copy(p_file, os.path.join(name_release_dir, p_file))
# Copy dirs
for p_dir in REL_DIRS:
if p_dir == 'media':
shutil.copytree(p_dir, os.path.join(name_release_dir, p_dir), ignore=shutil.ignore_patterns('*.bat', '*.sh', 'Thumbs.db'))
else:
shutil.copytree(p_dir, os.path.join(name_release_dir, p_dir))
# Create distribution package
d_package_path = '%s-%s.zip' % (name, release)
d_package = zipfile.ZipFile(d_package_path, 'w', zipfile.ZIP_DEFLATED)
# Append root files
for p_file in REL_FILES:
d_package.write(os.path.join(name_release_dir, p_file))
# Package directories
for p_dir in REL_DIRS:
d_package.write(os.path.join(name_release_dir, p_dir))
for root, dirs, files in os.walk(os.path.join(name_release_dir, p_dir)):
for name in files:
d_package.write(os.path.join(root, name))
for name in dirs:
d_package.write(os.path.join(root, name))
d_package.testzip()
d_package.comment = 'Official packaging by CodeTRAX'
d_package.printdir()
d_package.close()
# Remove the release dir
shutil.rmtree(name_release_dir)