-
Notifications
You must be signed in to change notification settings - Fork 14
/
plistyamlplist.py
executable file
·319 lines (292 loc) · 13.2 KB
/
plistyamlplist.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""If this script is run directly, it takes an input file and an output file
from the command line. It detects if the input file is a YAML or a PLIST file,
and converts to the other format:
plistyamlplist.py <input-file> <output-file>
The output file can be omitted. In this case, the name of the output file is
taken from the input file, with .yaml added to or taken off the end.
"""
import sys
import os
import shutil
import glob
import re
from plistyamlplist_lib.plist_yaml import plist_yaml
from plistyamlplist_lib.yaml_plist import yaml_plist
from plistyamlplist_lib.json_plist import json_plist
from plistyamlplist_lib.yaml_tidy import tidy_yaml
from plistyamlplist_lib.version import __version__
VERSION = __version__
def usage():
"""print help."""
print("Usage: plistyamlplist.py <input> <output>\n")
print(
"If <input> is a PLIST file and <output> is omitted,\n"
"<input> is converted to <input>.yaml in the same folder.\n"
)
print(
"If <input> ends in .yaml or .yml and <output> is omitted,\n"
"<input>.yaml is converted to PLIST format with name <input>\n"
)
print(
"If <input> is a folder with 'YAML' or 'JSON' in the path,\n"
"all yaml files in the subfolders will be converted to plists in\n"
"the corresponding subfolder structure above the YAML or JSON file.\n"
"Or, if <output> is specified as another folder, the corresponding\n"
"folder structure will be reproduced under the <output> folder"
)
print(
"If <input> is a folder with 'PLIST' in the path,\n"
"and <output> is specified as another folder,"
"all yaml files in the subfolders will be converted to plist in\n"
"the corresponding subfolder structure under the <output> folder."
)
print("If <output> is --tidy,\n" "<input>.yaml is tidied up for AutoPkg.\n")
def check_if_plist(in_path):
"""rather than restrict by filename, check if the file is a plist by
reading the second line of the file for the PLIST declaration."""
is_plist = False
with open(in_path) as fp:
try:
for i, line in enumerate(fp):
if i == 1:
# print line
if line.find("PLIST 1.0") != -1:
is_plist = True
elif i > 2:
break
except UnicodeDecodeError:
pass
return is_plist
def check_for_yaml_folder(check_path):
"""Check folder hierarchy for a YAML or _YAML folder. Output to same folder structure outwith YAML
folder if it exists,
e.g. /path/to/YAML/folder/subfolder/my.plist.yaml ==> /path/to/folder/subfolder/my.plist
Note there is no reverse option at this time"""
check_abspath = os.path.abspath(check_path)
yaml_folders = ["_YAML", "YAML"]
for yf in yaml_folders:
if yf in check_abspath:
print("{} folder exists : {}".format(yf, check_abspath))
top_path, base_path = check_abspath.split("{}/".format(yf))
out_path = os.path.dirname(os.path.join(top_path, base_path))
if os.path.exists(out_path):
print("Path exists : {}".format(out_path))
return out_path
else:
print("Path does not exist : {}".format(out_path))
print("Please create this folder and try again")
exit(1)
def check_for_json_folder(check_path):
"""Check folder hierarchy for a JSON or _JSON folder. Output to same folder structure outwith JSON
folder if it exists,
e.g. /path/to/JSON/folder/subfolder/my.plist.json ==> /path/to/folder/subfolder/my.plist
Note there is no reverse option at this time"""
check_abspath = os.path.abspath(check_path)
json_folders = ["_JSON", "JSON"]
for jf in json_folders:
if jf in check_abspath:
print("{} folder exists : {}".format(jf, check_abspath))
top_path, base_path = check_abspath.split("{}/".format(jf))
out_path = os.path.dirname(os.path.join(top_path, base_path))
if os.path.exists(out_path):
print("Path exists : {}".format(out_path))
return out_path
else:
print("Path does not exist : {}".format(out_path))
print("Please create this folder and try again")
exit(1)
def get_out_path(in_path, filetype):
"""determine the out_path when none given"""
if filetype == "yaml":
out_dir = check_for_yaml_folder(in_path)
if out_dir:
filename, _ = os.path.splitext(os.path.basename(in_path))
out_path = os.path.join(out_dir, filename)
else:
filename, _ = os.path.splitext(os.path.abspath(in_path))
out_path = filename
elif filetype == "json":
out_dir = check_for_json_folder(in_path)
if out_dir:
filename, _ = os.path.splitext(os.path.basename(in_path))
out_path = os.path.join(out_dir, filename)
else:
filename, _ = os.path.splitext(os.path.abspath(in_path))
out_path = filename
else:
if check_if_plist(in_path):
out_path = in_path + ".yaml"
else:
print("\nERROR: File is not PLIST, JSON or YAML format.\n")
usage()
exit(1)
return out_path
def main():
"""get the command line inputs if running this script directly."""
print(f"plist-yaml-plist version {VERSION}")
if len(sys.argv) < 2:
usage()
exit(1)
in_path = sys.argv[1]
# auto-determine which direction the conversion should go
if in_path.endswith(".yaml") or in_path.endswith(".yaml"):
filetype = "yaml"
elif in_path.endswith(".json"):
filetype = "json"
elif in_path.endswith(".plist"):
filetype = "plist"
else:
filetype = "other"
if filetype == "yaml" or filetype == "json":
# allow for converting whole folders if a glob is provided
_, glob_files = os.path.split(in_path)
if "*" in glob_files:
glob_files = glob.glob(in_path)
for glob_file in glob_files:
out_path = get_out_path(glob_file, filetype)
if filetype == "yaml":
print("Processing YAML folder with globs...")
yaml_plist(glob_file, out_path)
elif filetype == "json":
print("Processing JSON folder with globs...")
json_plist(glob_file, out_path)
else:
try:
sys.argv[2]
except IndexError:
out_path = get_out_path(in_path, filetype)
else:
out_path = sys.argv[2]
if filetype == "yaml":
print("Processing yaml file...")
if out_path == "--tidy":
tidy_yaml(in_path)
else:
yaml_plist(in_path, out_path)
elif filetype == "json":
print("Processing json file...")
json_plist(in_path, out_path)
# allow for converting whole folders if 'YAML' or 'JSON' is in the path
# and the path supplied is a folder
elif os.path.isdir(in_path) and "YAML" in in_path:
print("Processing YAML folder...")
filetype = "yaml"
try:
if sys.argv[2] == "--tidy":
print("WARNING! Processing all subfolders...\n")
for root, dirs, files in os.walk(in_path):
for name in files:
tidy_yaml(os.path.join(root, name))
for name in dirs:
tidy_yaml(os.path.join(root, name))
elif os.path.isdir(sys.argv[2]):
# allow batch replication of folder structure and conversion of yaml to plist
# also copies other file types without conversion to the same place in the
# hierarchy
out_path_base = os.path.abspath(sys.argv[2])
print("Writing to {}".format(out_path_base))
for root, dirs, files in os.walk(in_path):
for name in dirs:
working_dir = os.path.join(out_path_base, name)
if not os.path.isdir(working_dir):
print("Creating new folder " + working_dir)
os.mkdir(working_dir)
for name in files:
source_path = os.path.join(root, name)
print("In path: " + in_path)
sub_path = re.sub(in_path, "", source_path)
print("Subdirectory path: " + sub_path)
filename, _ = os.path.splitext(
os.path.join(out_path_base, sub_path)
)
print("Source path: " + source_path)
if source_path.endswith(".yaml"):
dest_path = filename + ".plist"
print("Destination path for plist: " + dest_path)
yaml_plist(source_path, dest_path)
else:
dest_path = os.path.join(
os.path.join(out_path_base, sub_path)
)
print("Destination path: " + dest_path)
try:
shutil.copy(source_path, dest_path)
if os.path.isfile(dest_path):
print("Written to " + dest_path + "\n")
except IOError:
print("ERROR: could not copy " + source_path + "\n")
except IndexError:
for in_file in os.listdir(in_path):
in_file_path = os.path.join(in_path, in_file)
out_path = get_out_path(in_file_path, filetype)
yaml_plist(in_file_path, out_path)
elif os.path.isdir(in_path) and "JSON" in in_path:
print("Processing JSON folder...")
filetype = "json"
for in_file in os.listdir(in_path):
in_file_path = os.path.join(in_path, in_file)
out_path = get_out_path(in_file_path, filetype)
json_plist(in_file_path, out_path)
elif os.path.isdir(in_path) and "PLIST" in in_path:
print("Processing PLIST folder...")
filetype = "plist"
if os.path.isdir(sys.argv[2]):
# allow batch replication of folder structure and conversion of plist to yaml
# also copies other file types without conversion to the same place in the
# hierarchy
out_path_base = os.path.abspath(sys.argv[2])
print("Writing to " + out_path_base)
for root, dirs, files in os.walk(in_path):
for name in dirs:
source_dir = os.path.join(root, name)
sub_dir = re.sub(in_path, "", source_dir)
working_dir = out_path_base + sub_dir
if "YAML" in working_dir:
# chances are we don't want to copy the contents of a YAML
# folder here
continue
if not os.path.isdir(working_dir):
print("Creating new folder " + working_dir)
os.mkdir(working_dir)
for name in files:
source_path = os.path.join(root, name)
if "YAML" in source_path:
# chances are we don't want to copy the contents of a YAML
# folder here
continue
print("In path: " + in_path)
sub_path = re.sub(in_path, "", source_path)
print("Subdirectory path: " + sub_path)
print("Source path: " + source_path)
if check_if_plist(source_path):
filename = re.sub(".plist", "", out_path_base + sub_path)
dest_path = filename + ".yaml"
print("Destination path for yaml: " + dest_path)
plist_yaml(source_path, dest_path)
else:
dest_path = out_path_base + sub_path
print("Destination path: " + dest_path)
try:
shutil.copy(source_path, dest_path)
if os.path.isfile(dest_path):
print("Written to " + dest_path + "\n")
except IOError:
print("ERROR: could not copy " + source_path + "\n")
else:
if check_if_plist(in_path):
try:
sys.argv[2]
except IndexError:
out_path = get_out_path(in_path, filetype)
else:
out_path = sys.argv[2]
print("Processing plist file...")
plist_yaml(in_path, out_path)
else:
print("\nERROR: Input File is not PLIST, JSON or YAML format.\n")
usage()
exit(1)
if __name__ == "__main__":
main()