-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
executable file
·1285 lines (1022 loc) · 41.2 KB
/
update.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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This module has been written in order to be the main entry for every tests in a
directory which contain a list to test.
Authors:
- @Funilrys, Nissar Chababy <contactTAfunilrysTODcom>
- @mitchellkrogza, Mitchell Krog <mitchellkrogTAgmailTODcom
Contributors:
Let's contribute !
@GitHubUsername, Name, Email (optional)
"""
# pylint: disable=bad-continuation, too-many-lines, logging-format-interpolation
import logging
from json import decoder, dump, loads
from os import environ, getcwd, path, remove
from os import sep as directory_separator
from os import walk
from re import compile as comp
from re import escape
from re import sub as substrings
from shutil import copyfileobj, rmtree
from subprocess import PIPE, Popen
from tarfile import open as tarfile_open
from time import ctime, strftime
from domain2idna import get as domain2idna
from requests import get
class Settings: # pylint: disable=too-few-public-methods
"""
This class will save all data that can be called from anywhere in the code.
"""
# This variable will help us keep a track on info.json content.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
informations = {}
# This variable should be initiated with the raw link to the hosts file or the
# list we are going to test.
#
# Note: The variable name should not be changed.
# Note: This variable is auto updated by Initiate()
#
# Example:
# "https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt"
raw_link = ""
travis_file = ".travis.yml"
# This variable should be initiated with the name of the list once downloaded.
# Recommended formats:
# - GitHub Repository:
# - GitHub organization:
# - Others:
#
# Note: The variable name should not be changed.
#
# Example: "[email protected]"
list_name = "domains.list"
# This variable is used to set the location of the file for the list without
# dead/inactive domains.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
clean_list_file = "clean.list"
# This variable is used to set the location of the file for the list without
# dead/inactive domains and whitelisted domains.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
whitelisted_list_file = "whitelisted.list"
# This variable is used to set the location of the file for the list without
# dead/inactive domains.
# The difference between the purpose of this file and the clean.list file
# is that this file do not take the "SPECIAL" flag of PyFunceble in
# consideration.
volatile_list_file = "volatile.list"
# This variable will help us know where we are working into the filesystem.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
current_directory = getcwd() + directory_separator
# This variable will help us know which file we are going to test.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
file_to_test = current_directory + list_name
# This variable will help us know what how many days we have to wait until next test.
#
# Note: This variable is auto updated by Initiate()
days_until_next_test = 0
# This variable will help us know the date of the last test.
#
# Note: This variable is auto updated by Initiate()
last_test = 0
# This variable will help us manage the implementation of days_until_next_test and last_test.
#
# Note: This variable is auto updated by Initiate()
currently_under_test = False
# This variable will help us know where should the info.json file be located.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
repository_info = current_directory + "info.json"
# This variable will help us know which version of PyFunceble we are going to use.
#
# Note: True = master | False = dev
# Note: This variable is auto updated by Initiate()
stable = False
# This variable represent the PyFunceble infrastructure.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
PyFunceble = {
".PyFunceble_production.yaml": "https://raw.githubusercontent.com/funilrys/PyFunceble/master/.PyFunceble_production.yaml", # pylint: disable=line-too-long
".PyFunceble_LICENSE": "https://raw.githubusercontent.com/funilrys/PyFunceble/master/LICENSE", # pylint: disable=line-too-long
}
# This variable is used to match [ci skip] from the git log.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
regex_travis = "[ci skip]"
# This variable is used ot match the special whitelist elements.
regex_whitelist = r"(ALL|REG|RZD)\s"
# This variable is used to set the number of minutes before we stop the script under Travis CI.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
autosave_minutes = 15
# This variable is used to set the default commit message when we commit
# under Travic CI.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
commit_autosave_message = ""
# This variable is used to set permanent_license_link.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
permanent_license_link = "https://raw.githubusercontent.com/Ultimate-Hosts-Blacklist/repository-structure/master/LICENSE" # pylint: disable=line-too-long
# This variable is used to set the permanant config links
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
permanent_config_link = "https://raw.githubusercontent.com/Ultimate-Hosts-Blacklist/repository-structure/master/.PyFunceble_cross_input_sources.yaml" # pylint: disable=line-too-long
# This variable is used to get the latest travis fil.
#
# Noe: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
permanent_travis_file_link = "https://raw.githubusercontent.com/Ultimate-Hosts-Blacklist/repository-structure/master/.travis.yml" # pylint: disable=line-too-long
# This variable is used to set the arguments when executing PyFunceble.py
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
arguments = []
# This variable is used to know if we need to delete INACTIVE and INVALID
# domain from the original file.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
clean_original = False
# This variable is used to know if we need to convert the list into idna
# before we write the file PyFunceble is going to test.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
# Note: This variable is auto updated by Initiate()
convert_to_idna = True
# This variable is used to know the marker which we have to match in
# order to start from the begining.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
launch_test_marker = r"Launch\stest"
# This variable set the name of the administration file.
#
# Note: DO NOT TOUCH UNLESS YOU KNOW WHAT IT MEANS!
administration_script = "administration.py"
class TravisCI:
"""
Manage everything needed when working with
Travis CI.
"""
@classmethod
def configure_git(cls):
"""
Prepare Git for push.
"""
try:
_ = environ["TRAVIS_BUILD_DIR"]
logging.debug("Deletion of the remote origin.")
Helpers.Command("git remote rm origin", False).execute()
logging.debug("Addition of the correct remote origin.")
Helpers.Command(
"git remote add origin https://"
+ "%[email protected]/%s.git"
% (environ["GH_TOKEN"], environ["TRAVIS_REPO_SLUG"]),
False,
).execute()
logging.debug("Addition of the GIT email.")
Helpers.Command(
'git config --global user.email "%s"' % (environ["GIT_EMAIL"]), False
).execute()
logging.debug("Addition of the GIT name.")
Helpers.Command(
'git config --global user.name "%s"' % (environ["GIT_NAME"]), False
).execute()
logging.debug("Setting the push mode to `simple`.")
Helpers.Command("git config --global push.default simple", False).execute()
logging.debug("Checkout `{}`".format(environ["GIT_BRANCH"]))
Helpers.Command("git checkout %s" % environ["GIT_BRANCH"], False).execute()
except KeyError:
pass
@classmethod
def fix_permissions(cls):
"""
Fix the permissions of the TRAVIS_BUILD_DIR.
"""
try:
build_dir = environ["TRAVIS_BUILD_DIR"]
commands = [
"sudo chown -R travis:travis %s" % (build_dir),
"sudo chgrp -R travis %s" % (build_dir),
"sudo chmod -R g+rwX %s" % (build_dir),
"sudo chmod 777 -Rf %s.git" % (build_dir + directory_separator),
r"sudo find %s -type d -exec chmod g+x '{}' \;" % (build_dir),
]
for command in commands:
logging.debug("Executing: {0}".format(command))
Helpers.Command(command, False).execute()
if (
Helpers.Command("git config core.sharedRepository", False).execute()
== ""
):
Helpers.Command(
"git config core.sharedRepository group", False
).execute()
except KeyError:
pass
def __init__(self, init_instance=False):
if init_instance:
self.configure_git()
class PyFunceble:
"""
Manage the way we work and execute PyFunceble.
"""
@classmethod
def install(cls):
"""
Install the right version of PyFunceble.
"""
logging.debug("Starting of the installation process of PyFunceble.")
if Settings.stable:
to_download = "PyFunceble"
else:
to_download = "PyFunceble-dev"
logging.debug("To download: {}".format(to_download))
logging.debug("Installing {}".format(to_download))
Helpers.Command("pip3 install --upgrade %s" % to_download, False).execute()
@classmethod
def download_complementary_files(cls):
"""
Download all complementary PyFunceble's related files.
"""
for file, link in Settings.PyFunceble.items():
file_path = Settings.current_directory + file
if not Settings.stable:
link = link.replace("master", "dev")
else:
link = link.replace("dev", "master")
logging.debug("Looking for `{}` at `{}`".format(file, link))
if not Helpers.Download(link, file_path).link():
raise Exception("Unable to download %s." % link)
logging.debug("Deletion of old files.")
Helpers.File(Settings.current_directory + "tool.py").delete()
Helpers.File(Settings.current_directory + "PyFunceble.py").delete()
Helpers.File(Settings.current_directory + "requirements.txt").delete()
@classmethod
def clean(cls):
"""
Clean the output directory if present.
"""
if path.isdir(Settings.current_directory + "output"):
logging.debug("Starting of the cleaning process.")
Helpers.Command("PyFunceble --clean", False).execute()
@classmethod
def construct_arguments(cls):
"""
Construct the arguments to pass to PyFunceble.
"""
to_use = []
to_use.append(
"--cmd-before-end %s" % Settings.current_directory
+ Settings.administration_script
)
if Settings.convert_to_idna:
to_use.append("--idna")
if Settings.arguments != []:
to_use.extend(Settings.arguments)
if to_use:
return " ".join(to_use)
return ""
@classmethod
def is_test_allowed(cls):
"""
Authorize a test run.
"""
if Helpers.Regex(
Helpers.Command("git log -1", False).execute(),
Settings.launch_test_marker,
return_data=False,
escape=False,
).match():
logging.debug("Allowed because of launch marker.")
cls.clean()
return True
if not Settings.currently_under_test:
logging.debug("Allowed because currently under test.")
cls.clean()
return True
if Settings.days_until_next_test >= 1 and Settings.last_test != 0:
retest_date = Settings.last_test + (
24 * Settings.days_until_next_test * 3600
)
if int(strftime("%s")) >= retest_date or Settings.currently_under_test:
logging.debug("Allowed because of time.")
return True
logging.debug("Disallowed because of time.")
return False
logging.debug("Allowed because of !?.")
return True
@classmethod
def run(cls):
"""
Run PyFunceble.
"""
# pylint: disable=invalid-name
PyFunceble_path = "PyFunceble"
command_to_execute = "%s -v && " % (PyFunceble_path)
command_to_execute += "export PYFUNCEBLE_AUTO_CONFIGURATION=PyFunceble && "
command_to_execute += "%s --directory-structure -q && " % PyFunceble_path
command_to_execute += "%s %s -f %s" % (
PyFunceble_path,
cls.construct_arguments(),
Settings.file_to_test,
)
if cls.is_test_allowed():
logging.debug("Downloading the latest project LICENSE.")
Helpers.Download(
Settings.permanent_license_link, Settings.current_directory + "LICENSE"
).link()
logging.debug("Update informations (info.json)")
Settings.informations.update(
{"last_test": strftime("%s"), "currently_under_test": str(int(True))}
)
for index in ["clean_list_file", "list_name"]:
if index in Settings.informations:
logging.debug(
"Deletion of `{}` from the administration file.".format(index)
)
del Settings.informations[index]
logging.debug(
"Latest administration content: {}".format(Settings.informations)
)
Helpers.Dict(Settings.informations).to_json(Settings.repository_info)
logging.debug("Launching: {}".format(command_to_execute))
Helpers.Command(command_to_execute, True).execute()
else:
print(
"No need to test until %s."
% ctime(
Settings.last_test + (24 * Settings.days_until_next_test * 3600)
)
)
exit(0)
class Configuration:
"""
Manage the way we generate and install the PyFunceble's configuration.
"""
@classmethod
def update(cls):
"""
Update the cross repository configuration.
"""
destination = Settings.permanent_config_link.split("/")[-1]
logging.debug("Destination is {}".format(destination))
if path.isfile(destination):
logging.debug("Destination file exists.")
if not Settings.stable:
to_download = Settings.PyFunceble[
".PyFunceble_production.yaml"
].replace("master", "dev")
else:
to_download = Settings.PyFunceble[
".PyFunceble_production.yaml"
].replace("dev", "master")
logging.debug(
"Downloading `{}` into `{}`".format(to_download, destination)
)
Helpers.Download(to_download, destination).link()
try:
_ = environ["TRAVIS_BUILD_DIR"]
travis_branch = environ["GIT_BRANCH"]
except KeyError:
travis_branch = "master"
to_replace = {
r"less:.*": "less: False",
r"plain_list_domain:.*": "plain_list_domain: True",
r"seconds_before_http_timeout:.*": "seconds_before_http_timeout: 6",
r"share_logs:.*": "share_logs: True",
r"show_execution_time:.*": "show_execution_time: True",
r"split:.*": "split: True",
r"travis:.*": "travis: True",
r"travis_autosave_commit:.*": 'travis_autosave_commit: "[Autosave] Testing for Ultimate Hosts Blacklist"', # pylint: disable=line-too-long
r"travis_autosave_final_commit:.*": 'travis_autosave_final_commit: "[Results] Testing for Ultimate Hosts Blacklist"', # pylint: disable=line-too-long
r"travis_branch:.*": "travis_branch: %s" % travis_branch,
r"travis_autosave_minutes:.*": "travis_autosave_minutes: %s"
% Settings.autosave_minutes,
}
content = Helpers.File(destination).read()
for regex, replacement in to_replace.items():
content = Helpers.Regex(
content, regex, replace_with=replacement, return_data=True
).replace()
logging.debug("Latest config: {}".format(content))
logging.debug("Writting latest config into `{}`".format(destination))
Helpers.File(destination).write(content, overwrite=True)
logging.debug("Writting latest config into `.PyFunceble.yaml`")
Helpers.File(".PyFunceble.yaml").write(content, overwrite=True)
else:
try:
_ = environ["TRAVIS_BUILD_DIR"]
with open(
Settings.current_directory + Settings.travis_file,
"r",
encoding="utf-8",
) as file_stream:
local_data = yaml_load(file_stream)
if not Settings.stable:
to_download = Settings.permanent_travis_file_link.replace(
"master", "dev"
)
else:
to_download = Settings.permanent_travis_file_link.replace(
"dev", "master"
)
upstream_data = yaml_load(
Helpers.Download(to_download, None).link()
)
to_update = [
"install",
"notifications",
"addons",
"cache",
"dist",
"language",
"matrix",
"python",
"script",
"sudo",
]
for index in to_update:
local_data[index] = upstream_data[index]
with open(
Settings.current_directory + Settings.travis_file,
"w",
encoding="utf-8",
) as file_stream:
file_stream.write(
yaml_dump(
local_data,
encoding="utf-8",
allow_unicode=True,
indent=4,
default_flow_style=False,
).decode("utf-8")
)
print(
Helpers.Command(
"git status --porcelain {0}".format(
Settings.current_directory + Settings.travis_file
),
allow_stdout=False,
)
.execute()
.strip()
.startswith("M")
)
if (
Helpers.Command(
"git status --porcelain {0}".format(
Settings.current_directory + Settings.travis_file
),
allow_stdout=False,
)
.execute()
.strip()
.startswith("M")
):
print(
Helpers.Command(
"git add {0} && git commit -m 'Update of the configuration file' && git push origin {1}".format( # pylint: disable=line-too-long
Settings.current_directory + Settings.travis_file,
environ["GIT_BRANCH"],
)
).execute()
)
exit(0)
except KeyError:
pass
@classmethod
def install(cls):
"""
Install the cross repository configuration.
"""
if not path.isfile(Settings.permanent_config_link.split("/")[-1]):
Helpers.Download(
Settings.permanent_config_link, ".PyFunceble.yaml"
).link()
class DomainsList:
"""
Construct, generate or update the domains.list file.
"""
@classmethod
def format_domain(cls, extracted_domain):
"""
Format the extracted domain before passing it to the system.
Argument:
- extracted_domain: str
The extracted domain or line from the file.
"""
extracted_domain = extracted_domain.strip()
if not extracted_domain.startswith("#"):
if "#" in extracted_domain:
extracted_domain = extracted_domain[
: extracted_domain.find("#")
].strip()
if " " in extracted_domain or "\t" in extracted_domain:
splited_line = extracted_domain.split()
for element in splited_line[1:]:
if element:
return element
return extracted_domain
return ""
@classmethod
def extract_lines(cls, file):
"""
This method extract and format each line to get the domain.
Argument:
- file: str
The file to read.
"""
from PyFunceble import is_subdomain, syntax_check
result = []
for line in Helpers.File(file).to_list():
if not line:
continue
if line.startswith("#"):
continue
if Helpers.Regex(line, Settings.regex_whitelist, return_data=False).match():
result.append(line)
continue
formated = cls.format_domain(line.strip())
if formated.startswith("www."):
if formated[4:] not in result:
result.append(formated[4:])
elif syntax_check(domain2idna(formated)) and not is_subdomain(
domain2idna(formated)
):
new_version = f"www.{formated}"
if new_version not in result:
result.append(new_version)
result.append(formated)
return result
@classmethod
def generate_from_tar_gz(cls):
"""
This method will search download and decompress .tar.gz.
+ searches for `domains` files.
"""
download_filename = Settings.raw_link.split("/")[-1]
extraction_directory = "./temp"
if Helpers.Download(Settings.raw_link, download_filename).link():
Helpers.File(download_filename).tar_gz_decompress(extraction_directory)
formated_content = []
for root, dirs, files in walk( # pylint: disable=unused-variable
extraction_directory
):
for file in files:
if file.startswith("domains"):
formated_content.extend(
cls.extract_lines(path.join(root, file))
)
formated_content = Helpers.List(formated_content).format()
Helpers.File(Settings.list_name).write(
"\n".join(formated_content), overwrite=True
)
Helpers.Directory(extraction_directory).delete()
Helpers.File(download_filename).delete()
else:
raise Exception("Unable to download the the file. Please check the link.")
@classmethod
def construct(cls):
"""
Construct or update the file we are going to test.
"""
restart = (
Helpers.Regex(
Helpers.Command("git log -1", False).execute(),
Settings.launch_test_marker,
return_data=False,
escape=False,
).match()
or not Settings.currently_under_test
)
if restart:
if Settings.raw_link.endswith(".tar.gz"):
cls.generate_from_tar_gz()
elif Helpers.Download(Settings.raw_link, Settings.file_to_test).link():
Helpers.Command("dos2unix " + Settings.file_to_test, False).execute()
formated_content = Helpers.List(
cls.extract_lines(Settings.file_to_test)
).format()
Helpers.File(Settings.file_to_test).write(
"\n".join(formated_content), overwrite=True
)
del formated_content
elif not Settings.raw_link and path.isfile(Settings.file_to_test):
print("\n")
new_file_content = Helpers.List(
cls.extract_lines(Settings.file_to_test)
).format()
Helpers.File(Settings.file_to_test).write(
"\n".join(new_file_content), overwrite=True
)
del new_file_content
if restart:
Settings.currently_under_test = False
PyFunceble.clean()
class Administration:
"""
Administration file/info manipulation.
"""
@classmethod
def update_settings(cls, index):
"""
Update Setting.index.
Argument:
- index: str
A valid index name.
"""
try:
getattr(Settings, index)
if (
index
in [
"stable",
"currently_under_test",
"clean_original",
"convert_to_idna",
]
and Settings.informations[index].isdigit()
):
setattr(Settings, index, bool(int(Settings.informations[index])))
elif (
index in ["days_until_next_test", "last_test", "autosave_minutes"]
and Settings.informations[index].isdigit()
):
setattr(Settings, index, int(Settings.informations[index]))
else:
setattr(Settings, index, Settings.informations[index])
except AttributeError:
raise Exception(
'"%s" into %s is unknown.' % (index, Settings.repository_info)
)
@classmethod
def parse_file(cls):
"""
Parse the administation file.
"""
if path.isfile(Settings.repository_info):
logging.debug("Getting content of the info.json.")
content = Helpers.File(Settings.repository_info).read()
logging.debug("Content: {}".format(content))
Settings.informations = Helpers.Dict().from_json(content)
to_ignore = ["raw_link", "name"]
for index in Settings.informations:
if Settings.informations[index] != "":
if index not in to_ignore[1:]:
cls.update_settings(index)
elif index in to_ignore:
continue
else:
raise Exception(
'Please complete "%s" into %s'
% (index, Settings.repository_info)
)
logging.debug("Content understood and decoded correctly.")
else:
raise Exception(
"Impossible to read %s" % Settings.current_directory + "info.json"
)
def __call__(self):
"""
Prepare the repository structure.
"""
travis = TravisCI(init_instance=True)
travis.fix_permissions()
PyFunceble.Configuration.update()
PyFunceble.Configuration.install()
self.parse_file()
PyFunceble.install()
PyFunceble.download_complementary_files()
DomainsList.construct()
travis.fix_permissions()
PyFunceble.run()
class Helpers: # pylint: disable=too-few-public-methods
"""
Well thanks to those helpers I wrote :)
"""
class Dict:
"""
Dictionary manipulations.
Argument:
- main_dictionnary: dict
The main_dictionnary to pass to the whole class.
"""
def __init__(self, main_dictionnary=None):
if main_dictionnary is None:
self.main_dictionnary = {}
else:
self.main_dictionnary = main_dictionnary
def to_json(self, destination):
"""
Save a dictionnary into a JSON file.
Argument:
- destination: str
A path to a file where we're going to Write the
converted dict into a JSON format.
"""
with open(destination, "w") as file:
dump(
self.main_dictionnary,
file,
ensure_ascii=False,
indent=4,
sort_keys=True,
)
@classmethod
def from_json(cls, data):
"""
Convert a JSON formated string into a dictionary.
Argument:
data: str
A JSON formeted string to convert to dict format.
"""
try:
return loads(data)
except decoder.JSONDecodeError:
return {}
class List: # pylint: disable=too-few-public-methods
"""
List manipulation.
"""
def __init__(self, main_list=None):
if main_list is None:
self.main_list = []
else:
self.main_list = main_list
def format(self):
"""
Return a well formated list. Basicaly, it's sort a list and remove duplicate.
"""
try:
return sorted(list(set(self.main_list)), key=str.lower)
except TypeError:
return self.main_list
class Directory: # pylint: disable=too-few-public-methods
"""
Directory treatment/manipulations.
Argument:
- directory: str
A path to the directory to manipulate.
"""
def __init__(self, directory):
self.directory = directory
def delete(self):
"""
This method delete the given directory.
"""
try:
rmtree(self.directory)
except FileNotFoundError:
pass
class File: # pylint: disable=too-few-public-methods
"""
File treatment/manipulations.
Argument:
- file: str
- a path to the file to manipulate.
"""
def __init__(self, file):
self.file = file
def read(self):
"""
Read a given file path and return its content.
"""
with open(self.file, "r", encoding="utf-8") as file:
funilrys = file.read()
return funilrys