forked from metabrainz/picard-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
158 lines (124 loc) · 4.77 KB
/
generate.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
#!/usr/bin/env python
import os
import re
import sys
import json
import zipfile
import zlib
from hashlib import md5
from subprocess import call
re_name = re.compile(r'PLUGIN_NAME = (?:_\(u|u|)((?:\"\"\"|\'\'\'|\"|\'))(.*)\1')
re_author = re.compile(r'PLUGIN_AUTHOR = (?:_\(u|u|)((?:\"\"\"|\'\'\'|\"|\'))(.*)\1')
re_ver = re.compile(r'PLUGIN_VERSION = (?:_\(u|u|)((?:\"\"\"|\'\'\'|\"|\'))(.*?)\1')
re_api = re.compile(r'PLUGIN_API_VERSIONS = \[((?:\"\"\"|\'\'\'|\"|\'))(.*?)\1\]')
# Descriptions are spread out in multiple lines so these will be handled separately
re_desc_start = re.compile(r'PLUGIN_DESCRIPTION = (?:_\(u|u|)(.*)')
re_desc_end = re.compile(r'PLUGIN_(.*)')
re_desc = re.compile(r'PLUGIN_DESCRIPTION = (?:_\(u|u|)((?:\"\"\"|\'\'\'|\"|\'))(.*?)\1', re.DOTALL)
def get_data(filepath):
"""
Extract usable information from plugin files.
"""
data = {}
desc_lines = []
desc_flag = False
with open(filepath) as f:
for line in f:
if 'name' not in data:
name = re.match(re_name, line)
if name:
data['name'] = name.group(2)
if 'author' not in data:
author = re.match(re_author, line)
if author:
data['author'] = author.group(2)
if 'description' not in data:
if re.match(re_desc_start, line):
desc_flag = True
elif re.match(re_desc_end, line):
desc_flag = False
desc = re.match(re_desc, re.sub(r'[\\\n]', '', "".join(desc_lines)))
if desc:
data['description'] = desc.group(2)
if desc_flag:
desc_lines.append(line)
if 'version' not in data:
ver = re.match(re_ver, line)
if ver:
data['version'] = ver.group(2)
if 'api_version' not in data:
apiver = re.match(re_api, line)
if apiver:
data['api_version'] = apiver.group(2)
return data
def build_json():
"""
Traverse the plugins directory to generate json data.
"""
# Read the existing data
if os.path.isfile(plugin_file):
with open(plugin_file, "r") as in_file:
plugins = json.load(in_file)["plugins"]
else:
plugins = {}
# All top level directories in plugin_dir are plugins
for dirname in next(os.walk(plugin_dir))[1]:
files = {}
data = {}
if dirname in [".git"]:
continue
dirpath = os.path.join(plugin_dir, dirname)
for root, dirs, filenames in os.walk(dirpath):
for filename in filenames:
ext = os.path.splitext(filename)[1]
if ext not in [".pyc"]:
file_path = os.path.join(root, filename)
with open(file_path, "rb") as md5file:
md5Hash = md5(md5file.read()).hexdigest()
files[file_path.split(os.path.join(dirpath, ''))[1]] = md5Hash
if not data:
data = get_data(os.path.join(plugin_dir, dirname, filename))
if dirname in plugins:
print("Updated: " + dirname)
if data:
for key, value in data.items():
plugins[dirname][key] = value
plugins[dirname]["files"] = files
else:
print("Added: " + dirname)
data['files'] = files
data['downloads'] = 0
plugins[dirname] = data
with open(plugin_file, "w") as out_file:
json.dump({"plugins": plugins}, out_file, sort_keys=True, indent=2)
def zip_files():
"""
Zip up plugin folders
"""
for dirname in next(os.walk(plugin_dir))[1]:
archive_path = os.path.join(plugin_dir, dirname)
archive = zipfile.ZipFile(archive_path + ".zip", "w")
dirpath = os.path.join(plugin_dir, dirname)
for root, dirs, filenames in os.walk(dirpath):
for filename in filenames:
file_path = os.path.join(root, filename)
archive.write(file_path,
file_path.split(os.path.join(dirpath, ''))[1],
compress_type=zipfile.ZIP_DEFLATED)
print("Created: " + dirname + ".zip")
# The file that contains json data
plugin_file = "plugins.json"
# The directory which contains plugin files
plugin_dir = "plugins"
if __name__ == '__main__':
if 1 in sys.argv:
if sys.argv[1] == "pull":
call(["git", "pull", "-q"])
elif sys.argv[1] == "json":
build_json()
elif sys.argv[1] == "zip":
zip_files()
else:
# call(["git", "pull", "-q"])
build_json()
zip_files()