[P @sdZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl mZddlmZdd Zd d Zd d ZdaddZddZdddZdaaaddZddZdddddZddZdddddddddZd d!Zdddd"d#Z ddddd$d%Z!Gd&d'd'Z"dS)(zudistutils.util Miscellaneous utility functions -- anything that doesn't fit into one of the other *util.py modules. N)DistutilsPlatformError)newer)spawn)log)DistutilsByteCompileErrorcCstjdkrd}tjj|}|d*kr:tjStjjd|}tj|t||j}|dkrdS|dkrdStjSd tjkrtjd Stjd kst td  rtjStj \}}}}}|jj d d }|j dd}|j d d}|dddkrTd||fS|dddkr|ddkrd}dt |dd|ddf}idd6dd6} |d | tj 7}qn|dd!d"krd||fS|ddd#kr"d$|||fS|dd%d&krzd&}tjd'tj} | j|} | r| j}qn[|dd%d(krddl} ddl} | j| jj|||\}}}nd)|||fS)+aReturn a string that identifies the current platform. This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and version and the architecture (as supplied by 'os.uname()'), although the exact information included depends on the OS; eg. for IRIX the architecture isn't particularly important (IRIX only runs on SGI hardware), but for Linux the kernel version isn't particularly important. Examples of returned values: linux-i586 linux-alpha (?) solaris-2.6-sun4u irix-5.3 irix64-6.2 Windows will return one of: win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc) win-ia64 (64bit Windows on Itanium) win32 (all others - specifically, sys.platform is returned) For other non-POSIX platforms, currently just returns 'sys.platform'. ntz bit ()Zamd64z win-amd64Zitaniumzwin-ia64Z_PYTHON_HOST_PLATFORMposixuname/ _-NZlinuxz%s-%sZsunosr5Zsolarisz%d.%sZ32bitiZ64bitlz.%sZirixZaixz%s-%s.%scygwinz[\d.]+darwinz%s-%s-%s)osnamesysversionfindplatformlenlowerenvironhasattrr replaceintmaxsizerecompileASCIImatchgroup _osx_supportZdistutils.sysconfigZget_platform_osxZ sysconfigZget_config_vars)prefixijZlookZosnameZhostreleasermachineZbitnessZrel_remr, distutilsr4$/usr/lib/python3.4/distutils/util.py get_platformsX #   ( r6cCstjdkr|S|s|S|ddkr@td|n|ddkrctd|n|jd}xd|kr|jdquW|stjStjj|S)aReturn 'pathname' as a name that will work on the native filesystem, i.e. split it on '/' and put it back together again using the current directory separator. Needed because filenames in the setup script are always supplied in Unix style, and have to be converted to the local convention before we can actually use them in the filesystem. Raises ValueError on non-Unix-ish systems if 'pathname' either starts or ends with a slash. r rzpath '%s' cannot be absoluterzpath '%s' cannot end with '/'.r)rsep ValueErrorsplitremovecurdirpathjoin)pathnameZpathsr4r4r5 convert_pathos r@cCstjdkrTtjj|s4tjj||Stjj||ddSnptjdkrtjj|\}}|ddkr|dd}ntjj||StdtjdS)a Return 'pathname' with 'new_root' prepended. If 'pathname' is relative, this is equivalent to "os.path.join(new_root,pathname)". Otherwise, it requires making 'pathname' relative and then joining the two, which is tricky on DOS/Windows and Mac OS. r rNrr\z!nothing known about platform '%s')rrr=isabsr> splitdriver)Znew_rootr?Zdriver=r4r4r5 change_roots rDcCstr dStjdkrWdtjkrWddl}|jtjdtjd._substz\$([a-zA-Z_][a-zA-Z_0-9]*)zinvalid variable '$%s'N)rJr'subKeyErrorr9)srLrMZvarr4r4r5 subst_varss rQzerror: cCs|t|S)N)rK)excr-r4r4r5grok_environment_errorsrScCs8tjdtjatjdatjdadS)Nz [^\\\'\"%s ]*z'(?:[^'\\]|\\.)*'z"(?:[^"\\]|\\.)*")r'r(string whitespace _wordchars_re _squote_re _dquote_rer4r4r4r5 _init_regexsrYcCs tdkrtn|j}g}d}x|rtj||}|j}|t|kr|j|d|Pn||tjkr|j|d|||dj }d}n||dkr |d|||dd}|d}n||dkr/t j||}n9||dkrTt j||}nt d|||dkrt d||n|j\}}|d|||d|d||d}|jd }|t|kr1|j|Pq1q1W|S) aSplit a string up according to Unix shell-like rules for quotes and backslashes. In short: words are delimited by spaces, as long as those spaces are not escaped by a backslash, or inside a quoted string. Single and double quotes are equivalent, and the quote characters can be backslash-escaped. The backslash is stripped from any two-character escape sequence, leaving only the escaped character. The quote characters are stripped from any quoted string. Returns a list of words. NrrAr'"z!this can't happen (bad char '%c')z"bad string (mismatched %s quotes?)r)rVrYstripr*endr appendrTrUlstriprWrX RuntimeErrorr9span)rPZwordsposr2r]Zbegr4r4r5 split_quoteds@     "  4 rccCss|dkrOd|j|f}|dddkrO|ddd}qOntj||so||ndS) aPerform some action that affects the outside world (eg. by writing to the filesystem). Such actions are special because they are disabled by the 'dry_run' flag. This method takes care of all that bureaucracy for you; all you have to do is supply the function to call and an argument tuple for it (to embody the "external action" being performed), and an optional message to print. Nz%s%rrz,)rr rd)__name__rinfo)funcargsmsgverbosedry_runr4r4r5executes  rlcCsC|j}|dkrdS|dkr,dStd|fdS)zConvert a string representation of truth to true (1) or false (0). True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if 'val' is anything else. yyesttrueon1rnnoffalseoff0rzinvalid truth value %rN)rmrnrorprqrr)rsrtrurvrwrx)r!r9)valr4r4r5 strtobool0s    rzrcCs%tjrtdn|dkr9to3|dk}n|sy&ddlm}|d\} } Wn5tk rddlm} d| d} } YnXtj d| |s@| dk rt j | d} nt | d} | j d | j d jtt|d | j d |||||f| jntj| g} |d krn| jd dn|dkr| jd dnt| d|tt j| fd| d|naddlm}xN|D]F}|dddkrqn|dkr!tjj|d| }ntjj|}|}|r|dt||krttd||fn|t|d}n|rt jj||}nt jj|}|r|st ||rtj d|||s||||qqtj!d||qqWdS)aByte-compile a collection of Python source files to either .pyc or .pyo files in a __pycache__ subdirectory. 'py_files' is a list of files to compile; any files that don't end in ".py" are silently skipped. 'optimize' must be one of the following: 0 - don't optimize (generate .pyc) 1 - normal optimization (like "python -O") 2 - extra optimization (like "python -OO") If 'force' is true, all files are recompiled regardless of timestamps. The source filename encoded in each bytecode file defaults to the filenames listed in 'py_files'; you can modify these with 'prefix' and 'basedir'. 'prefix' is a string that will be stripped off of each source filename, and 'base_dir' is a directory name that will be prepended (after 'prefix' is stripped). You can supply either or both (or neither) of 'prefix' and 'base_dir', as you wish. If 'dry_run' is true, doesn't actually do anything that would affect the filesystem. Byte-compilation is either done directly in this interpreter process with the standard py_compile module, or indirectly by writing a temporary script and executing it. Normally, you should let 'byte_compile()' figure out to use direct compilation or not (see the source for details). The 'direct' flag is used by the script generated in indirect mode; unless you know what you're doing, leave it set to None. zbyte-compiling is disabled.Nr)mkstempz.py)mktempz$writing byte-compilation script '%s'wz2from distutils.util import byte_compile files = [ z, z] z byte_compile(files, optimize=%r, force=%r, prefix=%r, base_dir=%r, verbose=%r, dry_run=0, direct=1) rz-Orz-OOrkz removing %s)r(rdebug_overridez1invalid prefix: filename %r doesn't start with %rzbyte-compiling %s to %sz%skipping byte-compilation of %s to %s)"rdont_write_bytecoder __debug__Ztempfiler{ ImportErrorr|rrfrfdopenopenwriter>mapreprclose executableinsertrrlr;Z py_compiler( importlibutilcache_from_sourcer r9r=basenamerdebug)Zpy_filesoptimizeZforcer-Zbase_dirrjrkZdirectr{Z script_fdZ script_namer|Zscriptcmdr(filecfiledfileZ cfile_baser4r4r5 byte_compile@sl"    #        rcCs"|jd}d}|j|S)zReturn a version of the string escaped for inclusion in an RFC-822 header, by ensuring there are 8 spaces space after each newline.  rz z )r:r>)headerlinesr8r4r4r5 rfc822_escapesrcCsz|s dSddlm}m}Gddd|}|dkrQ|d}n||d|}|j|dd dS) a Invoke 2to3 on a list of Python files. The files should all come from the build area, as the modification is done in-place. To reduce the build time, only files modified since the last invocation of this function should be passed in the files argument.Nr)RefactoringToolget_fixers_from_packagec@s4eZdZddZddZddZdS)z*run_2to3..DistutilsRefactoringToolc_stj||dS)N)rerror)selfrirhkwr4r4r5 log_errorsz4run_2to3..DistutilsRefactoringTool.log_errorcWstj||dS)N)rrf)rrirhr4r4r5 log_messagesz6run_2to3..DistutilsRefactoringTool.log_messagecWstj||dS)N)rr)rrirhr4r4r5 log_debugsz4run_2to3..DistutilsRefactoringTool.log_debugN)re __module__ __qualname__rrrr4r4r4r5DistutilsRefactoringTools   rz lib2to3.fixesoptionsrT)Zlib2to3.refactorrrZrefactor)files fixer_namesrexplicitrrrrr4r4r5run_2to3s rcCs|ddlm}ddlm}ddlm}|} tj} tj|z| j Wdtj| X| j | j dd<|rx<|j D]+} | j } | sqn| j| qWng} xz| j D]o} tjj|| }|tjj||tjj|| |dd}|dr| j|qqWtdd | Dd |d |d || S) zRecursively copy a directory, only copying new and changed files, running run_2to3 over all newly copied Python modules afterward. If you give a template string, it's parsed like a MANIFEST.in. r)mkpath) copy_file)FileListNupdatercSs+g|]!}|jjdr|qS)z.py)r!endswith).0fnr4r4r5 s z$copydir_run_2to3..rrr)Zdistutils.dir_utilrZdistutils.file_utilrZdistutils.filelistrrgetcwdchdirfindallZallfilesr splitlinesr\Zprocess_template_liner=r>dirnamer^r)srcdesttemplaterrrrrrZfilelistr<lineZcopiedfilenameZoutnameZresr4r4r5copydir_run_2to3s4    $ rc@s4eZdZdZdZdZdZddZdS) Mixin2to3zMixin class for commands that run 2to3. To configure 2to3, setup scripts may either change the class variables, or inherit from individual commands to override how 2to3 is invoked.NcCst||j|j|jS)N)rrrr)rrr4r4r5r(szMixin2to3.run_2to3)rerr__doc__rrrrr4r4r4r5rs r)#rrr'importlib.utilrrrTZdistutils.errorsrZdistutils.dep_utilrZdistutils.spawnrr3rrr6r@rDrFrJrQrSrVrWrXrYrcrlrzrrrrrr4r4r4r5s>      ]      =