g.extension.py 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: g.extension
  5. # AUTHOR(S): Markus Neteler (original shell script)
  6. # Martin Landa <landa.martin gmail com> (Pythonized & upgraded for GRASS 7)
  7. # Vaclav Petras <wenzeslaus gmail com> (support for general sources)
  8. # PURPOSE: Tool to download and install extensions into local installation
  9. #
  10. # COPYRIGHT: (C) 2009-2021 by Markus Neteler, and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. # TODO: - update temporary workaround of using grass7 subdir of addon-repo, see
  17. # https://github.com/OSGeo/grass-addons/issues/528
  18. # - add sudo support where needed (i.e. check first permission to write into
  19. # $GISBASE directory)
  20. # - fix toolbox support in install_private_extension_xml()
  21. #############################################################################
  22. # %module
  23. # % label: Maintains GRASS Addons extensions in local GRASS installation.
  24. # % description: Downloads and installs extensions from GRASS Addons repository or other source into the local GRASS installation or removes installed extensions.
  25. # % keyword: general
  26. # % keyword: installation
  27. # % keyword: extensions
  28. # % keyword: addons
  29. # % keyword: download
  30. # %end
  31. # %option
  32. # % key: extension
  33. # % type: string
  34. # % key_desc: name
  35. # % label: Name of extension to install or remove
  36. # % description: Name of toolbox (set of extensions) when -t flag is given
  37. # % required: yes
  38. # %end
  39. # %option
  40. # % key: operation
  41. # % type: string
  42. # % description: Operation to be performed
  43. # % required: yes
  44. # % options: add,remove
  45. # % answer: add
  46. # %end
  47. # %option
  48. # % key: url
  49. # % type: string
  50. # % key_desc: url
  51. # % label: URL or directory to get the extension from (supported only on Linux and Mac)
  52. # % description: The official repository is used by default. User can specify a ZIP file, directory or a repository on common hosting services. If not identified, Subversion repository is assumed. See manual for all options.
  53. # %end
  54. # %option
  55. # % key: prefix
  56. # % type: string
  57. # % key_desc: path
  58. # % description: Prefix where to install extension (ignored when flag -s is given)
  59. # % answer: $GRASS_ADDON_BASE
  60. # % required: no
  61. # %end
  62. # %option
  63. # % key: proxy
  64. # % type: string
  65. # % key_desc: proxy
  66. # % description: Set the proxy with: "http=<value>,ftp=<value>"
  67. # % required: no
  68. # % multiple: yes
  69. # %end
  70. # %option
  71. # % key: branch
  72. # % type: string
  73. # % key_desc: branch
  74. # % description: Specific branch to fetch addon from (only used when fetching from git)
  75. # % required: no
  76. # % multiple: no
  77. # %end
  78. # %flag
  79. # % key: l
  80. # % description: List available extensions in the official GRASS GIS Addons repository
  81. # % guisection: Print
  82. # % suppress_required: yes
  83. # %end
  84. # %flag
  85. # % key: c
  86. # % description: List available extensions in the official GRASS GIS Addons repository including module description
  87. # % guisection: Print
  88. # % suppress_required: yes
  89. # %end
  90. # %flag
  91. # % key: g
  92. # % description: List available extensions in the official GRASS GIS Addons repository (shell script style)
  93. # % guisection: Print
  94. # % suppress_required: yes
  95. # %end
  96. # %flag
  97. # % key: a
  98. # % description: List locally installed extensions
  99. # % guisection: Print
  100. # % suppress_required: yes
  101. # %end
  102. # %flag
  103. # % key: s
  104. # % description: Install system-wide (may need system administrator rights)
  105. # % guisection: Install
  106. # %end
  107. # %flag
  108. # % key: d
  109. # % description: Download source code and exit
  110. # % guisection: Install
  111. # %end
  112. # %flag
  113. # % key: i
  114. # % description: Do not install new extension, just compile it
  115. # % guisection: Install
  116. # %end
  117. # %flag
  118. # % key: f
  119. # % description: Force removal when uninstalling extension (operation=remove)
  120. # % guisection: Remove
  121. # %end
  122. # %flag
  123. # % key: t
  124. # % description: Operate on toolboxes instead of single modules (experimental)
  125. # % suppress_required: yes
  126. # %end
  127. # %flag
  128. # % key: o
  129. # % description: url refers to a fork of the official extension repository
  130. # %end
  131. # %flag
  132. # % key: j
  133. # % description: Generates JSON file containing the download URLs of the official Addons
  134. # % guisection: Install
  135. # % suppress_required: yes
  136. # %end
  137. # %rules
  138. # % required: extension, -l, -c, -g, -a, -j
  139. # % exclusive: extension, -l, -c, -g
  140. # % exclusive: extension, -l, -c, -a
  141. # % requires: -o, url
  142. # % requires: branch, url
  143. # %end
  144. # TODO: solve addon-extension(-module) confusion
  145. from __future__ import print_function
  146. import fileinput
  147. import http
  148. import os
  149. import codecs
  150. import sys
  151. import re
  152. import atexit
  153. import shutil
  154. import zipfile
  155. import tempfile
  156. import json
  157. import xml.etree.ElementTree as etree
  158. from distutils.dir_util import copy_tree
  159. from six.moves.urllib import request as urlrequest
  160. from six.moves.urllib.error import HTTPError, URLError
  161. from six.moves.urllib.parse import urlparse
  162. # Get the XML parsing exceptions to catch. The behavior changed with Python 2.7
  163. # and ElementTree 1.3.
  164. from xml.parsers import expat # TODO: works for any Python?
  165. if hasattr(etree, "ParseError"):
  166. ETREE_EXCEPTIONS = (etree.ParseError, expat.ExpatError)
  167. else:
  168. ETREE_EXCEPTIONS = expat.ExpatError
  169. import grass.script as gscript
  170. from grass.script.utils import try_rmdir
  171. from grass.script import core as grass
  172. from grass.script import task as gtask
  173. # temp dir
  174. REMOVE_TMPDIR = True
  175. PROXIES = {}
  176. HEADERS = {
  177. "User-Agent": "Mozilla/5.0",
  178. }
  179. HTTP_STATUS_CODES = list(http.HTTPStatus)
  180. GIT_URL = "https://github.com/OSGeo/grass-addons"
  181. # MAKE command
  182. # GRASS Makefile are type of GNU Make and not BSD Make
  183. # On FreeBSD (and other BSD and maybe unix) we have to
  184. # use GNU Make program often "gmake" to distinct with the (bsd) "make"
  185. if sys.platform.startswith("freebsd"):
  186. MAKE = "gmake"
  187. else:
  188. MAKE = "make"
  189. def replace_shebang_win(python_file):
  190. """
  191. Replaces "python" with "python3" in python files
  192. using UTF8 encoding on MS Windows
  193. """
  194. cur_dir = os.path.dirname(python_file)
  195. tmp_name = os.path.join(cur_dir, gscript.tempname(12))
  196. with codecs.open(python_file, "r", encoding="utf8") as in_file, codecs.open(
  197. tmp_name, "w", encoding="utf8"
  198. ) as out_file:
  199. for line in in_file:
  200. new_line = line.replace(
  201. "#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"
  202. )
  203. out_file.write(new_line)
  204. os.remove(python_file) # remove original
  205. os.rename(tmp_name, python_file) # rename temp to original name
  206. def urlretrieve(url, filename, *args, **kwargs):
  207. """Same function as 'urlretrieve', but with the ability to
  208. define headers.
  209. """
  210. request = urlrequest.Request(url, headers=HEADERS)
  211. response = urlrequest.urlopen(request, *args, **kwargs)
  212. with open(filename, "wb") as f:
  213. f.write(response.read())
  214. def urlopen(url, *args, **kwargs):
  215. """Wrapper around urlopen. Same function as 'urlopen', but with the
  216. ability to define headers.
  217. """
  218. request = urlrequest.Request(url, headers=HEADERS)
  219. return urlrequest.urlopen(request, *args, **kwargs)
  220. def get_version_branch(major_version):
  221. """Check if version branch for the current GRASS version exists,
  222. if not, take branch for the previous version
  223. For the official repo we assume that at least one version branch is present"""
  224. version_branch = f"grass{major_version}"
  225. try:
  226. urlrequest.urlopen(f"{GIT_URL}/tree/{version_branch}/src")
  227. except URLError:
  228. version_branch = "grass{}".format(int(major_version) - 1)
  229. return version_branch
  230. def get_github_branches(
  231. github_api_url="https://api.github.com/repos/OSGeo/grass-addons/branches",
  232. version_only=True,
  233. ):
  234. """Get ordered list of branch names in repo using github API
  235. For the official repo we assume that at least one version branch is present
  236. Due to strict rate limits in the github API (60 calls per hour) this function
  237. is currently not used."""
  238. req = urlrequest.urlopen(github_api_url)
  239. content = json.loads(req.read())
  240. branches = [repo_branch["name"] for repo_branch in content]
  241. if version_only:
  242. branches = [
  243. version_branch
  244. for version_branch in branches
  245. if version_branch.startswith("grass")
  246. ]
  247. branches.sort()
  248. return branches
  249. def get_default_branch(full_url):
  250. """Get default branch for repository in known hosting services
  251. (currently only implemented for github, gitlab and bitbucket API)
  252. In all other cases "main" is used as default"""
  253. # Parse URL
  254. url_parts = urlparse(full_url)
  255. # Get organization and repository component
  256. try:
  257. organization, repository = url_parts.path.split("/")[1:3]
  258. except URLError:
  259. gscript.fatal(
  260. _(
  261. "Cannot retrieve organization and repository from URL: <{}>.".format(
  262. full_url
  263. )
  264. )
  265. )
  266. # Construct API call and retrieve default branch
  267. api_calls = {
  268. "github.com": f"https://api.github.com/repos/{organization}/{repository}",
  269. "gitlab.com": f"https://gitlab.com/api/v4/projects/{organization}%2F{repository}",
  270. "bitbucket.org": f"https://api.bitbucket.org/2.0/repositories/{organization}/{repository}/branching-model?",
  271. }
  272. # Try to get default branch via API. The API call is known to fail a) if the full_url
  273. # does not belong to an implemented hosting service or b) if the rate limit of the
  274. # API is exceeded
  275. try:
  276. req = urlrequest.urlopen(api_calls.get(url_parts.netloc))
  277. content = json.loads(req.read())
  278. # For github and gitlab
  279. default_branch = content.get("default_branch")
  280. # For bitbucket
  281. if not default_branch:
  282. default_branch = content.get("development").get("name")
  283. except URLError:
  284. default_branch = "main"
  285. return default_branch
  286. def download_addons_paths_file(url, response_format, *args, **kwargs):
  287. """Generates JSON file containing the download URLs of the official
  288. Addons
  289. :param str url: url address
  290. :param str response_format: content type
  291. :return response: urllib.request.urlopen response object or None
  292. """
  293. try:
  294. response = urlopen(url, *args, **kwargs)
  295. if not response.code == 200:
  296. index = HTTP_STATUS_CODES.index(response.code)
  297. desc = HTTP_STATUS_CODES[index].description
  298. gscript.fatal(
  299. _(
  300. "Download file from <{url}>, "
  301. "return status code {code}, "
  302. "{desc}".format(
  303. url=url,
  304. code=response.code,
  305. desc=desc,
  306. ),
  307. ),
  308. )
  309. if response_format not in response.getheader("Content-Type"):
  310. gscript.fatal(
  311. _(
  312. "Wrong downloaded file format. "
  313. "Check url <{url}>. Allowed file format is "
  314. "{response_format}.".format(
  315. url=url,
  316. response_format=response_format,
  317. ),
  318. ),
  319. )
  320. return response
  321. except HTTPError as err:
  322. if err.code == 403 and err.msg == "rate limit exceeded":
  323. gscript.warning(
  324. _(
  325. "The download of the json file with add-ons paths "
  326. "from the github server wasn't successful, "
  327. "{}. The previous downloaded json file "
  328. " will be used if exists.".format(err.msg)
  329. ),
  330. )
  331. else:
  332. return download_addons_paths_file(
  333. url=url.replace("main", "master"),
  334. response_format=response_format,
  335. )
  336. except URLError:
  337. gscript.fatal(
  338. _(
  339. "Download file from <{url}>, "
  340. "failed. Check internet connection.".format(
  341. url=url,
  342. ),
  343. ),
  344. )
  345. def etree_fromfile(filename):
  346. """Create XML element tree from a given file name"""
  347. with open(filename, "r") as file_:
  348. return etree.fromstring(file_.read())
  349. def etree_fromurl(url):
  350. """Create XML element tree from a given URL"""
  351. try:
  352. file_ = urlopen(url)
  353. except URLError:
  354. gscript.fatal(
  355. _(
  356. "Download file from <{url}>,"
  357. " failed. File is not on the server or"
  358. " check your internet connection.".format(
  359. url=url,
  360. ),
  361. ),
  362. )
  363. return etree.fromstring(file_.read())
  364. def check_progs():
  365. """Check if the necessary programs are available"""
  366. # git to be tested once supported instead of `svn`
  367. for prog in (MAKE, "gcc", "svn"):
  368. if not grass.find_program(prog, "--help"):
  369. grass.fatal(_("'%s' required. Please install '%s' first.") % (prog, prog))
  370. # expand prefix to class name
  371. def expand_module_class_name(class_letters):
  372. """Convert module class (family) letter or letters to class (family) name
  373. The letter or letters are used in module names, e.g. r.slope.aspect.
  374. The names are used in directories in Addons but also in the source code.
  375. >>> expand_module_class_name('r')
  376. 'raster'
  377. >>> expand_module_class_name('v')
  378. 'vector'
  379. """
  380. name = {
  381. "d": "display",
  382. "db": "db",
  383. "g": "general",
  384. "i": "imagery",
  385. "m": "misc",
  386. "ps": "postscript",
  387. "p": "paint",
  388. "r": "raster",
  389. "r3": "raster3d",
  390. "s": "sites",
  391. "t": "temporal",
  392. "v": "vector",
  393. "wx": "gui/wxpython",
  394. }
  395. return name.get(class_letters, class_letters)
  396. def get_module_class_name(module_name):
  397. """Return class (family) name for a module
  398. The names are used in directories in Addons but also in the source code.
  399. >>> get_module_class_name('r.slope.aspect')
  400. 'raster'
  401. >>> get_module_class_name('v.to.rast')
  402. 'vector'
  403. """
  404. classchar = module_name.split(".", 1)[0]
  405. return expand_module_class_name(classchar)
  406. def get_installed_extensions(force=False):
  407. """Get list of installed extensions or toolboxes (if -t is set)"""
  408. if flags["t"]:
  409. return get_installed_toolboxes(force)
  410. # TODO: extension != module
  411. return get_installed_modules(force)
  412. def list_installed_extensions(toolboxes=False):
  413. """List installed extensions"""
  414. elist = get_installed_extensions()
  415. if elist:
  416. if toolboxes:
  417. grass.message(_("List of installed extensions (toolboxes):"))
  418. else:
  419. grass.message(_("List of installed extensions (modules):"))
  420. sys.stdout.write("\n".join(elist))
  421. sys.stdout.write("\n")
  422. else:
  423. if toolboxes:
  424. grass.info(_("No extension (toolbox) installed"))
  425. else:
  426. grass.info(_("No extension (module) installed"))
  427. def get_installed_toolboxes(force=False):
  428. """Get list of installed toolboxes
  429. Writes toolboxes file if it does not exist.
  430. Creates a new toolboxes file if it is not possible
  431. to read the current one.
  432. """
  433. xml_file = os.path.join(options["prefix"], "toolboxes.xml")
  434. if not os.path.exists(xml_file):
  435. write_xml_toolboxes(xml_file)
  436. # read XML file
  437. try:
  438. tree = etree_fromfile(xml_file)
  439. except ETREE_EXCEPTIONS + (OSError, IOError):
  440. os.remove(xml_file)
  441. write_xml_toolboxes(xml_file)
  442. return []
  443. ret = list()
  444. for tnode in tree.findall("toolbox"):
  445. ret.append(tnode.get("code"))
  446. return ret
  447. def get_installed_modules(force=False):
  448. """Get list of installed modules.
  449. Writes modules file if it does not exist and *force* is set to ``True``.
  450. Creates a new modules file if it is not possible
  451. to read the current one.
  452. """
  453. xml_file = os.path.join(options["prefix"], "modules.xml")
  454. if not os.path.exists(xml_file):
  455. if force:
  456. write_xml_modules(xml_file)
  457. else:
  458. grass.debug("No addons metadata file available", 1)
  459. return []
  460. # read XML file
  461. try:
  462. tree = etree_fromfile(xml_file)
  463. except ETREE_EXCEPTIONS + (OSError, IOError):
  464. os.remove(xml_file)
  465. write_xml_modules(xml_file)
  466. return []
  467. ret = list()
  468. for tnode in tree.findall("task"):
  469. if flags["g"]:
  470. desc, keyw = get_optional_params(tnode)
  471. ret.append("name={0}".format(tnode.get("name").strip()))
  472. ret.append("description={0}".format(desc))
  473. ret.append("keywords={0}".format(keyw))
  474. ret.append(
  475. "executables={0}".format(",".join(get_module_executables(tnode)))
  476. )
  477. else:
  478. ret.append(tnode.get("name").strip())
  479. return ret
  480. # list extensions (read XML file from grass.osgeo.org/addons)
  481. def list_available_extensions(url):
  482. """List available extensions/modules or toolboxes (if -t is given)
  483. For toolboxes it lists also all modules.
  484. """
  485. gscript.debug("list_available_extensions(url={0})".format(url))
  486. if flags["t"]:
  487. grass.message(_("List of available extensions (toolboxes):"))
  488. tlist = get_available_toolboxes(url)
  489. tkeys = sorted(tlist.keys())
  490. for toolbox_code in tkeys:
  491. toolbox_data = tlist[toolbox_code]
  492. if flags["g"]:
  493. print("toolbox_name=" + toolbox_data["name"])
  494. print("toolbox_code=" + toolbox_code)
  495. else:
  496. print("%s (%s)" % (toolbox_data["name"], toolbox_code))
  497. if flags["c"] or flags["g"]:
  498. list_available_modules(url, toolbox_data["modules"])
  499. else:
  500. if toolbox_data["modules"]:
  501. print(os.linesep.join(["* " + x for x in toolbox_data["modules"]]))
  502. else:
  503. grass.message(_("List of available extensions (modules):"))
  504. # TODO: extensions with several modules + lib
  505. list_available_modules(url)
  506. def get_available_toolboxes(url):
  507. """Return toolboxes available in the repository"""
  508. tdict = dict()
  509. url = url + "toolboxes.xml"
  510. try:
  511. tree = etree_fromurl(url)
  512. for tnode in tree.findall("toolbox"):
  513. mlist = list()
  514. clist = list()
  515. tdict[tnode.get("code")] = {
  516. "name": tnode.get("name"),
  517. "correlate": clist,
  518. "modules": mlist,
  519. }
  520. for cnode in tnode.findall("correlate"):
  521. clist.append(cnode.get("name"))
  522. for mnode in tnode.findall("task"):
  523. mlist.append(mnode.get("name"))
  524. except (HTTPError, IOError, OSError):
  525. grass.fatal(_("Unable to fetch addons metadata file"))
  526. return tdict
  527. def get_toolbox_extensions(url, name):
  528. """Get extensions inside a toolbox in toolbox file at given URL
  529. :param url: URL of the directory (file name will be attached)
  530. :param name: toolbox name
  531. """
  532. # dictionary of extensions
  533. edict = dict()
  534. url = url + "toolboxes.xml"
  535. try:
  536. tree = etree_fromurl(url)
  537. for tnode in tree.findall("toolbox"):
  538. if name == tnode.get("code"):
  539. for enode in tnode.findall("task"):
  540. # extension name
  541. ename = enode.get("name")
  542. edict[ename] = dict()
  543. # list of modules installed by this extension
  544. edict[ename]["mlist"] = list()
  545. # list of files installed by this extension
  546. edict[ename]["flist"] = list()
  547. break
  548. except (HTTPError, IOError, OSError):
  549. grass.fatal(_("Unable to fetch addons metadata file"))
  550. return edict
  551. def get_module_files(mnode):
  552. """Return list of module files
  553. :param mnode: XML node for a module
  554. """
  555. flist = []
  556. if mnode.find("binary") is None:
  557. return flist
  558. for file_node in mnode.find("binary").findall("file"):
  559. filepath = file_node.text
  560. flist.append(filepath)
  561. return flist
  562. def get_module_executables(mnode):
  563. """Return list of module executables
  564. :param mnode: XML node for a module
  565. """
  566. flist = []
  567. for filepath in get_module_files(mnode):
  568. if filepath.startswith(options["prefix"] + os.path.sep + "bin") or (
  569. sys.platform != "win32"
  570. and filepath.startswith(options["prefix"] + os.path.sep + "scripts")
  571. ):
  572. filename = os.path.basename(filepath)
  573. if sys.platform == "win32":
  574. filename = os.path.splitext(filename)[0]
  575. flist.append(filename)
  576. return flist
  577. def get_optional_params(mnode):
  578. """Return description and keywords of a module as a tuple
  579. :param mnode: XML node for a module
  580. """
  581. try:
  582. desc = mnode.find("description").text
  583. except AttributeError:
  584. desc = ""
  585. if desc is None:
  586. desc = ""
  587. try:
  588. keyw = mnode.find("keywords").text
  589. except AttributeError:
  590. keyw = ""
  591. if keyw is None:
  592. keyw = ""
  593. return desc, keyw
  594. def list_available_modules(url, mlist=None):
  595. """List modules available in the repository
  596. Tries to use XML metadata file first. Fallbacks to HTML page with a list.
  597. :param url: URL of the directory (file name will be attached)
  598. :param mlist: list only modules in this list
  599. """
  600. file_url = url + "modules.xml"
  601. grass.debug("url=%s" % file_url, 1)
  602. try:
  603. tree = etree_fromurl(file_url)
  604. except ETREE_EXCEPTIONS:
  605. grass.warning(
  606. _(
  607. "Unable to parse '%s'. Trying to scan"
  608. " SVN repository (may take some time)..."
  609. )
  610. % file_url
  611. )
  612. list_available_extensions_svn(url)
  613. return
  614. except (HTTPError, URLError, IOError, OSError):
  615. list_available_extensions_svn(url)
  616. return
  617. for mnode in tree.findall("task"):
  618. name = mnode.get("name").strip()
  619. if mlist and name not in mlist:
  620. continue
  621. if flags["c"] or flags["g"]:
  622. desc, keyw = get_optional_params(mnode)
  623. if flags["g"]:
  624. print("name=" + name)
  625. print("description=" + desc)
  626. print("keywords=" + keyw)
  627. elif flags["c"]:
  628. if mlist:
  629. print("*", end="")
  630. print(name + " - " + desc)
  631. else:
  632. print(name)
  633. # TODO: this is now broken/dead code, SVN is basically not used
  634. # fallback for Trac should parse Trac HTML page
  635. # this might be useful for potential SVN repos or anything
  636. # which would list the extensions/addons as list
  637. # TODO: fail when nothing is accessible
  638. def list_available_extensions_svn(url):
  639. """List available extensions from HTML given by URL
  640. Filename is generated based on the module class/family.
  641. This works well for the structure which is in grass-addons repository.
  642. ``<li><a href=...`` is parsed to find module names.
  643. This works well for HTML page generated by Subversion.
  644. :param url: a directory URL (filename will be attached)
  645. """
  646. gscript.debug("list_available_extensions_svn(url=%s)" % url, 2)
  647. grass.message(
  648. _(
  649. "Fetching list of extensions from"
  650. " GRASS-Addons SVN repository (be patient)..."
  651. )
  652. )
  653. pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
  654. if flags["c"]:
  655. grass.warning(_("Flag 'c' ignored, addons metadata file not available"))
  656. if flags["g"]:
  657. grass.warning(_("Flag 'g' ignored, addons metadata file not available"))
  658. prefixes = ["d", "db", "g", "i", "m", "ps", "p", "r", "r3", "s", "t", "v"]
  659. for prefix in prefixes:
  660. modclass = expand_module_class_name(prefix)
  661. grass.verbose(_("Checking for '%s' modules...") % modclass)
  662. # construct a full URL of a file
  663. file_url = "%s/%s" % (url, modclass)
  664. grass.debug("url = %s" % file_url, debug=2)
  665. try:
  666. file_ = urlopen(url)
  667. except (HTTPError, IOError, OSError):
  668. grass.debug(_("Unable to fetch '%s'") % file_url, debug=1)
  669. continue
  670. for line in file_.readlines():
  671. # list extensions
  672. sline = pattern.search(line)
  673. if not sline:
  674. continue
  675. name = sline.group(2).rstrip("/")
  676. if name.split(".", 1)[0] == prefix:
  677. print(name)
  678. # get_wxgui_extensions(url)
  679. # TODO: this is a dead code, not clear why not used, but seems not needed
  680. def get_wxgui_extensions(url):
  681. """Return list of extensions/addons in wxGUI directory at given URL
  682. :param url: a directory URL (filename will be attached)
  683. """
  684. mlist = list()
  685. grass.debug(
  686. "Fetching list of wxGUI extensions from "
  687. "GRASS-Addons SVN repository (be patient)..."
  688. )
  689. pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE)
  690. grass.verbose(_("Checking for '%s' modules...") % "gui/wxpython")
  691. # construct a full URL of a file
  692. url = "%s/%s" % (url, "gui/wxpython")
  693. grass.debug("url = %s" % url, debug=2)
  694. file_ = urlopen(url)
  695. if not file_:
  696. grass.warning(_("Unable to fetch '%s'") % url)
  697. return
  698. for line in file_.readlines():
  699. # list extensions
  700. sline = pattern.search(line)
  701. if not sline:
  702. continue
  703. name = sline.group(2).rstrip("/")
  704. if name not in ("..", "Makefile"):
  705. mlist.append(name)
  706. return mlist
  707. def cleanup():
  708. """Cleanup after the downloads and copilation"""
  709. if REMOVE_TMPDIR:
  710. try_rmdir(TMPDIR)
  711. else:
  712. grass.message("\n%s\n" % _("Path to the source code:"))
  713. sys.stderr.write("%s\n" % os.path.join(TMPDIR, options["extension"]))
  714. def write_xml_modules(name, tree=None):
  715. """Write element tree as a modules matadata file
  716. If the *tree* is not given, an empty file is created.
  717. :param name: file name
  718. :param tree: XML element tree
  719. """
  720. file_ = open(name, "w")
  721. file_.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  722. file_.write('<!DOCTYPE task SYSTEM "grass-addons.dtd">\n')
  723. file_.write('<addons version="%s">\n' % version[0])
  724. libgis_revison = grass.version()["libgis_revision"]
  725. if tree is not None:
  726. for tnode in tree.findall("task"):
  727. indent = 4
  728. file_.write('%s<task name="%s">\n' % (" " * indent, tnode.get("name")))
  729. indent += 4
  730. file_.write(
  731. "%s<description>%s</description>\n"
  732. % (" " * indent, tnode.find("description").text)
  733. )
  734. file_.write(
  735. "%s<keywords>%s</keywords>\n"
  736. % (" " * indent, tnode.find("keywords").text)
  737. )
  738. bnode = tnode.find("binary")
  739. if bnode is not None:
  740. file_.write("%s<binary>\n" % (" " * indent))
  741. indent += 4
  742. for fnode in bnode.findall("file"):
  743. file_.write(
  744. "%s<file>%s</file>\n"
  745. % (" " * indent, os.path.join(options["prefix"], fnode.text))
  746. )
  747. indent -= 4
  748. file_.write("%s</binary>\n" % (" " * indent))
  749. file_.write('%s<libgis revision="%s" />\n' % (" " * indent, libgis_revison))
  750. indent -= 4
  751. file_.write("%s</task>\n" % (" " * indent))
  752. file_.write("</addons>\n")
  753. file_.close()
  754. def write_xml_extensions(name, tree=None):
  755. """Write element tree as a modules matadata file
  756. If the *tree* is not given, an empty file is created.
  757. :param name: file name
  758. :param tree: XML element tree
  759. """
  760. file_ = open(name, "w")
  761. file_.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  762. file_.write('<!DOCTYPE task SYSTEM "grass-addons.dtd">\n')
  763. file_.write('<addons version="%s">\n' % version[0])
  764. libgis_revison = grass.version()["libgis_revision"]
  765. if tree is not None:
  766. for tnode in tree.findall("task"):
  767. indent = 4
  768. # extension name
  769. file_.write('%s<task name="%s">\n' % (" " * indent, tnode.get("name")))
  770. indent += 4
  771. """
  772. file_.write('%s<description>%s</description>\n' %
  773. (' ' * indent, tnode.find('description').text))
  774. file_.write('%s<keywords>%s</keywords>\n' %
  775. (' ' * indent, tnode.find('keywords').text))
  776. """
  777. # extension files
  778. bnode = tnode.find("binary")
  779. if bnode is not None:
  780. file_.write("%s<binary>\n" % (" " * indent))
  781. indent += 4
  782. for fnode in bnode.findall("file"):
  783. file_.write(
  784. "%s<file>%s</file>\n"
  785. % (" " * indent, os.path.join(options["prefix"], fnode.text))
  786. )
  787. indent -= 4
  788. file_.write("%s</binary>\n" % (" " * indent))
  789. # extension modules
  790. mnode = tnode.find("modules")
  791. if mnode is not None:
  792. file_.write("%s<modules>\n" % (" " * indent))
  793. indent += 4
  794. for fnode in mnode.findall("module"):
  795. file_.write("%s<module>%s</module>\n" % (" " * indent, fnode.text))
  796. indent -= 4
  797. file_.write("%s</modules>\n" % (" " * indent))
  798. file_.write('%s<libgis revision="%s" />\n' % (" " * indent, libgis_revison))
  799. indent -= 4
  800. file_.write("%s</task>\n" % (" " * indent))
  801. file_.write("</addons>\n")
  802. file_.close()
  803. def write_xml_toolboxes(name, tree=None):
  804. """Write element tree as a toolboxes matadata file
  805. If the *tree* is not given, an empty file is created.
  806. :param name: file name
  807. :param tree: XML element tree
  808. """
  809. file_ = open(name, "w")
  810. file_.write('<?xml version="1.0" encoding="UTF-8"?>\n')
  811. file_.write('<!DOCTYPE toolbox SYSTEM "grass-addons.dtd">\n')
  812. file_.write('<addons version="%s">\n' % version[0])
  813. if tree is not None:
  814. for tnode in tree.findall("toolbox"):
  815. indent = 4
  816. file_.write(
  817. '%s<toolbox name="%s" code="%s">\n'
  818. % (" " * indent, tnode.get("name"), tnode.get("code"))
  819. )
  820. indent += 4
  821. for cnode in tnode.findall("correlate"):
  822. file_.write(
  823. '%s<correlate code="%s" />\n' % (" " * indent, tnode.get("code"))
  824. )
  825. for mnode in tnode.findall("task"):
  826. file_.write(
  827. '%s<task name="%s" />\n' % (" " * indent, mnode.get("name"))
  828. )
  829. indent -= 4
  830. file_.write("%s</toolbox>\n" % (" " * indent))
  831. file_.write("</addons>\n")
  832. file_.close()
  833. def install_extension(source, url, xmlurl, branch):
  834. """Install extension (e.g. one module) or a toolbox (list of modules)"""
  835. gisbase = os.getenv("GISBASE")
  836. if not gisbase:
  837. grass.fatal(_("$GISBASE not defined"))
  838. if options["extension"] in get_installed_extensions(force=True):
  839. grass.warning(
  840. _("Extension <%s> already installed. Re-installing...")
  841. % options["extension"]
  842. )
  843. # create a dictionary of extensions
  844. # for each extension
  845. # - a list of modules installed by this extension
  846. # - a list of files installed by this extension
  847. edict = None
  848. if flags["t"]:
  849. grass.message(_("Installing toolbox <%s>...") % options["extension"])
  850. edict = get_toolbox_extensions(xmlurl, options["extension"])
  851. else:
  852. edict = dict()
  853. edict[options["extension"]] = dict()
  854. # list of modules installed by this extension
  855. edict[options["extension"]]["mlist"] = list()
  856. # list of files installed by this extension
  857. edict[options["extension"]]["flist"] = list()
  858. if not edict:
  859. grass.warning(_("Nothing to install"))
  860. return
  861. ret = 0
  862. tmp_dir = None
  863. new_modules = list()
  864. for extension in edict:
  865. ret1 = 0
  866. new_modules_ext = None
  867. if sys.platform == "win32":
  868. ret1, new_modules_ext, new_files_ext = install_extension_win(extension)
  869. else:
  870. (
  871. ret1,
  872. new_modules_ext,
  873. new_files_ext,
  874. tmp_dir,
  875. ) = install_extension_std_platforms(
  876. extension, source=source, url=url, branch=branch
  877. )
  878. if not flags["d"] and not flags["i"]:
  879. edict[extension]["mlist"].extend(new_modules_ext)
  880. edict[extension]["flist"].extend(new_files_ext)
  881. new_modules.extend(new_modules_ext)
  882. ret += ret1
  883. if len(edict) > 1:
  884. print("-" * 60)
  885. if flags["d"] or flags["i"]:
  886. return
  887. if ret != 0:
  888. grass.warning(
  889. _("Installation failed, sorry." " Please check above error messages.")
  890. )
  891. else:
  892. # update extensions metadata file
  893. grass.message(_("Updating extensions metadata file..."))
  894. install_extension_xml(edict)
  895. # update modules metadata file
  896. grass.message(_("Updating extension modules metadata file..."))
  897. install_module_xml(new_modules)
  898. for module in new_modules:
  899. update_manual_page(module)
  900. grass.message(
  901. _("Installation of <%s> successfully finished") % options["extension"]
  902. )
  903. if not os.getenv("GRASS_ADDON_BASE"):
  904. grass.warning(
  905. _(
  906. "This add-on module will not function until"
  907. " you set the GRASS_ADDON_BASE environment"
  908. ' variable (see "g.manual variables")'
  909. )
  910. )
  911. def get_toolboxes_metadata(url):
  912. """Return metadata for all toolboxes from given URL
  913. :param url: URL of a modules matadata file
  914. :param mlist: list of modules to get metadata for
  915. :returns: tuple where first item is dictionary with module names as keys
  916. and dictionary with dest, keyw, files keys as value, the second item
  917. is list of 'binary' files (installation files)
  918. """
  919. data = dict()
  920. try:
  921. tree = etree_fromurl(url)
  922. for tnode in tree.findall("toolbox"):
  923. clist = list()
  924. for cnode in tnode.findall("correlate"):
  925. clist.append(cnode.get("code"))
  926. mlist = list()
  927. for mnode in tnode.findall("task"):
  928. mlist.append(mnode.get("name"))
  929. code = tnode.get("code")
  930. data[code] = {
  931. "name": tnode.get("name"),
  932. "correlate": clist,
  933. "modules": mlist,
  934. }
  935. except (HTTPError, IOError, OSError):
  936. grass.error(_("Unable to read addons metadata file " "from the remote server"))
  937. return data
  938. def install_toolbox_xml(url, name):
  939. """Update local toolboxes metadata file"""
  940. # read metadata from remote server (toolboxes)
  941. url = url + "toolboxes.xml"
  942. data = get_toolboxes_metadata(url)
  943. if not data:
  944. grass.warning(_("No addons metadata available"))
  945. return
  946. if name not in data:
  947. grass.warning(_("No addons metadata available for <%s>") % name)
  948. return
  949. xml_file = os.path.join(options["prefix"], "toolboxes.xml")
  950. # create an empty file if not exists
  951. if not os.path.exists(xml_file):
  952. write_xml_modules(xml_file)
  953. # read XML file
  954. with open(xml_file, "r") as xml:
  955. tree = etree.fromstring(xml.read())
  956. # update tree
  957. tnode = None
  958. for node in tree.findall("toolbox"):
  959. if node.get("code") == name:
  960. tnode = node
  961. break
  962. tdata = data[name]
  963. if tnode is not None:
  964. # update existing node
  965. for cnode in tnode.findall("correlate"):
  966. tnode.remove(cnode)
  967. for mnode in tnode.findall("task"):
  968. tnode.remove(mnode)
  969. else:
  970. # create new node for task
  971. tnode = etree.Element("toolbox", attrib={"name": tdata["name"], "code": name})
  972. tree.append(tnode)
  973. for cname in tdata["correlate"]:
  974. cnode = etree.Element("correlate", attrib={"code": cname})
  975. tnode.append(cnode)
  976. for tname in tdata["modules"]:
  977. mnode = etree.Element("task", attrib={"name": tname})
  978. tnode.append(mnode)
  979. write_xml_toolboxes(xml_file, tree)
  980. def get_addons_metadata(url, mlist):
  981. """Return metadata for list of modules from given URL
  982. :param url: URL of a modules matadata file
  983. :param mlist: list of modules to get metadata for
  984. :returns: tuple where first item is dictionary with module names as keys
  985. and dictionary with dest, keyw, files keys as value, the second item
  986. is list of 'binary' files (installation files)
  987. """
  988. # TODO: extensions with multiple modules
  989. data = {}
  990. bin_list = []
  991. try:
  992. tree = etree_fromurl(url)
  993. except (HTTPError, URLError, IOError, OSError) as error:
  994. grass.error(
  995. _(
  996. "Unable to read addons metadata file" " from the remote server: {0}"
  997. ).format(error)
  998. )
  999. return data, bin_list
  1000. except ETREE_EXCEPTIONS as error:
  1001. grass.warning(_("Unable to parse '%s': {0}").format(error) % url)
  1002. return data, bin_list
  1003. for mnode in tree.findall("task"):
  1004. name = mnode.get("name")
  1005. if name not in mlist:
  1006. continue
  1007. file_list = list()
  1008. bnode = mnode.find("binary")
  1009. windows = sys.platform == "win32"
  1010. if bnode is not None:
  1011. for fnode in bnode.findall("file"):
  1012. path = fnode.text.split("/")
  1013. if path[0] == "bin":
  1014. bin_list.append(path[-1])
  1015. if windows:
  1016. path[-1] += ".exe"
  1017. elif path[0] == "scripts":
  1018. bin_list.append(path[-1])
  1019. if windows:
  1020. path[-1] += ".py"
  1021. file_list.append(os.path.sep.join(path))
  1022. desc, keyw = get_optional_params(mnode)
  1023. data[name] = {
  1024. "desc": desc,
  1025. "keyw": keyw,
  1026. "files": file_list,
  1027. }
  1028. return data, bin_list
  1029. def install_extension_xml(edict):
  1030. """Update XML files with metadata about installed modules and toolbox
  1031. of an private addon
  1032. """
  1033. # TODO toolbox
  1034. # if len(mlist) > 1:
  1035. # # read metadata from remote server (toolboxes)
  1036. # install_toolbox_xml(url, options['extension'])
  1037. xml_file = os.path.join(options["prefix"], "extensions.xml")
  1038. # create an empty file if not exists
  1039. if not os.path.exists(xml_file):
  1040. write_xml_extensions(xml_file)
  1041. # read XML file
  1042. tree = etree_fromfile(xml_file)
  1043. # update tree
  1044. for name in edict:
  1045. # so far extensions do not have description or keywords
  1046. # only modules have
  1047. """
  1048. try:
  1049. desc = gtask.parse_interface(name).description
  1050. # mname = gtask.parse_interface(name).name
  1051. keywords = gtask.parse_interface(name).keywords
  1052. except Exception as e:
  1053. grass.warning(_("No addons metadata available."
  1054. " Addons metadata file not updated."))
  1055. return []
  1056. """
  1057. tnode = None
  1058. for node in tree.findall("task"):
  1059. if node.get("name") == name:
  1060. tnode = node
  1061. break
  1062. if tnode is None:
  1063. # create new node for task
  1064. tnode = etree.Element("task", attrib={"name": name})
  1065. """
  1066. dnode = etree.Element('description')
  1067. dnode.text = desc
  1068. tnode.append(dnode)
  1069. knode = etree.Element('keywords')
  1070. knode.text = (',').join(keywords)
  1071. tnode.append(knode)
  1072. """
  1073. # create binary
  1074. bnode = etree.Element("binary")
  1075. # list of all installed files for this extension
  1076. for file_name in edict[name]["flist"]:
  1077. fnode = etree.Element("file")
  1078. fnode.text = file_name
  1079. bnode.append(fnode)
  1080. tnode.append(bnode)
  1081. # create modules
  1082. msnode = etree.Element("modules")
  1083. # list of all installed modules for this extension
  1084. for module_name in edict[name]["mlist"]:
  1085. mnode = etree.Element("module")
  1086. mnode.text = module_name
  1087. msnode.append(mnode)
  1088. tnode.append(msnode)
  1089. tree.append(tnode)
  1090. else:
  1091. grass.verbose(
  1092. "Extension already listed in metadata file; metadata not updated!"
  1093. )
  1094. write_xml_extensions(xml_file, tree)
  1095. return None
  1096. def get_multi_addon_addons_which_install_only_html_man_page():
  1097. """Get multi-addon addons which install only manual html page
  1098. :return list addons: list of multi-addon addons which install
  1099. only manual html page
  1100. """
  1101. addons = []
  1102. all_addon_dirs = []
  1103. addon_dirs_with_source_module = [] # *.py, *.c file
  1104. addon_pattern = re.compile(r".*{}".format(options["extension"]))
  1105. addon_src_file_pattern = re.compile(r".*.py$|.*.c$")
  1106. addons_paths_file = os.path.join(
  1107. options["prefix"],
  1108. get_addons_paths.json_file,
  1109. )
  1110. if not os.path.exists(addons_paths_file):
  1111. get_addons_paths(gg_addons_base_dir=options["prefix"])
  1112. with open(addons_paths_file) as f:
  1113. addons_paths = json.loads(f.read())
  1114. for addon in addons_paths["tree"]:
  1115. if re.match(addon_pattern, addon["path"]) and addon["type"] == "blob":
  1116. if re.match(addon_src_file_pattern, addon["path"]):
  1117. # Add addon dirs which contains source module *.py, *.c file
  1118. addon_dirs_with_source_module.append(
  1119. os.path.dirname(addon["path"]),
  1120. )
  1121. elif re.match(addon_pattern, addon["path"]) and addon["type"] == "tree":
  1122. # Add all addon dirs
  1123. all_addon_dirs.append(addon["path"])
  1124. for addon in set(all_addon_dirs) ^ set(addon_dirs_with_source_module):
  1125. addons.append(os.path.basename(addon))
  1126. return addons
  1127. def filter_multi_addon_addons(mlist):
  1128. """Filter out list of multi-addon addons which contains
  1129. and installs only *.html manual page, without source/binary
  1130. excutable module and doesn't need to check metadata.
  1131. e.g. the i.sentinel multi-addon consists of several full i.sentinel.*
  1132. addons along with a i.sentinel.html overview file.
  1133. :param list mlist: list of multi-addons (groups of addons
  1134. with respective addon overview HTML pages)
  1135. :return list mlist: list of individual multi-addons without respective
  1136. addon overview HTML pages
  1137. """
  1138. # Filters out add-ons that only contain the *.html man page,
  1139. # e.g. multi-addon i.sentinel (root directory) contains only
  1140. # the *.html manual page for installation, it does not need
  1141. # to check if metadata is available if there is no executable module.
  1142. for addon in get_multi_addon_addons_which_install_only_html_man_page():
  1143. if addon in mlist:
  1144. mlist.pop(mlist.index(addon))
  1145. return mlist
  1146. def install_module_xml(mlist):
  1147. """Update XML files with metadata about installed modules and toolbox
  1148. of an private addon
  1149. """
  1150. xml_file = os.path.join(options["prefix"], "modules.xml")
  1151. # create an empty file if not exists
  1152. if not os.path.exists(xml_file):
  1153. write_xml_modules(xml_file)
  1154. # read XML file
  1155. tree = etree_fromfile(xml_file)
  1156. # Filter multi-addon addons
  1157. if len(mlist) > 1:
  1158. mlist = filter_multi_addon_addons(
  1159. mlist.copy()
  1160. ) # mlist.copy() keep the original list of add-ons
  1161. # update tree
  1162. for name in mlist:
  1163. try:
  1164. desc = gtask.parse_interface(name).description
  1165. # mname = gtask.parse_interface(name).name
  1166. keywords = gtask.parse_interface(name).keywords
  1167. except Exception as error:
  1168. grass.warning(
  1169. _("No metadata available for module '{name}': {error}").format(
  1170. name=name, error=error
  1171. )
  1172. )
  1173. continue
  1174. tnode = None
  1175. for node in tree.findall("task"):
  1176. if node.get("name") == name:
  1177. tnode = node
  1178. break
  1179. if tnode is None:
  1180. # create new node for task
  1181. tnode = etree.Element("task", attrib={"name": name})
  1182. dnode = etree.Element("description")
  1183. dnode.text = desc
  1184. tnode.append(dnode)
  1185. knode = etree.Element("keywords")
  1186. knode.text = (",").join(keywords)
  1187. tnode.append(knode)
  1188. # binary files installed with an extension are now
  1189. # listed in extensions.xml
  1190. """
  1191. # create binary
  1192. bnode = etree.Element('binary')
  1193. list_of_binary_files = []
  1194. for file_name in os.listdir(url):
  1195. file_type = os.path.splitext(file_name)[-1]
  1196. file_n = os.path.splitext(file_name)[0]
  1197. html_path = os.path.join(options['prefix'], 'docs', 'html')
  1198. c_path = os.path.join(options['prefix'], 'bin')
  1199. py_path = os.path.join(options['prefix'], 'scripts')
  1200. # html or image file
  1201. if file_type in ['.html', '.jpg', '.png'] \
  1202. and file_n in os.listdir(html_path):
  1203. list_of_binary_files.append(os.path.join(html_path, file_name))
  1204. # c file
  1205. elif file_type in ['.c'] and file_name in os.listdir(c_path):
  1206. list_of_binary_files.append(os.path.join(c_path, file_n))
  1207. # python file
  1208. elif file_type in ['.py'] and file_name in os.listdir(py_path):
  1209. list_of_binary_files.append(os.path.join(py_path, file_n))
  1210. # man file
  1211. man_path = os.path.join(options['prefix'], 'docs', 'man', 'man1')
  1212. if name + '.1' in os.listdir(man_path):
  1213. list_of_binary_files.append(os.path.join(man_path, name + '.1'))
  1214. # add binaries to xml file
  1215. for binary_file_name in list_of_binary_files:
  1216. fnode = etree.Element('file')
  1217. fnode.text = binary_file_name
  1218. bnode.append(fnode)
  1219. tnode.append(bnode)
  1220. """
  1221. tree.append(tnode)
  1222. else:
  1223. grass.verbose(
  1224. "Extension module already listed in metadata file; metadata not updated!"
  1225. )
  1226. write_xml_modules(xml_file, tree)
  1227. return mlist
  1228. def install_extension_win(name):
  1229. """Install extension on MS Windows"""
  1230. grass.message(
  1231. _("Downloading precompiled GRASS Addons <{}>...").format(options["extension"])
  1232. )
  1233. # build base URL
  1234. base_url = (
  1235. "http://wingrass.fsv.cvut.cz/"
  1236. "grass{major}{minor}/addons/"
  1237. "grass-{major}.{minor}.{patch}".format(
  1238. major=version[0], minor=version[1], patch=version[2]
  1239. )
  1240. )
  1241. # resolve ZIP URL
  1242. source, url = resolve_source_code(url="{0}/{1}.zip".format(base_url, name))
  1243. # to hide non-error messages from subprocesses
  1244. if grass.verbosity() <= 2:
  1245. outdev = open(os.devnull, "w")
  1246. else:
  1247. outdev = sys.stdout
  1248. # download Addons ZIP file
  1249. os.chdir(TMPDIR) # this is just to not leave something behind
  1250. srcdir = os.path.join(TMPDIR, name)
  1251. download_source_code(
  1252. source=source,
  1253. url=url,
  1254. name=name,
  1255. outdev=outdev,
  1256. directory=srcdir,
  1257. tmpdir=TMPDIR,
  1258. )
  1259. # collect module names and file names
  1260. module_list = list()
  1261. for r, d, f in os.walk(srcdir):
  1262. for file in f:
  1263. # Filter GRASS module name patterns
  1264. if re.search(r"^[d,db,g,i,m,p,ps,r,r3,s,t,v,wx]\..*[\.py,\.exe]$", file):
  1265. modulename = os.path.splitext(file)[0]
  1266. module_list.append(modulename)
  1267. # remove duplicates in case there are .exe wrappers for python scripts
  1268. module_list = set(module_list)
  1269. # change shebang from python to python3
  1270. pyfiles = []
  1271. for r, d, f in os.walk(srcdir):
  1272. for file in f:
  1273. if file.endswith(".py"):
  1274. pyfiles.append(os.path.join(r, file))
  1275. for filename in pyfiles:
  1276. replace_shebang_win(filename)
  1277. # collect old files
  1278. old_file_list = list()
  1279. for r, d, f in os.walk(options["prefix"]):
  1280. for filename in f:
  1281. fullname = os.path.join(r, filename)
  1282. old_file_list.append(fullname)
  1283. # copy Addons copy tree to destination directory
  1284. move_extracted_files(
  1285. extract_dir=srcdir, target_dir=options["prefix"], files=os.listdir(srcdir)
  1286. )
  1287. # collect new files
  1288. file_list = list()
  1289. for r, d, f in os.walk(options["prefix"]):
  1290. for filename in f:
  1291. fullname = os.path.join(r, filename)
  1292. if fullname not in old_file_list:
  1293. file_list.append(fullname)
  1294. return 0, module_list, file_list
  1295. def download_source_code_svn(url, name, outdev, directory=None):
  1296. """Download source code from a Subversion repository
  1297. .. note:
  1298. Stdout is passed to to *outdev* while stderr is will be just printed.
  1299. :param url: URL of the repository
  1300. (module class/family and name are attached)
  1301. :param name: module name
  1302. :param outdev: output divide for the standard output of the svn command
  1303. :param directory: directory where the source code will be downloaded
  1304. (default is the current directory with name attached)
  1305. :returns: full path to the directory with the source code
  1306. (useful when you not specify directory, if *directory* is specified
  1307. the return value is equal to it)
  1308. """
  1309. if not directory:
  1310. directory = os.path.join(os.getcwd, name)
  1311. classchar = name.split(".", 1)[0]
  1312. moduleclass = expand_module_class_name(classchar)
  1313. url = url + "/" + moduleclass + "/" + name
  1314. if grass.call(["svn", "checkout", url, directory], stdout=outdev) != 0:
  1315. grass.fatal(_("GRASS Addons <%s> not found") % name)
  1316. return directory
  1317. def download_source_code_official_github(url, name, outdev, directory=None):
  1318. """Download source code from a official GitHub repository
  1319. .. note:
  1320. Stdout is passed to to *outdev* while stderr is will be just printed.
  1321. :param url: URL of the repository
  1322. (module class/family and name are attached)
  1323. :param name: module name
  1324. :param outdev: output divide for the standard output of the svn command
  1325. :param directory: directory where the source code will be downloaded
  1326. (default is the current directory with name attached)
  1327. :returns: full path to the directory with the source code
  1328. (useful when you not specify directory, if *directory* is specified
  1329. the return value is equal to it)
  1330. """
  1331. if not directory:
  1332. directory = os.path.join(os.getcwd, name)
  1333. if grass.call(["svn", "export", url, directory], stdout=outdev) != 0:
  1334. grass.fatal(_("GRASS Addons <%s> not found") % name)
  1335. return directory
  1336. def move_extracted_files(extract_dir, target_dir, files):
  1337. """Fix state of extracted files by moving them to different directory
  1338. When extracting, it is not clear what will be the root directory
  1339. or if there will be one at all. So this function moves the files to
  1340. a different directory in the way that if there was one directory extracted,
  1341. the contained files are moved.
  1342. """
  1343. gscript.debug("move_extracted_files({0})".format(locals()))
  1344. if len(files) == 1:
  1345. shutil.copytree(os.path.join(extract_dir, files[0]), target_dir)
  1346. else:
  1347. if not os.path.exists(target_dir):
  1348. os.mkdir(target_dir)
  1349. for file_name in files:
  1350. actual_file = os.path.join(extract_dir, file_name)
  1351. if os.path.isdir(actual_file):
  1352. # shutil.copytree() replaced by copy_tree() because
  1353. # shutil's copytree() fails when subdirectory exists
  1354. copy_tree(actual_file, os.path.join(target_dir, file_name))
  1355. else:
  1356. shutil.copy(actual_file, os.path.join(target_dir, file_name))
  1357. # Original copyright and license of the original version of the CRLF function
  1358. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
  1359. # Python Software Foundation; All Rights Reserved
  1360. # Python Software Foundation License Version 2
  1361. # http://svn.python.org/projects/python/trunk/Tools/scripts/crlf.py
  1362. def fix_newlines(directory):
  1363. """Replace CRLF with LF in all files in the directory
  1364. Binary files are ignored. Recurses into subdirectories.
  1365. """
  1366. # skip binary files
  1367. # see https://stackoverflow.com/a/7392391
  1368. textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7F})
  1369. def is_binary_string(bytes):
  1370. return bool(bytes.translate(None, textchars))
  1371. for root, unused, files in os.walk(directory):
  1372. for name in files:
  1373. filename = os.path.join(root, name)
  1374. if is_binary_string(open(filename, "rb").read(1024)):
  1375. continue # ignore binary files
  1376. # read content of text file
  1377. with open(filename, "rb") as fd:
  1378. data = fd.read()
  1379. # we don't expect there would be CRLF file by
  1380. # purpose if we want to allow CRLF files we would
  1381. # have to whitelite .py etc
  1382. newdata = data.replace(b"\r\n", b"\n")
  1383. if newdata != data:
  1384. with open(filename, "wb") as newfile:
  1385. newfile.write(newdata)
  1386. def extract_zip(name, directory, tmpdir):
  1387. """Extract a ZIP file into a directory"""
  1388. gscript.debug(
  1389. "extract_zip(name={name}, directory={directory},"
  1390. " tmpdir={tmpdir})".format(name=name, directory=directory, tmpdir=tmpdir),
  1391. 3,
  1392. )
  1393. try:
  1394. zip_file = zipfile.ZipFile(name, mode="r")
  1395. file_list = zip_file.namelist()
  1396. # we suppose we can write to parent of the given dir
  1397. # (supposing a tmp dir)
  1398. extract_dir = os.path.join(tmpdir, "extract_dir")
  1399. os.mkdir(extract_dir)
  1400. for subfile in file_list:
  1401. if "__pycache__" in subfile:
  1402. continue
  1403. zip_file.extract(subfile, extract_dir)
  1404. files = os.listdir(extract_dir)
  1405. move_extracted_files(extract_dir=extract_dir, target_dir=directory, files=files)
  1406. except zipfile.BadZipfile as error:
  1407. gscript.fatal(_("ZIP file is unreadable: {0}").format(error))
  1408. # TODO: solve the other related formats
  1409. def extract_tar(name, directory, tmpdir):
  1410. """Extract a TAR or a similar file into a directory"""
  1411. gscript.debug(
  1412. "extract_tar(name={name}, directory={directory},"
  1413. " tmpdir={tmpdir})".format(name=name, directory=directory, tmpdir=tmpdir),
  1414. 3,
  1415. )
  1416. try:
  1417. import tarfile # we don't need it anywhere else
  1418. tar = tarfile.open(name)
  1419. extract_dir = os.path.join(tmpdir, "extract_dir")
  1420. os.mkdir(extract_dir)
  1421. tar.extractall(path=extract_dir)
  1422. files = os.listdir(extract_dir)
  1423. move_extracted_files(extract_dir=extract_dir, target_dir=directory, files=files)
  1424. except tarfile.TarError as error:
  1425. gscript.fatal(_("Archive file is unreadable: {0}").format(error))
  1426. extract_tar.supported_formats = ["tar.gz", "gz", "bz2", "tar", "gzip", "targz"]
  1427. def download_source_code(
  1428. source, url, name, outdev, directory=None, tmpdir=None, branch=None
  1429. ):
  1430. """Get source code to a local directory for compilation"""
  1431. gscript.verbose(_("Type of source identified as '{source}'.").format(source=source))
  1432. if source == "official":
  1433. gscript.message(
  1434. _("Fetching <%s> from " "GRASS GIS Addons repository (be patient)...")
  1435. % name
  1436. )
  1437. download_source_code_official_github(url, name, outdev, directory)
  1438. elif source == "official_fork":
  1439. gscript.message(
  1440. _("Fetching <{name}> from " "<{url}> (be patient)...").format(
  1441. name=name, url=url
  1442. )
  1443. )
  1444. download_source_code_official_github(url, name, outdev, directory)
  1445. elif source == "svn":
  1446. gscript.message(
  1447. _("Fetching <{name}> from " "<{url}> (be patient)...").format(
  1448. name=name, url=url
  1449. )
  1450. )
  1451. download_source_code_svn(url, name, outdev, directory)
  1452. elif source in ["remote_zip"]: # , 'official'
  1453. gscript.message(
  1454. _("Fetching <{name}> from " "<{url}> (be patient)...").format(
  1455. name=name, url=url
  1456. )
  1457. )
  1458. # we expect that the module.zip file is not by chance in the archive
  1459. zip_name = os.path.join(tmpdir, "extension.zip")
  1460. try:
  1461. response = urlopen(url)
  1462. except URLError:
  1463. # Try download add-on from 'master' branch if default "main" fails
  1464. if not branch:
  1465. try:
  1466. url = url.replace("main", "master")
  1467. gscript.message(
  1468. _(
  1469. "Expected default branch not found. "
  1470. "Trying again from <{url}>..."
  1471. ).format(url=url)
  1472. )
  1473. response = urlopen(url)
  1474. except URLError:
  1475. grass.fatal(
  1476. _(
  1477. "Extension <{name}> not found. Please check "
  1478. "'url' and 'branch' options".format(name=name)
  1479. )
  1480. )
  1481. else:
  1482. grass.fatal(_("Extension <%s> not found") % name)
  1483. with open(zip_name, "wb") as out_file:
  1484. shutil.copyfileobj(response, out_file)
  1485. extract_zip(name=zip_name, directory=directory, tmpdir=tmpdir)
  1486. fix_newlines(directory)
  1487. elif (
  1488. source.startswith("remote_")
  1489. and source.split("_")[1] in extract_tar.supported_formats
  1490. ):
  1491. # we expect that the module.tar.gz file is not by chance in the archive
  1492. archive_name = os.path.join(tmpdir, "extension." + source.split("_")[1])
  1493. urlretrieve(url, archive_name)
  1494. extract_tar(name=archive_name, directory=directory, tmpdir=tmpdir)
  1495. fix_newlines(directory)
  1496. elif source == "zip":
  1497. extract_zip(name=url, directory=directory, tmpdir=tmpdir)
  1498. fix_newlines(directory)
  1499. elif source in extract_tar.supported_formats:
  1500. extract_tar(name=url, directory=directory, tmpdir=tmpdir)
  1501. fix_newlines(directory)
  1502. elif source == "dir":
  1503. shutil.copytree(url, directory)
  1504. fix_newlines(directory)
  1505. else:
  1506. # probably programmer error
  1507. grass.fatal(
  1508. _(
  1509. "Unknown extension (addon) source type '{0}'."
  1510. " Please report this to the grass-user mailing list."
  1511. ).format(source)
  1512. )
  1513. assert os.path.isdir(directory)
  1514. def install_extension_std_platforms(name, source, url, branch):
  1515. """Install extension on standard platforms"""
  1516. gisbase = os.getenv("GISBASE")
  1517. # to hide non-error messages from subprocesses
  1518. if grass.verbosity() <= 2:
  1519. outdev = open(os.devnull, "w")
  1520. else:
  1521. outdev = sys.stdout
  1522. os.chdir(TMPDIR) # this is just to not leave something behind
  1523. srcdir = os.path.join(TMPDIR, name)
  1524. download_source_code(
  1525. source=source,
  1526. url=url,
  1527. name=name,
  1528. outdev=outdev,
  1529. directory=srcdir,
  1530. tmpdir=TMPDIR,
  1531. branch=branch,
  1532. )
  1533. os.chdir(srcdir)
  1534. pgm_not_found_message = _(
  1535. "Module name not found." " Check module Makefile syntax (PGM variable)."
  1536. )
  1537. # collect module names
  1538. module_list = list()
  1539. for r, d, f in os.walk(srcdir):
  1540. for filename in f:
  1541. if filename == "Makefile":
  1542. # get the module name: PGM = <module name>
  1543. with open(os.path.join(r, "Makefile")) as fp:
  1544. for line in fp.readlines():
  1545. if re.match(r"PGM.*.=|PGM=", line):
  1546. try:
  1547. modulename = line.split("=")[1].strip()
  1548. if modulename:
  1549. if modulename not in module_list:
  1550. module_list.append(modulename)
  1551. else:
  1552. grass.fatal(pgm_not_found_message)
  1553. except IndexError:
  1554. grass.fatal(pgm_not_found_message)
  1555. # change shebang from python to python3
  1556. pyfiles = []
  1557. # r=root, d=directories, f = files
  1558. for r, d, f in os.walk(srcdir):
  1559. for file in f:
  1560. if file.endswith(".py"):
  1561. pyfiles.append(os.path.join(r, file))
  1562. for filename in pyfiles:
  1563. with fileinput.FileInput(filename, inplace=True) as file:
  1564. for line in file:
  1565. print(
  1566. line.replace("#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"),
  1567. end="",
  1568. )
  1569. dirs = {
  1570. "bin": os.path.join(TMPDIR, name, "bin"),
  1571. "docs": os.path.join(TMPDIR, name, "docs"),
  1572. "html": os.path.join(TMPDIR, name, "docs", "html"),
  1573. "rest": os.path.join(TMPDIR, name, "docs", "rest"),
  1574. "man": os.path.join(TMPDIR, name, "docs", "man"),
  1575. "script": os.path.join(TMPDIR, name, "scripts"),
  1576. # TODO: handle locales also for addons
  1577. # 'string' : os.path.join(TMPDIR, name, 'locale'),
  1578. "string": os.path.join(TMPDIR, name),
  1579. "etc": os.path.join(TMPDIR, name, "etc"),
  1580. }
  1581. make_cmd = [
  1582. MAKE,
  1583. "MODULE_TOPDIR=%s" % gisbase.replace(" ", r"\ "),
  1584. "RUN_GISRC=%s" % os.environ["GISRC"],
  1585. "BIN=%s" % dirs["bin"],
  1586. "HTMLDIR=%s" % dirs["html"],
  1587. "RESTDIR=%s" % dirs["rest"],
  1588. "MANBASEDIR=%s" % dirs["man"],
  1589. "SCRIPTDIR=%s" % dirs["script"],
  1590. "STRINGDIR=%s" % dirs["string"],
  1591. "ETC=%s" % os.path.join(dirs["etc"]),
  1592. "SOURCE_URL=%s" % url,
  1593. ]
  1594. install_cmd = [
  1595. MAKE,
  1596. "MODULE_TOPDIR=%s" % gisbase,
  1597. "ARCH_DISTDIR=%s" % os.path.join(TMPDIR, name),
  1598. "INST_DIR=%s" % options["prefix"],
  1599. "install",
  1600. ]
  1601. if flags["d"]:
  1602. grass.message("\n%s\n" % _("To compile run:"))
  1603. sys.stderr.write(" ".join(make_cmd) + "\n")
  1604. grass.message("\n%s\n" % _("To install run:"))
  1605. sys.stderr.write(" ".join(install_cmd) + "\n")
  1606. return 0, None, None, None
  1607. os.chdir(os.path.join(TMPDIR, name))
  1608. grass.message(_("Compiling..."))
  1609. if not os.path.exists(os.path.join(gisbase, "include", "Make", "Module.make")):
  1610. grass.fatal(_("Please install GRASS development package"))
  1611. if 0 != grass.call(make_cmd, stdout=outdev):
  1612. grass.fatal(
  1613. _("Compilation failed, sorry." " Please check above error messages.")
  1614. )
  1615. if flags["i"]:
  1616. return 0, None, None, None
  1617. # collect old files
  1618. old_file_list = list()
  1619. for r, d, f in os.walk(options["prefix"]):
  1620. for filename in f:
  1621. fullname = os.path.join(r, filename)
  1622. old_file_list.append(fullname)
  1623. grass.message(_("Installing..."))
  1624. ret = grass.call(install_cmd, stdout=outdev)
  1625. # collect new files
  1626. file_list = list()
  1627. for r, d, f in os.walk(options["prefix"]):
  1628. for filename in f:
  1629. fullname = os.path.join(r, filename)
  1630. if fullname not in old_file_list:
  1631. file_list.append(fullname)
  1632. return ret, module_list, file_list, os.path.join(TMPDIR, name)
  1633. def remove_extension(force=False):
  1634. """Remove existing extension
  1635. extension or toolbox with extensions if -t is given)"""
  1636. if flags["t"]:
  1637. edict = get_toolbox_extensions(options["prefix"], options["extension"])
  1638. else:
  1639. edict = dict()
  1640. edict[options["extension"]] = dict()
  1641. # list of modules installed by this extension
  1642. edict[options["extension"]]["mlist"] = list()
  1643. # list of files installed by this extension
  1644. edict[options["extension"]]["flist"] = list()
  1645. # collect modules and files installed by these extensions
  1646. mlist = list()
  1647. xml_file = os.path.join(options["prefix"], "extensions.xml")
  1648. if os.path.exists(xml_file):
  1649. # read XML file
  1650. tree = None
  1651. try:
  1652. tree = etree_fromfile(xml_file)
  1653. except ETREE_EXCEPTIONS + (OSError, IOError):
  1654. os.remove(xml_file)
  1655. write_xml_extensions(xml_file)
  1656. if tree is not None:
  1657. for tnode in tree.findall("task"):
  1658. ename = tnode.get("name").strip()
  1659. if ename in edict:
  1660. # modules installed by this extension
  1661. mnode = tnode.find("modules")
  1662. if mnode:
  1663. for fnode in mnode.findall("module"):
  1664. mname = fnode.text.strip()
  1665. edict[ename]["mlist"].append(mname)
  1666. mlist.append(mname)
  1667. # files installed by this extension
  1668. bnode = tnode.find("binary")
  1669. if bnode:
  1670. for fnode in bnode.findall("file"):
  1671. bname = fnode.text.strip()
  1672. edict[ename]["flist"].append(bname)
  1673. else:
  1674. if force:
  1675. write_xml_extensions(xml_file)
  1676. xml_file = os.path.join(options["prefix"], "modules.xml")
  1677. if not os.path.exists(xml_file):
  1678. if force:
  1679. write_xml_modules(xml_file)
  1680. else:
  1681. grass.debug("No addons metadata file available", 1)
  1682. # read XML file
  1683. tree = None
  1684. try:
  1685. tree = etree_fromfile(xml_file)
  1686. except ETREE_EXCEPTIONS + (OSError, IOError):
  1687. os.remove(xml_file)
  1688. write_xml_modules(xml_file)
  1689. return []
  1690. if tree is not None:
  1691. for tnode in tree.findall("task"):
  1692. ename = tnode.get("name").strip()
  1693. if ename in edict:
  1694. # assume extension name == module name
  1695. edict[ename]["mlist"].append(ename)
  1696. mlist.append(ename)
  1697. # files installed by this extension
  1698. bnode = tnode.find("binary")
  1699. if bnode:
  1700. for fnode in bnode.findall("file"):
  1701. bname = fnode.text.strip()
  1702. edict[ename]["flist"].append(bname)
  1703. if force:
  1704. grass.verbose(_("List of removed files:"))
  1705. else:
  1706. grass.info(_("Files to be removed:"))
  1707. eremoved = remove_extension_files(edict, force)
  1708. if force:
  1709. if len(eremoved) > 0:
  1710. grass.message(_("Updating addons metadata file..."))
  1711. remove_extension_xml(mlist, edict)
  1712. for ename in edict:
  1713. if ename in eremoved:
  1714. grass.message(_("Extension <%s> successfully uninstalled.") % ename)
  1715. else:
  1716. if flags["t"]:
  1717. grass.warning(
  1718. _(
  1719. "Toolbox <%s> not removed. "
  1720. "Re-run '%s' with '-f' flag to force removal"
  1721. )
  1722. % (options["extension"], "g.extension")
  1723. )
  1724. else:
  1725. grass.warning(
  1726. _(
  1727. "Extension <%s> not removed. "
  1728. "Re-run '%s' with '-f' flag to force removal"
  1729. )
  1730. % (options["extension"], "g.extension")
  1731. )
  1732. # remove existing extension(s) (reading XML file)
  1733. def remove_extension_files(edict, force=False):
  1734. """Remove extensions specified in a dictionary
  1735. Uses the file names from the file list of the dictionary
  1736. Fallbacks to standard layout of files on prefix path on error.
  1737. """
  1738. # try to read XML metadata file first
  1739. xml_file = os.path.join(options["prefix"], "extensions.xml")
  1740. einstalled = list()
  1741. eremoved = list()
  1742. if os.path.exists(xml_file):
  1743. tree = etree_fromfile(xml_file)
  1744. if tree is not None:
  1745. for task in tree.findall("task"):
  1746. ename = task.get("name").strip()
  1747. einstalled.append(ename)
  1748. else:
  1749. tree = None
  1750. for name in edict:
  1751. removed = True
  1752. if len(edict[name]["flist"]) > 0:
  1753. err = list()
  1754. for fpath in edict[name]["flist"]:
  1755. grass.verbose(fpath)
  1756. if force:
  1757. try:
  1758. os.remove(fpath)
  1759. except OSError:
  1760. msg = "Unable to remove file '%s'"
  1761. err.append((_(msg) % fpath))
  1762. removed = False
  1763. if len(err) > 0:
  1764. for error_line in err:
  1765. grass.error(error_line)
  1766. else:
  1767. if name not in einstalled:
  1768. # try even if module does not seem to be available,
  1769. # as the user may be trying to get rid of left over cruft
  1770. grass.warning(_("Extension <%s> not found") % name)
  1771. remove_extension_std(name, force)
  1772. removed = False
  1773. if removed is True:
  1774. eremoved.append(name)
  1775. return eremoved
  1776. def remove_extension_std(name, force=False):
  1777. """Remove extension/module expecting the standard layout
  1778. Any images for manuals or files installed in etc will not be
  1779. removed
  1780. """
  1781. for fpath in [
  1782. os.path.join(options["prefix"], "bin", name),
  1783. os.path.join(options["prefix"], "scripts", name),
  1784. os.path.join(options["prefix"], "docs", "html", name + ".html"),
  1785. os.path.join(options["prefix"], "docs", "rest", name + ".txt"),
  1786. os.path.join(options["prefix"], "docs", "man", "man1", name + ".1"),
  1787. ]:
  1788. if os.path.isfile(fpath):
  1789. grass.verbose(fpath)
  1790. if force:
  1791. os.remove(fpath)
  1792. # remove module libraries under GRASS_ADDONS/etc/{name}/*
  1793. libpath = os.path.join(options["prefix"], "etc", name)
  1794. if os.path.isdir(libpath):
  1795. grass.verbose(libpath)
  1796. if force:
  1797. shutil.rmtree(libpath)
  1798. def remove_from_toolbox_xml(name):
  1799. """Update local meta-file when removing existing toolbox"""
  1800. xml_file = os.path.join(options["prefix"], "toolboxes.xml")
  1801. if not os.path.exists(xml_file):
  1802. return
  1803. # read XML file
  1804. tree = etree_fromfile(xml_file)
  1805. for node in tree.findall("toolbox"):
  1806. if node.get("code") != name:
  1807. continue
  1808. tree.remove(node)
  1809. write_xml_toolboxes(xml_file, tree)
  1810. def remove_extension_xml(mlist, edict):
  1811. """Update local meta-file when removing existing extension"""
  1812. if len(edict) > 1:
  1813. # update also toolboxes metadata
  1814. remove_from_toolbox_xml(options["extension"])
  1815. # modules
  1816. xml_file = os.path.join(options["prefix"], "modules.xml")
  1817. if os.path.exists(xml_file):
  1818. # read XML file
  1819. tree = etree_fromfile(xml_file)
  1820. for name in mlist:
  1821. for node in tree.findall("task"):
  1822. if node.get("name") != name:
  1823. continue
  1824. tree.remove(node)
  1825. write_xml_modules(xml_file, tree)
  1826. # extensions
  1827. xml_file = os.path.join(options["prefix"], "extensions.xml")
  1828. if os.path.exists(xml_file):
  1829. # read XML file
  1830. tree = etree_fromfile(xml_file)
  1831. for name in edict:
  1832. for node in tree.findall("task"):
  1833. if node.get("name") != name:
  1834. continue
  1835. tree.remove(node)
  1836. write_xml_extensions(xml_file, tree)
  1837. # check links in CSS
  1838. def check_style_file(name):
  1839. """Ensures that a specified HTML documentation support file exists
  1840. If the file, e.g. a CSS file does not exist, the file is copied from
  1841. the distribution.
  1842. If the files are missing, a warning is issued.
  1843. """
  1844. dist_file = os.path.join(os.getenv("GISBASE"), "docs", "html", name)
  1845. addons_file = os.path.join(options["prefix"], "docs", "html", name)
  1846. if os.path.isfile(addons_file):
  1847. return
  1848. try:
  1849. shutil.copyfile(dist_file, addons_file)
  1850. except OSError as error:
  1851. grass.warning(
  1852. _(
  1853. "Unable to create '{filename}': {error}."
  1854. " Is the GRASS GIS documentation package installed?"
  1855. " Installation continues,"
  1856. " but documentation may not look right."
  1857. ).format(filename=addons_file, error=error)
  1858. )
  1859. def create_dir(path):
  1860. """Creates the specified directory (with all dirs in between)
  1861. NOOP for existing directory.
  1862. """
  1863. if os.path.isdir(path):
  1864. return
  1865. try:
  1866. os.makedirs(path)
  1867. except OSError as error:
  1868. grass.fatal(_("Unable to create '%s': %s") % (path, error))
  1869. grass.debug("'%s' created" % path)
  1870. def check_dirs():
  1871. """Ensure that the necessary directories in prefix path exist"""
  1872. create_dir(os.path.join(options["prefix"], "bin"))
  1873. create_dir(os.path.join(options["prefix"], "docs", "html"))
  1874. create_dir(os.path.join(options["prefix"], "docs", "rest"))
  1875. check_style_file("grass_logo.png")
  1876. check_style_file("grassdocs.css")
  1877. create_dir(os.path.join(options["prefix"], "etc"))
  1878. create_dir(os.path.join(options["prefix"], "docs", "man", "man1"))
  1879. create_dir(os.path.join(options["prefix"], "scripts"))
  1880. # fix file URI in manual page
  1881. def update_manual_page(module):
  1882. """Fix manual page for addons which are at different directory
  1883. than core modules"""
  1884. if module.split(".", 1)[0] == "wx":
  1885. return # skip for GUI modules
  1886. grass.verbose(_("Manual page for <%s> updated") % module)
  1887. # read original html file
  1888. htmlfile = os.path.join(options["prefix"], "docs", "html", module + ".html")
  1889. try:
  1890. oldfile = open(htmlfile)
  1891. shtml = oldfile.read()
  1892. except IOError as error:
  1893. gscript.fatal(_("Unable to read manual page: %s") % error)
  1894. else:
  1895. oldfile.close()
  1896. pos = []
  1897. # fix logo URL
  1898. pattern = r'''<a href="([^"]+)"><img src="grass_logo.png"'''
  1899. for match in re.finditer(pattern, shtml):
  1900. if match.group(1)[:4] == "http":
  1901. continue
  1902. pos.append(match.start(1))
  1903. # find URIs
  1904. pattern = r"""<a href="([^"]+)">([^>]+)</a>"""
  1905. addons = get_installed_extensions(force=True)
  1906. # Multi-addon
  1907. if len(addons) > 1:
  1908. for a in get_multi_addon_addons_which_install_only_html_man_page():
  1909. # Add multi-addon addons which install only manual html page
  1910. addons.append(a)
  1911. for match in re.finditer(pattern, shtml):
  1912. if match.group(1)[:4] == "http":
  1913. continue
  1914. if match.group(1).replace(".html", "") in addons:
  1915. continue
  1916. pos.append(match.start(1))
  1917. if not pos:
  1918. return # no match
  1919. # replace file URIs
  1920. prefix = "file://" + "/".join([os.getenv("GISBASE"), "docs", "html"])
  1921. ohtml = shtml[: pos[0]]
  1922. for i in range(1, len(pos)):
  1923. ohtml += prefix + "/" + shtml[pos[i - 1] : pos[i]]
  1924. ohtml += prefix + "/" + shtml[pos[-1] :]
  1925. # write updated html file
  1926. try:
  1927. newfile = open(htmlfile, "w")
  1928. newfile.write(ohtml)
  1929. except IOError as error:
  1930. gscript.fatal(_("Unable for write manual page: %s") % error)
  1931. else:
  1932. newfile.close()
  1933. def resolve_install_prefix(path, to_system):
  1934. """Determine and check the path for installation"""
  1935. if to_system:
  1936. path = os.environ["GISBASE"]
  1937. if path == "$GRASS_ADDON_BASE":
  1938. if not os.getenv("GRASS_ADDON_BASE"):
  1939. grass.warning(
  1940. _("GRASS_ADDON_BASE is not defined, " "installing to ~/.grass%s/addons")
  1941. % version[0]
  1942. )
  1943. path = os.path.join(os.environ["HOME"], ".grass%s" % version[0], "addons")
  1944. else:
  1945. path = os.environ["GRASS_ADDON_BASE"]
  1946. if os.path.exists(path) and not os.access(path, os.W_OK):
  1947. grass.fatal(
  1948. _(
  1949. "You don't have permission to install extension to <{0}>."
  1950. " Try to run {1} with administrator rights"
  1951. " (su or sudo)."
  1952. ).format(path, "g.extension")
  1953. )
  1954. # ensure dir sep at the end for cases where path is used as URL and pasted
  1955. # together with file names
  1956. if not path.endswith(os.path.sep):
  1957. path = path + os.path.sep
  1958. return os.path.abspath(path) # make likes absolute paths
  1959. def resolve_xmlurl_prefix(url, source=None):
  1960. """Determine and check the URL where the XML metadata files are stored
  1961. It ensures that there is a single slash at the end of URL, so we can attach
  1962. file name easily:
  1963. >>> resolve_xmlurl_prefix('https://grass.osgeo.org/addons')
  1964. 'https://grass.osgeo.org/addons/'
  1965. >>> resolve_xmlurl_prefix('https://grass.osgeo.org/addons/')
  1966. 'https://grass.osgeo.org/addons/'
  1967. """
  1968. gscript.debug("resolve_xmlurl_prefix(url={0}, source={1})".format(url, source))
  1969. if source in ("official", "official_fork"):
  1970. # use pregenerated modules XML file
  1971. # Define branch to fetch from (latest or current version)
  1972. version_branch = get_version_branch(version[0])
  1973. url = "https://grass.osgeo.org/addons/{}/".format(version_branch)
  1974. # else try to get extensions XMl from SVN repository (provided URL)
  1975. # the exact action depends on subsequent code (somewhere)
  1976. if not url.endswith("/"):
  1977. url = url + "/"
  1978. return url
  1979. KNOWN_HOST_SERVICES_INFO = {
  1980. "OSGeo Trac": {
  1981. "domain": "trac.osgeo.org",
  1982. "ignored_suffixes": ["format=zip"],
  1983. "possible_starts": ["", "https://", "http://"],
  1984. "url_start": "https://",
  1985. "url_end": "?format=zip",
  1986. },
  1987. "GitHub": {
  1988. "domain": "github.com",
  1989. "ignored_suffixes": [".zip", ".tar.gz"],
  1990. "possible_starts": ["", "https://", "http://"],
  1991. "url_start": "https://",
  1992. "url_end": "/archive/{branch}.zip",
  1993. },
  1994. "GitLab": {
  1995. "domain": "gitlab.com",
  1996. "ignored_suffixes": [".zip", ".tar.gz", ".tar.bz2", ".tar"],
  1997. "possible_starts": ["", "https://", "http://"],
  1998. "url_start": "https://",
  1999. "url_end": "/-/archive/{branch}/{name}-{branch}.zip",
  2000. },
  2001. "Bitbucket": {
  2002. "domain": "bitbucket.org",
  2003. "ignored_suffixes": [".zip", ".tar.gz", ".gz", ".bz2"],
  2004. "possible_starts": ["", "https://", "http://"],
  2005. "url_start": "https://",
  2006. "url_end": "/get/{branch}.zip",
  2007. },
  2008. }
  2009. # TODO: support ZIP URLs which don't end with zip
  2010. # https://gitlab.com/user/reponame/repository/archive.zip?ref=b%C3%A9po
  2011. def resolve_known_host_service(url, name, branch):
  2012. """Determine source type and full URL for known hosting service
  2013. If the service is not determined from the provided URL, tuple with
  2014. is two ``None`` values is returned.
  2015. :param url: URL
  2016. :param name: module name
  2017. """
  2018. match = None
  2019. actual_start = None
  2020. for key, value in KNOWN_HOST_SERVICES_INFO.items():
  2021. for start in value["possible_starts"]:
  2022. if url.startswith(start + value["domain"]):
  2023. match = value
  2024. actual_start = start
  2025. gscript.verbose(
  2026. _("Identified {0} as known hosting service").format(key)
  2027. )
  2028. for suffix in value["ignored_suffixes"]:
  2029. if url.endswith(suffix):
  2030. gscript.verbose(
  2031. _(
  2032. "Not using {service} as known hosting service"
  2033. " because the URL ends with '{suffix}'"
  2034. ).format(service=key, suffix=suffix)
  2035. )
  2036. return None, None
  2037. if match:
  2038. if not actual_start:
  2039. actual_start = match["url_start"]
  2040. else:
  2041. actual_start = ""
  2042. if "branch" in match["url_end"]:
  2043. suffix = match["url_end"].format(
  2044. name=name,
  2045. branch=branch if branch else get_default_branch(url),
  2046. )
  2047. else:
  2048. suffix = match["url_end"].format(name=name)
  2049. url = "{prefix}{base}{suffix}".format(
  2050. prefix=actual_start, base=url.rstrip("/"), suffix=suffix
  2051. )
  2052. gscript.verbose(_("Will use the following URL for download: {0}").format(url))
  2053. return "remote_zip", url
  2054. else:
  2055. return None, None
  2056. # TODO: add also option to enforce the source type
  2057. # TODO: workaround, https://github.com/OSGeo/grass-addons/issues/528
  2058. def resolve_source_code(url=None, name=None, branch=None, fork=False):
  2059. """Return type and URL or path of the source code
  2060. Local paths are not presented as URLs to be usable in standard functions.
  2061. Path is identified as local path if the directory of file exists which
  2062. has the unfortunate consequence that the not existing files are evaluated
  2063. as remote URLs. When path is not evaluated, Subversion is assumed for
  2064. backwards compatibility. When GitHub repository is specified, ZIP file
  2065. link is returned. The ZIP is for {branch} branch, not the default one because
  2066. GitHub does not provide the default branch in the URL (July 2015).
  2067. :returns: tuple with type of source and full URL or path
  2068. Official repository:
  2069. >>> resolve_source_code(name='g.example') # doctest: +SKIP
  2070. ('official', 'https://trac.osgeo.org/.../general/g.example')
  2071. Subversion:
  2072. >>> resolve_source_code('https://svn.osgeo.org/grass/grass-addons/grass7')
  2073. ('svn', 'https://svn.osgeo.org/grass/grass-addons/grass7')
  2074. ZIP files online:
  2075. >>> resolve_source_code('https://trac.osgeo.org/.../r.modis?format=zip') # doctest: +SKIP
  2076. ('remote_zip', 'https://trac.osgeo.org/.../r.modis?format=zip')
  2077. Local directories and ZIP files:
  2078. >>> resolve_source_code(os.path.expanduser("~")) # doctest: +ELLIPSIS
  2079. ('dir', '...')
  2080. >>> resolve_source_code('/local/directory/downloaded.zip') # doctest: +SKIP
  2081. ('zip', '/local/directory/downloaded.zip')
  2082. OSGeo Trac:
  2083. >>> resolve_source_code('trac.osgeo.org/.../r.agent.aco') # doctest: +SKIP
  2084. ('remote_zip', 'https://trac.osgeo.org/.../r.agent.aco?format=zip')
  2085. >>> resolve_source_code('https://trac.osgeo.org/.../r.agent.aco') # doctest: +SKIP
  2086. ('remote_zip', 'https://trac.osgeo.org/.../r.agent.aco?format=zip')
  2087. GitHub:
  2088. >>> resolve_source_code('github.com/user/g.example') # doctest: +SKIP
  2089. ('remote_zip', 'https://github.com/user/g.example/archive/master.zip')
  2090. >>> resolve_source_code('github.com/user/g.example/') # doctest: +SKIP
  2091. ('remote_zip', 'https://github.com/user/g.example/archive/master.zip')
  2092. >>> resolve_source_code('https://github.com/user/g.example') # doctest: +SKIP
  2093. ('remote_zip', 'https://github.com/user/g.example/archive/master.zip')
  2094. >>> resolve_source_code('https://github.com/user/g.example/') # doctest: +SKIP
  2095. ('remote_zip', 'https://github.com/user/g.example/archive/master.zip')
  2096. GitLab:
  2097. >>> resolve_source_code('gitlab.com/JoeUser/GrassModule') # doctest: +SKIP
  2098. ('remote_zip', 'https://gitlab.com/JoeUser/GrassModule/-/archive/master/GrassModule-master.zip')
  2099. >>> resolve_source_code('https://gitlab.com/JoeUser/GrassModule') # doctest: +SKIP
  2100. ('remote_zip', 'https://gitlab.com/JoeUser/GrassModule/-/archive/master/GrassModule-master.zip')
  2101. Bitbucket:
  2102. >>> resolve_source_code('bitbucket.org/joe-user/grass-module') # doctest: +SKIP
  2103. ('remote_zip', 'https://bitbucket.org/joe-user/grass-module/get/default.zip')
  2104. >>> resolve_source_code('https://bitbucket.org/joe-user/grass-module') # doctest: +SKIP
  2105. ('remote_zip', 'https://bitbucket.org/joe-user/grass-module/get/default.zip')
  2106. """
  2107. # Handle URL for the offical repo
  2108. if name and (not url or fork):
  2109. module_class = get_module_class_name(name)
  2110. # note: 'trunk' is required to make URL usable for 'svn export' call
  2111. # and fetches the default branch
  2112. if not branch:
  2113. # Fetch from default branch
  2114. version_branch = get_version_branch(version[0])
  2115. try:
  2116. url = url.rstrip("/") if url else GIT_URL
  2117. urlrequest.urlopen(f"{url}/tree/{version_branch}/src")
  2118. svn_reference = "branches/{}".format(version_branch)
  2119. except URLError:
  2120. svn_reference = "trunk"
  2121. else:
  2122. svn_reference = "branches/{}".format(branch)
  2123. if not url or url == GIT_URL:
  2124. # Set URL for the given GRASS version
  2125. git_url = f"{GIT_URL}/{svn_reference}/src/{module_class}/{name}"
  2126. return "official", git_url
  2127. else:
  2128. # Forks from the official repo should reflect the current structure
  2129. url = url.rstrip("/")
  2130. git_url = f"{url}/{svn_reference}/src/{module_class}/{name}"
  2131. return "official_fork", git_url
  2132. # Check if URL can be found
  2133. # Catch corner case if local URL is given starting with file://
  2134. url = url[6:] if url.startswith("file://") else url
  2135. if not os.path.exists(url):
  2136. url_validated = False
  2137. if url.startswith("http"):
  2138. try:
  2139. open_url = urlopen(url)
  2140. open_url.close()
  2141. url_validated = True
  2142. except URLError:
  2143. pass
  2144. else:
  2145. try:
  2146. open_url = urlopen("http://" + url)
  2147. open_url.close()
  2148. url_validated = True
  2149. except URLError:
  2150. pass
  2151. try:
  2152. open_url = urlopen("https://" + url)
  2153. open_url.close()
  2154. url_validated = True
  2155. except URLError:
  2156. pass
  2157. if not url_validated:
  2158. grass.fatal(_("Cannot open URL: {}".format(url)))
  2159. # Handle local URLs
  2160. if os.path.isdir(url):
  2161. return "dir", os.path.abspath(url)
  2162. elif os.path.exists(url):
  2163. if url.endswith(".zip"):
  2164. return "zip", os.path.abspath(url)
  2165. for suffix in extract_tar.supported_formats:
  2166. if url.endswith("." + suffix):
  2167. return suffix, os.path.abspath(url)
  2168. # Handle remote URLs
  2169. else:
  2170. source, resolved_url = resolve_known_host_service(url, name, branch)
  2171. if source:
  2172. return source, resolved_url
  2173. # we allow URL to end with =zip or ?zip and not only .zip
  2174. # unfortunately format=zip&version=89612 would require something else
  2175. # special option to force the source type would solve it
  2176. if url.endswith("zip"):
  2177. return "remote_zip", url
  2178. for suffix in extract_tar.supported_formats:
  2179. if url.endswith(suffix):
  2180. return "remote_" + suffix, url
  2181. # fallback to the classic behavior
  2182. return "svn", url
  2183. def get_addons_paths(gg_addons_base_dir):
  2184. """Get and save addons paths from GRASS GIS Addons GitHub repo API
  2185. as 'addons_paths.json' file in the gg_addons_base_dir. The file
  2186. serves as a list of all addons, and their paths (required for
  2187. mkhmtl.py tool)
  2188. :param str gg_addons_base_dir: dir path where addons are installed
  2189. """
  2190. # Define branch to fetch from (latest or current version)
  2191. addons_branch = get_version_branch(version[0])
  2192. url = f"https://api.github.com/repos/OSGeo/grass-addons/git/trees/{addons_branch}?recursive=1"
  2193. response = download_addons_paths_file(
  2194. url=url,
  2195. response_format="application/json",
  2196. )
  2197. if response:
  2198. addons_paths = json.loads(gscript.decode(response.read()))
  2199. with open(
  2200. os.path.join(gg_addons_base_dir, get_addons_paths.json_file), "w"
  2201. ) as f:
  2202. json.dump(addons_paths, f)
  2203. get_addons_paths.json_file = "addons_paths.json"
  2204. def main():
  2205. # check dependencies
  2206. if not flags["a"] and sys.platform != "win32":
  2207. check_progs()
  2208. original_url = options["url"]
  2209. branch = options["branch"]
  2210. # manage proxies
  2211. global PROXIES
  2212. if options["proxy"]:
  2213. PROXIES = {}
  2214. for ptype, purl in (p.split("=") for p in options["proxy"].split(",")):
  2215. PROXIES[ptype] = purl
  2216. proxy = urlrequest.ProxyHandler(PROXIES)
  2217. opener = urlrequest.build_opener(proxy)
  2218. urlrequest.install_opener(opener)
  2219. # Required for mkhtml.py script (get addon git commit from GitHub API server)
  2220. os.environ["GRASS_PROXY"] = options["proxy"]
  2221. # define path
  2222. options["prefix"] = resolve_install_prefix(
  2223. path=options["prefix"], to_system=flags["s"]
  2224. )
  2225. if flags["j"]:
  2226. get_addons_paths(gg_addons_base_dir=options["prefix"])
  2227. return 0
  2228. # list available extensions
  2229. if flags["l"] or flags["c"] or (flags["g"] and not flags["a"]):
  2230. # using dummy extension, we don't need any extension URL now,
  2231. # but will work only as long as the function does not check
  2232. # if the URL is actually valid or something
  2233. source, url = resolve_source_code(
  2234. name="dummy", url=original_url, branch=branch, fork=flags["o"]
  2235. )
  2236. xmlurl = resolve_xmlurl_prefix(original_url, source=source)
  2237. list_available_extensions(xmlurl)
  2238. return 0
  2239. elif flags["a"]:
  2240. list_installed_extensions(toolboxes=flags["t"])
  2241. return 0
  2242. if flags["d"] or flags["i"]:
  2243. flag = "d" if flags["d"] else "i"
  2244. if options["operation"] != "add":
  2245. grass.warning(
  2246. _(
  2247. "Flag '{}' is relevant only to"
  2248. " 'operation=add'. Ignoring this flag."
  2249. ).format(flag)
  2250. )
  2251. else:
  2252. global REMOVE_TMPDIR
  2253. REMOVE_TMPDIR = False
  2254. if options["operation"] == "add":
  2255. check_dirs()
  2256. if original_url == "" or flags["o"]:
  2257. """
  2258. Query GitHub API only if extension will be downloaded
  2259. from official GRASS GIS addon repository
  2260. """
  2261. get_addons_paths(gg_addons_base_dir=options["prefix"])
  2262. source, url = resolve_source_code(
  2263. name=options["extension"], url=original_url, branch=branch, fork=flags["o"]
  2264. )
  2265. xmlurl = resolve_xmlurl_prefix(original_url, source=source)
  2266. install_extension(source=source, url=url, xmlurl=xmlurl, branch=branch)
  2267. else: # remove
  2268. remove_extension(force=flags["f"])
  2269. return 0
  2270. if __name__ == "__main__":
  2271. if len(sys.argv) == 2 and sys.argv[1] == "--doctest":
  2272. import doctest
  2273. sys.exit(doctest.testmod().failed)
  2274. options, flags = grass.parser()
  2275. global TMPDIR
  2276. TMPDIR = tempfile.mkdtemp()
  2277. atexit.register(cleanup)
  2278. grass_version = grass.version()
  2279. version = grass_version["version"].split(".")
  2280. sys.exit(main())