o vf+@spdZddlZddlZddlZddlZddlZddlZddlZddZddZ d dd Z ddgdddfd d Z dS) zFunctions to manage sandboxesNc Csi}|ddd|ddD]'}|sqz |dd\}}Wnty4td|Yqw|||<qdd |DS) zDetermine necessary packages for given report. Return list of (pkgname, version) pairs. version might be None for unknown package versions. Package DependenciesN%invalid Package/Dependencies line: %scSsg|]\}}||fqSr).0pvrr5/usr/lib/python3/dist-packages/apport/sandboxutils.py %sz#needed_packages..)get splitlinesstripsplit ValueErrorapportwarningitems)reportpkgslinepkgversionrrr needed_packagess$   rc Csxi}|ddd|ddD]'}|sqz |dd\}}Wnty4td|Yqw|||<q|S)z0Return package -> version dictionary from reportrrrrNrr)rrrrrrr)rpkg_versrrrrrr report_package_versions(s$   rFc st}t}d|vr<|dD]*}|sq|}t|dvr:d|dvr:d|dvr:tj|d} || qn t j |d }tj |sQt||D]+}t jj|d||d |d d } | rx|rrt d || f|| qSt d |qSfdd|DS)aDetermine necessary runtime packages for given report. This determines libraries dynamically loaded at runtime in two cases: 1. The executable has already run: /proc/pid/maps is used, from the report 2. The executable has not already run: shared_libraries() is used The libraries are resolved to the packages that installed them. Return list of (pkgname, None) pairs. When pkgmap_cache_dir is specified, it is used as a cache for get_file_package(). ProcMaps)xz.soExecutablePathT DistroRelease Architecturereleasearchz0dynamically loaded %s needs package %s, queueingz/%s is needed, but cannot be mapped to a packagecsg|] }||fqSr)r)r r  pkg_versionsrr r hsz+needed_runtime_packages..)setrrrlenospathrealpathaddr fileutilsshared_librariesvaluesexistsmakedirs packagingget_file_packagerlogr) rsandboxpkgmap_cache_dirr+verboserlibsrcolslibrrr*r needed_runtime_packages:s<$     r@cCs|rtj|}tj|st|d}ntjdd}tt j |d}|r.tj|}n tjdd}tt j |tj ||d} g} d|vsXd| d d | d d vr\t |} g} |D] } | | d fq`|d krpd }d } |r| d d d| d d }td}t||} | rtd| ztjj|||d| |||| d| d }Wnty}z tt|WYd }~nd }~ww| rz|tjj|||d| |||| d| ddd 7}Wnty}z tt|WYd }~nd }~wwt|}t||| ||} dD]N}||vrXtjj||d| |d| dd}|dkr6|ddkr6d|d<d}|rOtd||f|| || |fq td|||q | rz|tjj|||d| |||| d| d 7}Wnty}z tt|WYd }~nd }~wwd |vrdD]!}||vrtj|||std|||||d q|r||d<td|||fS) a0 Build a sandbox with the packages that belong to a particular report. This downloads and unpacks all packages from the report's Package and Dependencies fields, plus all packages that ship the files from ProcMaps (often, runtime plugins do not appear in Dependencies), plus optionally some extra ones, for the distro release and architecture of the report. For unpackaged executables, there are no Dependencies. Packages for shared libaries are unpacked. report is an apport.Report object to build a sandbox for. Presence of the Package field determines whether to determine dependencies through packaging (via the optional report['Dependencies'] field), or through ldd via needed_runtime_packages() -> shared_libraries(). Usually report['Architecture'] and report['Uname'] are present. config_dir points to a directory with by-release configuration files for the packaging system, or "system"; this is passed to apport.packaging.install_packages(), see that method for details. cache_dir points to a directory where the downloaded packages and debug symbols are kept, which is useful if you create sandboxes very often. If not given, the downloaded packages get deleted at program exit. sandbox_dir points to a directory with a permanently unpacked sandbox with the already unpacked packages. This speeds up operations even further if you need to create sandboxes for different reports very often; but the sandboxes can become very big over time, and you must ensure that an already existing sandbox matches the DistroRelease: and Architecture: of report. If not given, a temporary directory will be created which gets deleted at program exit. extra_packages can specify a list of additional packages to install which are not derived from the report and will be installed along with their dependencies. If verbose is True (False by default), this will write some additional logging to stdout. If log_timestamps is True, these log messages will be prefixed with the current time. If dynamic_origins is True (False by default), the sandbox will be built with packages from foreign origins that appear in the report's Packages:/Dependencies:. Return a tuple (sandbox_dir, cache_dir, outdated_msg). Tapport_sandbox_)prefixF apport_cache_r%rz[originrrrNsystemrz+\[origin: ([a-zA-Z0-9][a-zA-Z0-9\+\.\-]+)\]z Origins: %sr&) architectureorigins)rErF install_dbg install_deps)InterpreterPathr$r' systemctl SourcePackagesystemdz/bin/systemctlr$z%Installing extra package %s to get %sz%Cannot find package which ships %s %sz2%s %s does not exist (report specified package %s)RetraceOutdatedPackagesz built sandbox)r.r/abspathisdirr6tempfilemkdtempatexitregistershutilrmtreejoinrrappendrecompiler,findallrr9r7install_packages SystemErrorfatalstrrr@r8r5memdbg)r config_dir cache_dir sandbox_dirextra_packagesr<log_timestampsdynamic_originspermanent_rootdirr;r extra_pkgsr rFpkg_listm outdated_msger+r/rrrr make_sandboxks4     $                rl)F) __doc__rRr.os.pathrXrTrPrrrr@rlrrrr s0  1