def get_metadata(eggbasket, package, filename): package_file = Path(get_package_path(eggbasket), package, filename) try: return SDist(package_file) except Exception: return None
def _download_all(self, dir): reqs = list(self.reqs) already_downloaded = set() while reqs: r = reqs.pop() # Hack for virtualenvs if r.name.lower == 'python': continue if any(r.name == rq.name for rq in already_downloaded): logger.debug('debug: Already downloaded: {0}', r) continue if any(r == rq for rq in self.exclude): logger.info('Excluding {0}', r) continue logger.indent = 0 logger.info('{0}:', r) logger.indent = 8 try: dist = SDist(self._download(dir, r)) self.callback(r, dist) except ConfigParser.MissingSectionHeaderError: continue try: logger.info('Looking for {0} dependencies', r) logger.indent += 8 found = False try: requirements = dist.file('requires.txt') except KeyError: requirements = [] for requirement in requirements: rq = Requirement(requirement) if rq not in already_downloaded: logger.info('Found: {0}', requirement) reqs.append(rq) found = True if not found: logger.info('None found') except KeyError: logger.debug('debug: requires.txt not found for {0}', dist) try: as_req = dist.as_req except KeyError: as_req = str(r) already_downloaded.add(Requirement(as_req)) logger.indent = 0 logger.success('Finished processing dependencies')
def get_package_files(eggbasket, package): package_path = get_package_path(eggbasket) package = package_path.listdir()[[x.name.lower() for x in package_path.listdir()].index(package.lower())] package_dir = Path(package_path, package) files = [] for package_file in package_dir.listdir(): p = Package() p.filepath = package_file p.eggbasket = eggbasket try: p.name = SDist(package_file).name p.author = SDist(package_file).metadata['PKG-INFO']['Author'] except Exception: pass files.append(p) return sorted(files, reverse=True)
def get_packages(eggbasket): packages = [] folders = [Path(folder) for folder in get_package_path(eggbasket).listdir() if folder.isdir()]# and folder.listdir() != []] for package in folders: p = Package() p.filepath = package p.current = get_current_package(eggbasket, package) p.eggbasket = eggbasket try: p.author = SDist(p.current).metadata['PKG-INFO']['Author'] except Exception: pass packages.append(p) return packages
def bundle(self, include_manifest=True, build_dir=True, additional_files=[]): ''' Create a bundle of the specified package: 1. Download all required packages (included dependencies) 2. Clean the build directory 3. Collect the packages in a single zip file (bundle) 4. Add the manifest file 5. Move the bundle from the build dir to the destination ''' def _add_to_archive(zfile, dir): for file in os.listdir(dir): path = os.path.join(dir, file) if os.path.isfile(path): zfile.write(path, os.path.join(dir, file)[len(tempdir):]) elif os.path.isdir(path): _add_to_archive(zfile, path) with TempDir() as tempdir, TempDir() as bundle_dir: ## Step 1: we create the `build` directory ## If you choose to create a bundle without the build directory, ## be aware that your bundle will not be compatible with Pip. ##### if build_dir: build = os.path.join(tempdir, 'build') os.mkdir(build) else: build = tempdir tmp_bundle = os.path.join(bundle_dir, self.bundle_name) ## Step 2: we *recursively* download all required packages ##### reqs = list(self.reqs) already_downloaded = set() while reqs: r = reqs.pop() if any(rq.name == r.name and rq.match(r.version) for rq in self.exclude): logger.info('Excluding {0}', r) continue logger.indent = 0 logger.info('{0}:', r) logger.indent = 8 try: dist = SDist(self._download(build, r)) except ConfigParser.MissingSectionHeaderError: continue try: logger.info('Looking for {0} dependencies', r) logger.indent += 8 for requirement in dist.file('requires.txt'): if requirement not in already_downloaded: logger.info('Found: {0}', requirement) reqs.append(Requirement(requirement)) except KeyError: logger.debug('debug: requires.txt not found for {0}', dist) try: as_req = dist.as_req except KeyError: as_req = str(r) already_downloaded.add(as_req) logger.indent = 0 logger.success('Finished processing dependencies') ## Step 3: we remove all files in the build directory, so we make sure ## that when we collect packages we collect only dirs ##### self._clean(build) ## Step 4: we collect the downloaded packages and bundle all together ## in a single file (zipped) ##### logger.info('Adding packages to the bundle') bundle = zipfile.ZipFile(tmp_bundle, mode='w') _add_to_archive(bundle, build) ## Step 4: add the manifest file if include_manifest: logger.info('Adding the manifest file') bundle.writestr('pyg-manifest.txt', Bundler.MANIFEST.format('\n'.join(self.bundled))) # Additional files to add for path in additional_files: try: _add_to_archive(bundle, path) except (IOError, OSError): logger.debug('debug: Error while adding an additional file: {0}', path) bundle.close() ## Last step: move the bundle to the current working directory dest = os.path.join(os.getcwd(), self.bundle_name) if os.path.exists(dest): logger.debug('debug: dest already exists, removing it') os.remove(dest) shutil.move(tmp_bundle, os.getcwd())