def init(parsed, unknown): root = os.path.normpath(os.path.realpath(parsed.name)) name = os.path.basename(root) if os.path.exists(root): print(ansi.info() + ' converting existing directory to a Lore App') else: print(ansi.info() + ' creating new Lore App!') if parsed.bare: if not os.path.exists(root): os.makedirs(root) else: template = os.path.join(os.path.dirname(__file__), 'template', 'init') if os.path.exists(root): sys.exit( ansi.error() + ' "' + parsed.name + '" already exists in this directoy! Add --bare to avoid clobbering existing files.' ) shutil.copytree(template, root, symlinks=False, ignore=None) shutil.move(os.path.join(root, 'app'), os.path.join(root, name)) os.chdir(root) with open('requirements.txt', 'a+') as file: file.seek(0) lines = file.readlines() if next((line for line in lines if re.match(r'^lore[!<>=]', line)), None) is None: file.write('lore' + os.linesep) python_version = parsed.python_version or lore.env.read_version( 'runtime.txt') or '3.6.6' open('runtime.txt', 'w').write('python-' + python_version + '\n') module = os.path.join(root, name, '__init__.py') if not os.path.exists(os.path.dirname(module)): os.makedirs(os.path.dirname(module)) with open(module, 'a+') as file: file.seek(0) lines = file.readlines() if next((line for line in lines if re.match(r'\bimport lore\b', line)), None) is None: file.write('import lore' + os.linesep) lore.env.reload(lore.env) install(parsed, unknown)
def require(packages): """Ensures that a pypi package has been installed into the App's python environment. If not, the package will be installed and your env will be rebooted. Example: :: lore.env.require('pandas') # -> pandas is required. Dependencies added to requirements.txt :param packages: requirements.txt style name and versions of packages :type packages: [unicode] """ global INSTALLED_PACKAGES, _new_requirements if _new_requirements: INSTALLED_PACKAGES = None set_installed_packages() if not INSTALLED_PACKAGES: return if not isinstance(packages, list): packages = [packages] missing = [] for package in packages: name = re.split(r'[!<>=]', package)[0].lower() if name not in INSTALLED_PACKAGES: print(ansi.info() + ' %s is required.' % package) missing += [package] if missing: mode = 'a' if os.path.exists(REQUIREMENTS) else 'w' with open(REQUIREMENTS, mode) as requirements: requirements.write('\n' + '\n'.join(missing) + '\n') print(ansi.info() + ' Dependencies added to requirements.txt. Rebooting.') _new_requirements = True import lore.__main__ lore.__main__.install(None, None) reboot('--env-checked')
def freeze_requirements(): source = env.requirements print( ansi.success('EXECUTE') + ' ' + env.bin_python + ' -m pip freeze -r ' + source) vcs = split_vcs_lines() pip = subprocess.Popen( (env.bin_python, '-m', 'pip', 'freeze', '-r', source), stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = pip.communicate() pip.wait() restore_vcs_lines(vcs) present = stdout.decode('utf-8').split(os.linesep) errors = stderr.decode('utf-8').split(os.linesep) missing = [line for line in errors if 'package is not installed' in line] regex = re.compile(r'contains ([\w\-\_]+)') needed = [ m.group(1).lower() for l in missing for m in [regex.search(l)] if m ] added_index = present.index( '## The following requirements were added by pip freeze:') unsafe = None if added_index: added = present[added_index + 1:-1] present = set(present[0:added_index]) safe = set() unsafe = set() for package in added: name = package.split('==')[0] for bad in vcs: if name in bad: unsafe.add(package) continue if name.lower() in needed: needed.remove(name.lower()) safe.add(package) present |= safe present -= unsafe if needed: args = [env.bin_python, '-m', 'pip', 'install'] + needed print(ansi.success('EXECUTE ') + ' '.join(args)) subprocess.check_call(args) return freeze_requirements() if unsafe: if vcs: print(ansi.warning() + ' Non pypi packages were detected in your ' + ansi.underline('requirements.txt') + ' that can not be ' 'completely frozen by pip. ' + os.linesep + os.linesep + os.linesep.join(vcs)) print(ansi.info() + ' You should check the following packages in to ' + ansi.underline('requirements.txt') + ' or `lore pip uninstall` them') if vcs: print( ansi.warning('unless') + ' they are covered by the previously listed packages' ' that pip can not freeze.') print(os.linesep + os.linesep.join(unsafe) + os.linesep) with open(source, 'w', encoding='utf-8') as f: f.write( os.linesep.join(sorted(present, key=lambda s: s.lower())).strip() + os.linesep) if vcs: f.write(os.linesep.join(vcs) + os.linesep)