File: production.py

package info (click to toggle)
python-pyfunceble 4.2.29.dev-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,108 kB
  • sloc: python: 27,413; sh: 142; makefile: 27
file content (484 lines) | stat: -rw-r--r-- 15,708 bytes parent folder | download
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
"""
The tool to check the availability or syntax of domain, IP or URL.

::


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides some of our scripts.

Author:
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:
    https://pyfunceble.github.io/#/special-thanks

Contributors:
    https://pyfunceble.github.io/#/contributors

Project link:
    https://github.com/funilrys/PyFunceble

Project documentation:
    https://docs.pyfunceble.com

Project homepage:
    https://pyfunceble.github.io/

License:
::


    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024 Nissar Chababy

    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

        https://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 copy
import functools
import os
import pathlib
from typing import List, Optional

import PyFunceble.cli.storage
import PyFunceble.facility
import PyFunceble.storage
from PyFunceble.cli.filesystem.dir_structure.backup import DirectoryStructureBackup
from PyFunceble.helpers.dict import DictHelper
from PyFunceble.helpers.file import FileHelper
from PyFunceble.helpers.regex import RegexHelper
from PyFunceble.utils.version import VersionUtility


class ProductionPrep:
    """
    Provides an interface for the production file modification.
    The idea is that we always have 2 branches: the `dev` and the `master`
    branch.

    We want to fix all the URL to point to the right one, so this interface
    just provides everything needed for that job.

    Another important part is the cleanup of the production environment.
    What is meant is the cleanup of the `output/` directory and the
    construction of the dir_structure file.

    .. warning::
        This class assumes that you know what you are doing. Meaning that
        you should run this only if your are developing PyFunceble.
    """

    VERSION_FILE_PATH: str = os.path.join(
        PyFunceble.storage.CONFIG_DIRECTORY,
        PyFunceble.cli.storage.DISTRIBUTED_VERSION_FILENAME,
    )
    AVAILABLE_BRANCHES: List[str] = ["dev", "master"]

    regex_helper: RegexHelper = RegexHelper()
    file_helper: FileHelper = FileHelper()
    dict_helper: DictHelper = DictHelper()
    version_utility: VersionUtility = VersionUtility(PyFunceble.storage.PROJECT_VERSION)

    version_file_content: Optional[dict] = None
    """
    A copy of the local version file.
    """

    _branch: Optional[str] = None

    previous_version: Optional[str] = None
    """
    Provides the previous version (from :code:`version_file_content`)
    """

    def __init__(self, branch: Optional[str] = None) -> None:
        self.version_file_content = self.dict_helper.from_yaml_file(
            self.VERSION_FILE_PATH
        )

        self.previous_version = copy.deepcopy(
            self.version_file_content["current_version"]
        )

        if branch is not None:
            self.branch = branch

    def ensure_branch_is_given(func):  # pylint: disable=no-self-argument
        """
        Ensures that the branch is given before running the decorated method.

        :raise TypeError:
            When the :code:`self.branch` is not set.
        """

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            if not isinstance(self.branch, str):
                raise TypeError(
                    f"<self.branch> should be {str}, " f"{type(self.branch)} given."
                )

            return func(self, *args, **kwargs)  # pylint: disable=not-callable

        return wrapper

    @property
    def branch(self) -> Optional[str]:
        """
        Provides the current state of the :code:`_branch` attribute.
        """

        return self._branch

    @branch.setter
    def branch(self, value: str) -> None:
        """
        Sets the branch to act with.

        :param value:
            The value to set.

        :raise TypeError:
            When the given :code:`value` is not a :py:class:`str`.
        :raise ValueError:
            When the given :code:`value` is empty.
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} given.")

        if not value:
            raise ValueError("<value> should not be empty.")

        self._branch = value

    def set_branch(self, value: str) -> "ProductionPrep":
        """
        Sets the branch to act with.

        :param value:
            The value to set.
        """

        self.branch = value

        return self

    def should_be_deprecated(self, previous_version: str) -> bool:
        """
        Checks if we should deprecates the current version.
        """

        splitted = self.version_utility.get_splitted(previous_version)[0]
        local_splitted = self.version_utility.get_splitted(
            self.version_utility.local_version
        )[0]

        for index, version_part in enumerate(splitted[:2]):
            if int(version_part) < int(local_splitted[index]):
                return True

        return False

    @ensure_branch_is_given
    def update_urls(self, file: str) -> "ProductionPrep":
        """
        Updates the common URLS which are in the given file.

        :param file:
            The file to work with.

        :raise FileNotFoundError:
            When the given :code:`file` is not found.
        """

        if self.branch == "dev":
            regexes = [
                (r"PyFunceble\/%s\/" % "master", "PyFunceble/%s/" % "dev"),
                ("=%s" % "master", "=%s" % "dev"),
                (r"/{1,}en\/%s" % "latest", "/en/%s" % "dev"),
                (r"\/pyfunceble-dev.png", "/pyfunceble-%s.png" % "dev"),
                (r"\/project\/pyfunceble$", "/project/pyfunceble-%s" % "dev"),
                (
                    r"\/badge\/pyfunceble(/month|/week|)$",
                    "/badge/pyfunceble-%s\\1" % "dev",
                ),
                (r"\/blob\/%s\/" % "master", "/blob/%s/" % "dev"),
                (r"\/pypi\/v\/pyfunceble\.png$", "/pypi/v/pyfunceble-%s.png" % "dev"),
                (r"\/(logo|graphmls|gifs\/raw)\/%s\/" % "master", "/\\1/%s/" % "dev"),
                (r"\/(PyFunceble\/tree)\/%s" % "master", "/\\1/%s" % "dev"),
            ]
        elif self.branch == "master":
            regexes = [
                (r"PyFunceble\/%s\/" % "dev", "PyFunceble/%s/" % "master"),
                ("=%s" % "dev", "=%s" % "master"),
                (r"/{1,}en\/%s" % "dev", "/en/%s" % "latest"),
                (r"\/pyfunceble-dev.png", "/pyfunceble-dev.png"),
                (r"\/project\/pyfunceble-%s$" % "dev", "/project/pyfunceble"),
                (
                    r"\/badge\/pyfunceble-%s(/month|/week|)$" % "dev",
                    "/badge/pyfunceble\\1",
                ),
                (r"\/blob\/%s\/" % "dev", "/blob/%s/" % "master"),
                (
                    r"\/pypi\/v\/pyfunceble-%s\.png$" % "dev",
                    "/pypi/v/pyfunceble.png",
                ),
                (r"\/(logo|graphmls|gifs\/raw)\/%s\/" % "dev", "/\\1/%s/" % "master"),
                (r"\/(PyFunceble\/tree)\/%s" % "dev", "/\\1/%s" % "master"),
            ]
        else:
            regexes = {}

        self.file_helper.set_path(file)

        PyFunceble.facility.Logger.info(
            "Started to update our URL into %r", self.file_helper.path
        )

        if not self.file_helper.exists():
            raise FileNotFoundError(self.file_helper.path)

        to_update = self.file_helper.read()

        for regex, replacement in regexes:
            to_update = self.regex_helper.set_regex(regex).replace_match(
                to_update, replacement, multiline=True
            )

        self.file_helper.write(to_update, overwrite=True)

        PyFunceble.facility.Logger.info(
            "Finished to update our URL into %r", self.file_helper.path
        )

        return self

    @staticmethod
    def update_code_format() -> "ProductionPrep":
        """
        Updates the format of the source code using black.
        """

        # pylint: disable=import-outside-toplevel, import-error
        import black
        import isort

        def format_file(file: str, isortconfig: isort.settings.Config) -> None:
            """
            Formats the given file using black.

            :param file:
                The file to format.
            :parm isortconfig:
                The configuration to apply while sorting the imports.
            """

            isort.api.sort_file(pathlib.Path(file), config=isortconfig)

            black.format_file_in_place(
                pathlib.Path(file),
                fast=False,
                mode=black.Mode(),
                write_back=black.WriteBack.YES,
            )

            PyFunceble.facility.Logger.info("Update format of %r", file)

        isort_config = isort.settings.Config(settings_file="setup.cfg")

        files = [
            os.path.join(PyFunceble.storage.CONFIG_DIRECTORY, "setup.py"),
        ]

        for file in files:
            format_file(file, isort_config)

        for root, _, files in os.walk(
            os.path.join(
                PyFunceble.storage.CONFIG_DIRECTORY, PyFunceble.storage.PROJECT_NAME
            )
        ):
            if "__pycache__" in root:
                continue

            for file in files:
                if not file.endswith(".py"):
                    continue

                format_file(os.path.join(root, file), isort_config)

        for root, _, files in os.walk(
            os.path.join(PyFunceble.storage.CONFIG_DIRECTORY, "tests")
        ):
            if "__pycache__" in root:
                continue

            for file in files:
                if not file.endswith(".py"):
                    continue

                format_file(os.path.join(root, file), isort_config)

    def update_code_urls(self) -> "ProductionPrep":
        """
        Updates all URL in the source code.
        """

        to_ignore = [
            ".gitignore",
            ".keep",
        ]

        self.update_urls(os.path.join(PyFunceble.storage.CONFIG_DIRECTORY, "setup.py"))

        for root, _, files in os.walk(
            os.path.join(
                PyFunceble.storage.CONFIG_DIRECTORY, PyFunceble.storage.PROJECT_NAME
            )
        ):
            if "__pycache__" in root:
                continue

            for file in files:
                if file in to_ignore:
                    continue

                self.update_urls(os.path.join(root, file))

        for root, _, files in os.walk(
            os.path.join(PyFunceble.storage.CONFIG_DIRECTORY, "tests")
        ):
            if "__pycache__" in root:
                continue

            for file in files:
                if file in to_ignore:
                    continue

                self.update_urls(os.path.join(root, file))

    @ensure_branch_is_given
    def update_setup_py(self) -> "ProductionPrep":
        """
        Updates content of :code:`setup.py`.

        :raise FileNotFoundError:
            When the :code:`setup.py` file does not exists.
        """

        PyFunceble.facility.Logger.info(
            "Started to update setup.py.",
        )

        if self.branch == "dev":
            regexes = [
                (r'name=".*"', 'name="PyFunceble-dev"'),
                (r'"Development\sStatus\s::.*"', '"Development Status :: 4 - Beta"'),
            ]
        elif self.branch == "master":
            regexes = [
                (r'name=".*"', 'name="PyFunceble"'),
                (
                    r'"Development\sStatus\s::.*"',
                    '"Development Status :: 5 - Production/Stable"',
                ),
            ]
        else:
            regexes = [
                (r'name=".*"', 'name="PyFunceble-dev"'),
                (
                    r'"Development\sStatus\s::.*"',
                    '"Development Status :: 3 - Alpha"',
                ),
            ]

        self.file_helper.set_path(
            os.path.join(PyFunceble.storage.CONFIG_DIRECTORY, "setup.py")
        )

        if not self.file_helper.exists():
            raise FileNotFoundError(self.file_helper.path)

        to_update = self.file_helper.read()

        for regex, replacement in regexes:
            to_update = self.regex_helper.set_regex(regex).replace_match(
                to_update, replacement, multiline=True
            )

        self.file_helper.write(to_update, overwrite=True)

        PyFunceble.facility.Logger.info(
            "Started to update setup.py.",
        )

        return self

    def update_version_file(self) -> "ProductionPrep":
        """
        Updates the version file.
        """

        PyFunceble.facility.Logger.info(
            "Started to update version file.",
        )

        if self.should_be_deprecated(self.previous_version):
            to_append = ".".join(
                self.version_utility.get_splitted(self.version_utility.local_version)[0]
            )

            if to_append not in self.version_file_content["deprecated"]:
                self.version_file_content["deprecated"].append(to_append)

        self.version_file_content["current_version"] = (
            PyFunceble.storage.PROJECT_VERSION
        )

        self.dict_helper.set_subject(self.version_file_content).to_yaml_file(
            self.VERSION_FILE_PATH
        )

        PyFunceble.facility.Logger.info(
            "Finished to update version file.",
        )

        return self

    def update_dir_structure_file(self) -> "ProductionPrep":
        """
        Updates the directory structure.
        """

        DirectoryStructureBackup().start()

        return self

    def start(self) -> "ProductionPrep":
        """
        Starts the production process.
        """

        return (
            self.update_setup_py()
            .update_code_urls()
            .update_code_format()
            .update_version_file()
        )