def process_r_specs(package_names: ListLike, commands: ListLike) -> Packages: """Generate the install commands for R packages and return as the package spec.""" if len(package_names) != len(commands): raise RError( f"Must have same number of package names ({len(package_names)}) and install commands ({len(commands)})." ) packages = Packages() for package_name, command in zip(package_names, commands): packages.append(Package(package_name, spec=command)) return packages
def parse(cls, history_section: dict): """Parse the history file and create the packages.""" packages_instance = cls() for source, packages in history_section.items(): source_packages = Packages() for name, spec in packages.items(): if spec == "*": source_packages.append_spec(name) else: source_packages.append(Package(name, spec)) packages_instance.update_packages(packages=source_packages, source=source) return packages_instance
def clean_r_specs(package_names: ListLike, commands: ListLike) -> Packages: """Cleaning all R package specs and converting to a package class.""" packages = Packages() for package_name, command in zip_longest(package_names, commands): if not package_name or not command: raise RError( ( "Must have same number of R package names and install commands.\n" f"Package names: {package_names}\n" f"and install commands: {commands}" ) ) packages.append(Package(package_name, command)) return packages
def _handle_r_extra_log(env: Environment, history: History, index: int) -> Environment: log = history.logs[index] if "remove.packages(" in log: packages = history.logs.extra_removed_packages(index=index) RHandler(env=env).remove(packages=packages) else: package_names = [ package_name for package_name in history.revisions.diffs[index]["r"]["upsert"] ] packages = Packages() for package_name in package_names: packages.append( history.revisions.packages[index]["r"][package_name]) RHandler(env=env).install(packages=packages) return env
def parse(cls, history_section: dict): """Parse the history file and create the packages.""" packages_instance = cls() for source, packages in history_section.items(): source_packages = Packages() for name, spec in packages.items(): if spec == "*": source_packages.append_spec(name) elif spec[0].isdigit() and all( letter.isalnum() or letter in [".", "=", "_"] for letter in spec): source_packages.append_spec(name + cls.separators[source] + spec) else: source_packages.append(Package(name, spec)) packages_instance.update_packages(packages=source_packages, source=source) return packages_instance
def _install_missing_r_packages( env: Environment, local_history: History, remote_history: History ) -> Environment: local_r_packages = local_history.packages.get("r", {}) remote_r_packages = remote_history.packages.get("r", {}) handler = RHandler(env=env) packages_to_install = Packages() packages_to_add_to_history = Packages() for package_name, package in local_r_packages.items(): if package_name not in remote_r_packages: if package_name in env.dependencies.get("r", {}): packages_to_add_to_history.append(package) else: packages_to_install.append(package) if packages_to_install: handler.install(packages_to_install) if packages_to_add_to_history: handler.update_history_install(packages_to_add_to_history) return env
def extract_r_packages(self, index: int) -> Optional[Packages]: """Extract R packages with versions (if the version was specified in the log).""" if self[index].startswith(R_COMMAND): r_packages = Packages() quote = '"' package_names = self._get_r_package_names(self[index]) start = self[index].index(quote) + len(quote) r_commands = self[index] for package_name in package_names: i_name = r_commands.index(package_name) try: end = r_commands.index(";", i_name) except ValueError: end = r_commands.rindex(quote, i_name) spec = r_commands[start:end].strip() start = end + 1 r_packages.append( Package(package_name, spec.replace(r"\"", '"'))) return r_packages return None