[Python-checkins] distutils2: added an automatic mode to fill [files]

tarek.ziade python-checkins at python.org
Sun Oct 24 03:48:04 CEST 2010


tarek.ziade pushed 4decfdbd2ed9 to distutils2:

http://hg.python.org/distutils2/rev/4decfdbd2ed9
changeset:   783:4decfdbd2ed9
tag:         tip
user:        Tarek Ziade <tarek at ziade.org>
date:        Sun Oct 24 03:47:49 2010 +0200
summary:     added an automatic mode to fill [files]
files:       distutils2/mkcfg.py, distutils2/tests/test_mkcfg.py

diff --git a/distutils2/mkcfg.py b/distutils2/mkcfg.py
--- a/distutils2/mkcfg.py
+++ b/distutils2/mkcfg.py
@@ -242,24 +242,95 @@
               self.data.get('home_page'), _helptext['home_page'],
               required=False)
 
-        while ask_yn('Do you want to add a single module ?'
-                     ' (you will be able to add full packages next)',
-                  helptext=_helptext['modules']) == 'y':
-            self._set_multi('Module name', 'modules')
+        if ask_yn('Do you want me to automatically build the file list '
+              'with everything I can find in the current directory ? '
+              'If you say no, you will have to define them manually.') == 'y':
+            self._find_files()
+        else:
+            while ask_yn('Do you want to add a single module ?'
+                        ' (you will be able to add full packages next)',
+                    helptext=_helptext['modules']) == 'y':
+                self._set_multi('Module name', 'modules')
 
-        while ask_yn('Do you want to add a package ?',
-                  helptext=_helptext['packages']) == 'y':
-            self._set_multi('Package name', 'packages')
+            while ask_yn('Do you want to add a package ?',
+                    helptext=_helptext['packages']) == 'y':
+                self._set_multi('Package name', 'packages')
 
-        while ask_yn('Do you want to add an extra file ?',
-                     helptext=_helptext['extra_files']) == 'y':
-            self._set_multi('Extra file/dir name', 'extra_files')
+            while ask_yn('Do you want to add an extra file ?',
+                        helptext=_helptext['extra_files']) == 'y':
+                self._set_multi('Extra file/dir name', 'extra_files')
 
 
         if ask_yn('Do you want to set Trove classifiers?',
                   helptext=_helptext['do_classifier']) == 'y':
             self.set_classifier()
 
+    def _find_files(self):
+        # we are looking for python modules and packages,
+        # other stuff are added as regular files
+        pkgs = self.data['packages']
+        modules = self.data['modules']
+        extra_files = self.data['extra_files']
+
+        def is_package(path):
+            return os.path.exists(os.path.join(path, '__init__.py'))
+
+        curdir = os.getcwd()
+        scanned = []
+        _pref = ['lib', 'include', 'dist', 'build', '.', '~']
+        _suf = ['.pyc']
+
+
+        def to_skip(path):
+            path = relative(path)
+
+            if any([path.startswith(pref) for pref in _pref]):
+                return True
+
+            if any([path.endswith(suf) for suf in _suf]):
+                return True
+            return False
+
+        def relative(path):
+            return path[len(curdir) + 1:]
+
+        def dotted(path):
+            res = relative(path).replace(os.path.sep, '.')
+            if res.endswith('.py'):
+                res = res[:-len('.py')]
+            return res
+
+        # first pass : packages
+        for root, dirs, files in os.walk(curdir):
+            if to_skip(root):
+                continue
+            for dir_ in dirs:
+                if to_skip(dir_):
+                    continue
+                fullpath = os.path.join(root, dir_)
+                dotted_name = dotted(fullpath)
+                if is_package(fullpath) and dotted_name not in pkgs:
+                    pkgs.append(dotted_name)
+                    scanned.append(fullpath)
+
+        # modules and extra files
+        for root, dirs, files in os.walk(curdir):
+            if to_skip(root):
+                continue
+
+            if any([root.startswith(path) for path in scanned]):
+                continue
+
+            for file in files:
+                fullpath = os.path.join(root, file)
+                if to_skip(fullpath):
+                    continue
+                # single module ?
+                if os.path.splitext(file)[-1] == '.py':
+                    modules.append(dotted(fullpath))
+                else:
+                    extra_files.append(relative(fullpath))
+
     def _set_multi(self, question, name):
         existing_values = self.data[name]
         value = ask(question, helptext=_helptext[name]).strip()
diff --git a/distutils2/tests/test_mkcfg.py b/distutils2/tests/test_mkcfg.py
new file mode 100644
--- /dev/null
+++ b/distutils2/tests/test_mkcfg.py
@@ -0,0 +1,47 @@
+"""Tests for distutils.mkcfg."""
+import os
+from distutils2.tests import run_unittest, support, unittest
+from distutils2.mkcfg import MainProgram
+
+
+class MkcfgTestCase(support.TempdirManager,
+                    unittest.TestCase):
+
+    def test_find_files(self):
+        # making sure we scan a project dir correctly
+        main = MainProgram()
+
+        # building the structure
+        tempdir = self.mkdtemp()
+        dirs = ['pkg1', 'data', 'pkg2', 'pkg2/sub']
+        files = ['README', 'setup.cfg', 'foo.py',
+                 'pkg1/__init__.py', 'pkg1/bar.py',
+                 'data/data1', 'pkg2/__init__.py',
+                 'pkg2/sub/__init__.py']
+
+        for dir_ in dirs:
+            os.mkdir(os.path.join(tempdir, dir_))
+
+        for file_ in files:
+            path = os.path.join(tempdir, file_)
+            self.write_file(path, 'xxx')
+
+        old_dir = os.getcwd()
+        os.chdir(tempdir)
+        try:
+            main._find_files()
+        finally:
+            os.chdir(old_dir)
+
+        # do we have what we want ?
+        self.assertEqual(main.data['packages'], ['pkg1', 'pkg2', 'pkg2.sub'])
+        self.assertEqual(main.data['modules'], ['foo'])
+        self.assertEqual(main.data['extra_files'],
+                         ['setup.cfg', 'README', 'data/data1'])
+
+
+def test_suite():
+    return unittest.makeSuite(MkcfgTestCase)
+
+if __name__ == '__main__':
+    run_unittest(test_suite())

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list