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 all mo
(self, pathname, basename="", filterfunc=None)
| 2193 | self._optimize = optimize |
| 2194 | |
| 2195 | def writepy(self, pathname, basename="", filterfunc=None): |
| 2196 | """Add all files from "pathname" to the ZIP archive. |
| 2197 | |
| 2198 | If pathname is a package directory, search the directory and |
| 2199 | all package subdirectories recursively for all *.py and enter |
| 2200 | the modules into the archive. If pathname is a plain |
| 2201 | directory, listdir *.py and enter all modules. Else, pathname |
| 2202 | must be a Python *.py file and the module will be put into the |
| 2203 | archive. Added modules are always module.pyc. |
| 2204 | This method will compile the module.py into module.pyc if |
| 2205 | necessary. |
| 2206 | If filterfunc(pathname) is given, it is called with every argument. |
| 2207 | When it is False, the file or directory is skipped. |
| 2208 | """ |
| 2209 | pathname = os.fspath(pathname) |
| 2210 | if filterfunc and not filterfunc(pathname): |
| 2211 | if self.debug: |
| 2212 | label = 'path' if os.path.isdir(pathname) else 'file' |
| 2213 | print('%s %r skipped by filterfunc' % (label, pathname)) |
| 2214 | return |
| 2215 | dir, name = os.path.split(pathname) |
| 2216 | if os.path.isdir(pathname): |
| 2217 | initname = os.path.join(pathname, "__init__.py") |
| 2218 | if os.path.isfile(initname): |
| 2219 | # This is a package directory, add it |
| 2220 | if basename: |
| 2221 | basename = "%s/%s" % (basename, name) |
| 2222 | else: |
| 2223 | basename = name |
| 2224 | if self.debug: |
| 2225 | print("Adding package in", pathname, "as", basename) |
| 2226 | arcname, bytecode = self._get_code(initname[0:-3], basename) |
| 2227 | if self.debug: |
| 2228 | print("Adding", arcname) |
| 2229 | self.writestr(arcname, bytecode) |
| 2230 | dirlist = sorted(os.listdir(pathname)) |
| 2231 | dirlist.remove("__init__.py") |
| 2232 | # Add all *.py files and package subdirectories |
| 2233 | for filename in dirlist: |
| 2234 | path = os.path.join(pathname, filename) |
| 2235 | root, ext = os.path.splitext(filename) |
| 2236 | if os.path.isdir(path): |
| 2237 | if os.path.isfile(os.path.join(path, "__init__.py")): |
| 2238 | # This is a package directory, add it |
| 2239 | self.writepy(path, basename, |
| 2240 | filterfunc=filterfunc) # Recursive call |
| 2241 | elif ext == ".py": |
| 2242 | if filterfunc and not filterfunc(path): |
| 2243 | if self.debug: |
| 2244 | print('file %r skipped by filterfunc' % path) |
| 2245 | continue |
| 2246 | arcname, bytecode = self._get_code(path[0:-3], basename) |
| 2247 | if self.debug: |
| 2248 | print("Adding", arcname) |
| 2249 | self.writestr(arcname, bytecode) |
| 2250 | else: |
| 2251 | # This is NOT a package directory, add its files at top level |
| 2252 | if self.debug: |