installers.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.installers
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for installer/packager DSLs and formats.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups, using, this, default
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Punctuation, Generic, Number, Whitespace
  13. __all__ = ['NSISLexer', 'RPMSpecLexer', 'SourcesListLexer',
  14. 'DebianControlLexer']
  15. class NSISLexer(RegexLexer):
  16. """
  17. For `NSIS <http://nsis.sourceforge.net/>`_ scripts.
  18. .. versionadded:: 1.6
  19. """
  20. name = 'NSIS'
  21. aliases = ['nsis', 'nsi', 'nsh']
  22. filenames = ['*.nsi', '*.nsh']
  23. mimetypes = ['text/x-nsis']
  24. flags = re.IGNORECASE
  25. tokens = {
  26. 'root': [
  27. (r'[;#].*\n', Comment),
  28. (r"'.*?'", String.Single),
  29. (r'"', String.Double, 'str_double'),
  30. (r'`', String.Backtick, 'str_backtick'),
  31. include('macro'),
  32. include('interpol'),
  33. include('basic'),
  34. (r'\$\{[a-z_|][\w|]*\}', Keyword.Pseudo),
  35. (r'/[a-z_]\w*', Name.Attribute),
  36. ('.', Text),
  37. ],
  38. 'basic': [
  39. (r'(\n)(Function)(\s+)([._a-z][.\w]*)\b',
  40. bygroups(Text, Keyword, Text, Name.Function)),
  41. (r'\b([_a-z]\w*)(::)([a-z][a-z0-9]*)\b',
  42. bygroups(Keyword.Namespace, Punctuation, Name.Function)),
  43. (r'\b([_a-z]\w*)(:)', bygroups(Name.Label, Punctuation)),
  44. (r'(\b[ULS]|\B)([!<>=]?=|\<\>?|\>)\B', Operator),
  45. (r'[|+-]', Operator),
  46. (r'\\', Punctuation),
  47. (r'\b(Abort|Add(?:BrandingImage|Size)|'
  48. r'Allow(?:RootDirInstall|SkipFiles)|AutoCloseWindow|'
  49. r'BG(?:Font|Gradient)|BrandingText|BringToFront|Call(?:InstDLL)?|'
  50. r'(?:Sub)?Caption|ChangeUI|CheckBitmap|ClearErrors|CompletedText|'
  51. r'ComponentText|CopyFiles|CRCCheck|'
  52. r'Create(?:Directory|Font|Shortcut)|Delete(?:INI(?:Sec|Str)|'
  53. r'Reg(?:Key|Value))?|DetailPrint|DetailsButtonText|'
  54. r'Dir(?:Show|Text|Var|Verify)|(?:Disabled|Enabled)Bitmap|'
  55. r'EnableWindow|EnumReg(?:Key|Value)|Exch|Exec(?:Shell|Wait)?|'
  56. r'ExpandEnvStrings|File(?:BufSize|Close|ErrorText|Open|'
  57. r'Read(?:Byte)?|Seek|Write(?:Byte)?)?|'
  58. r'Find(?:Close|First|Next|Window)|FlushINI|Function(?:End)?|'
  59. r'Get(?:CurInstType|CurrentAddress|DlgItem|DLLVersion(?:Local)?|'
  60. r'ErrorLevel|FileTime(?:Local)?|FullPathName|FunctionAddress|'
  61. r'InstDirError|LabelAddress|TempFileName)|'
  62. r'Goto|HideWindow|Icon|'
  63. r'If(?:Abort|Errors|FileExists|RebootFlag|Silent)|'
  64. r'InitPluginsDir|Install(?:ButtonText|Colors|Dir(?:RegKey)?)|'
  65. r'Inst(?:ProgressFlags|Type(?:[GS]etText)?)|Int(?:CmpU?|Fmt|Op)|'
  66. r'IsWindow|LangString(?:UP)?|'
  67. r'License(?:BkColor|Data|ForceSelection|LangString|Text)|'
  68. r'LoadLanguageFile|LockWindow|Log(?:Set|Text)|MessageBox|'
  69. r'MiscButtonText|Name|Nop|OutFile|(?:Uninst)?Page(?:Ex(?:End)?)?|'
  70. r'PluginDir|Pop|Push|Quit|Read(?:(?:Env|INI|Reg)Str|RegDWORD)|'
  71. r'Reboot|(?:Un)?RegDLL|Rename|RequestExecutionLevel|ReserveFile|'
  72. r'Return|RMDir|SearchPath|Section(?:Divider|End|'
  73. r'(?:(?:Get|Set)(?:Flags|InstTypes|Size|Text))|Group(?:End)?|In)?|'
  74. r'SendMessage|Set(?:AutoClose|BrandingImage|Compress(?:ionLevel|'
  75. r'or(?:DictSize)?)?|CtlColors|CurInstType|DatablockOptimize|'
  76. r'DateSave|Details(?:Print|View)|Error(?:s|Level)|FileAttributes|'
  77. r'Font|OutPath|Overwrite|PluginUnload|RebootFlag|ShellVarContext|'
  78. r'Silent|StaticBkColor)|'
  79. r'Show(?:(?:I|Uni)nstDetails|Window)|Silent(?:Un)?Install|Sleep|'
  80. r'SpaceTexts|Str(?:CmpS?|Cpy|Len)|SubSection(?:End)?|'
  81. r'Uninstall(?:ButtonText|(?:Sub)?Caption|EXEName|Icon|Text)|'
  82. r'UninstPage|Var|VI(?:AddVersionKey|ProductVersion)|WindowIcon|'
  83. r'Write(?:INIStr|Reg(:?Bin|DWORD|(?:Expand)?Str)|Uninstaller)|'
  84. r'XPStyle)\b', Keyword),
  85. (r'\b(CUR|END|(?:FILE_ATTRIBUTE_)?'
  86. r'(?:ARCHIVE|HIDDEN|NORMAL|OFFLINE|READONLY|SYSTEM|TEMPORARY)|'
  87. r'HK(CC|CR|CU|DD|LM|PD|U)|'
  88. r'HKEY_(?:CLASSES_ROOT|CURRENT_(?:CONFIG|USER)|DYN_DATA|'
  89. r'LOCAL_MACHINE|PERFORMANCE_DATA|USERS)|'
  90. r'ID(?:ABORT|CANCEL|IGNORE|NO|OK|RETRY|YES)|'
  91. r'MB_(?:ABORTRETRYIGNORE|DEFBUTTON[1-4]|'
  92. r'ICON(?:EXCLAMATION|INFORMATION|QUESTION|STOP)|'
  93. r'OK(?:CANCEL)?|RETRYCANCEL|RIGHT|SETFOREGROUND|TOPMOST|USERICON|'
  94. r'YESNO(?:CANCEL)?)|SET|SHCTX|'
  95. r'SW_(?:HIDE|SHOW(?:MAXIMIZED|MINIMIZED|NORMAL))|'
  96. r'admin|all|auto|both|bottom|bzip2|checkbox|colored|current|false|'
  97. r'force|hide|highest|if(?:diff|newer)|lastused|leave|left|'
  98. r'listonly|lzma|nevershow|none|normal|off|on|pop|push|'
  99. r'radiobuttons|right|show|silent|silentlog|smooth|textonly|top|'
  100. r'true|try|user|zlib)\b', Name.Constant),
  101. ],
  102. 'macro': [
  103. (r'\!(addincludedir(?:dir)?|addplugindir|appendfile|cd|define|'
  104. r'delfilefile|echo(?:message)?|else|endif|error|execute|'
  105. r'if(?:macro)?n?(?:def)?|include|insertmacro|macro(?:end)?|packhdr|'
  106. r'search(?:parse|replace)|system|tempfilesymbol|undef|verbose|'
  107. r'warning)\b', Comment.Preproc),
  108. ],
  109. 'interpol': [
  110. (r'\$(R?[0-9])', Name.Builtin.Pseudo), # registers
  111. (r'\$(ADMINTOOLS|APPDATA|CDBURN_AREA|COOKIES|COMMONFILES(?:32|64)|'
  112. r'DESKTOP|DOCUMENTS|EXE(?:DIR|FILE|PATH)|FAVORITES|FONTS|HISTORY|'
  113. r'HWNDPARENT|INTERNET_CACHE|LOCALAPPDATA|MUSIC|NETHOOD|PICTURES|'
  114. r'PLUGINSDIR|PRINTHOOD|PROFILE|PROGRAMFILES(?:32|64)|QUICKLAUNCH|'
  115. r'RECENT|RESOURCES(?:_LOCALIZED)?|SENDTO|SM(?:PROGRAMS|STARTUP)|'
  116. r'STARTMENU|SYSDIR|TEMP(?:LATES)?|VIDEOS|WINDIR|\{NSISDIR\})',
  117. Name.Builtin),
  118. (r'\$(CMDLINE|INSTDIR|OUTDIR|LANGUAGE)', Name.Variable.Global),
  119. (r'\$[a-z_]\w*', Name.Variable),
  120. ],
  121. 'str_double': [
  122. (r'"', String, '#pop'),
  123. (r'\$(\\[nrt"]|\$)', String.Escape),
  124. include('interpol'),
  125. (r'.', String.Double),
  126. ],
  127. 'str_backtick': [
  128. (r'`', String, '#pop'),
  129. (r'\$(\\[nrt"]|\$)', String.Escape),
  130. include('interpol'),
  131. (r'.', String.Double),
  132. ],
  133. }
  134. class RPMSpecLexer(RegexLexer):
  135. """
  136. For RPM ``.spec`` files.
  137. .. versionadded:: 1.6
  138. """
  139. name = 'RPMSpec'
  140. aliases = ['spec']
  141. filenames = ['*.spec']
  142. mimetypes = ['text/x-rpm-spec']
  143. _directives = ('(?:package|prep|build|install|clean|check|pre[a-z]*|'
  144. 'post[a-z]*|trigger[a-z]*|files)')
  145. tokens = {
  146. 'root': [
  147. (r'#.*\n', Comment),
  148. include('basic'),
  149. ],
  150. 'description': [
  151. (r'^(%' + _directives + ')(.*)$',
  152. bygroups(Name.Decorator, Text), '#pop'),
  153. (r'\n', Text),
  154. (r'.', Text),
  155. ],
  156. 'changelog': [
  157. (r'\*.*\n', Generic.Subheading),
  158. (r'^(%' + _directives + ')(.*)$',
  159. bygroups(Name.Decorator, Text), '#pop'),
  160. (r'\n', Text),
  161. (r'.', Text),
  162. ],
  163. 'string': [
  164. (r'"', String.Double, '#pop'),
  165. (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})', String.Escape),
  166. include('interpol'),
  167. (r'.', String.Double),
  168. ],
  169. 'basic': [
  170. include('macro'),
  171. (r'(?i)^(Name|Version|Release|Epoch|Summary|Group|License|Packager|'
  172. r'Vendor|Icon|URL|Distribution|Prefix|Patch[0-9]*|Source[0-9]*|'
  173. r'Requires\(?[a-z]*\)?|[a-z]+Req|Obsoletes|Suggests|Provides|Conflicts|'
  174. r'Build[a-z]+|[a-z]+Arch|Auto[a-z]+)(:)(.*)$',
  175. bygroups(Generic.Heading, Punctuation, using(this))),
  176. (r'^%description', Name.Decorator, 'description'),
  177. (r'^%changelog', Name.Decorator, 'changelog'),
  178. (r'^(%' + _directives + ')(.*)$', bygroups(Name.Decorator, Text)),
  179. (r'%(attr|defattr|dir|doc(?:dir)?|setup|config(?:ure)?|'
  180. r'make(?:install)|ghost|patch[0-9]+|find_lang|exclude|verify)',
  181. Keyword),
  182. include('interpol'),
  183. (r"'.*?'", String.Single),
  184. (r'"', String.Double, 'string'),
  185. (r'.', Text),
  186. ],
  187. 'macro': [
  188. (r'%define.*\n', Comment.Preproc),
  189. (r'%\{\!\?.*%define.*\}', Comment.Preproc),
  190. (r'(%(?:if(?:n?arch)?|else(?:if)?|endif))(.*)$',
  191. bygroups(Comment.Preproc, Text)),
  192. ],
  193. 'interpol': [
  194. (r'%\{?__[a-z_]+\}?', Name.Function),
  195. (r'%\{?_([a-z_]+dir|[a-z_]+path|prefix)\}?', Keyword.Pseudo),
  196. (r'%\{\?\w+\}', Name.Variable),
  197. (r'\$\{?RPM_[A-Z0-9_]+\}?', Name.Variable.Global),
  198. (r'%\{[a-zA-Z]\w+\}', Keyword.Constant),
  199. ]
  200. }
  201. class SourcesListLexer(RegexLexer):
  202. """
  203. Lexer that highlights debian sources.list files.
  204. .. versionadded:: 0.7
  205. """
  206. name = 'Debian Sourcelist'
  207. aliases = ['sourceslist', 'sources.list', 'debsources']
  208. filenames = ['sources.list']
  209. mimetype = ['application/x-debian-sourceslist']
  210. tokens = {
  211. 'root': [
  212. (r'\s+', Text),
  213. (r'#.*?$', Comment),
  214. (r'^(deb(?:-src)?)(\s+)',
  215. bygroups(Keyword, Text), 'distribution')
  216. ],
  217. 'distribution': [
  218. (r'#.*?$', Comment, '#pop'),
  219. (r'\$\(ARCH\)', Name.Variable),
  220. (r'[^\s$[]+', String),
  221. (r'\[', String.Other, 'escaped-distribution'),
  222. (r'\$', String),
  223. (r'\s+', Text, 'components')
  224. ],
  225. 'escaped-distribution': [
  226. (r'\]', String.Other, '#pop'),
  227. (r'\$\(ARCH\)', Name.Variable),
  228. (r'[^\]$]+', String.Other),
  229. (r'\$', String.Other)
  230. ],
  231. 'components': [
  232. (r'#.*?$', Comment, '#pop:2'),
  233. (r'$', Text, '#pop:2'),
  234. (r'\s+', Text),
  235. (r'\S+', Keyword.Pseudo),
  236. ]
  237. }
  238. def analyse_text(text):
  239. for line in text.splitlines():
  240. line = line.strip()
  241. if line.startswith('deb ') or line.startswith('deb-src '):
  242. return True
  243. class DebianControlLexer(RegexLexer):
  244. """
  245. Lexer for Debian ``control`` files and ``apt-cache show <pkg>`` outputs.
  246. .. versionadded:: 0.9
  247. """
  248. name = 'Debian Control file'
  249. aliases = ['control', 'debcontrol']
  250. filenames = ['control']
  251. tokens = {
  252. 'root': [
  253. (r'^(Description)', Keyword, 'description'),
  254. (r'^(Maintainer)(:\s*)', bygroups(Keyword, Text), 'maintainer'),
  255. (r'^((Build-)?Depends)', Keyword, 'depends'),
  256. (r'^((?:Python-)?Version)(:\s*)(\S+)$',
  257. bygroups(Keyword, Text, Number)),
  258. (r'^((?:Installed-)?Size)(:\s*)(\S+)$',
  259. bygroups(Keyword, Text, Number)),
  260. (r'^(MD5Sum|SHA1|SHA256)(:\s*)(\S+)$',
  261. bygroups(Keyword, Text, Number)),
  262. (r'^([a-zA-Z\-0-9\.]*?)(:\s*)(.*?)$',
  263. bygroups(Keyword, Whitespace, String)),
  264. ],
  265. 'maintainer': [
  266. (r'<[^>]+>', Generic.Strong),
  267. (r'<[^>]+>$', Generic.Strong, '#pop'),
  268. (r',\n?', Text),
  269. (r'.', Text),
  270. ],
  271. 'description': [
  272. (r'(.*)(Homepage)(: )(\S+)',
  273. bygroups(Text, String, Name, Name.Class)),
  274. (r':.*\n', Generic.Strong),
  275. (r' .*\n', Text),
  276. default('#pop'),
  277. ],
  278. 'depends': [
  279. (r':\s*', Text),
  280. (r'(\$)(\{)(\w+\s*:\s*\w+)', bygroups(Operator, Text, Name.Entity)),
  281. (r'\(', Text, 'depend_vers'),
  282. (r',', Text),
  283. (r'\|', Operator),
  284. (r'[\s]+', Text),
  285. (r'[})]\s*$', Text, '#pop'),
  286. (r'\}', Text),
  287. (r'[^,]$', Name.Function, '#pop'),
  288. (r'([+.a-zA-Z0-9-])(\s*)', bygroups(Name.Function, Text)),
  289. (r'\[.*?\]', Name.Entity),
  290. ],
  291. 'depend_vers': [
  292. (r'\),', Text, '#pop'),
  293. (r'\)[^,]', Text, '#pop:2'),
  294. (r'([><=]+)(\s*)([^)]+)', bygroups(Operator, Text, Number))
  295. ]
  296. }