installers.py 13 KB

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