installers.py 13 KB

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