[Reproducible-commits] [diffoscope] 01/02: Rework how diffoscope main() is run

Jérémy Bobbio lunar at moszumanska.debian.org
Wed Sep 2 10:31:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository diffoscope.

commit 30135782c1ad924418b7e2546410da41746db09e
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Wed Sep 2 09:58:59 2015 +0000

    Rework how diffoscope main() is run
    
    What used to be in diffoscope.py has now moved to diffoscope.__main__. This
    will allow easier testing of main(). We now leave to setuptools to create
    the glue script that will run the main function when `diffoscope` is called.
    
    diffoscope can also be run by specifying `python -m diffoscope`.
    
    For development purposes, `bin/diffoscope` should now be used instead of
    `./diffoscope.py`. The executable name now stays the same in development
    and production. This little script will mangle the import path before
    calling the main function.
    
    This was inspired by Chris Warrick's blog post:
    https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
    and Yaroslav Halchenko's suggestions on the bug report.
    
    Closes: #796196
---
 README                                  |  4 ++--
 bin/diffoscope                          | 31 +++++++++++++++++++++++++++++++
 debian/rules                            |  4 ----
 diffoscope.py => diffoscope/__main__.py | 11 ++++++++---
 setup.py                                |  6 +++++-
 5 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/README b/README
index c08d152..d71adae 100644
--- a/README
+++ b/README
@@ -24,7 +24,7 @@ debbindiff.
 Example
 -------
 
-    $ ./diffoscope.py --html output.html build1.changes build2.changes
+    $ bin/diffoscope.py --html output.html build1.changes build2.changes
 
 This will compare `build1.changes` and `build2.changes` and create
 `output.html` if there are differences between the two files.
@@ -35,7 +35,7 @@ External dependencies
 The various comparators rely on external commands being available. To
 get a list of them, please run:
 
-    $ ./diffoscope.py --list-tools
+    $ bin/diffoscope.py --list-tools
 
 Contributors
 ------------
diff --git a/bin/diffoscope b/bin/diffoscope
new file mode 100755
index 0000000..1ac89a8
--- /dev/null
+++ b/bin/diffoscope
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2014-2015 Jérémy Bobbio <lunar at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+import os.path
+import sys
+for root in ['.', '..']:
+    if os.path.exists(os.path.join(root, 'diffoscope/__init__.py')):
+        sys.path = [root] + sys.path
+        break
+
+from diffoscope.__main__ import main
+
+if __name__ == '__main__':
+    main()
diff --git a/debian/rules b/debian/rules
index 78ea5ce..0c5ec78 100755
--- a/debian/rules
+++ b/debian/rules
@@ -17,10 +17,6 @@ override_dh_gencontrol:
 		sed -e 's/\(^\| \)\(coreutils\|diffutils\|e2fsprogs\|findutils\|gzip\)\(,\|$$\)//g')" >> debian/diffoscope.substvars
 	dh_gencontrol -O--buildsystem=pybuild
 
-override_dh_install:
-	dh_install -O--buildsystem=pybuild
-	mv debian/diffoscope/usr/bin/diffoscope.py debian/diffoscope/usr/bin/diffoscope
-
 debian/diffoscope.1: debian/diffoscope.1.rst
 	rst2man $< $@
 
diff --git a/diffoscope.py b/diffoscope/__main__.py
old mode 100755
new mode 100644
similarity index 97%
rename from diffoscope.py
rename to diffoscope/__main__.py
index bcf935f..262f436
--- a/diffoscope.py
+++ b/diffoscope/__main__.py
@@ -91,7 +91,7 @@ class ListToolsAction(argparse.Action):
         sys.exit(0)
 
 
-def main():
+def run_diffoscope():
     Config.general.max_diff_block_lines = parsed_args.max_diff_block_lines
     Config.general.max_diff_input_lines = parsed_args.max_diff_input_lines
     Config.general.max_report_size = parsed_args.max_report_size
@@ -110,10 +110,12 @@ def main():
         return 1
     return 0
 
-if __name__ == '__main__':
+def main(args=None):
+    if args is None:
+        args = sys.argv[1:]
     try:
         parser = create_parser()
-        parsed_args = parser.parse_args(sys.argv[1:])
+        parsed_args = parser.parse_args(args)
         sys.exit(main())
     except KeyboardInterrupt:
         logger.info('Keyboard Interrupt')
@@ -124,3 +126,6 @@ if __name__ == '__main__':
             import pdb
             pdb.post_mortem()
         sys.exit(2)
+
+if __name__ == '__main__':
+    main()
diff --git a/setup.py b/setup.py
index 6c805fe..f394c6c 100644
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,11 @@ setup(name='diffoscope',
       packages=find_packages(),
       tests_require=['pytest'],
       cmdclass = {'test': PyTest},
-      scripts=['diffoscope.py'],
+      entry_points={
+          'console_scripts': [
+              'diffoscope=diffoscope.__main__:main'
+              ],
+          },
       install_requires=[
           'python-debian',
           'magic',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list