Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1306050?usp=email )

Change subject: make_dist: use pyclean to cleanup the repository
......................................................................

make_dist: use pyclean to cleanup the repository

Also simplify the run installer method.

Change-Id: I0464b1c5604d4441d0eba0d1d2f9f6c8d5501062
---
M make_dist.py
M tests/make_dist_tests.py
2 files changed, 45 insertions(+), 27 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/make_dist.py b/make_dist.py
index e4cc08d..00fd21d 100755
--- a/make_dist.py
+++ b/make_dist.py
@@ -30,7 +30,7 @@

 Usage::

-    [pwb] make_dist [repo] [options]
+    python -B -m [pwb] make_dist [repo] [options]

 .. version-added:: 7.3
 .. version-changed:: 7.4
@@ -64,13 +64,14 @@
 import sys
 from contextlib import suppress
 from dataclasses import dataclass, field
-from importlib import import_module
+from importlib.util import find_spec
 from pathlib import Path
 from subprocess import check_call, run

 from pywikibot import __version__, error, info, input_yn, warning


+MODULE, COMMAND = range(2)
 pip = f'{sys.executable} -m pip'


@@ -103,13 +104,11 @@
         """Delete old dist folders.

         .. version-added:: 7.5
+        .. version-changed:: 11.5
+           Use pyclean for cleanup.
         """
         info('<<lightyellow>>Removing old dist folders... ', newline=False)
-        shutil.rmtree(self.folder / 'build', ignore_errors=True)
-        shutil.rmtree(self.folder / 'dist', ignore_errors=True)
-        shutil.rmtree(self.folder / 'pywikibot.egg-info', ignore_errors=True)
-        shutil.rmtree(self.folder / 'pywikibot_scripts.egg-info',
-                      ignore_errors=True)
+        check_call('pyclean . -v --debris')
         info('<<lightyellow>>done')

     @abc.abstractmethod
@@ -120,34 +119,51 @@
     def cleanup(self) -> None:
         """Cleanup copied files."""

+    @staticmethod
+    def _check_module(module: str, module_type: int) -> bool:
+        """Return whether a module or CLI command is available.
+
+        .. version-added: 11.5
+
+        :param module: Module or command name.
+        :param module_type: Type of module, either MODULE or COMMAND.
+        :return: Whether the module or command is available.
+        :raises ValueError: Invalid module type.
+        """
+        if module_type == MODULE:
+            return find_spec(module) is not None
+        if module_type == COMMAND:
+            return shutil.which(module) is not None
+
+        raise ValueError(f'Invalid module type: {module_type}')
+
     def run(self) -> bool:
         """Run the installer script.

         :return: True if no error occurs, else False
         """
+        tools = (
+            ('build', MODULE),
+            ('pyclean', COMMAND),
+            ('twine', MODULE),
+        )
+        for tool, tool_type in tools:
+            if not self._check_module(tool, tool_type):
+                if not self.upgrade:
+                    error(f'<<lightred>>{tool} not found')
+                    info('<<lightblue>>You may use -upgrade option to install')
+                    return False
+
+                info(f'<<lightyellow>>Install or upgrade {tool}')
+                check_call(f'{pip} install {tool}', shell=True)
+            elif self.upgrade:
+                check_call(f'{pip} install --upgrade {tool}', shell=True)
+
         if self.local or self.remote or self.clear:
             self.clear_old_dist()
             if self.clear:
                 return True

-        if self.upgrade:  # pragma: no cover
-            check_call(f'{pip} install --upgrade pip', shell=True)
-            for module in ('build', 'twine'):
-                info(f'<<lightyellow>>Install or upgrade {module}')
-                try:
-                    import_module(module)
-                except ModuleNotFoundError:
-                    check_call(f'{pip} install {module}', shell=True)
-                else:
-                    check_call(f'{pip} install --upgrade {module}', shell=True)
-        else:
-            for module in ('build', 'twine'):
-                try:
-                    import_module(module)
-                except ModuleNotFoundError as e:
-                    error(f'<<lightred>>{e}')
-                    info('<<lightblue>>You may use -upgrade option to install')
-                    return False
         return self.build()  # pragma: no cover

     def build(self) -> bool:  # pragma: no cover
diff --git a/tests/make_dist_tests.py b/tests/make_dist_tests.py
index 5008f6f..5b70779 100755
--- a/tests/make_dist_tests.py
+++ b/tests/make_dist_tests.py
@@ -9,6 +9,7 @@

 import sys
 import unittest
+from unittest.mock import patch

 import make_dist
 from pywikibot import __version__
@@ -54,11 +55,12 @@
         else:
             self.assertEqual(msg, '')

-    def test_main(self) -> None:
+    @patch.object(make_dist.SetupBase, '_check_module', return_value=False)
+    def test_main(self, mock) -> None:
         """Test main result."""
         saved_argv = sys.argv
         sys.argv = [*saved_argv, '-clear']
-        self.assertTrue(make_dist.main())
+        self.assertFalse(make_dist.main())

         try:
             import build  # noqa: autoflake

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1306050?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I0464b1c5604d4441d0eba0d1d2f9f6c8d5501062
Gerrit-Change-Number: 1306050
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to