jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/913914 )

Change subject: [bugfix] Take into account that a repository clone may have no 
tags
......................................................................

[bugfix] Take into account that a repository clone may have no tags

Pywikibot repository may be cloned without depth and may have no tags.
This patch solves the IndexError which is raised in such case. This is
necessary for CI tests.

Call sys.exit(1) if an error occurs to signal CI tools to fail the test.
Also update coverage and use f-strings

Bug: T335676
Change-Id: I9d8b2990e286772efe4827c2a11fee50af598308
---
M setup.py
M make_dist.py
2 files changed, 62 insertions(+), 39 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/make_dist.py b/make_dist.py
index 1fca5b1..0453ba1 100755
--- a/make_dist.py
+++ b/make_dist.py
@@ -98,9 +98,12 @@
     def cleanup(self) -> None:
         """Cleanup copied files."""

-    def run(self) -> None:  # pragma: no cover
-        """Run the installer script."""
-        if self.upgrade:
+    def run(self) -> bool:
+        """Run the installer script.
+
+        :return: True if no error occurs, else False
+        """
+        if self.upgrade:  # pragma: no cover
             check_call('python -m pip install --upgrade pip', shell=True)
             check_call(
                 'pip install --upgrade setuptools wheel twine ', shell=True)
@@ -108,20 +111,20 @@
         if self.local or self.remote or self.clear:
             self.clear_old_dist()
             if self.clear:
-                return
+                return True  # pragma: no cover

         self.copy_files()
         try:
             setup.main()  # create a new package
-        except SystemExit as e:
+        except SystemExit as e:  # pragma: no cover
             error(e)
-            return
+            return False
         finally:
             self.cleanup()

         # check description
         if run('twine check dist/*', shell=True).returncode:
-            return
+            return False  # pragma: no cover

         if self.local:
             check_call('pip uninstall pywikibot -y', shell=True)
@@ -131,7 +134,8 @@

         if self.remote and input_yn(
                 '<<lightblue>>Upload dist to pypi', automatic_quit=False):
-            check_call('twine upload dist/*', shell=True)
+            check_call('twine upload dist/*', shell=True)  # pragma: no cover
+        return True


 class SetupPywikibot(SetupBase):
@@ -149,7 +153,7 @@
         self.target = target
         self.source = source

-    def copy_files(self) -> None:  # pragma: no cover
+    def copy_files(self) -> None:
         """Copy i18n files to pywikibot.scripts folder.

         Pywikibot i18n files are used for some translations. They are copied
@@ -162,7 +166,7 @@
         shutil.copytree(self.source, self.target)
         info('done')

-    def cleanup(self) -> None:  # pragma: no cover
+    def cleanup(self) -> None:
         """Remove all copied files from pywikibot scripts folder."""
         info('Remove copied files... ', newline=False)
         shutil.rmtree(self.target)
@@ -182,7 +186,7 @@
     :return: Return whether dist is to be installed locally or to be
         uploaded
     """
-    if '-help' in sys.argv:  # pragma: no cover
+    if '-help' in sys.argv:
         info(__doc__)
         info(setup.__doc__)
         sys.exit()
@@ -192,7 +196,7 @@
     clear = '-clear' in sys.argv
     upgrade = '-upgrade' in sys.argv

-    if remote and 'dev' in __version__:
+    if remote and 'dev' in __version__:  # pragma: no cover
         warning('Distribution must not be a developmental release to upload.')
         remote = False

@@ -200,11 +204,12 @@
     return local, remote, clear, upgrade


-def main() -> None:  # pragma: no cover
+def main() -> None:
     """Script entry point."""
     args = handle_args()
-    SetupPywikibot(*args).run()
+    return SetupPywikibot(*args).run()


-if __name__ == '__main__':  # pragma: no cover
-    main()
+if __name__ == '__main__':
+    if not main():
+        sys.exit(1)  # pragma: no cover
diff --git a/setup.py b/setup.py
index 8ac1364..de00fc5 100755
--- a/setup.py
+++ b/setup.py
@@ -120,7 +120,7 @@
 assert metadata.__name__ == name


-def get_validated_version() -> str:  # pragma: no cover
+def get_validated_version() -> str:
     """Get a validated pywikibot module version string.

     The version number from pywikibot.__metadata__.__version__ is used.
@@ -134,7 +134,7 @@
     """
     version = metadata.__version__
     if 'sdist' not in sys.argv:
-        return version
+        return version  # pragma: no cover

     # validate version for sdist
     from contextlib import suppress
@@ -144,33 +144,34 @@
     try:
         tags = run(['git', 'tag'], check=True, stdout=PIPE,
                    universal_newlines=True).stdout.splitlines()
-    except Exception as e:
+    except Exception as e:  # pragma: no cover
         print(e)
         sys.exit('Creating source distribution canceled.')

-    for tag in ('stable', 'python2'):
-        with suppress(ValueError):
-            tags.remove(tag)
+    last_tag = None
+    if tags:  # pragma: no cover
+        for tag in ('stable', 'python2'):
+            with suppress(ValueError):
+                tags.remove(tag)

-    last_tag = tags[-1]
+        last_tag = tags[-1]

     warnings = []
-    if parse_version(version) < parse_version('0'):
+    if parse_version(version) < parse_version('0'):  # pragma: no cover
         # any version which is not a valid PEP 440 version will be considered
         # less than any valid PEP 440 version
         warnings.append(
             version + ' is not a valid version string following PEP 440.')
-    elif safe_version(version) != version:
-        warnings.append(
-            '{} does not follow PEP 440. Use {} as version string instead.'
-            .format(version, safe_version(version)))
+    elif safe_version(version) != version:  # pragma: no cover
+        warnings.append(f'{version} does not follow PEP 440. Use '
+                        f'{safe_version(version)} as version string instead.')

-    if parse_version(version) <= parse_version(last_tag):
-        warnings.append(
-            'New version "{}" is not higher than last version "{}".'
-            .format(version, last_tag))
+    if last_tag and parse_version(version) <= parse_version(last_tag):
+        warnings.append(  # pragma: no cover
+            f'New version {version!r} is not higher than last version '
+            f'{last_tag!r}.')

-    if warnings:
+    if warnings:  # pragma: no cover
         print(__doc__)
         print('\n\n'.join(warnings))
         sys.exit('\nBuild of distribution package canceled.')
@@ -178,7 +179,7 @@
     return version


-def read_desc(filename) -> str:  # pragma: no cover
+def read_desc(filename) -> str:
     """Read long description.

     Combine included restructured text files which must be done before
@@ -193,14 +194,14 @@
                 if os.path.exists(include):
                     with open(include) as g:
                         desc.append(re.sub(pattern[0], pattern[1], g.read()))
-                else:
-                    print('Cannot include {}; file not found'.format(include))
+                else:  # pragma: no cover
+                    print(f'Cannot include {include}; file not found')
             else:
                 desc.append(re.sub(pattern[0], pattern[1], line))
     return ''.join(desc)


-def get_packages(name) -> List[str]:  # pragma: no cover
+def get_packages(name) -> List[str]:
     """Find framework packages."""
     try:
         from setuptools import find_namespace_packages
@@ -211,7 +212,7 @@
     return [str(name)] + packages


-def main() -> None:  # pragma: no cover
+def main() -> None:
     """Setup entry point."""
     version = get_validated_version()
     setup(
@@ -350,7 +351,7 @@

     # Finally show distribution version before uploading
     if 'sdist' in sys.argv:
-        print('\nDistribution package created for version {}'.format(version))
+        print(f'\nDistribution package created for version {version}')


 if __name__ == '__main__':  # pragma: no cover

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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I9d8b2990e286772efe4827c2a11fee50af598308
Gerrit-Change-Number: 913914
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to