ELSG @sFddlmZddlmZddlmZddlmZmZmZddl m Z ddl Z ddl Z e j ddlZddlZddlZddlZddlZddlZddlZddlZy<ddlmZmZmZmZmZddlmZWnJek rfddlmZmZmZmZmZddl mZYnXdd l!m!Z!Gd d d e"Z#d d Z$ddZ%ddZ&dddZ'ddZ(ddddZ)ddZ*ddZ+ddZ,d d!Z-d"d#Z.d$d%Z/Gd&d'd'eZ0dd(d)Z1dd*d+Z2d,d-Z3d.d/Z4d0d1Z5d2d3Z6d4d5Z7dd6d7Z8d8d9Z9dd:d;Z:dd<d=Z;d>d?Z<d@dAZ=dBdCZ>dDdEZ?dFdGZ@dHdIZAdJdKZBeCdLkrBe<ddMdNndS)O)print_function)gettext)ngettext)S_IMODEST_MODES_IXUSR)ceilN) ProxyHandlerRequest build_openerinstall_openerurlopen)urlsplit)copyc@s=eZdZdZdddZddZddZd S) ExecutionTimez Helper that can be used in with statements to have a simple measure of the timing of a particular block of code, e.g. with ExecutionTime("db flush"): db.flush() cCs ||_dS)N)info)selfrr3/usr/lib/python3/dist-packages/DistUpgrade/utils.py__init__HszExecutionTime.__init__cCstj|_dS)N)timenow)rrrr __enter__KszExecutionTime.__enter__cCs(td|jtj|jfdS)Nz%s: %s)printrrr)rtypevaluestackrrr__exit__NszExecutionTime.__exit__N)__name__ __module__ __qualname____doc__rrrrrrrrAs  rcCsst|}t|j}|jrB|jj|jd|_n|jri|jj|jd|_nt|S)Nzhidden-uzhidden-p)rruriZusernamereplaceZpasswordstr)entryZtmpZ url_partsrrr)get_string_with_no_auth_from_source_entryRs   r'cCsNd}tjd}x1tjd|D]}|tjj|7}q*W|S)zE estimate the amount of space that the current kernel takes in /boot rz /boot/*%s*)osunameglobpathgetsize)sizeZkverfrrrestimate_kernel_size_in_boot\s r0c CsMd}y+ddl}|j}|jd}WntjdYnX|S)z+ return True if Unity is currently running FrNzcom.canonical.Unityz&could not check for Unity dbus service)dbusZ SessionBusZname_has_ownerlogging exception)Z unity_runningr1busrrris_unity_runninges  r5c Cs|stj}nx|dkrd|}t|}|j}WdQX|jddjdd}||krdSt|jddjd}qWdS) Nrz /proc/%s/stat(r()TF)r)getpidopenread partitionintsplit)Z processnamepidZ stat_fileZstat_fstatZcommandrrris_child_of_process_nameqs   'rAcCsFtjjd s&tjjd r*dStjdtjdkS)z, returns True if we are inside a chroot z/procz/proc/1T/z /proc/1/root)r)r,existsr@rrrr inside_chroots&rDFrcCsid}x\|jD]N}t||jdt||krS|d|7}n||d7}qW|S)z helpers inspired after textwrap - unfortunately we can not use textwrap directly because it break packagenames with "-" in them into new lines r  )r>lenrfind)twidthZsubsequent_indentoutsrrrwraps )rNcKsGd}|jd}x+|D]#}t||}||d7}qW|S)NrrF)r>rN)rMkwargsmsgZparasZparrrrtwraps  rQcCsg}tjjdsgStjdgdtjdd}|jdjd}|d=x.|D]&}|rd|j|jdqdqdW|S)z= return list of loaded modules (or [] if lsmod is not found) z /sbin/lsmodstdoutuniversal_newlinesTrrF) r)r,rC subprocessPopenPIPE communicater>append)modulesplineslinerrrlsmods  r]cCsZtjj|sdSttj|t}|t@tksVtj||tBndS)zB check if a given binary has the executable bit and if not, add itN)r)r,rCrr@rrchmod)r,moderrrcheck_and_fix_xbits r`cCsidd6}dtjkr dStjdj}|dd|kr[||ddSd|kr|jddjdd }d |kr|jd d}n|dS|dd dSdS) z: helper to get the country mirror from the current locale rcZLANGN_.rr8@r()r)environlowerr>)Z lang_mirrorZlangZcountryrrrcountry_mirrors    rhcCstjjd}|r-tjd||Sddlm}m}|dddgd|d d }|j}|dkrt j j d |d S|j j j}|j j|S) z2 return the codename of the current runing distro ZMETA_RELEASE_FAKE_CODENAMEzQusing fake release name '%s' (because of META_RELEASE_FAKE_CODENAME environment) r)rUrV lsb_releasez-cz-srRrSTz"lsb_release returned exitcode: %i zunknown distribution)r)rfgetr2warningrTrUrVwaitsysstderrwriterRreadlinestripclose)ZdistrUrVrZresrrrget_dists     rtcCstjjd}|r-tjd||Sddlm}m}|dddgd|d d }|j}|dkrt j j d |d S|j j j}|j j|S) z2 return the version of the current running distro ZMETA_RELEASE_FAKE_VERSIONzSusing fake release version '%s' (because of META_RELEASE_FAKE_VERSION environment) r)rUrVriz-rz-srRrSTz"lsb_release returned exitcode: %i zunknown distribution)r)rfrjr2rkrTrUrVrlrmrnrorRrprqrr)ZdescrUrVrZrsrrrget_dist_versions     ruc@seZdZddZdS) HeadRequestcCsdS)NZHEADr)rrrr get_methodszHeadRequest.get_methodN)rr r!rwrrrrrvs rvc Cs|sddn|d|t|\}}}}}|d|||||f|dkry7tt|}|j|jdkrdSdSWqtk r}z|d |dSWYd d }~XqXn|d krd d l} y| j|} | j| j t j j || j t j j|} | j|rb|d | n| d krrdSWqtk r}z |r|d|ndSWYd d }~XqXndS)z helper that checks if the given uri exists and is downloadable (supports optional debug_func function handler to support e.g. logging) Supports http (via HEAD) and ftp (via size request) cSsdS)NTr)xrrr sz"url_downloadable..zurl_downloadable: %sz"s='%s' n='%s' p='%s' q='%s' f='%s'httpTFzerror from httplib: '%s'NZftprzftplib.size() returned: %szerror from ftplib: '%s')rr rvrrcode ExceptionftplibZFTPZlogincwdr)r,dirnamer.basenamequit) r#Z debug_funcschemeZnetlocr,ZquerryZfragmentZ http_fileer~r/r.rrrurl_downloadablesB        rc Cs;d}d}tjjddkr9tjjd}ntjj|rtj}tj|||jdd}|r|jd}t |j d}|r|rd ||f}qqn|r7t j d |st d |d tjdSti|d 6}t|}t|tjd|n|S)z init proxy settings * first check for http_proxy environment (always wins), * then check the apt.conf http proxy, * then look into synaptics conffile * then into gconf (if gconfclient was supplied) z/root/.synaptic/synaptic.confNzAcquire::http::ProxyrzSynaptic::useProxyFzSynaptic::httpProxyzSynaptic::httpProxyPortz http://%s:%s/z http://\w+zproxy '%s' looks invalidfilerzZ http_proxy)apt_pkgconfigfindr)r,rCZ ConfigurationZread_config_fileZfind_br%Zfind_irematchrrmrnr r r putenv) Z gsettingsZSYNAPTIC_CONF_FILEproxyZcnfZ use_proxyZ proxy_hostZ proxy_portZ proxy_supportZopenerrrr init_proxy,s,    rcCs yddl}|j|jj}y8|jdd}|j|d}|jddSWnz|jjk r}zTd}|j|krn|jdd }|j|d}|jdd SWYdd}~XnXWn)t k r}z d SWYdd}~XnXdS) z Check via dbus if the system is running on battery. This function is using UPower per default, if UPower is not available it falls-back to DeviceKit.Power. rNzorg.freedesktop.UPowerz/org/freedesktop/UPowerzorg.freedesktop.DBus.PropertiesZ OnBatteryz)org.freedesktop.DBus.Error.ServiceUnknownzorg.freedesktop.DeviceKit.Powerz /org/freedesktop/DeviceKit/Power on_batteryF) r1BusZ TYPE_SYSTEM get_object InterfaceZGet exceptionsZ DBusExceptionZ_dbus_error_namer})r1r4devobjdevrZ error_unknownrrrrPs$     'rc Csyeddl}|j|jj}|jdd}|j|d}|jdd}||fSWntk r}d SYnXdS) zs Send a dbus signal to power-manager to not suspend the system, using the freedesktop common interface rNzorg.freedesktop.PowerManagementz(/org/freedesktop/PowerManagement/Inhibitz'org.freedesktop.PowerManagement.InhibitZ UpdateManagerzUpdating systemF)FF)r1rZ TYPE_SESSIONrrZInhibitr})r1r4rrcookierrr inhibit_sleepls    rcCsKy|j|Wn3tk rF}ztd|WYdd}~XnXdS)zNSend a dbus signal to gnome-power-manager to allow a suspending the systemz,could not send the dbus UnInhibit signal: %sN)Z UnInhibitr}r)rrrrrr allow_sleep~srcCs&|dks|jdkr"dSdS)N0ZFALSEFT)upper)r%rrr str_to_boolsrc CsPddl}ytj\}}|SWn#tk rK|jddSYnXdS)Nrzgedefaultlocale() failed)r2localeZgetdefaultlocaler}r3)r2Zlocale_sencodingrrrget_langs   rcCs#td|}|jdddS)z6 try to guess the flavor based on the running desktop cache-r8r)get_ubuntu_flavor_packager>)rpkgrrrget_ubuntu_flavorsrcCs,ddlm}|d}|jddS)Nr)DistUpgradeConfigz"/usr/share/ubuntu-release-upgraderZDistroZMetaPkgs)Z#DistUpgrade.DistUpgradeConfigParserrZgetlist)rparserrrr_load_meta_pkg_lists rcCsdg}y|jttWn2tk rW}ztd|WYdd}~XnX|dkrstj}nx=|D]5}||kr||nd}|rz|jrz|SqzWdS)zB try to guess the flavor metapackage based on the running desktop zubuntu-desktopz%Could not load list of meta packages:N)extendsortedrr}raptZCacheZ is_installed)rZ meta_pkgsrZmeta_pkgZ cache_pkgrrrrs    rcCstd|}idd6}||kr0||S|jdrYt|jdddS|jdrt|jdddSdSd S) z; try to guess the flavor name based on the running desktop rz Ubuntu Studiozubuntustudio-desktopz-desktopr8rz-netbookZUbuntuN)rendswithcapitalize_first_wordrsplit)rrlookuprrrget_ubuntu_flavor_names  rc Csddlm}m}|jd|d|jjd|jjd|jj }|j d||f|j |j j |jj|jd|j|jd S) Nr)GtkGdkparentflagsrZbuttonsz%s %srF)Z gi.repositoryrrZ MessageDialogZ DialogFlagsZMODALZ MessageTypeZERRORZ ButtonsTypeZCLOSEZ set_markupZrealizeZ get_windowZ set_functionsZ WMFunctionZMOVEZ set_titleZrunZdestroy)rsummarymessagerrdrrrerrors      rcCse|dkrCtt|td}tdd|i|d6Stjtd|ddSdS)zG Convert a given size in bytes to a nicer better readable unit iz %(size).0f kBr.z%.1f MBg@@Ni@B)r=rfloatrr format_stringrc)bytesZ size_in_kbrrr humanize_sizes   rcCstjjdS)NzAPT::Architecture)rrrrrrrget_archsrc Csd}d}d}td}x|D]|}|j}|sFq(n|j}||}||krnq(n||jdd}t|d} | |kr(dSq(WWdQXd S) z@ check if the current system is listening on the given tcp port r8Z0Az /proc/net/tcp:TNF)r:rqr>r=) ZportZINDEX_LOCAL_ADDRZ INDEX_STATEZSTATE_LISTENINGZnet_tcpr\valuesstateZlocal_port_strZ local_portrrris_port_already_listenings"      rcCsd}tjdkr'tdntjjds=dStjddgdtjd d jd}||kr{dSd S) z# Return True if iptables is active zChain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination rz%Need root to check the iptables statez/sbin/iptablesFZiptablesz-nLrRrST) r)getuidOSErrorr,rCrTrUrVrW)Ziptables_emptyrLrrriptables_active s   rcCsSt|dkrO|djrO|dj rO|dj|ddS|S)z3 this uppercases the first word's first letter r8rN)rHisalphaisupper capitalize)stringrrrr$s3rcCst|jdd}t|S)zX this takes a package synopsis and uppercases the first word's first letter rr)getattr candidater)rnamerrrget_package_label,sr__main__ZbarZbaz)DZ __future__rrrcrr@rrrZmathrrrZ init_configrr2rr)r+rTrmrZurllib.requestr r r r r Z urllib.parser ImportErrorZurllib2Zurlparserobjectrr'r0r5rArDrNrQr]r`rhrtrurvrrrrrrrrrrrrrrrrrrrrrrrsp           ( (      +$