f @dZddlZddlZddlmZddlmZmZddlm Z m Z m Z eje ZdZdedefd Zdefd Zdeefd Zdedefd Zdefd ZdefdZdeefdZdZdededefdZGdde j6Zee j:ffgZdZy)zA Datasource to support the Windows Subsystem for Linux platform. N)PurePath)Listcast)sourcessubputilz/usr/bin/wslpathpathreturncrtjtd|g\}}t|jS)a@ Translates a path inside the current WSL instance's filesystem to a Windows accessible path. Example: # Running under an instance named "CoolInstance" root = wslpath2win("/") # root == "//wsl.localhost/CoolInstance/" :param path: string representing a Linux path, whether existing or not. z-amr WSLPATH_CMDrrstripr out_s A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.pywsl_path_2_winrs.YY UD1 2FC CJJL !!c0td}|jS)zL Returns the name of the current WSL instance as seen from outside. /)rname) root_net_paths r instance_namer#s#3'M   rcd}d}g}tjjD]'}|d|k(s ||dvs|j|d)|S)z Return a list of mount points of the Windows drives inside the current WSL instance, if drives are mounted, or an empty list otherwise 9pz aname=drvfsfstypeopts mountpoint)rmountsvaluesappend)FS_TYPEOPTIONS_CONTAINmountedmnts rmounted_win_drivesr&+s` G#OG{{}##%. x=G #3v;(F NN3|, -. Nrcrtjtd|g\}}t|jS)a_ Returns a translation of a Windows path to a Linux path that can be accessed inside the current instance filesystem. It requires the Windows drive mounting feature to be enabled and the disk drive must be muonted for this to succeed. Example: # Assuming Windows drives are mounted under /mnt/ and "S:" doesn't exist: p = winpath2wsl("C:\ProgramData") # p == "/mnt/c/ProgramData/" n = winpath2wsl("S:\CoolFolder") # Exception! S: is not mounted. :param path: string representing a Windows path. The root drive must exist, although the path is not required to. z-aur rs rwin_path_2_wslr(;s. YY UD1 2FC CJJL !!rct}|s tdd}|D]N}||z}tj|tjs-t j d|t|cStddj|z)z? Returns the Linux path to the Windows host's cmd.exe. zWindows drives are not mounted.z%s/Windows/System32/cmd.exezFound cmd.exe at <%s>z,Couldn't find cmd.exe in any mount point: %sz, ) r&IOErrorosaccessX_OKLOGdebugrjoin)r candidater%cmds rcmd_executabler3Os  !F 788.I#oyybgg&  )3/}  669JJ rc~t}tjd|jddg\}}|j}|stjdt |}t jj|d}t jj|std|zt|S)z Returns the Windows user profile directory translated as a Linux path accessible inside the current WSL instance. z/initz/Czecho %USERPROFILE%z4No output from cmd.exe to show the user profile dir.z .cloud-initz%s directory doesn't exist.) r3ras_posixrProcessExecutionErrorr(r+r r0isdirFileNotFoundErrorr)r2homerwin_profile_dirseed_dirs rcloud_init_data_dirr<gs  C ii#,,.$8LMNGD! ;;=D (( B  %T*Oww||O];H 77== " = HII H rcbtj\}}}|r|n|}d|z|d|dd|zdgS)z Return a list of candidate file names that may contain user-data in some supported format, ordered by precedence. z %s.user-data-z .user-dataz%s-all.user-datazdefault.user-data)rget_linux_distro)rdistribution_id version_idversion_codenameversions rcandidate_user_data_file_namesrDsK 594I4I4K1OZ!1&j,>$"7"7 "FG }H4M?+""* -  #o O   J   s(BB54B5c8eZdZdZdedefdZdefdZdefdZy) DataSourceWSLWSLr;r ctj|Dcic]'}|jj|j)}}|st d|zt |jDcgc]}|j}}|D]$}||jvst||cSt d|zcc}wcc}w)z Finds the most precendent of the candidate files that may contain user-data, if any, or None otherwise. z%s directory is emptyz6%s doesn't contain any of the expected user-data files) r+scandirrcasefoldr r*rDrkeysr)selfr;efexisting_filesf folded_namesfilenames rfind_user_data_filez!DataSourceWSL.find_user_data_files35**X2F ,.BGG    '  1H<= =4D4F4FG  JJL  % :H>..00x 899 : Dx O    s ,B=-Cc|j}|sy tt|j}||j dk(S#t t f$r)}tjdt|Yd}~yd}~wwxYw)NFrGz2Unable to check_instance_id from metadata file: %s) get_instance_idrPr<rgetr*rLr.warningstr)rXsys_cfgcurrentrMerrs rcheck_instance_idzDataSourceWSL.check_instance_ids{&&( -#%t'9'9Hhll=99 9$  KKDC   s1AA?A::A?cd|_t}t|_ t||j|_|j |}t ttj|j|_ y#ttf$r)}tjdt|Yd}~yd}~wwxYw)NTz"Unable to setup WSL datasource: %sF)vendordata_rawr<rrPrMr^rrcrrJr5 userdata_rawrLr*r.rK)rXr;filerfs r _get_datazDataSourceWSL._get_datas"&(*_ 2$,,DM++H5D $T**4==?;!D G$  II:CH E sA"BB=B88B=N) __name__ __module__ __qualname__dsnamerr^boolrgrlrrrRrRs2 F H  <D(4rrRc6tj|tS)N)rlist_from_depends datasources)dependss rget_datasource_listrws  $ $Wk ::r) __doc__loggingr+pathlibrtypingrr cloudinitrrr getLoggerrmr.r rcrrr&r(r3r<rDrHdictrP DataSourcerRDEP_FILESYSTEMrurwrrrrrs H ))g!  " " "s DI  """(0X4T#Y(+#$8FG&&FVW++-. ;r