pygments.rb 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +3 -0
  4. data/lexers +0 -0
  5. data/lib/pygments/popen.rb +3 -2
  6. data/lib/pygments/version.rb +1 -1
  7. data/pygments.rb.gemspec +2 -1
  8. data/vendor/pygments-main/AUTHORS +20 -1
  9. data/vendor/pygments-main/CHANGES +55 -3
  10. data/vendor/pygments-main/LICENSE +1 -1
  11. data/vendor/pygments-main/MANIFEST.in +1 -1
  12. data/vendor/pygments-main/Makefile +5 -8
  13. data/vendor/pygments-main/REVISION +1 -1
  14. data/vendor/pygments-main/doc/Makefile +153 -0
  15. data/vendor/pygments-main/doc/_static/favicon.ico +0 -0
  16. data/vendor/pygments-main/doc/_static/logo_new.png +0 -0
  17. data/vendor/pygments-main/doc/_static/logo_only.png +0 -0
  18. data/vendor/pygments-main/doc/_templates/docssidebar.html +3 -0
  19. data/vendor/pygments-main/doc/_templates/indexsidebar.html +25 -0
  20. data/vendor/pygments-main/doc/_themes/pygments14/layout.html +98 -0
  21. data/vendor/pygments-main/doc/_themes/pygments14/static/bodybg.png +0 -0
  22. data/vendor/pygments-main/doc/_themes/pygments14/static/docbg.png +0 -0
  23. data/vendor/pygments-main/doc/_themes/pygments14/static/listitem.png +0 -0
  24. data/vendor/pygments-main/doc/_themes/pygments14/static/logo.png +0 -0
  25. data/vendor/pygments-main/doc/_themes/pygments14/static/pocoo.png +0 -0
  26. data/vendor/pygments-main/doc/_themes/pygments14/static/pygments14.css_t +401 -0
  27. data/vendor/pygments-main/doc/_themes/pygments14/theme.conf +15 -0
  28. data/vendor/pygments-main/doc/conf.py +249 -0
  29. data/vendor/pygments-main/doc/docs/api.rst +316 -0
  30. data/vendor/pygments-main/doc/docs/authors.rst +4 -0
  31. data/vendor/pygments-main/doc/docs/changelog.rst +1 -0
  32. data/vendor/pygments-main/{docs/src/cmdline.txt → doc/docs/cmdline.rst} +9 -11
  33. data/vendor/pygments-main/{docs/src/filterdevelopment.txt → doc/docs/filterdevelopment.rst} +1 -1
  34. data/vendor/pygments-main/{docs/src/filters.txt → doc/docs/filters.rst} +4 -5
  35. data/vendor/pygments-main/{docs/src/formatterdevelopment.txt → doc/docs/formatterdevelopment.rst} +1 -1
  36. data/vendor/pygments-main/{docs/src/formatters.txt → doc/docs/formatters.rst} +6 -6
  37. data/vendor/pygments-main/doc/docs/index.rst +66 -0
  38. data/vendor/pygments-main/{docs/src/integrate.txt → doc/docs/integrate.rst} +3 -7
  39. data/vendor/pygments-main/{docs/src/java.txt → doc/docs/java.rst} +0 -0
  40. data/vendor/pygments-main/{docs/src/lexerdevelopment.txt → doc/docs/lexerdevelopment.rst} +13 -14
  41. data/vendor/pygments-main/{docs/src/lexers.txt → doc/docs/lexers.rst} +7 -5
  42. data/vendor/pygments-main/{docs/src/moinmoin.txt → doc/docs/moinmoin.rst} +0 -0
  43. data/vendor/pygments-main/{docs/src/plugins.txt → doc/docs/plugins.rst} +0 -0
  44. data/vendor/pygments-main/{docs/src/quickstart.txt → doc/docs/quickstart.rst} +22 -19
  45. data/vendor/pygments-main/{docs/src/rstdirective.txt → doc/docs/rstdirective.rst} +0 -0
  46. data/vendor/pygments-main/{docs/src/styles.txt → doc/docs/styles.rst} +2 -2
  47. data/vendor/pygments-main/{docs/src/tokens.txt → doc/docs/tokens.rst} +10 -7
  48. data/vendor/pygments-main/{docs/src/unicode.txt → doc/docs/unicode.rst} +8 -7
  49. data/vendor/pygments-main/doc/download.rst +41 -0
  50. data/vendor/pygments-main/doc/faq.rst +143 -0
  51. data/vendor/pygments-main/doc/index.rst +53 -0
  52. data/vendor/pygments-main/doc/languages.rst +151 -0
  53. data/vendor/pygments-main/doc/make.bat +190 -0
  54. data/vendor/pygments-main/{docs → doc}/pygmentize.1 +0 -0
  55. data/vendor/pygments-main/external/autopygmentize +65 -48
  56. data/vendor/pygments-main/external/markdown-processor.py +15 -15
  57. data/vendor/pygments-main/external/moin-parser.py +1 -1
  58. data/vendor/pygments-main/external/rst-directive.py +2 -3
  59. data/vendor/pygments-main/ez_setup.py +340 -234
  60. data/vendor/pygments-main/pygments/__init__.py +4 -4
  61. data/vendor/pygments-main/pygments/cmdline.py +81 -68
  62. data/vendor/pygments-main/pygments/console.py +1 -1
  63. data/vendor/pygments-main/pygments/filter.py +1 -1
  64. data/vendor/pygments-main/pygments/filters/__init__.py +11 -9
  65. data/vendor/pygments-main/pygments/formatter.py +3 -3
  66. data/vendor/pygments-main/pygments/formatters/__init__.py +5 -3
  67. data/vendor/pygments-main/pygments/formatters/_mapping.py +16 -5
  68. data/vendor/pygments-main/pygments/formatters/bbcode.py +1 -1
  69. data/vendor/pygments-main/pygments/formatters/html.py +44 -26
  70. data/vendor/pygments-main/pygments/formatters/img.py +21 -14
  71. data/vendor/pygments-main/pygments/formatters/latex.py +104 -12
  72. data/vendor/pygments-main/pygments/formatters/other.py +53 -6
  73. data/vendor/pygments-main/pygments/formatters/rtf.py +30 -16
  74. data/vendor/pygments-main/pygments/formatters/svg.py +2 -2
  75. data/vendor/pygments-main/pygments/formatters/terminal.py +41 -1
  76. data/vendor/pygments-main/pygments/formatters/terminal256.py +2 -2
  77. data/vendor/pygments-main/pygments/lexer.py +59 -39
  78. data/vendor/pygments-main/pygments/lexers/__init__.py +38 -15
  79. data/vendor/pygments-main/pygments/lexers/_asybuiltins.py +1 -1
  80. data/vendor/pygments-main/pygments/lexers/_clbuiltins.py +15 -15
  81. data/vendor/pygments-main/pygments/lexers/_cocoabuiltins.py +73 -0
  82. data/vendor/pygments-main/pygments/lexers/_lassobuiltins.py +11 -1
  83. data/vendor/pygments-main/pygments/lexers/_luabuiltins.py +14 -8
  84. data/vendor/pygments-main/pygments/lexers/_mapping.py +81 -30
  85. data/vendor/pygments-main/pygments/lexers/_openedgebuiltins.py +1 -1
  86. data/vendor/pygments-main/pygments/lexers/_phpbuiltins.py +1067 -95
  87. data/vendor/pygments-main/pygments/lexers/_postgres_builtins.py +7 -7
  88. data/vendor/pygments-main/pygments/lexers/_robotframeworklexer.py +5 -4
  89. data/vendor/pygments-main/pygments/lexers/_scilab_builtins.py +1 -1
  90. data/vendor/pygments-main/pygments/lexers/_sourcemodbuiltins.py +12 -7
  91. data/vendor/pygments-main/pygments/lexers/_stan_builtins.py +112 -18
  92. data/vendor/pygments-main/pygments/lexers/_vimbuiltins.py +1 -1
  93. data/vendor/pygments-main/pygments/lexers/agile.py +644 -382
  94. data/vendor/pygments-main/pygments/lexers/asm.py +66 -29
  95. data/vendor/pygments-main/pygments/lexers/compiled.py +1643 -174
  96. data/vendor/pygments-main/pygments/lexers/dalvik.py +27 -6
  97. data/vendor/pygments-main/pygments/lexers/dotnet.py +45 -33
  98. data/vendor/pygments-main/pygments/lexers/foxpro.py +3 -3
  99. data/vendor/pygments-main/pygments/lexers/functional.py +1510 -570
  100. data/vendor/pygments-main/pygments/lexers/graph.py +81 -0
  101. data/vendor/pygments-main/pygments/lexers/hdl.py +29 -30
  102. data/vendor/pygments-main/pygments/lexers/inferno.py +96 -0
  103. data/vendor/pygments-main/pygments/lexers/jvm.py +622 -207
  104. data/vendor/pygments-main/pygments/lexers/math.py +438 -70
  105. data/vendor/pygments-main/pygments/lexers/other.py +885 -171
  106. data/vendor/pygments-main/pygments/lexers/parsers.py +24 -24
  107. data/vendor/pygments-main/pygments/lexers/qbasic.py +157 -0
  108. data/vendor/pygments-main/pygments/lexers/rdf.py +99 -0
  109. data/vendor/pygments-main/pygments/lexers/shell.py +28 -25
  110. data/vendor/pygments-main/pygments/lexers/special.py +7 -8
  111. data/vendor/pygments-main/pygments/lexers/sql.py +56 -23
  112. data/vendor/pygments-main/pygments/lexers/templates.py +377 -59
  113. data/vendor/pygments-main/pygments/lexers/text.py +208 -46
  114. data/vendor/pygments-main/pygments/lexers/web.py +729 -264
  115. data/vendor/pygments-main/pygments/modeline.py +1 -1
  116. data/vendor/pygments-main/pygments/plugin.py +1 -1
  117. data/vendor/pygments-main/pygments/scanner.py +1 -1
  118. data/vendor/pygments-main/pygments/sphinxext.py +153 -0
  119. data/vendor/pygments-main/pygments/style.py +3 -2
  120. data/vendor/pygments-main/pygments/styles/__init__.py +5 -1
  121. data/vendor/pygments-main/pygments/styles/autumn.py +1 -1
  122. data/vendor/pygments-main/pygments/styles/borland.py +1 -1
  123. data/vendor/pygments-main/pygments/styles/bw.py +1 -1
  124. data/vendor/pygments-main/pygments/styles/colorful.py +1 -1
  125. data/vendor/pygments-main/pygments/styles/default.py +1 -1
  126. data/vendor/pygments-main/pygments/styles/emacs.py +1 -1
  127. data/vendor/pygments-main/pygments/styles/friendly.py +1 -1
  128. data/vendor/pygments-main/pygments/styles/fruity.py +1 -1
  129. data/vendor/pygments-main/pygments/styles/igor.py +29 -0
  130. data/vendor/pygments-main/pygments/styles/manni.py +1 -1
  131. data/vendor/pygments-main/pygments/styles/monokai.py +4 -4
  132. data/vendor/pygments-main/pygments/styles/murphy.py +1 -1
  133. data/vendor/pygments-main/pygments/styles/native.py +1 -1
  134. data/vendor/pygments-main/pygments/styles/paraiso_dark.py +125 -0
  135. data/vendor/pygments-main/pygments/styles/paraiso_light.py +125 -0
  136. data/vendor/pygments-main/pygments/styles/pastie.py +1 -1
  137. data/vendor/pygments-main/pygments/styles/perldoc.py +1 -1
  138. data/vendor/pygments-main/pygments/styles/rrt.py +1 -1
  139. data/vendor/pygments-main/pygments/styles/tango.py +1 -1
  140. data/vendor/pygments-main/pygments/styles/trac.py +1 -1
  141. data/vendor/pygments-main/pygments/styles/vim.py +1 -1
  142. data/vendor/pygments-main/pygments/styles/vs.py +1 -1
  143. data/vendor/pygments-main/pygments/styles/xcode.py +51 -0
  144. data/vendor/pygments-main/pygments/token.py +4 -1
  145. data/vendor/pygments-main/pygments/unistring.py +6 -5
  146. data/vendor/pygments-main/pygments/util.py +35 -21
  147. data/vendor/pygments-main/scripts/check_sources.py +28 -44
  148. data/vendor/pygments-main/scripts/detect_missing_analyse_text.py +5 -4
  149. data/vendor/pygments-main/scripts/find_codetags.py +27 -19
  150. data/vendor/pygments-main/scripts/find_error.py +16 -13
  151. data/vendor/pygments-main/scripts/get_vimkw.py +3 -3
  152. data/vendor/pygments-main/scripts/vim2pygments.py +9 -7
  153. data/vendor/pygments-main/setup.py +19 -19
  154. data/vendor/pygments-main/tests/examplefiles/99_bottles_of_beer.chpl +118 -0
  155. data/vendor/pygments-main/tests/examplefiles/Error.pmod +38 -0
  156. data/vendor/pygments-main/tests/examplefiles/FakeFile.pike +360 -0
  157. data/vendor/pygments-main/tests/examplefiles/{ANTLRv3.g → antlr_ANTLRv3.g} +0 -0
  158. data/vendor/pygments-main/tests/examplefiles/clojure-weird-keywords.clj +5 -0
  159. data/vendor/pygments-main/tests/examplefiles/core.cljs +52 -0
  160. data/vendor/pygments-main/tests/examplefiles/demo.cfm +13 -1
  161. data/vendor/pygments-main/tests/examplefiles/demo.hbs +12 -0
  162. data/vendor/pygments-main/tests/examplefiles/ember.handlebars +33 -0
  163. data/vendor/pygments-main/tests/examplefiles/example.als +217 -0
  164. data/vendor/pygments-main/tests/examplefiles/example.c +1 -1
  165. data/vendor/pygments-main/tests/examplefiles/example.chai +6 -0
  166. data/vendor/pygments-main/tests/examplefiles/example.coffee +27 -0
  167. data/vendor/pygments-main/tests/examplefiles/example.e +124 -0
  168. data/vendor/pygments-main/tests/examplefiles/example.f90 +8 -0
  169. data/vendor/pygments-main/tests/examplefiles/example.feature +16 -0
  170. data/vendor/pygments-main/tests/examplefiles/example.gd +23 -0
  171. data/vendor/pygments-main/tests/examplefiles/example.gi +64 -0
  172. data/vendor/pygments-main/tests/examplefiles/example.groovy +2 -0
  173. data/vendor/pygments-main/tests/examplefiles/example.hs +27 -0
  174. data/vendor/pygments-main/tests/examplefiles/example.hx +44 -1
  175. data/vendor/pygments-main/tests/examplefiles/example.i6t +32 -0
  176. data/vendor/pygments-main/tests/examplefiles/example.i7x +45 -0
  177. data/vendor/pygments-main/tests/examplefiles/example.inf +374 -0
  178. data/vendor/pygments-main/tests/examplefiles/example.j +564 -0
  179. data/vendor/pygments-main/tests/examplefiles/example.java +16 -0
  180. data/vendor/pygments-main/tests/examplefiles/example.kal +75 -0
  181. data/vendor/pygments-main/tests/examplefiles/example.liquid +42 -0
  182. data/vendor/pygments-main/tests/examplefiles/example.ma +8 -0
  183. data/vendor/pygments-main/tests/examplefiles/example.mq4 +187 -0
  184. data/vendor/pygments-main/tests/examplefiles/example.mqh +123 -0
  185. data/vendor/pygments-main/tests/examplefiles/example.ni +57 -0
  186. data/vendor/pygments-main/tests/examplefiles/example.nix +80 -0
  187. data/vendor/pygments-main/tests/examplefiles/example.pp +8 -0
  188. data/vendor/pygments-main/tests/examplefiles/example.red +257 -0
  189. data/vendor/pygments-main/tests/examplefiles/example.reds +150 -0
  190. data/vendor/pygments-main/tests/examplefiles/example.rkt +677 -29
  191. data/vendor/pygments-main/tests/examplefiles/example.sh +22 -0
  192. data/vendor/pygments-main/tests/examplefiles/example.slim +31 -0
  193. data/vendor/pygments-main/tests/examplefiles/example.sls +51 -0
  194. data/vendor/pygments-main/tests/examplefiles/example.stan +2 -0
  195. data/vendor/pygments-main/tests/examplefiles/example.todotxt +9 -0
  196. data/vendor/pygments-main/tests/examplefiles/exampleScript.cfc +241 -0
  197. data/vendor/pygments-main/tests/examplefiles/exampleTag.cfc +18 -0
  198. data/vendor/pygments-main/tests/examplefiles/example_elixir.ex +192 -350
  199. data/vendor/pygments-main/tests/examplefiles/hash_syntax.rb +5 -0
  200. data/vendor/pygments-main/tests/examplefiles/hello.at +6 -0
  201. data/vendor/pygments-main/tests/examplefiles/hello.golo +5 -0
  202. data/vendor/pygments-main/tests/examplefiles/hello.lsl +12 -0
  203. data/vendor/pygments-main/tests/examplefiles/{File.hy → hybris_File.hy} +0 -0
  204. data/vendor/pygments-main/tests/examplefiles/{mg_sample.pro → idl_sample.pro} +0 -0
  205. data/vendor/pygments-main/tests/examplefiles/iex_example +23 -0
  206. data/vendor/pygments-main/tests/examplefiles/inet_pton6.dg +24 -24
  207. data/vendor/pygments-main/tests/examplefiles/language.hy +165 -0
  208. data/vendor/pygments-main/tests/examplefiles/limbo.b +456 -0
  209. data/vendor/pygments-main/tests/examplefiles/livescript-demo.ls +3 -1
  210. data/vendor/pygments-main/tests/examplefiles/main.cmake +2 -0
  211. data/vendor/pygments-main/tests/examplefiles/objc_example.m +163 -16
  212. data/vendor/pygments-main/tests/examplefiles/{example.p → openedge_example} +0 -0
  213. data/vendor/pygments-main/tests/examplefiles/pawn_example +25 -0
  214. data/vendor/pygments-main/tests/examplefiles/qbasic_example +2 -0
  215. data/vendor/pygments-main/tests/examplefiles/r6rs-comments.scm +23 -0
  216. data/vendor/pygments-main/tests/examplefiles/{robotframework.txt → robotframework_test.txt} +0 -0
  217. data/vendor/pygments-main/tests/examplefiles/rql-queries.rql +34 -0
  218. data/vendor/pygments-main/tests/examplefiles/scope.cirru +43 -0
  219. data/vendor/pygments-main/tests/examplefiles/sparql.rq +23 -0
  220. data/vendor/pygments-main/tests/examplefiles/test.R +37 -5
  221. data/vendor/pygments-main/tests/examplefiles/test.apl +26 -0
  222. data/vendor/pygments-main/tests/examplefiles/test.cyp +123 -0
  223. data/vendor/pygments-main/tests/examplefiles/test.idr +93 -0
  224. data/vendor/pygments-main/tests/examplefiles/test.mask +41 -0
  225. data/vendor/pygments-main/tests/examplefiles/test.pan +54 -0
  226. data/vendor/pygments-main/tests/examplefiles/test.php +3 -1
  227. data/vendor/pygments-main/tests/examplefiles/test.pig +148 -0
  228. data/vendor/pygments-main/tests/examplefiles/test.pwn +253 -0
  229. data/vendor/pygments-main/tests/examplefiles/test.r3 +27 -7
  230. data/vendor/pygments-main/tests/examplefiles/test.rsl +111 -0
  231. data/vendor/pygments-main/tests/examplefiles/test.swift +65 -0
  232. data/vendor/pygments-main/tests/examplefiles/test.zep +33 -0
  233. data/vendor/pygments-main/tests/examplefiles/unicode.go +10 -0
  234. data/vendor/pygments-main/tests/examplefiles/{test.bas → vbnet_test.bas} +0 -0
  235. data/vendor/pygments-main/tests/examplefiles/vctreestatus_hg +4 -0
  236. data/vendor/pygments-main/tests/examplefiles/vimrc +21 -0
  237. data/vendor/pygments-main/tests/run.py +14 -19
  238. data/vendor/pygments-main/tests/string_asserts.py +22 -0
  239. data/vendor/pygments-main/tests/test_basic_api.py +28 -14
  240. data/vendor/pygments-main/tests/test_cfm.py +46 -0
  241. data/vendor/pygments-main/tests/test_clexer.py +208 -3
  242. data/vendor/pygments-main/tests/test_cmdline.py +6 -5
  243. data/vendor/pygments-main/tests/test_examplefiles.py +30 -19
  244. data/vendor/pygments-main/tests/test_html_formatter.py +15 -13
  245. data/vendor/pygments-main/tests/test_latex_formatter.py +4 -2
  246. data/vendor/pygments-main/tests/test_lexers_other.py +11 -11
  247. data/vendor/pygments-main/tests/test_objectiveclexer.py +81 -0
  248. data/vendor/pygments-main/tests/test_perllexer.py +1 -1
  249. data/vendor/pygments-main/tests/test_qbasiclexer.py +43 -0
  250. data/vendor/pygments-main/tests/test_regexlexer.py +8 -1
  251. data/vendor/pygments-main/tests/test_rtf_formatter.py +109 -0
  252. data/vendor/pygments-main/tests/test_string_asserts.py +39 -0
  253. data/vendor/pygments-main/tests/test_token.py +3 -3
  254. data/vendor/pygments-main/tests/test_using_api.py +1 -1
  255. data/vendor/pygments-main/tests/test_util.py +1 -1
  256. metadata +138 -51
  257. data/vendor/pygments-main/docs/generate.py +0 -472
  258. data/vendor/pygments-main/docs/src/api.txt +0 -270
  259. data/vendor/pygments-main/docs/src/authors.txt +0 -5
  260. data/vendor/pygments-main/docs/src/changelog.txt +0 -5
  261. data/vendor/pygments-main/docs/src/index.txt +0 -69
  262. data/vendor/pygments-main/docs/src/installation.txt +0 -71
  263. data/vendor/pygments-main/external/rst-directive-old.py +0 -77
  264. data/vendor/pygments-main/scripts/reindent.py +0 -291
  265. data/vendor/pygments-main/tests/examplefiles/import.hs +0 -4
  266. data/vendor/pygments-main/tests/examplefiles/objc_example2.m +0 -24
  267. data/vendor/pygments-main/tests/old_run.py +0 -138
@@ -1,64 +1,81 @@
1
- #!/bin/sh
1
+ #!/bin/bash
2
2
  # Best effort auto-pygmentization with transparent decompression
3
3
  # (c) Reuben Thomas 2012-2013
4
4
  # This program is in the public domain.
5
5
 
6
6
  # Strategy: first see if pygmentize can find a lexer; if not, ask file; if that finds nothing, fail
7
- # Set the environment variable PYGMENTIZE_OPTS to configure pygments.
7
+ # Set the environment variable PYGMENTIZE_OPTS or pass options before the file path to configure pygments.
8
8
 
9
9
  # This program can be used as a .lessfilter for the less pager to auto-color less's output
10
10
 
11
- lexer=`pygmentize -N "$1"`
12
- if [ "$lexer" = "text" ]; then
13
- file_common_opts="--brief --dereference --uncompress"
11
+ file="${!#}" # last argument
12
+ options=${@:1:$(($#-1))} # handle others args as options to pass to pygmentize
14
13
 
15
- unset lexer
16
- case `file --mime-type $file_common_opts "$1"` in
17
- application/xml|image/svg+xml) lexer=xml;;
18
- text/html) lexer=html;;
19
- text/troff) lexer=nroff;;
20
- text/x-asm) lexer=nasm;;
21
- text/x-awk) lexer=awk;;
22
- text/x-c) lexer=c;;
23
- text/x-c++) lexer=cpp;;
24
- text/x-diff) lexer=diff;;
25
- text/x-fortran) lexer=fortran;;
26
- text/x-gawk) lexer=gawk;;
27
- text/x-java) lexer=java;;
28
- text/x-lisp) lexer=common-lisp;;
29
- text/x-lua) lexer=lua;;
30
- text/x-makefile) lexer=make;;
31
- text/x-msdos-batch) lexer=bat;;
32
- text/x-nawk) lexer=nawk;;
33
- text/x-pascal) lexer=pascal;;
34
- text/x-perl) lexer=perl;;
35
- text/x-php) lexer=php;;
36
- text/x-po) lexer=po;;
37
- text/x-python) lexer=python;;
38
- text/x-ruby) lexer=ruby;;
39
- text/x-shellscript) lexer=sh;;
40
- text/x-tcl) lexer=tcl;;
41
- text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
42
-
43
- # Types that file outputs which pygmentize didn't support as of file 5.11, pygments 1.6rc1
44
- # text/calendar
45
- # text/PGP
46
- # text/rtf
47
- # text/texmacs
48
- # text/x-bcpl
49
- # text/x-info
50
- # text/x-m4
51
- # text/x-vcard
52
- # text/x-xmcd
53
- esac
14
+ file_common_opts="--brief --dereference"
15
+
16
+ lexer=$(pygmentize -N "$file")
17
+ if [[ "$lexer" == text ]]; then
18
+ unset lexer
19
+ case $(file --mime-type --uncompress $file_common_opts "$file") in
20
+ application/xml|image/svg+xml) lexer=xml;;
21
+ text/html) lexer=html;;
22
+ text/troff) lexer=nroff;;
23
+ text/x-asm) lexer=nasm;;
24
+ text/x-awk) lexer=awk;;
25
+ text/x-c) lexer=c;;
26
+ text/x-c++) lexer=cpp;;
27
+ text/x-diff) lexer=diff;;
28
+ text/x-fortran) lexer=fortran;;
29
+ text/x-gawk) lexer=gawk;;
30
+ text/x-java) lexer=java;;
31
+ text/x-lisp) lexer=common-lisp;;
32
+ text/x-lua) lexer=lua;;
33
+ text/x-makefile) lexer=make;;
34
+ text/x-msdos-batch) lexer=bat;;
35
+ text/x-nawk) lexer=nawk;;
36
+ text/x-pascal) lexer=pascal;;
37
+ text/x-perl) lexer=perl;;
38
+ text/x-php) lexer=php;;
39
+ text/x-po) lexer=po;;
40
+ text/x-python) lexer=python;;
41
+ text/x-ruby) lexer=ruby;;
42
+ text/x-shellscript) lexer=sh;;
43
+ text/x-tcl) lexer=tcl;;
44
+ text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
45
+
46
+ # Types that file outputs which pygmentize didn't support as of file 5.11, pygments 1.6rc1
47
+ # text/calendar
48
+ # text/PGP
49
+ # text/rtf
50
+ # text/texmacs
51
+ # text/x-bcpl
52
+ # text/x-info
53
+ # text/x-m4
54
+ # text/x-vcard
55
+ # text/x-xmcd
56
+
57
+ text/plain) # special filenames. TODO: insert more
58
+ case $(basename "$file") in
59
+ .zshrc) lexer=sh;;
60
+ esac
61
+ ;;
62
+ esac
54
63
  fi
55
64
 
56
- encoding=`file --brief --mime-encoding $file_common_opts "$1"`
65
+ encoding=$(file --mime-encoding --uncompress $file_common_opts "$file")
66
+ if [[ $encoding == "us-asciibinarybinary" ]]; then
67
+ encoding="us-ascii"
68
+ fi
57
69
 
58
- if [ -n "$lexer" ]; then
70
+ if [[ -n "$lexer" ]]; then
71
+ concat=cat
72
+ case $(file $file_common_opts --mime-type "$file") in
73
+ application/x-gzip) concat=zcat;;
74
+ application/x-bzip2) concat=bzcat;;
75
+ application/x-xz) concat=xzcat;;
76
+ esac
59
77
  # FIXME: Specify input encoding rather than output encoding https://bitbucket.org/birkenfeld/pygments-main/issue/800
60
- zcat "$1" | pygmentize -O encoding=$encoding,outencoding=UTF-8 $PYGMENTIZE_OPTS -l $lexer
61
- exit 0
78
+ exec $concat "$file" | pygmentize -f terminal256 -O style=native,encoding=$encoding,outencoding=UTF-8 $PYGMENTIZE_OPTS $options -l $lexer
62
79
  fi
63
80
 
64
81
  exit 1
@@ -6,14 +6,9 @@
6
6
  This fragment is a Markdown_ preprocessor that renders source code
7
7
  to HTML via Pygments. To use it, invoke Markdown like so::
8
8
 
9
- from markdown import Markdown
9
+ import markdown
10
10
 
11
- md = Markdown()
12
- md.textPreprocessors.insert(0, CodeBlockPreprocessor())
13
- html = md.convert(someText)
14
-
15
- markdown is then a callable that can be passed to the context of
16
- a template and used in that template, for example.
11
+ html = markdown.markdown(someText, extensions=[CodeBlockExtension()])
17
12
 
18
13
  This uses CSS classes by default, so use
19
14
  ``pygmentize -S <some style> -f html > pygments.css``
@@ -25,9 +20,9 @@
25
20
  some code
26
21
  [/sourcecode]
27
22
 
28
- .. _Markdown: http://www.freewisdom.org/projects/python-markdown/
23
+ .. _Markdown: https://pypi.python.org/pypi/Markdown
29
24
 
30
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
25
+ :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
31
26
  :license: BSD, see LICENSE for details.
32
27
  """
33
28
 
@@ -40,17 +35,17 @@ INLINESTYLES = False
40
35
 
41
36
  import re
42
37
 
43
- from markdown import TextPreprocessor
38
+ from markdown.preprocessors import Preprocessor
39
+ from markdown.extensions import Extension
44
40
 
45
41
  from pygments import highlight
46
42
  from pygments.formatters import HtmlFormatter
47
43
  from pygments.lexers import get_lexer_by_name, TextLexer
48
44
 
49
45
 
50
- class CodeBlockPreprocessor(TextPreprocessor):
46
+ class CodeBlockPreprocessor(Preprocessor):
51
47
 
52
- pattern = re.compile(
53
- r'\[sourcecode:(.+?)\](.+?)\[/sourcecode\]', re.S)
48
+ pattern = re.compile(r'\[sourcecode:(.+?)\](.+?)\[/sourcecode\]', re.S)
54
49
 
55
50
  formatter = HtmlFormatter(noclasses=INLINESTYLES)
56
51
 
@@ -63,5 +58,10 @@ class CodeBlockPreprocessor(TextPreprocessor):
63
58
  code = highlight(m.group(2), lexer, self.formatter)
64
59
  code = code.replace('\n\n', '\n&nbsp;\n').replace('\n', '<br />')
65
60
  return '\n\n<div class="code">%s</div>\n\n' % code
66
- return self.pattern.sub(
67
- repl, lines)
61
+ joined_lines = "\n".join(lines)
62
+ joined_lines = self.pattern.sub(repl, joined_lines)
63
+ return joined_lines.split("\n")
64
+
65
+ class CodeBlockExtension(Extension):
66
+ def extendMarkdown(self, md, md_globals):
67
+ md.preprocessors.add('CodeBlockPreprocessor', CodeBlockPreprocessor(), '_begin')
@@ -31,7 +31,7 @@
31
31
  If you do not want to do that and are willing to accept larger HTML
32
32
  output, you can set the INLINESTYLES option below to True.
33
33
 
34
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
34
+ :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
35
35
  :license: BSD, see LICENSE for details.
36
36
  """
37
37
 
@@ -31,7 +31,7 @@
31
31
  .. _directive documentation:
32
32
  http://docutils.sourceforge.net/docs/howto/rst-directives.html
33
33
 
34
- :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
34
+ :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
35
35
  :license: BSD, see LICENSE for details.
36
36
  """
37
37
 
@@ -75,9 +75,8 @@ class Pygments(Directive):
75
75
  # no lexer found - use the text one instead of an exception
76
76
  lexer = TextLexer()
77
77
  # take an arbitrary option if more than one is given
78
- formatter = self.options and VARIANTS[self.options.keys()[0]] or DEFAULT
78
+ formatter = self.options and VARIANTS[list(self.options)[0]] or DEFAULT
79
79
  parsed = highlight(u'\n'.join(self.content), lexer, formatter)
80
80
  return [nodes.raw('', parsed, format='html')]
81
81
 
82
82
  directives.register_directive('sourcecode', Pygments)
83
-
@@ -13,264 +13,370 @@ the appropriate options to ``use_setuptools()``.
13
13
 
14
14
  This file can also be run as a script to install or upgrade setuptools.
15
15
  """
16
+ import os
17
+ import shutil
16
18
  import sys
17
- DEFAULT_VERSION = "0.6c9"
18
- DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3]
19
-
20
- md5_data = {
21
- 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca',
22
- 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb',
23
- 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b',
24
- 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a',
25
- 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618',
26
- 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac',
27
- 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5',
28
- 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4',
29
- 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c',
30
- 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b',
31
- 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27',
32
- 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277',
33
- 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa',
34
- 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e',
35
- 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e',
36
- 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f',
37
- 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2',
38
- 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc',
39
- 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167',
40
- 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64',
41
- 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d',
42
- 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20',
43
- 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab',
44
- 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53',
45
- 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2',
46
- 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e',
47
- 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372',
48
- 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902',
49
- 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de',
50
- 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b',
51
- 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03',
52
- 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a',
53
- 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6',
54
- 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a',
55
- }
56
-
57
- import sys, os
58
- try: from hashlib import md5
59
- except ImportError: from md5 import md5
60
-
61
- def _validate_md5(egg_name, data):
62
- if egg_name in md5_data:
63
- digest = md5(data).hexdigest()
64
- if digest != md5_data[egg_name]:
65
- print >>sys.stderr, (
66
- "md5 validation of %s failed! (Possible download problem?)"
67
- % egg_name
68
- )
69
- sys.exit(2)
70
- return data
71
-
72
- def use_setuptools(
73
- version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
74
- download_delay=15
75
- ):
76
- """Automatically find/download setuptools and make it available on sys.path
77
-
78
- `version` should be a valid setuptools version number that is available
79
- as an egg for download under the `download_base` URL (which should end with
80
- a '/'). `to_dir` is the directory where setuptools will be downloaded, if
81
- it is not already available. If `download_delay` is specified, it should
82
- be the number of seconds that will be paused before initiating a download,
83
- should one be required. If an older version of setuptools is installed,
84
- this routine will print a message to ``sys.stderr`` and raise SystemExit in
85
- an attempt to abort the calling script.
86
- """
87
- was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules
88
- def do_download():
89
- egg = download_setuptools(version, download_base, to_dir, download_delay)
90
- sys.path.insert(0, egg)
91
- import setuptools; setuptools.bootstrap_install_from = egg
19
+ import tempfile
20
+ import tarfile
21
+ import optparse
22
+ import subprocess
23
+ import platform
24
+
25
+ from distutils import log
26
+
27
+ try:
28
+ from site import USER_SITE
29
+ except ImportError:
30
+ USER_SITE = None
31
+
32
+ DEFAULT_VERSION = "1.4.2"
33
+ DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
34
+
35
+ def _python_cmd(*args):
36
+ args = (sys.executable,) + args
37
+ return subprocess.call(args) == 0
38
+
39
+ def _check_call_py24(cmd, *args, **kwargs):
40
+ res = subprocess.call(cmd, *args, **kwargs)
41
+ class CalledProcessError(Exception):
42
+ pass
43
+ if not res == 0:
44
+ msg = "Command '%s' return non-zero exit status %d" % (cmd, res)
45
+ raise CalledProcessError(msg)
46
+ vars(subprocess).setdefault('check_call', _check_call_py24)
47
+
48
+ def _install(tarball, install_args=()):
49
+ # extracting the tarball
50
+ tmpdir = tempfile.mkdtemp()
51
+ log.warn('Extracting in %s', tmpdir)
52
+ old_wd = os.getcwd()
53
+ try:
54
+ os.chdir(tmpdir)
55
+ tar = tarfile.open(tarball)
56
+ _extractall(tar)
57
+ tar.close()
58
+
59
+ # going in the directory
60
+ subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
61
+ os.chdir(subdir)
62
+ log.warn('Now working in %s', subdir)
63
+
64
+ # installing
65
+ log.warn('Installing Setuptools')
66
+ if not _python_cmd('setup.py', 'install', *install_args):
67
+ log.warn('Something went wrong during the installation.')
68
+ log.warn('See the error message above.')
69
+ # exitcode will be 2
70
+ return 2
71
+ finally:
72
+ os.chdir(old_wd)
73
+ shutil.rmtree(tmpdir)
74
+
75
+
76
+ def _build_egg(egg, tarball, to_dir):
77
+ # extracting the tarball
78
+ tmpdir = tempfile.mkdtemp()
79
+ log.warn('Extracting in %s', tmpdir)
80
+ old_wd = os.getcwd()
81
+ try:
82
+ os.chdir(tmpdir)
83
+ tar = tarfile.open(tarball)
84
+ _extractall(tar)
85
+ tar.close()
86
+
87
+ # going in the directory
88
+ subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
89
+ os.chdir(subdir)
90
+ log.warn('Now working in %s', subdir)
91
+
92
+ # building an egg
93
+ log.warn('Building a Setuptools egg in %s', to_dir)
94
+ _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
95
+
96
+ finally:
97
+ os.chdir(old_wd)
98
+ shutil.rmtree(tmpdir)
99
+ # returning the result
100
+ log.warn(egg)
101
+ if not os.path.exists(egg):
102
+ raise IOError('Could not build the egg.')
103
+
104
+
105
+ def _do_download(version, download_base, to_dir, download_delay):
106
+ egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
107
+ % (version, sys.version_info[0], sys.version_info[1]))
108
+ if not os.path.exists(egg):
109
+ tarball = download_setuptools(version, download_base,
110
+ to_dir, download_delay)
111
+ _build_egg(egg, tarball, to_dir)
112
+ sys.path.insert(0, egg)
113
+
114
+ # Remove previously-imported pkg_resources if present (see
115
+ # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
116
+ if 'pkg_resources' in sys.modules:
117
+ del sys.modules['pkg_resources']
118
+
119
+ import setuptools
120
+ setuptools.bootstrap_install_from = egg
121
+
122
+
123
+ def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
124
+ to_dir=os.curdir, download_delay=15):
125
+ # making sure we use the absolute path
126
+ to_dir = os.path.abspath(to_dir)
127
+ was_imported = 'pkg_resources' in sys.modules or \
128
+ 'setuptools' in sys.modules
92
129
  try:
93
130
  import pkg_resources
94
131
  except ImportError:
95
- return do_download()
132
+ return _do_download(version, download_base, to_dir, download_delay)
96
133
  try:
97
- pkg_resources.require("setuptools>="+version); return
98
- except pkg_resources.VersionConflict, e:
134
+ pkg_resources.require("setuptools>=" + version)
135
+ return
136
+ except pkg_resources.VersionConflict:
137
+ e = sys.exc_info()[1]
99
138
  if was_imported:
100
- print >>sys.stderr, (
101
- "The required version of setuptools (>=%s) is not available, and\n"
102
- "can't be installed while this script is running. Please install\n"
103
- " a more recent version first, using 'easy_install -U setuptools'."
104
- "\n\n(Currently using %r)"
105
- ) % (version, e.args[0])
139
+ sys.stderr.write(
140
+ "The required version of setuptools (>=%s) is not available,\n"
141
+ "and can't be installed while this script is running. Please\n"
142
+ "install a more recent version first, using\n"
143
+ "'easy_install -U setuptools'."
144
+ "\n\n(Currently using %r)\n" % (version, e.args[0]))
106
145
  sys.exit(2)
107
146
  else:
108
147
  del pkg_resources, sys.modules['pkg_resources'] # reload ok
109
- return do_download()
148
+ return _do_download(version, download_base, to_dir,
149
+ download_delay)
110
150
  except pkg_resources.DistributionNotFound:
111
- return do_download()
151
+ return _do_download(version, download_base, to_dir,
152
+ download_delay)
112
153
 
113
- def download_setuptools(
114
- version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir,
115
- delay = 15
116
- ):
117
- """Download setuptools from a specified location and return its filename
154
+ def _clean_check(cmd, target):
155
+ """
156
+ Run the command to download target. If the command fails, clean up before
157
+ re-raising the error.
158
+ """
159
+ try:
160
+ subprocess.check_call(cmd)
161
+ except subprocess.CalledProcessError:
162
+ if os.access(target, os.F_OK):
163
+ os.unlink(target)
164
+ raise
118
165
 
119
- `version` should be a valid setuptools version number that is available
120
- as an egg for download under the `download_base` URL (which should end
121
- with a '/'). `to_dir` is the directory where the egg will be downloaded.
122
- `delay` is the number of seconds to pause before an actual download attempt.
166
+ def download_file_powershell(url, target):
123
167
  """
124
- import urllib2, shutil
125
- egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3])
126
- url = download_base + egg_name
127
- saveto = os.path.join(to_dir, egg_name)
128
- src = dst = None
129
- if not os.path.exists(saveto): # Avoid repeated downloads
168
+ Download the file at url to target using Powershell (which will validate
169
+ trust). Raise an exception if the command cannot complete.
170
+ """
171
+ target = os.path.abspath(target)
172
+ cmd = [
173
+ 'powershell',
174
+ '-Command',
175
+ "(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
176
+ ]
177
+ _clean_check(cmd, target)
178
+
179
+ def has_powershell():
180
+ if platform.system() != 'Windows':
181
+ return False
182
+ cmd = ['powershell', '-Command', 'echo test']
183
+ devnull = open(os.path.devnull, 'wb')
184
+ try:
130
185
  try:
131
- from distutils import log
132
- if delay:
133
- log.warn("""
134
- ---------------------------------------------------------------------------
135
- This script requires setuptools version %s to run (even to display
136
- help). I will attempt to download it for you (from
137
- %s), but
138
- you may need to enable firewall access for this script first.
139
- I will start the download in %d seconds.
140
-
141
- (Note: if this machine does not have network access, please obtain the file
142
-
143
- %s
144
-
145
- and place it in this directory before rerunning this script.)
146
- ---------------------------------------------------------------------------""",
147
- version, download_base, delay, url
148
- ); from time import sleep; sleep(delay)
149
- log.warn("Downloading %s", url)
150
- src = urllib2.urlopen(url)
151
- # Read/write all in one block, so we don't create a corrupt file
152
- # if the download is interrupted.
153
- data = _validate_md5(egg_name, src.read())
154
- dst = open(saveto,"wb"); dst.write(data)
155
- finally:
156
- if src: src.close()
157
- if dst: dst.close()
158
- return os.path.realpath(saveto)
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
- def main(argv, version=DEFAULT_VERSION):
196
- """Install or upgrade setuptools and EasyInstall"""
186
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
187
+ except:
188
+ return False
189
+ finally:
190
+ devnull.close()
191
+ return True
192
+
193
+ download_file_powershell.viable = has_powershell
194
+
195
+ def download_file_curl(url, target):
196
+ cmd = ['curl', url, '--silent', '--output', target]
197
+ _clean_check(cmd, target)
198
+
199
+ def has_curl():
200
+ cmd = ['curl', '--version']
201
+ devnull = open(os.path.devnull, 'wb')
197
202
  try:
198
- import setuptools
199
- except ImportError:
200
- egg = None
201
203
  try:
202
- egg = download_setuptools(version, delay=0)
203
- sys.path.insert(0,egg)
204
- from setuptools.command.easy_install import main
205
- return main(list(argv)+[egg]) # we're done here
206
- finally:
207
- if egg and os.path.exists(egg):
208
- os.unlink(egg)
209
- else:
210
- if setuptools.__version__ == '0.0.1':
211
- print >>sys.stderr, (
212
- "You have an obsolete version of setuptools installed. Please\n"
213
- "remove it from your system entirely before rerunning this script."
214
- )
215
- sys.exit(2)
216
-
217
- req = "setuptools>="+version
218
- import pkg_resources
204
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
205
+ except:
206
+ return False
207
+ finally:
208
+ devnull.close()
209
+ return True
210
+
211
+ download_file_curl.viable = has_curl
212
+
213
+ def download_file_wget(url, target):
214
+ cmd = ['wget', url, '--quiet', '--output-document', target]
215
+ _clean_check(cmd, target)
216
+
217
+ def has_wget():
218
+ cmd = ['wget', '--version']
219
+ devnull = open(os.path.devnull, 'wb')
219
220
  try:
220
- pkg_resources.require(req)
221
- except pkg_resources.VersionConflict:
222
221
  try:
223
- from setuptools.command.easy_install import main
224
- except ImportError:
225
- from easy_install import main
226
- main(list(argv)+[download_setuptools(delay=0)])
227
- sys.exit(0) # try to force an exit
228
- else:
229
- if argv:
230
- from setuptools.command.easy_install import main
231
- main(argv)
232
- else:
233
- print "Setuptools version",version,"or greater has been installed."
234
- print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)'
222
+ subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
223
+ except:
224
+ return False
225
+ finally:
226
+ devnull.close()
227
+ return True
235
228
 
236
- def update_md5(filenames):
237
- """Update our built-in md5 registry"""
229
+ download_file_wget.viable = has_wget
238
230
 
239
- import re
240
-
241
- for name in filenames:
242
- base = os.path.basename(name)
243
- f = open(name,'rb')
244
- md5_data[base] = md5(f.read()).hexdigest()
245
- f.close()
246
-
247
- data = [" %r: %r,\n" % it for it in md5_data.items()]
248
- data.sort()
249
- repl = "".join(data)
250
-
251
- import inspect
252
- srcfile = inspect.getsourcefile(sys.modules[__name__])
253
- f = open(srcfile, 'rb'); src = f.read(); f.close()
231
+ def download_file_insecure(url, target):
232
+ """
233
+ Use Python to download the file, even though it cannot authenticate the
234
+ connection.
235
+ """
236
+ try:
237
+ from urllib.request import urlopen
238
+ except ImportError:
239
+ from urllib2 import urlopen
240
+ src = dst = None
241
+ try:
242
+ src = urlopen(url)
243
+ # Read/write all in one block, so we don't create a corrupt file
244
+ # if the download is interrupted.
245
+ data = src.read()
246
+ dst = open(target, "wb")
247
+ dst.write(data)
248
+ finally:
249
+ if src:
250
+ src.close()
251
+ if dst:
252
+ dst.close()
253
+
254
+ download_file_insecure.viable = lambda: True
255
+
256
+ def get_best_downloader():
257
+ downloaders = [
258
+ download_file_powershell,
259
+ download_file_curl,
260
+ download_file_wget,
261
+ download_file_insecure,
262
+ ]
263
+
264
+ for dl in downloaders:
265
+ if dl.viable():
266
+ return dl
267
+
268
+ def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
269
+ to_dir=os.curdir, delay=15,
270
+ downloader_factory=get_best_downloader):
271
+ """Download setuptools from a specified location and return its filename
254
272
 
255
- match = re.search("\nmd5_data = {\n([^}]+)}", src)
256
- if not match:
257
- print >>sys.stderr, "Internal error!"
258
- sys.exit(2)
273
+ `version` should be a valid setuptools version number that is available
274
+ as an egg for download under the `download_base` URL (which should end
275
+ with a '/'). `to_dir` is the directory where the egg will be downloaded.
276
+ `delay` is the number of seconds to pause before an actual download
277
+ attempt.
259
278
 
260
- src = src[:match.start(1)] + repl + src[match.end(1):]
261
- f = open(srcfile,'w')
262
- f.write(src)
263
- f.close()
279
+ ``downloader_factory`` should be a function taking no arguments and
280
+ returning a function for downloading a URL to a target.
281
+ """
282
+ # making sure we use the absolute path
283
+ to_dir = os.path.abspath(to_dir)
284
+ tgz_name = "setuptools-%s.tar.gz" % version
285
+ url = download_base + tgz_name
286
+ saveto = os.path.join(to_dir, tgz_name)
287
+ if not os.path.exists(saveto): # Avoid repeated downloads
288
+ log.warn("Downloading %s", url)
289
+ downloader = downloader_factory()
290
+ downloader(url, saveto)
291
+ return os.path.realpath(saveto)
264
292
 
265
293
 
266
- if __name__=='__main__':
267
- if len(sys.argv)>2 and sys.argv[1]=='--md5update':
268
- update_md5(sys.argv[2:])
294
+ def _extractall(self, path=".", members=None):
295
+ """Extract all members from the archive to the current working
296
+ directory and set owner, modification time and permissions on
297
+ directories afterwards. `path' specifies a different directory
298
+ to extract to. `members' is optional and must be a subset of the
299
+ list returned by getmembers().
300
+ """
301
+ import copy
302
+ import operator
303
+ from tarfile import ExtractError
304
+ directories = []
305
+
306
+ if members is None:
307
+ members = self
308
+
309
+ for tarinfo in members:
310
+ if tarinfo.isdir():
311
+ # Extract directories with a safe mode.
312
+ directories.append(tarinfo)
313
+ tarinfo = copy.copy(tarinfo)
314
+ tarinfo.mode = 448 # decimal for oct 0700
315
+ self.extract(tarinfo, path)
316
+
317
+ # Reverse sort directories.
318
+ if sys.version_info < (2, 4):
319
+ def sorter(dir1, dir2):
320
+ return cmp(dir1.name, dir2.name)
321
+ directories.sort(sorter)
322
+ directories.reverse()
269
323
  else:
270
- main(sys.argv[1:])
271
-
272
-
273
-
274
-
324
+ directories.sort(key=operator.attrgetter('name'), reverse=True)
275
325
 
326
+ # Set correct owner, mtime and filemode on directories.
327
+ for tarinfo in directories:
328
+ dirpath = os.path.join(path, tarinfo.name)
329
+ try:
330
+ self.chown(tarinfo, dirpath)
331
+ self.utime(tarinfo, dirpath)
332
+ self.chmod(tarinfo, dirpath)
333
+ except ExtractError:
334
+ e = sys.exc_info()[1]
335
+ if self.errorlevel > 1:
336
+ raise
337
+ else:
338
+ self._dbg(1, "tarfile: %s" % e)
339
+
340
+
341
+ def _build_install_args(options):
342
+ """
343
+ Build the arguments to 'python setup.py install' on the setuptools package
344
+ """
345
+ install_args = []
346
+ if options.user_install:
347
+ if sys.version_info < (2, 6):
348
+ log.warn("--user requires Python 2.6 or later")
349
+ raise SystemExit(1)
350
+ install_args.append('--user')
351
+ return install_args
352
+
353
+ def _parse_args():
354
+ """
355
+ Parse the command line for options
356
+ """
357
+ parser = optparse.OptionParser()
358
+ parser.add_option(
359
+ '--user', dest='user_install', action='store_true', default=False,
360
+ help='install in user site package (requires Python 2.6 or later)')
361
+ parser.add_option(
362
+ '--download-base', dest='download_base', metavar="URL",
363
+ default=DEFAULT_URL,
364
+ help='alternative URL from where to download the setuptools package')
365
+ parser.add_option(
366
+ '--insecure', dest='downloader_factory', action='store_const',
367
+ const=lambda: download_file_insecure, default=get_best_downloader,
368
+ help='Use internal, non-validating downloader'
369
+ )
370
+ options, args = parser.parse_args()
371
+ # positional arguments are ignored
372
+ return options
373
+
374
+ def main(version=DEFAULT_VERSION):
375
+ """Install or upgrade setuptools and EasyInstall"""
376
+ options = _parse_args()
377
+ tarball = download_setuptools(download_base=options.download_base,
378
+ downloader_factory=options.downloader_factory)
379
+ return _install(tarball, _build_install_args(options))
276
380
 
381
+ if __name__ == '__main__':
382
+ sys.exit(main())