Add all files from "pathname" to the ZIP archive. If pathname is a package directory, search the directory and all package subdirectories recursively for all *.py and enter the modules into the archive. If pathname is a plain directory, listdir *.py and enter a
(self, pathname, basename="", filterfunc=None)
| 2026 | self._optimize = optimize |
| 2027 | |
| 2028 | def writepy(self, pathname, basename="", filterfunc=None): |
| 2029 | """Add all files from "pathname" to the ZIP archive. |
| 2030 | |
| 2031 | If pathname is a package directory, search the directory and |
| 2032 | all package subdirectories recursively for all *.py and enter |
| 2033 | the modules into the archive. If pathname is a plain |
| 2034 | directory, listdir *.py and enter all modules. Else, pathname |
| 2035 | must be a Python *.py file and the module will be put into the |
| 2036 | archive. Added modules are always module.pyc. |
| 2037 | This method will compile the module.py into module.pyc if |
| 2038 | necessary. |
| 2039 | If filterfunc(pathname) is given, it is called with every argument. |
| 2040 | When it is False, the file or directory is skipped. |
| 2041 | """ |
| 2042 | pathname = os.fspath(pathname) |
| 2043 | if filterfunc and not filterfunc(pathname): |
| 2044 | if self.debug: |
| 2045 | label = 'path' if os.path.isdir(pathname) else 'file' |
| 2046 | print('%s %r skipped by filterfunc' % (label, pathname)) |
| 2047 | return |
| 2048 | dir, name = os.path.split(pathname) |
| 2049 | if os.path.isdir(pathname): |
| 2050 | initname = os.path.join(pathname, "__init__.py") |
| 2051 | if os.path.isfile(initname): |
| 2052 | # This is a package directory, add it |
| 2053 | if basename: |
| 2054 | basename = "%s/%s" % (basename, name) |
| 2055 | else: |
| 2056 | basename = name |
| 2057 | if self.debug: |
| 2058 | print("Adding package in", pathname, "as", basename) |
| 2059 | fname, arcname = self._get_codename(initname[0:-3], basename) |
| 2060 | if self.debug: |
| 2061 | print("Adding", arcname) |
| 2062 | self.write(fname, arcname) |
| 2063 | dirlist = sorted(os.listdir(pathname)) |
| 2064 | dirlist.remove("__init__.py") |
| 2065 | # Add all *.py files and package subdirectories |
| 2066 | for filename in dirlist: |
| 2067 | path = os.path.join(pathname, filename) |
| 2068 | root, ext = os.path.splitext(filename) |
| 2069 | if os.path.isdir(path): |
| 2070 | if os.path.isfile(os.path.join(path, "__init__.py")): |
| 2071 | # This is a package directory, add it |
| 2072 | self.writepy(path, basename, |
| 2073 | filterfunc=filterfunc) # Recursive call |
| 2074 | elif ext == ".py": |
| 2075 | if filterfunc and not filterfunc(path): |
| 2076 | if self.debug: |
| 2077 | print('file %r skipped by filterfunc' % path) |
| 2078 | continue |
| 2079 | fname, arcname = self._get_codename(path[0:-3], |
| 2080 | basename) |
| 2081 | if self.debug: |
| 2082 | print("Adding", arcname) |
| 2083 | self.write(fname, arcname) |
| 2084 | else: |
| 2085 | # This is NOT a package directory, add its files at top level |