-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.py
764 lines (613 loc) · 24.2 KB
/
setup.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
########################################################################################################################
### Do not forget to adjust the following variables to your own plugin.
# The plugin's identifier, has to be unique
plugin_identifier = "pi_support"
# The plugin's python package, should be "octoprint_<plugin identifier>", has to be unique
plugin_package = "octoprint_pi_support"
# The plugin's human readable name. Can be overwritten within OctoPrint's internal data via __plugin_name__ in the
# plugin module
plugin_name = "OctoPrint-PiSupport"
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "2023.10.10"
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
plugin_description = """Provides additional information about your Pi in the UI"""
# The plugin's author. Can be overwritten within OctoPrint's internal data via __plugin_author__ in the plugin module
plugin_author = "Gina Häußge"
# The plugin's author's mail address.
plugin_author_email = "[email protected]"
# The plugin's homepage URL. Can be overwritten within OctoPrint's internal data via __plugin_url__ in the plugin module
plugin_url = "https://github.com/OctoPrint/OctoPrint-PiSupport"
# The plugin's license. Can be overwritten within OctoPrint's internal data via __plugin_license__ in the plugin module
plugin_license = "AGPLv3"
# Any additional requirements besides OctoPrint should be listed here
plugin_requires = []
### --------------------------------------------------------------------------------------------------------------------
### More advanced options that you usually shouldn't have to touch follow after this point
### --------------------------------------------------------------------------------------------------------------------
# Additional package data to install for this plugin. The subfolders "templates", "static" and "translations" will
# already be installed automatically if they exist. Note that if you add something here you'll also need to update
# MANIFEST.in to match to ensure that python setup.py sdist produces a source distribution that contains all your
# files. This is sadly due to how python's setup.py works, see also http://stackoverflow.com/a/14159430/2028598
plugin_additional_data = []
# Any additional python packages you need to install with your plugin that are not contained in <plugin_package>.*
plugin_additional_packages = []
# Any python packages within <plugin_package>.* you do NOT want to install with your plugin
plugin_ignored_packages = []
########################################################################################################################
import glob
import os
import shutil
from distutils.command.clean import clean as _clean
from setuptools import Command, setup
def package_data_dirs(source, sub_folders):
dirs = []
for d in sub_folders:
folder = os.path.join(source, d)
if not os.path.exists(folder):
continue
for dirname, _, files in os.walk(folder):
dirname = os.path.relpath(dirname, source)
for f in files:
dirs.append(os.path.join(dirname, f))
return dirs
def recursively_handle_files(
directory, file_matcher, folder_matcher=None, folder_handler=None, file_handler=None
):
applied_handler = False
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if file_handler is not None and file_matcher(filename):
file_handler(path)
applied_handler = True
elif os.path.isdir(path) and (
folder_matcher is None or folder_matcher(directory, filename, path)
):
sub_applied_handler = recursively_handle_files(
path,
file_matcher,
folder_handler=folder_handler,
file_handler=file_handler,
)
if sub_applied_handler:
applied_handler = True
if folder_handler is not None:
folder_handler(path, sub_applied_handler)
return applied_handler
def has_requirement(requirement, requirements):
if requirement is None or requirements is None:
return False
# from past.builtins import basestring
# assert isinstance(requirement, basestring)
# assert isinstance(requirements, (list, tuple))
# assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements)))
requirement = requirement.lower()
requirements = [r.lower() for r in requirements]
compat = [
requirement.lower() + c for c in ("<", "<=", "!=", "==", ">=", ">", "~=", "===")
]
return requirement in requirements or any(
any(r.startswith(c) for c in compat) for r in requirements
)
class CleanCommand(_clean):
user_options = _clean.user_options + [
("orig", None, "behave like original clean command"),
("noeggs", None, "don't clean up eggs"),
("nopyc", None, "don't clean up pyc files"),
]
boolean_options = _clean.boolean_options + ["orig", "noeggs", "nopyc"]
source_folder = "src"
eggs = None
@classmethod
def for_options(cls, source_folder="src", eggs=None):
if eggs is None:
eggs = []
return type(cls)(
cls.__name__, (cls,), dict(source_folder=source_folder, eggs=eggs)
)
def initialize_options(self):
_clean.initialize_options(self)
self.orig = None
self.noeggs = None
self.nopyc = None
def finalize_options(self):
_clean.finalize_options(self)
if not self.orig:
self.all = True
def run(self):
_clean.run(self)
if self.orig:
return
# eggs
if not self.noeggs:
for egg in self.eggs:
globbed_eggs = glob.glob(egg)
for globbed_egg in globbed_eggs:
print("deleting '%s' egg" % globbed_egg)
if not self.dry_run:
shutil.rmtree(globbed_egg)
# pyc files
if not self.nopyc:
def delete_folder_if_empty(path, applied_handler):
if not applied_handler:
return
if len(os.listdir(path)) == 0:
if not self.dry_run:
shutil.rmtree(path)
print(
"removed %s since it was empty" % path[len(self.source_folder) :]
)
def delete_file(path):
print("removing '%s'" % path[len(self.source_folder) :])
if not self.dry_run:
os.remove(path)
import fnmatch
print("recursively removing *.pyc from '%s'" % self.source_folder)
recursively_handle_files(
os.path.abspath(self.source_folder),
lambda name: fnmatch.fnmatch(name.lower(), "*.pyc"),
folder_matcher=lambda dir, name, path: name != ".git",
folder_handler=delete_folder_if_empty,
file_handler=delete_file,
)
class NewTranslation(Command):
description = "create a new translation"
user_options = [
("locale=", "l", "locale for the new translation"),
]
boolean_options = []
pot_file = None
output_dir = None
@classmethod
def for_options(cls, pot_file=None, output_dir=None):
if pot_file is None:
raise ValueError("pot_file must not be None")
if output_dir is None:
raise ValueError("output_dir must not be None")
return type(cls)(
cls.__name__, (cls,), dict(pot_file=pot_file, output_dir=output_dir)
)
def __init__(self, dist, **kw):
from babel.messages import frontend as babel
self.babel_init_messages = babel.init_catalog(dist)
Command.__init__(self, dist, **kw)
def initialize_options(self):
self.locale = None
self.babel_init_messages.initialize_options()
def finalize_options(self):
self.babel_init_messages.locale = self.locale
self.babel_init_messages.input_file = self.__class__.pot_file
self.babel_init_messages.output_dir = self.__class__.output_dir
self.babel_init_messages.finalize_options()
def run(self):
self.babel_init_messages.run()
class ExtractTranslation(Command):
description = "extract translations"
user_options = []
boolean_options = []
mail_address = "[email protected]"
copyright_holder = "The OctoPrint Project"
mapping_file = None
pot_file = None
input_dirs = None
@classmethod
def for_options(
cls,
mail_address="[email protected]",
copyright_holder="The OctoPrint Project",
mapping_file=None,
pot_file=None,
input_dirs=None,
):
if mapping_file is None:
raise ValueError("mapping_file must not be None")
if pot_file is None:
raise ValueError("pot_file must not be None")
if input_dirs is None:
raise ValueError("input_dirs must not be None")
return type(cls)(
cls.__name__,
(cls,),
dict(
mapping_file=mapping_file,
pot_file=pot_file,
input_dirs=input_dirs,
mail_address=mail_address,
copyright_holder=copyright_holder,
),
)
def __init__(self, dist, **kw):
from babel.messages import frontend as babel
self.babel_extract_messages = babel.extract_messages(dist)
Command.__init__(self, dist, **kw)
def initialize_options(self):
self.babel_extract_messages.initialize_options()
def finalize_options(self):
self.babel_extract_messages.mapping_file = self.__class__.mapping_file
self.babel_extract_messages.output_file = self.__class__.pot_file
self.babel_extract_messages.input_dirs = self.__class__.input_dirs
self.babel_extract_messages.msgid_bugs_address = self.__class__.mail_address
self.babel_extract_messages.copyright_holder = self.__class__.copyright_holder
self.babel_extract_messages.finalize_options()
def run(self):
self.babel_extract_messages.run()
class RefreshTranslation(Command):
description = "refresh translations"
user_options = [
("locale=", "l", "locale for the translation to refresh"),
]
boolean_options = []
mail_address = "[email protected]"
copyright_holder = "The OctoPrint Project"
mapping_file = None
pot_file = None
input_dirs = None
output_dir = None
@classmethod
def for_options(
cls,
mail_address="[email protected]",
copyright_holder="The OctoPrint Project",
mapping_file=None,
pot_file=None,
input_dirs=None,
output_dir=None,
):
if mapping_file is None:
raise ValueError("mapping_file must not be None")
if pot_file is None:
raise ValueError("pot_file must not be None")
if input_dirs is None:
raise ValueError("input_dirs must not be None")
if output_dir is None:
raise ValueError("output_dir must not be None")
return type(cls)(
cls.__name__,
(cls,),
dict(
mapping_file=mapping_file,
pot_file=pot_file,
input_dirs=input_dirs,
mail_address=mail_address,
copyright_holder=copyright_holder,
output_dir=output_dir,
),
)
def __init__(self, dist, **kw):
from babel.messages import frontend as babel
self.babel_extract_messages = babel.extract_messages(dist)
self.babel_update_messages = babel.update_catalog(dist)
Command.__init__(self, dist, **kw)
def initialize_options(self):
self.locale = None
self.babel_extract_messages.initialize_options()
self.babel_update_messages.initialize_options()
def finalize_options(self):
self.babel_extract_messages.mapping_file = self.__class__.mapping_file
self.babel_extract_messages.output_file = self.__class__.pot_file
self.babel_extract_messages.input_dirs = self.__class__.input_dirs
self.babel_extract_messages.msgid_bugs_address = self.__class__.mail_address
self.babel_extract_messages.copyright_holder = self.__class__.copyright_holder
self.babel_extract_messages.finalize_options()
self.babel_update_messages.input_file = self.__class__.pot_file
self.babel_update_messages.output_dir = self.__class__.output_dir
self.babel_update_messages.locale = self.locale
self.babel_update_messages.finalize_options()
def run(self):
self.babel_extract_messages.run()
self.babel_update_messages.run()
class CompileTranslation(Command):
description = "compile translations"
user_options = []
boolean_options = []
output_dir = None
@classmethod
def for_options(cls, output_dir=None):
if output_dir is None:
raise ValueError("output_dir must not be None")
return type(cls)(cls.__name__, (cls,), dict(output_dir=output_dir))
def __init__(self, dist, **kw):
from babel.messages import frontend as babel
self.babel_compile_messages = babel.compile_catalog(dist)
Command.__init__(self, dist, **kw)
def initialize_options(self):
self.babel_compile_messages.initialize_options()
def finalize_options(self):
self.babel_compile_messages.directory = self.__class__.output_dir
self.babel_compile_messages.finalize_options()
def run(self):
self.babel_compile_messages.run()
class BundleTranslation(Command):
description = "bundles translations"
user_options = [("locale=", "l", "locale for the translation to bundle")]
boolean_options = []
source_dir = None
target_dir = None
@classmethod
def for_options(cls, source_dir=None, target_dir=None):
if source_dir is None:
raise ValueError("source_dir must not be None")
if target_dir is None:
raise ValueError("target_dir must not be None")
return type(cls)(
cls.__name__, (cls,), dict(source_dir=source_dir, target_dir=target_dir)
)
def initialize_options(self):
self.locale = None
def finalize_options(self):
pass
def run(self):
locale = self.locale
source_path = os.path.join(self.__class__.source_dir, locale)
target_path = os.path.join(self.__class__.target_dir, locale)
if not os.path.exists(source_path):
raise RuntimeError("source path " + source_path + " does not exist")
if os.path.exists(target_path):
if not os.path.isdir(target_path):
raise RuntimeError(
"target path " + target_path + " exists and is not a directory"
)
shutil.rmtree(target_path)
print(
f"Copying translations for locale {locale} from {source_path} to {target_path}..."
)
shutil.copytree(source_path, target_path)
class PackTranslation(Command):
description = "creates language packs for translations"
user_options = [
("locale=", "l", "locale for the translation to pack"),
("author=", "a", "author of the translation"),
("target=", "t", "target folder for the pack"),
]
boolean_options = []
source_dir = None
pack_name_prefix = None
pack_path_prefix = None
@classmethod
def for_options(cls, source_dir=None, pack_name_prefix=None, pack_path_prefix=None):
if source_dir is None:
raise ValueError("source_dir must not be None")
if pack_name_prefix is None:
raise ValueError("pack_name_prefix must not be None")
if pack_path_prefix is None:
raise ValueError("pack_path_prefix must not be None")
return type(cls)(
cls.__name__,
(cls,),
dict(
source_dir=source_dir,
pack_name_prefix=pack_name_prefix,
pack_path_prefix=pack_path_prefix,
),
)
def initialize_options(self):
self.locale = None
self.author = None
self.target = None
def finalize_options(self):
if self.locale is None:
raise ValueError("locale must be provided")
def run(self):
locale = self.locale
locale_dir = os.path.join(self.__class__.source_dir, locale)
if not os.path.isdir(locale_dir):
raise RuntimeError("translation does not exist, please create it first")
import datetime
now = datetime.datetime.utcnow().replace(microsecond=0)
if self.target is None:
self.target = self.__class__.source_dir
zip_path = os.path.join(
self.target,
"{prefix}{locale}_{date}.zip".format(
prefix=self.__class__.pack_name_prefix,
locale=locale,
date=now.strftime("%Y%m%d%H%M%S"),
),
)
print(f"Packing translation to {zip_path}")
def add_recursively(zip, path, prefix):
if not os.path.isdir(path):
return
for entry in os.listdir(path):
entry_path = os.path.join(path, entry)
new_prefix = prefix + "/" + entry
if os.path.isdir(entry_path):
add_recursively(zip, entry_path, new_prefix)
elif os.path.isfile(entry_path):
print(f"Adding {entry_path} as {new_prefix}")
zip.write(entry_path, new_prefix)
meta_str = f"last_update: {now.isoformat()}\n"
if self.author:
meta_str += f"author: {self.author}\n"
zip_locale_root = self.__class__.pack_path_prefix + locale
import zipfile
with zipfile.ZipFile(zip_path, "w") as zip:
add_recursively(zip, locale_dir, zip_locale_root)
print(f"Adding meta.yaml as {zip_locale_root}/meta.yaml")
zip.writestr(zip_locale_root + "/meta.yaml", meta_str)
def get_babel_commandclasses(
pot_file=None,
mapping_file="babel.cfg",
input_dirs=".",
output_dir=None,
pack_name_prefix=None,
pack_path_prefix=None,
bundled_dir=None,
mail_address="[email protected]",
copyright_holder="The OctoPrint Project",
):
result = dict(
babel_new=NewTranslation.for_options(pot_file=pot_file, output_dir=output_dir),
babel_extract=ExtractTranslation.for_options(
mapping_file=mapping_file,
pot_file=pot_file,
input_dirs=input_dirs,
mail_address=mail_address,
copyright_holder=copyright_holder,
),
babel_refresh=RefreshTranslation.for_options(
mapping_file=mapping_file,
pot_file=pot_file,
input_dirs=input_dirs,
output_dir=output_dir,
mail_address=mail_address,
copyright_holder=copyright_holder,
),
babel_compile=CompileTranslation.for_options(output_dir=output_dir),
babel_pack=PackTranslation.for_options(
source_dir=output_dir,
pack_name_prefix=pack_name_prefix,
pack_path_prefix=pack_path_prefix,
),
)
if bundled_dir is not None:
result["babel_bundle"] = BundleTranslation.for_options(
source_dir=output_dir, target_dir=bundled_dir
)
return result
def create_plugin_setup_parameters(
identifier="todo",
name="TODO",
version="0.1",
description="TODO",
author="TODO",
mail="[email protected]",
url="TODO",
license="AGPLv3",
source_folder=".",
additional_data=None,
additional_packages=None,
ignored_packages=None,
requires=None,
extra_requires=None,
cmdclass=None,
eggs=None,
package=None,
dependency_links=None,
):
import pkg_resources
if package is None:
package = f"octoprint_{identifier}"
if additional_data is None:
additional_data = list()
if additional_packages is None:
additional_packages = list()
if ignored_packages is None:
ignored_packages = list()
if dependency_links is None:
dependency_links = list()
if requires is None:
requires = []
if not isinstance(requires, list):
raise ValueError("requires must be a list")
if extra_requires is None:
extra_requires = dict()
if not isinstance(extra_requires, dict):
raise ValueError("extra_requires must be a dict")
if cmdclass is None:
cmdclass = dict()
if not isinstance(cmdclass, dict):
raise ValueError("cmdclass must be a dict")
if eggs is None:
eggs = []
if not isinstance(eggs, list):
raise ValueError("eggs must be a list")
egg = "{name}*.egg-info".format(
name=pkg_resources.to_filename(pkg_resources.safe_name(name))
)
if egg not in eggs:
eggs = [egg] + eggs
cmdclass.update(
dict(
clean=CleanCommand.for_options(
source_folder=os.path.join(source_folder, package), eggs=eggs
)
)
)
translation_dir = os.path.join(source_folder, "translations")
pot_file = os.path.join(translation_dir, "messages.pot")
bundled_dir = os.path.join(source_folder, package, "translations")
cmdclass.update(
get_babel_commandclasses(
pot_file=pot_file,
output_dir=translation_dir,
bundled_dir=bundled_dir,
pack_name_prefix=f"{name}-i18n-",
pack_path_prefix=f"_plugins/{identifier}/",
)
)
from setuptools import find_packages
packages = set(
[package]
+ list(
filter(
lambda x: x.startswith(f"{package}."),
find_packages(where=source_folder, exclude=ignored_packages),
)
)
+ additional_packages
)
print(f"Found packages: {packages!r}")
return dict(
name=name,
version=version,
description=description,
author=author,
author_email=mail,
url=url,
license=license,
# adding new commands
cmdclass=cmdclass,
# we only have our plugin package to install
packages=packages,
# we might have additional data files in sub folders that need to be installed too
package_data={
package: package_data_dirs(
os.path.join(source_folder, package),
["static", "templates", "translations"] + additional_data,
)
},
include_package_data=True,
# If you have any package data that needs to be accessible on the file system, such as templates or static assets
# this plugin is not zip_safe.
zip_safe=False,
install_requires=requires,
extras_require=extra_requires,
dependency_links=dependency_links,
# Hook the plugin into the "octoprint.plugin" entry point, mapping the plugin_identifier to the plugin_package.
# That way OctoPrint will be able to find the plugin and load it.
entry_points={"octoprint.plugin": [f"{identifier} = {package}"]},
)
def read_file_contents(path):
import codecs
with codecs.open(path, encoding="utf-8") as f:
return f.read()
here = os.path.abspath(os.path.dirname(__file__))
setup_parameters = create_plugin_setup_parameters(
identifier=plugin_identifier,
package=plugin_package,
name=plugin_name,
version=plugin_version,
description=plugin_description,
author=plugin_author,
mail=plugin_author_email,
url=plugin_url,
license=plugin_license,
requires=plugin_requires,
additional_packages=plugin_additional_packages,
ignored_packages=plugin_ignored_packages,
additional_data=plugin_additional_data,
extra_requires={
"develop": [
# testing
"pytest",
"ddt",
"mock",
# pre-commit
"pre-commit",
]
},
)
setup_parameters["long_description"] = read_file_contents(os.path.join(here, "README.md"))
setup_parameters["long_description_content_type"] = "text/markdown"
setup(**setup_parameters)