grass.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. #!/usr/bin/env python
  2. #############################################################################
  3. #
  4. # MODULE: GRASS initialization (Python)
  5. # AUTHOR(S): Original author unknown - probably CERL
  6. # Andreas Lange <andreas.lange rhein-main.de>
  7. # Huidae Cho <grass4u gmail.com>
  8. # Justin Hickey <jhickey hpcc.nectec.or.th>
  9. # Markus Neteler <neteler osgeo.org>
  10. # Hamish Bowman <hamish_b yahoo,com>
  11. # Converted to Python (based on init.sh) by Glynn Clements
  12. # Martin Landa <landa.martin gmail.com>
  13. # Luca Delucchi <lucadeluge@gmail.com>
  14. # PURPOSE: Sets up environment variables, parses any remaining
  15. # command line options for setting the GISDBASE, LOCATION,
  16. # and/or MAPSET. Finally it starts GRASS with the appropriate
  17. # user interface and cleans up after it is finished.
  18. # COPYRIGHT: (C) 2000-2013 by the GRASS Development Team
  19. #
  20. # This program is free software under the GNU General
  21. # Public License (>=v2). Read the file COPYING that
  22. # comes with GRASS for details.
  23. #
  24. #############################################################################
  25. import sys
  26. import os
  27. import atexit
  28. import string
  29. import subprocess
  30. import re
  31. import platform
  32. import tempfile
  33. # Variables substituted during build process
  34. if 'GISBASE' in os.environ:
  35. gisbase = os.environ['GISBASE']
  36. else:
  37. gisbase = "@GISBASE@"
  38. cmd_name = "@START_UP@"
  39. grass_version = "@GRASS_VERSION_NUMBER@"
  40. ld_library_path_var = '@LD_LIBRARY_PATH_VAR@'
  41. if 'GRASS_PROJSHARE' in os.environ:
  42. config_projshare = os.environ['GRASS_PROJSHARE']
  43. else:
  44. config_projshare = "@CONFIG_PROJSHARE@"
  45. # configuration directory
  46. grass_env_file = None # see check_shell()
  47. if sys.platform == 'win32':
  48. grass_config_dirname = "GRASS7"
  49. grass_config_dir = os.path.join(os.getenv('APPDATA'), grass_config_dirname)
  50. else:
  51. grass_config_dirname = ".grass7"
  52. grass_config_dir = os.path.join(os.getenv('HOME'), grass_config_dirname)
  53. gisbase = os.path.normpath(gisbase)
  54. # i18N
  55. import gettext
  56. gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode=True)
  57. tmpdir = None
  58. lockfile = None
  59. remove_lockfile = True
  60. location = None
  61. create_new = None
  62. grass_gui = None
  63. def warning(text):
  64. sys.stderr.write(_("WARNING") + ': ' + text + os.linesep)
  65. def try_remove(path):
  66. try:
  67. os.remove(path)
  68. except:
  69. pass
  70. def try_rmdir(path):
  71. try:
  72. os.rmdir(path)
  73. except:
  74. pass
  75. def clean_env():
  76. env_curr = read_gisrc()
  77. env_new = {}
  78. for k,v in env_curr.iteritems():
  79. if 'MONITOR' not in k:
  80. env_new[k] = v
  81. write_gisrc(env_new)
  82. def cleanup_dir(path):
  83. if not path:
  84. return
  85. for root, dirs, files in os.walk(path, topdown=False):
  86. for name in files:
  87. try_remove(os.path.join(root, name))
  88. for name in dirs:
  89. try_rmdir(os.path.join(root, name))
  90. def cleanup():
  91. tmpdir, lockfile, remove_lockfile
  92. # all exits after setting up tmp dirs (system/location) should
  93. # also tidy it up
  94. cleanup_dir(tmpdir)
  95. try_rmdir(tmpdir)
  96. if location:
  97. tmpdir_loc = os.path.join(location, ".tmp")
  98. cleanup_dir(tmpdir_loc)
  99. try_rmdir(tmpdir_loc)
  100. # remove lock-file if requested
  101. if lockfile and remove_lockfile:
  102. try_remove(lockfile)
  103. def fatal(msg):
  104. sys.exit(msg)
  105. def message(msg):
  106. sys.stderr.write(msg + "\n")
  107. sys.stderr.flush()
  108. def readfile(path):
  109. f = open(path, 'r')
  110. s = f.read()
  111. f.close()
  112. return s
  113. def writefile(path, s):
  114. f = open(path, 'w')
  115. f.write(s)
  116. f.close()
  117. def call(cmd, **kwargs):
  118. if windows:
  119. kwargs['shell'] = True
  120. return subprocess.call(cmd, **kwargs)
  121. def Popen(cmd, **kwargs):
  122. if windows:
  123. kwargs['shell'] = True
  124. return subprocess.Popen(cmd, **kwargs)
  125. def gfile(*args):
  126. return os.path.join(gisbase, *args)
  127. help_text = r"""GRASS GIS %s
  128. Geographic Resources Analysis Support System (GRASS GIS).
  129. %s:
  130. $CMD_NAME [-h | -help | --help] [-v | --version] [-c | -c geofile | -c EPSG:code]
  131. [-text | -gui] [--config param]
  132. [[[<GISDBASE>/]<LOCATION_NAME>/]<MAPSET>]
  133. %s:
  134. -h or -help or --help %s
  135. -v or --version %s
  136. -c %s
  137. -text %s
  138. %s
  139. -gui %s
  140. %s
  141. --config %s
  142. %s
  143. %s:
  144. GISDBASE %s
  145. LOCATION_NAME %s
  146. MAPSET %s
  147. GISDBASE/LOCATION_NAME/MAPSET %s
  148. %s:
  149. GRASS_GUI %s
  150. GRASS_HTML_BROWSER %s
  151. GRASS_ADDON_PATH %s
  152. GRASS_ADDON_BASE %s
  153. GRASS_BATCH_JOB %s
  154. GRASS_PYTHON %s
  155. """ % (grass_version,
  156. _("Usage"),
  157. _("Flags"),
  158. _("print this help message"),
  159. _("show version information and exit"),
  160. _("create given database, location or mapset if it doesn't exist"),
  161. _("use text based interface"),
  162. _("and set as default"),
  163. _("use $DEFAULT_GUI graphical user interface"),
  164. _("and set as default"),
  165. _("print GRASS configuration parameters"),
  166. _("options: arch,build,compiler,path,revision"),
  167. _("Parameters"),
  168. _("initial database (path to GIS data)"),
  169. _("initial location"),
  170. _("initial mapset"),
  171. _("fully qualified initial mapset directory"),
  172. _("Environment variables relevant for startup"),
  173. _("select GUI (text, gui)"),
  174. _("set html web browser for help pages"),
  175. _("set additional path(s) to local GRASS modules or user scripts"),
  176. _("set additional GISBASE for locally installed GRASS Addons"),
  177. _("shell script to be processed as batch job"),
  178. _("set python shell name to override 'python'"))
  179. def help_message():
  180. t = string.Template(help_text)
  181. s = t.substitute(CMD_NAME=cmd_name, DEFAULT_GUI=default_gui)
  182. sys.stderr.write(s)
  183. def create_tmp():
  184. global tmpdir
  185. ## use $TMPDIR if it exists, then $TEMP, otherwise /tmp
  186. tmp = os.getenv('TMPDIR')
  187. if not tmp:
  188. tmp = os.getenv('TEMP')
  189. if not tmp:
  190. tmp = os.getenv('TMP')
  191. if not tmp:
  192. tmp = tempfile.gettempdir()
  193. if tmp:
  194. tmpdir = os.path.join(tmp, "grass7-%(user)s-%(lock)s" % {'user': user,
  195. 'lock': gis_lock})
  196. try:
  197. os.mkdir(tmpdir, 0700)
  198. except:
  199. tmp = None
  200. if not tmp:
  201. for ttmp in ("/tmp", "/var/tmp", "/usr/tmp"):
  202. tmp = ttmp
  203. tmpdir = os.path.join(tmp, "grass7-%(user)s-%(lock)s" % {
  204. 'user': user, 'lock': gis_lock})
  205. try:
  206. os.mkdir(tmpdir, 0700)
  207. except:
  208. tmp = None
  209. if tmp:
  210. break
  211. if not tmp:
  212. fatal(_("Unable to create temporary directory <grass7-%(user)s-" \
  213. "%(lock)s>! Exiting.") % {'user': user, 'lock': gis_lock})
  214. def create_gisrc():
  215. global gisrc, gisrcrc
  216. # Set the session grassrc file
  217. gisrc = os.path.join(tmpdir, "gisrc")
  218. os.environ['GISRC'] = gisrc
  219. # remove invalid GISRC file to avoid disturbing error messages:
  220. try:
  221. s = readfile(gisrcrc)
  222. if "UNKNOWN" in s:
  223. try_remove(gisrcrc)
  224. s = None
  225. except:
  226. s = None
  227. # Copy the global grassrc file to the session grassrc file
  228. if s:
  229. writefile(gisrc, s)
  230. def read_gisrc():
  231. kv = {}
  232. try:
  233. f = open(gisrc, 'r')
  234. except IOError:
  235. return kv
  236. for line in f:
  237. k, v = line.split(':', 1)
  238. kv[k.strip()] = v.strip()
  239. f.close()
  240. return kv
  241. def read_env_file(path):
  242. kv = {}
  243. f = open(path, 'r')
  244. for line in f:
  245. k, v = line.split(':', 1)
  246. kv[k.strip()] = v.strip()
  247. f.close()
  248. return kv
  249. def write_gisrc(kv):
  250. f = open(gisrc, 'w')
  251. for k, v in kv.iteritems():
  252. f.write("%s: %s\n" % (k, v))
  253. f.close()
  254. def read_gui():
  255. global grass_gui
  256. # At this point the GRASS user interface variable has been set from the
  257. # command line, been set from an external environment variable, or is
  258. # not set. So we check if it is not set
  259. if not grass_gui:
  260. # Check for a reference to the GRASS user interface in the grassrc file
  261. if os.access(gisrc, os.R_OK):
  262. kv = read_gisrc()
  263. if 'GRASS_GUI' in os.environ:
  264. grass_gui = os.environ['GRASS_GUI']
  265. elif 'GUI' in kv:
  266. grass_gui = kv['GUI']
  267. elif 'GRASS_GUI' in kv:
  268. # For backward compatibility (GRASS_GUI renamed to GUI)
  269. grass_gui = kv['GRASS_GUI']
  270. else:
  271. # Set the GRASS user interface to the default if needed
  272. grass_gui = default_gui
  273. if not grass_gui:
  274. grass_gui = default_gui
  275. if grass_gui == 'gui':
  276. grass_gui = default_gui
  277. # FIXME oldtcltk, gis.m, d.m no longer exist
  278. if grass_gui in ['d.m', 'gis.m', 'oldtcltk', 'tcltk']:
  279. warning(_("GUI <%s> not supported in this version") % grass_gui)
  280. grass_gui = default_gui
  281. def path_prepend(dir, var):
  282. path = os.getenv(var)
  283. if path:
  284. path = dir + os.pathsep + path
  285. else:
  286. path = dir
  287. os.environ[var] = path
  288. def path_append(dir, var):
  289. path = os.getenv(var)
  290. if path:
  291. path = path + os.pathsep + dir
  292. else:
  293. path = dir
  294. os.environ[var] = path
  295. def set_paths():
  296. # addons (path)
  297. addon_path = os.getenv('GRASS_ADDON_PATH')
  298. if addon_path:
  299. for path in addon_path.split(os.pathsep):
  300. path_prepend(addon_path, 'PATH')
  301. # addons (base)
  302. addon_base = os.getenv('GRASS_ADDON_BASE')
  303. if not addon_base:
  304. addon_base = os.path.join(grass_config_dir, 'addons')
  305. os.environ['GRASS_ADDON_BASE'] = addon_base
  306. path_prepend(os.path.join(addon_base, 'scripts'), 'PATH')
  307. path_prepend(os.path.join(addon_base, 'bin'), 'PATH')
  308. # standard installation
  309. path_prepend(gfile('scripts'), 'PATH')
  310. path_prepend(gfile('bin'), 'PATH')
  311. # Set PYTHONPATH to find GRASS Python modules
  312. path_prepend(gfile('etc', 'python'), 'PYTHONPATH')
  313. # set path for the GRASS man pages
  314. grass_man_path = os.path.join(gisbase, 'docs', 'man')
  315. addons_man_path = os.path.join(addon_base, 'docs', 'man')
  316. man_path = os.getenv('MANPATH')
  317. sys_man_path = None
  318. if man_path:
  319. path_prepend(addons_man_path, 'MANPATH')
  320. path_prepend(grass_man_path, 'MANPATH')
  321. else:
  322. try:
  323. nul = open(os.devnull, 'w')
  324. p = Popen(['manpath'], stdout=subprocess.PIPE, stderr=nul)
  325. nul.close()
  326. s = p.stdout.read()
  327. p.wait()
  328. sys_man_path = s.strip()
  329. except:
  330. pass
  331. if sys_man_path:
  332. os.environ['MANPATH'] = sys_man_path
  333. path_prepend(addons_man_path, 'MANPATH')
  334. path_prepend(grass_man_path, 'MANPATH')
  335. else:
  336. os.environ['MANPATH'] = addons_man_path
  337. path_prepend(grass_man_path, 'MANPATH')
  338. # Add .py (Python) to list of executable extensions to search for
  339. # in MS-Windows PATH
  340. if windows:
  341. path_append('.PY', 'PATHEXT')
  342. def find_exe(pgm):
  343. for dir in os.getenv('PATH').split(os.pathsep):
  344. path = os.path.join(dir, pgm)
  345. if os.access(path, os.X_OK):
  346. return path
  347. return None
  348. def set_defaults():
  349. # GRASS_PAGER
  350. if not os.getenv('GRASS_PAGER'):
  351. if find_exe("more"):
  352. pager = "more"
  353. elif find_exe("less"):
  354. pager = "less"
  355. elif windows:
  356. pager = "more"
  357. else:
  358. pager = "cat"
  359. os.environ['GRASS_PAGER'] = pager
  360. # GRASS_PYTHON
  361. if not os.getenv('GRASS_PYTHON'):
  362. if windows:
  363. os.environ['GRASS_PYTHON'] = "python.exe"
  364. else:
  365. os.environ['GRASS_PYTHON'] = "python"
  366. # GRASS_GNUPLOT
  367. if not os.getenv('GRASS_GNUPLOT'):
  368. os.environ['GRASS_GNUPLOT'] = "gnuplot -persist"
  369. # GRASS_PROJSHARE
  370. if not os.getenv('GRASS_PROJSHARE'):
  371. os.environ['GRASS_PROJSHARE'] = config_projshare
  372. def set_browser():
  373. # GRASS_HTML_BROWSER
  374. browser = os.getenv('GRASS_HTML_BROWSER')
  375. if not browser:
  376. if macosx:
  377. # OSX doesn't execute browsers from the shell PATH - route thru a
  378. # script
  379. browser = gfile('etc', "html_browser_mac.sh")
  380. os.environ['GRASS_HTML_BROWSER_MACOSX'] = "-b com.apple.helpviewer"
  381. if windows or cygwin:
  382. # MinGW startup moved to into init.bat
  383. browser = "explorer"
  384. else:
  385. # the usual suspects
  386. browsers = ["xdg-open", "htmlview", "konqueror", "mozilla",
  387. "mozilla-firefox", "firefox", "iceweasel", "opera",
  388. "netscape", "dillo", "lynx", "links", "w3c"]
  389. for b in browsers:
  390. if find_exe(b):
  391. browser = b
  392. break
  393. elif macosx:
  394. # OSX doesn't execute browsers from the shell PATH - route thru a
  395. # script
  396. os.environ['GRASS_HTML_BROWSER_MACOSX'] = "-b %s" % browser
  397. browser = gfile('etc', "html_browser_mac.sh")
  398. if not browser:
  399. warning(_("Searched for a web browser, but none found"))
  400. # even so we set konqueror to make lib/gis/parser.c happy:
  401. browser = "konqueror"
  402. os.environ['GRASS_HTML_BROWSER'] = browser
  403. def grass_intro():
  404. intro = r"""
  405. WELCOME TO GRASS %s
  406. 1) Have at your side all available GRASS tutorials
  407. 2) When working on your location, the following materials
  408. are extremely useful:
  409. - A topo map of your area
  410. - Current catalog of available computer maps
  411. 3) Check the GRASS webpages for feedback mailinglists and more:
  412. http://grass.osgeo.org
  413. http://www.grass-gis.org
  414. """ % grass_version
  415. sys.stderr.write(intro)
  416. sys.stderr.write("\n")
  417. sys.stderr.write(_("Hit RETURN to continue"))
  418. sys.stdin.readline()
  419. # for convenience, define pwd as GISDBASE:
  420. s = r"""GISDBASE: %s
  421. LOCATION_NAME: <UNKNOWN>
  422. MAPSET: <UNKNOWN>
  423. """ % os.getcwd()
  424. writefile(gisrc, s)
  425. def check_gui():
  426. global grass_gui, wxpython_base
  427. # Check if we are running X windows by checking the DISPLAY variable
  428. if os.getenv('DISPLAY') or windows:
  429. # Check if python is working properly
  430. if grass_gui == 'wxpython':
  431. nul = open(os.devnull, 'w')
  432. p = Popen([os.environ['GRASS_PYTHON']], stdin=subprocess.PIPE,
  433. stdout=nul, stderr=nul)
  434. nul.close()
  435. p.stdin.write("variable=True")
  436. p.stdin.close()
  437. p.wait()
  438. if p.returncode == 0:
  439. # Set the wxpython base directory
  440. wxpython_base = gfile("etc", "gui", "wxpython")
  441. else:
  442. # Python was not found - switch to text interface mode
  443. warning(_("The python command does not work as expected!\n"
  444. "Please check your GRASS_PYTHON environment variable.\n"
  445. "Use the -help option for details.\n"
  446. "Switching to text based interface mode.\n\n"
  447. "Hit RETURN to continue.\n"))
  448. sys.stdin.readline()
  449. grass_gui = 'text'
  450. else:
  451. # Display a message if a graphical interface was expected
  452. if grass_gui != 'text':
  453. # Set the interface mode to text
  454. warning(_("It appears that the X Windows system is not active.\n"
  455. "A graphical based user interface is not supported.\n"
  456. "Switching to text based interface mode.\n\n"
  457. "Hit RETURN to continue"""))
  458. sys.stdin.readline()
  459. grass_gui = 'text'
  460. # Save the user interface variable in the grassrc file - choose a temporary
  461. # file name that should not match another file
  462. if os.access(gisrc, os.F_OK):
  463. kv = read_gisrc()
  464. kv['GUI'] = grass_gui
  465. write_gisrc(kv)
  466. def non_interactive(arg, geofile=None):
  467. global gisdbase, location_name, mapset, location
  468. # Try non-interactive startup
  469. l = None
  470. if arg == '-':
  471. if location:
  472. l = location
  473. else:
  474. l = arg
  475. if l:
  476. if l == '.':
  477. l = os.getcwd()
  478. elif not os.path.isabs(l):
  479. l = os.path.abspath(l)
  480. l, mapset = os.path.split(l)
  481. if not mapset:
  482. l, mapset = os.path.split(l)
  483. l, location_name = os.path.split(l)
  484. gisdbase = l
  485. if gisdbase and location_name and mapset:
  486. location = os.path.join(gisdbase, location_name, mapset)
  487. if not os.access(os.path.join(location, "WIND"), os.R_OK):
  488. if location_name == "PERMANENT":
  489. fatal(_("<%s> is not a valid GRASS location") % location)
  490. else:
  491. # the user wants to create mapset on the fly
  492. if create_new:
  493. if not os.access(os.path.join(os.path.join(gisdbase,
  494. location_name,
  495. "PERMANENT",
  496. "DEFAULT_WIND")),
  497. os.F_OK):
  498. # create new location
  499. gisdbase = os.path.join(gisdbase, location_name)
  500. location_name = mapset
  501. mapset = "PERMANENT"
  502. if os.access(os.path.join(os.path.join(gisdbase,
  503. location_name,
  504. "PERMANENT",
  505. "DEFAULT_WIND")),
  506. os.F_OK):
  507. fatal(_("Failed to create new location. The " \
  508. "location <%s> already exists." % location_name))
  509. if gfile('etc', 'python') not in sys.path:
  510. sys.path.append(gfile('etc', 'python'))
  511. from grass.script import core as grass
  512. try:
  513. if geofile and geofile.find('EPSG:') > -1:
  514. epsg = geofile.split(':', 1)[1]
  515. grass.create_location(gisdbase, location_name,
  516. epsg=epsg)
  517. else:
  518. grass.create_location(gisdbase, location_name,
  519. filename=geofile)
  520. except grass.ScriptError, e:
  521. fatal(e.value.strip('"').strip("'").replace('\\n',
  522. os.linesep))
  523. else:
  524. # create new mapset
  525. os.mkdir(location)
  526. # copy PERMANENT/DEFAULT_WIND to <mapset>/WIND
  527. s = readfile(os.path.join(gisdbase, location_name,
  528. "PERMANENT", "DEFAULT_WIND"))
  529. writefile(os.path.join(location, "WIND"), s)
  530. message(_("Missing WIND file fixed"))
  531. else:
  532. fatal(_("<%s> is not a valid GRASS location") % location)
  533. if os.access(gisrc, os.R_OK):
  534. kv = read_gisrc()
  535. else:
  536. kv = {}
  537. kv['GISDBASE'] = gisdbase
  538. kv['LOCATION_NAME'] = location_name
  539. kv['MAPSET'] = mapset
  540. write_gisrc(kv)
  541. else:
  542. fatal(_("GISDBASE, LOCATION_NAME and MAPSET variables not set properly"
  543. ".\nInteractive startup needed."))
  544. def set_data():
  545. # User selects LOCATION and MAPSET if not set
  546. if not location:
  547. # Check for text interface
  548. if grass_gui == 'text':
  549. pass
  550. # Check for GUI
  551. elif grass_gui == 'wxpython':
  552. gui_startup()
  553. else:
  554. # Shouldn't need this but you never know
  555. fatal(_("Invalid user interface specified - <%s>.\n"
  556. "Use the --help option to see valid interface names.") % grass_gui)
  557. def gui_startup():
  558. if grass_gui == 'wxpython':
  559. thetest = call([os.getenv('GRASS_PYTHON'),
  560. gfile(wxpython_base, "gis_set.py")])
  561. if thetest == 0:
  562. pass
  563. elif thetest == 1:
  564. # The startup script printed an error message so wait
  565. # for user to read it
  566. message(_("Error in GUI startup. If necessary, please "
  567. "report this error to the GRASS developers.\n"
  568. "Switching to text mode now.\n\n"
  569. "Hit RETURN to continue..."))
  570. sys.stdin.readline()
  571. os.execlp(cmd_name, "-text")
  572. sys.exit(1)
  573. elif thetest == 2:
  574. # User wants to exit from GRASS
  575. message(_("Received EXIT message from GUI.\nGRASS is not started. Bye."))
  576. sys.exit(0)
  577. else:
  578. fatal(_("Invalid return code from GUI startup script.\n"
  579. "Please advise GRASS developers of this error."))
  580. def load_gisrc():
  581. global gisdbase, location_name, mapset, location
  582. kv = read_gisrc()
  583. gisdbase = kv.get('GISDBASE')
  584. location_name = kv.get('LOCATION_NAME')
  585. mapset = kv.get('MAPSET')
  586. if not gisdbase or not location_name or not mapset:
  587. fatal(_("Error reading data path information from g.gisenv.\n"
  588. "GISDBASE=%(gisbase)s\n"
  589. "LOCATION_NAME=%(location)s\n"
  590. "MAPSET=%(mapset)s\n\n"
  591. "Check the <%s(file)> file." % \
  592. {'gisbase': gisdbase, 'location': location_name,
  593. 'mapset': mapset, 'file': gisrcrc}))
  594. location = os.path.join(gisdbase, location_name, mapset)
  595. # load environmental variables from grass_env_file
  596. def load_env():
  597. if not os.access(grass_env_file, os.R_OK):
  598. return
  599. for line in readfile(grass_env_file).split(os.linesep):
  600. try:
  601. k, v = map(lambda x: x.strip(), line.strip().split(' ', 1)[1].split('=', 1))
  602. except:
  603. continue
  604. os.environ[k] = v
  605. def set_language():
  606. import locale
  607. language = os.getenv('LANG')
  608. if not language:
  609. return
  610. language = language.split('.')[0] # Split off ignored .encoding part if present
  611. orig_language = language
  612. try:
  613. locale.setlocale(locale.LC_ALL, language)
  614. except locale.Error, e:
  615. if sys.platform != 'win32': # Don't try on Windows, it will probably not work
  616. # sys.stderr.write("Failed to set LC_ALL to %s (%s)\n" % (language, e))
  617. try:
  618. # Locale lang.encoding might be missing. Let's try
  619. # UTF-8 encoding before giving up as on Linux systems
  620. # lang.UTF-8 locales are more common than legacy
  621. # ISO-8859 ones.
  622. language = locale.normalize('%s.UTF-8' % (language))
  623. locale.setlocale(locale.LC_ALL, language)
  624. except locale.Error, e:
  625. # If we got so far, provided locale is not supported
  626. # on this system
  627. sys.stderr.write("Failed to set LC_ALL to %s (%s)\n" % (language, e))
  628. ### locale.getdefaultlocale() is probably related to gettext?
  629. # try:
  630. # default_locale = locale.getdefaultlocale()
  631. # except:
  632. # default_locale = None
  633. # if default_locale and default_locale[0]:
  634. # language = default_locale[0]
  635. # else:
  636. language = 'C'
  637. # Set up environment for subprocesses
  638. for lc in ('LC_CTYPE', 'LC_MESSAGES', 'LC_TIME', 'LC_COLLATE',
  639. 'LC_MONETARY', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS',
  640. 'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION'):
  641. os.environ[lc] = language
  642. # Some code in GRASS might not like other decimal separators than .
  643. # Other potential sources for problems are: LC_TIME LC_CTYPE
  644. locale.setlocale(locale.LC_NUMERIC, 'C')
  645. os.environ['LC_NUMERIC'] = 'C'
  646. if os.getenv('LC_ALL'):
  647. del os.environ['LC_ALL'] # Remove LC_ALL to not override LC_NUMERIC
  648. # Even if setting locale has failed, let's set LANG in a hope,
  649. # that UI will use it GRASS texts will be in selected language,
  650. # system messages (i.e. OK, Cancel etc.) - in system default
  651. # language
  652. os.environ['LANGUAGE'] = orig_language
  653. os.environ['LANG'] = orig_language
  654. # Calling gettext.install twice seems to allow to see also
  655. # localized startup messages Black magic ;)
  656. gettext.install('grasslibs', os.path.join(gisbase, 'locale'), unicode=True)
  657. def check_lock():
  658. global lockfile
  659. if not os.path.exists(location):
  660. fatal(_("Path '%s' doesn't exist") % location)
  661. # Check for concurrent use
  662. lockfile = os.path.join(location, ".gislock")
  663. ret = call([gfile("etc", "lock"), lockfile, "%d" % os.getpid()])
  664. if ret == 0:
  665. msg = None
  666. elif ret == 2:
  667. msg = _("%(user)s is currently running GRASS in selected mapset (" \
  668. "file %(file)s found). Concurrent use not allowed." % {
  669. 'user': user, 'file': lockfile})
  670. else:
  671. msg = _("Unable to properly access '%s'.\n"
  672. "Please notify system personel.") % lockfile
  673. if msg:
  674. if grass_gui == "wxpython":
  675. thetest = call([os.getenv('GRASS_PYTHON'),
  676. os.path.join(wxpython_base, "gis_set_error.py"),
  677. msg])
  678. else:
  679. global remove_lockfile
  680. remove_lockfile = False
  681. fatal(msg)
  682. def make_fontcap():
  683. fc = os.getenv('GRASS_FONT_CAP')
  684. if fc and not os.access(fc, os.R_OK):
  685. message(_("Building user fontcap..."))
  686. call(["g.mkfontcap"])
  687. def check_shell():
  688. global sh, shellname, grass_env_file
  689. # cygwin has many problems with the shell setup
  690. # below, so i hardcoded everything here.
  691. if os.getenv('CYGWIN'):
  692. sh = "cygwin"
  693. shellname = "GNU Bash (Cygwin)"
  694. os.environ['SHELL'] = "/usr/bin/bash.exe"
  695. os.environ['OSTYPE'] = "cygwin"
  696. else:
  697. sh = os.path.basename(os.getenv('SHELL'))
  698. if windows and sh:
  699. sh = os.path.splitext(sh)[0]
  700. if sh == "ksh":
  701. shellname = "Korn Shell"
  702. elif sh == "csh":
  703. shellname = "C Shell"
  704. elif sh == "tcsh":
  705. shellname = "TC Shell"
  706. elif sh == "bash":
  707. shellname = "Bash Shell"
  708. elif sh == "sh":
  709. shellname = "Bourne Shell"
  710. elif sh == "zsh":
  711. shellname = "Z Shell"
  712. elif sh == "cmd":
  713. shellname = "Command Shell"
  714. else:
  715. shellname = "shell"
  716. if sh in ['csh', 'tcsh']:
  717. grass_env_file = os.path.join(grass_config_dir, 'cshrc')
  718. elif sh in ['bash', 'msh', 'cygwin', 'sh']:
  719. grass_env_file = os.path.join(grass_config_dir, 'bashrc')
  720. elif sh == 'zsh':
  721. grass_env_file = os.path.join(grass_config_dir, 'zshrc')
  722. elif sh == 'cmd':
  723. grass_env_file = os.path.join(grass_config_dir, 'env.bat')
  724. else:
  725. grass_env_file = os.path.join(grass_config_dir, 'bashrc')
  726. warning(_("Unsupported shell <%(sh)s>: %(env)s") % {'sh': sh,
  727. 'env': grass_env_file})
  728. # check for SHELL
  729. if not os.getenv('SHELL'):
  730. fatal(_("The SHELL variable is not set"))
  731. def check_batch_job():
  732. global batch_job
  733. # hack to process batch jobs:
  734. batch_job = os.getenv('GRASS_BATCH_JOB')
  735. if batch_job:
  736. # defined, but ...
  737. if not os.access(batch_job, os.F_OK):
  738. # wrong file
  739. fatal(_("Job file '%s' has been defined in "
  740. "the 'GRASS_BATCH_JOB' variable but not found. Exiting.\n\n"
  741. "Use 'unset GRASS_BATCH_JOB' to disable batch job processing.") % batch_job)
  742. elif not os.access(batch_job, os.X_OK):
  743. # right file, but ...
  744. fatal(_("Change file permission to 'executable' for '%s'") % batch_job)
  745. else:
  746. message(_("Executing '%s' ...") % batch_job)
  747. grass_gui = "text"
  748. shell = batch_job
  749. bj = Popen(shell, shell=True)
  750. bj.wait()
  751. message(_("Execution of '%s' finished.") % batch_job)
  752. def start_gui():
  753. # Start the chosen GUI but ignore text
  754. if grass_debug:
  755. message(_("GRASS GUI should be <%s>") % grass_gui)
  756. # Check for gui interface
  757. if grass_gui == "wxpython":
  758. Popen([os.getenv('GRASS_PYTHON'), gfile(wxpython_base, "wxgui.py")])
  759. def clear_screen():
  760. if windows:
  761. pass
  762. # TODO: uncomment when PDCurses works.
  763. # cls
  764. else:
  765. if not os.getenv('GRASS_BATCH_JOB') and not grass_debug:
  766. call(["tput", "clear"])
  767. def show_banner():
  768. sys.stderr.write(r"""
  769. __________ ___ __________ _______________
  770. / ____/ __ \/ | / ___/ ___/ / ____/ _/ ___/
  771. / / __/ /_/ / /| | \__ \\_ \ / / __ / / \__ \
  772. / /_/ / _, _/ ___ |___/ /__/ / / /_/ // / ___/ /
  773. \____/_/ |_/_/ |_/____/____/ \____/___//____/
  774. """)
  775. def say_hello():
  776. sys.stderr.write(_("Welcome to GRASS %s") % grass_version)
  777. def show_info():
  778. sys.stderr.write(
  779. r"""
  780. %-41shttp://grass.osgeo.org
  781. %-41s%s (%s)
  782. %-41sg.manual -i
  783. %-41sg.version -c
  784. """ % (_("GRASS homepage:"),
  785. _("This version running through:"),
  786. shellname, os.getenv('SHELL'),
  787. _("Help is available with the command:"),
  788. _("See the licence terms with:")))
  789. if grass_gui == 'wxpython':
  790. message("%-41sg.gui wxpython" % _("If required, restart the GUI with:"))
  791. else:
  792. message("%-41sg.gui %s" % (_("Start the GUI with:"), default_gui))
  793. message("%-41sexit" % _("When ready to quit enter:"))
  794. message("")
  795. def csh_startup():
  796. global exit_val
  797. userhome = os.getenv('HOME') # save original home
  798. home = location
  799. os.environ['HOME'] = home
  800. cshrc = os.path.join(home, ".cshrc")
  801. tcshrc = os.path.join(home, ".tcshrc")
  802. try_remove(cshrc)
  803. try_remove(tcshrc)
  804. f = open(cshrc, 'w')
  805. f.write("set home = %s\n" % userhome)
  806. f.write("set history = 3000 savehist = 3000 noclobber ignoreeof\n")
  807. f.write("set histfile = %s\n" % os.path.join(os.getenv('HOME'),
  808. ".history"))
  809. f.write("set prompt = '\\\n")
  810. f.write("Mapset <%s> in Location <%s> \\\n" % (mapset, location_name))
  811. f.write("GRASS %s > '\n" % grass_version)
  812. f.write("set BOGUS=``;unset BOGUS\n")
  813. path = os.path.join(userhome, ".grass.cshrc") # left for backward compatibility
  814. if os.access(path, os.R_OK):
  815. f.write(readfile(path) + '\n')
  816. if os.access(grass_env_file, os.R_OK):
  817. f.write(readfile(grass_env_file) + '\n')
  818. mail_re = re.compile(r"^ *set *mail *= *")
  819. for filename in [".cshrc", ".tcshrc", ".login"]:
  820. path = os.path.join(userhome, filename)
  821. if os.access(path, os.R_OK):
  822. s = readfile(path)
  823. lines = s.splitlines()
  824. for l in lines:
  825. if mail_re.match(l):
  826. f.write(l)
  827. path = os.getenv('PATH').split(':')
  828. f.write("set path = ( %s ) \n" % ' '.join(path))
  829. f.close()
  830. writefile(tcshrc, readfile(cshrc))
  831. exit_val = call([gfile("etc", "run"), os.getenv('SHELL')])
  832. os.environ['HOME'] = userhome
  833. def bash_startup():
  834. global exit_val
  835. # save command history in mapset dir and remember more
  836. os.environ['HISTFILE'] = os.path.join(location, ".bash_history")
  837. if not os.getenv('HISTSIZE') and not os.getenv('HISTFILESIZE'):
  838. os.environ['HISTSIZE'] = "3000"
  839. # instead of changing $HOME, start bash with: --rcfile "$LOCATION/.bashrc" ?
  840. # if so, must care be taken to explicity call .grass.bashrc et al for
  841. # non-interactive bash batch jobs?
  842. userhome = os.getenv('HOME') # save original home
  843. home = location # save .bashrc in $LOCATION
  844. os.environ['HOME'] = home
  845. bashrc = os.path.join(home, ".bashrc")
  846. try_remove(bashrc)
  847. f = open(bashrc, 'w')
  848. f.write("test -r ~/.alias && . ~/.alias\n")
  849. f.write("PS1='GRASS %s (%s):\w > '\n" % (grass_version, location_name))
  850. f.write("PROMPT_COMMAND=\"'%s'\"\n" % os.path.join(gisbase, 'etc',
  851. 'prompt.py'))
  852. # read environmental variables
  853. path = os.path.join(userhome, ".grass.bashrc") # left for backward compatibility
  854. if os.access(path, os.R_OK):
  855. f.write(readfile(path) + '\n')
  856. if os.access(grass_env_file, os.R_OK):
  857. f.write(readfile(grass_env_file) + '\n')
  858. f.write("export PATH=\"%s\"\n" % os.getenv('PATH'))
  859. f.write("export HOME=\"%s\"\n" % userhome) # restore user home path
  860. f.close()
  861. exit_val = call([gfile("etc", "run"), os.getenv('SHELL')])
  862. os.environ['HOME'] = userhome
  863. def default_startup():
  864. global exit_val
  865. if windows:
  866. os.environ['PS1'] = "GRASS %s> " % (grass_version)
  867. # "$ETC/run" doesn't work at all???
  868. exit_val = subprocess.call([os.getenv('SHELL')])
  869. cleanup_dir(os.path.join(location, ".tmp")) # remove GUI session files from .tmp
  870. else:
  871. os.environ['PS1'] = "GRASS %s (%s):\w > " % (grass_version, location_name)
  872. exit_val = call([gfile("etc", "run"), os.getenv('SHELL')])
  873. if exit_val != 0:
  874. fatal(_("Failed to start shell '%s'") % os.getenv('SHELL'))
  875. def done_message():
  876. if batch_job and os.access(batch_job, os.X_OK):
  877. message(_("Batch job '%s' (defined in GRASS_BATCH_JOB variable) was executed.") % batch_job)
  878. message(_("Goodbye from GRASS GIS"))
  879. sys.exit(exit_val)
  880. else:
  881. message(_("Done."))
  882. message("")
  883. message(_("Goodbye from GRASS GIS"))
  884. message("")
  885. def clean_temp():
  886. message(_("Cleaning up temporary files..."))
  887. nul = open(os.devnull, 'w')
  888. call([gfile("etc", "clean_temp")], stdout=nul, stderr=nul)
  889. nul.close()
  890. def grep(string,list):
  891. expr = re.compile(string)
  892. return [elem for elem in list if expr.match(elem)]
  893. def print_params():
  894. plat = gfile(gisbase, 'include', 'Make', 'Platform.make')
  895. fileplat = open(plat)
  896. linesplat = fileplat.readlines()
  897. fileplat.close()
  898. params = sys.argv[2:]
  899. if not params:
  900. params = ['arch', 'build', 'compiler', 'path', 'revision']
  901. for arg in params:
  902. if arg == 'path':
  903. sys.stdout.write("%s\n" % gisbase)
  904. elif arg == 'arch':
  905. val = grep('ARCH',linesplat)
  906. sys.stdout.write("%s\n" % val[0].split('=')[1].strip())
  907. elif arg == 'build':
  908. build = os.path.join(gisbase,'include','grass','confparms.h')
  909. filebuild = open(build)
  910. val = filebuild.readline()
  911. filebuild.close()
  912. sys.stdout.write("%s\n" % val.strip().strip('"').strip())
  913. elif arg == 'compiler':
  914. val = grep('CC',linesplat)
  915. sys.stdout.write("%s\n" % val[0].split('=')[1].strip())
  916. elif arg == 'revision':
  917. rev = os.path.join(gisbase,'include','grass','gis.h')
  918. filerev = open(rev)
  919. linesrev = filerev.readlines()
  920. val = grep('#define GIS_H_VERSION', linesrev)
  921. filerev.close()
  922. sys.stdout.write("%s\n" % val[0].split(':')[1].rstrip('$"\n').strip())
  923. else:
  924. message(_("Parameter <%s> not supported") % arg)
  925. def get_username():
  926. global user
  927. if windows:
  928. user = os.getenv('USERNAME')
  929. if not user:
  930. user = "user_name"
  931. else:
  932. user = os.getenv('USER')
  933. if not user:
  934. user = os.getenv('LOGNAME')
  935. if not user:
  936. try:
  937. p = Popen(['whoami'], stdout = subprocess.PIPE)
  938. s = p.stdout.read()
  939. p.wait()
  940. user = s.strip()
  941. except:
  942. pass
  943. if not user:
  944. user = "user_%d" % os.getuid()
  945. def parse_cmdline():
  946. global args, grass_gui, create_new
  947. args = []
  948. for i in sys.argv[1:]:
  949. # Check if the user asked for the version
  950. if i in ["-v", "--version"]:
  951. message('\n' + readfile(gfile("etc", "license")))
  952. sys.exit()
  953. # Check if the user asked for help
  954. elif i in ["help", "-h", "-help", "--help"]:
  955. help_message()
  956. sys.exit()
  957. # Check if the -text flag was given
  958. elif i in ["-text", "--text"]:
  959. grass_gui = 'text'
  960. # Check if the -gui flag was given
  961. elif i in ["-gui", "--gui"]:
  962. grass_gui = default_gui
  963. # Check if the -wxpython flag was given
  964. elif i in ["-wxpython", "-wx", "--wxpython", "--wx"]:
  965. grass_gui = 'wxpython'
  966. # Check if the user wants to create a new mapset
  967. elif i == "-c":
  968. create_new = True
  969. elif i == "--config":
  970. print_params()
  971. sys.exit()
  972. else:
  973. args.append(i)
  974. ### MAIN script starts here
  975. # Get the system name
  976. windows = sys.platform == 'win32'
  977. cygwin = "cygwin" in sys.platform
  978. macosx = "darwin" in sys.platform
  979. # Set GISBASE
  980. os.environ['GISBASE'] = gisbase
  981. # set HOME
  982. if windows and not os.getenv('HOME'):
  983. os.environ['HOME'] = os.path.join(os.getenv('HOMEDRIVE'),
  984. os.getenv('HOMEPATH'))
  985. # set SHELL
  986. if windows:
  987. if os.getenv('GRASS_SH'):
  988. os.environ['SHELL'] = os.getenv('GRASS_SH')
  989. if not os.getenv('SHELL'):
  990. os.environ['SHELL'] = os.getenv('COMSPEC', 'cmd.exe')
  991. atexit.register(cleanup)
  992. # Set default GUI
  993. default_gui = "wxpython"
  994. # the following is only meant to be an internal variable for debugging this
  995. # script. use 'g.gisenv set="DEBUG=[0-5]"' to turn GRASS debug mode on properly
  996. grass_debug = os.getenv('GRASS_DEBUG')
  997. # Set GRASS version number for R interface etc (must be an env_var for MS-Windows)
  998. os.environ['GRASS_VERSION'] = grass_version
  999. # Set the GIS_LOCK variable to current process id
  1000. gis_lock = str(os.getpid())
  1001. os.environ['GIS_LOCK'] = gis_lock
  1002. # Set the global grassrc file
  1003. batch_job = os.getenv('GRASS_BATCH_JOB')
  1004. if batch_job:
  1005. gisrcrc = os.path.join(grass_config_dir, "rc.%s" % platform.node())
  1006. if not os.access(gisrcrc, os.R_OK):
  1007. gisrcrc = os.path.join(grass_config_dir, "rc")
  1008. else:
  1009. gisrcrc = os.path.join(grass_config_dir, "rc")
  1010. # Set the username and working directory
  1011. get_username()
  1012. # Parse the command-line options
  1013. parse_cmdline()
  1014. # Create the temporary directory and session grassrc file
  1015. create_tmp()
  1016. # Create the session grassrc file
  1017. create_gisrc()
  1018. # Set shell (needs to be called before load_env())
  1019. check_shell()
  1020. # Load environmental variables from the file
  1021. load_env()
  1022. # Ensure GUI is set
  1023. read_gui()
  1024. # Set PATH, PYTHONPATH
  1025. set_paths()
  1026. # Set LD_LIBRARY_PATH (etc) to find GRASS shared libraries
  1027. path_prepend(gfile("lib"), ld_library_path_var)
  1028. # Set GRASS_PAGER, GRASS_PYTHON, GRASS_GNUPLOT, GRASS_PROJSHARE
  1029. set_defaults()
  1030. # Set GRASS_HTML_BROWSER
  1031. set_browser()
  1032. #predefine monitor size for certain architectures
  1033. if os.getenv('HOSTTYPE') == 'arm':
  1034. # small monitor on ARM (iPAQ, zaurus... etc)
  1035. os.environ['GRASS_HEIGHT'] = "320"
  1036. os.environ['GRASS_WIDTH'] = "240"
  1037. # First time user - GISRC is defined in the GRASS script
  1038. if not os.access(gisrc, os.F_OK):
  1039. if grass_gui == 'text' and len(args) == 0:
  1040. fatal(_("Unable to start GRASS. You can:\n"
  1041. " - Launch GRASS with '-gui' switch (`grass70 -gui`)\n"
  1042. " - Create manually GISRC file (%s)\n"
  1043. " - Launch GRASS with path to "
  1044. "the location/mapset as an argument (`grass70 /path/to/location/mapset`)") % gisrcrc)
  1045. grass_intro()
  1046. else:
  1047. clean_temp()
  1048. message(_("Starting GRASS GIS..."))
  1049. # Check that the GUI works
  1050. check_gui()
  1051. # Parsing argument to get LOCATION
  1052. if not args:
  1053. # Try interactive startup
  1054. location = None
  1055. else:
  1056. if create_new:
  1057. if len(args) > 1:
  1058. non_interactive(args[1], args[0])
  1059. else:
  1060. non_interactive(args[0])
  1061. else:
  1062. non_interactive(args[0])
  1063. # Set language
  1064. set_language()
  1065. # User selects LOCATION and MAPSET if not set
  1066. set_data()
  1067. # Set GISDBASE, LOCATION_NAME, MAPSET, LOCATION from $GISRC
  1068. load_gisrc()
  1069. # Check .gislock file
  1070. check_lock()
  1071. # build user fontcap if specified but not present
  1072. make_fontcap()
  1073. # predefine default driver if DB connection not defined
  1074. # is this really needed?? Modules should call this when/if required.
  1075. if not os.access(os.path.join(location, "VAR"), os.F_OK):
  1076. call(['db.connect', '-c', '--quiet'])
  1077. check_batch_job()
  1078. if not batch_job:
  1079. start_gui()
  1080. clear_screen()
  1081. # Display the version and license info
  1082. if batch_job:
  1083. say_hello()
  1084. grass_gui = 'text'
  1085. clear_screen()
  1086. clean_temp()
  1087. try_remove(lockfile)
  1088. sys.exit(0)
  1089. else:
  1090. show_banner()
  1091. say_hello()
  1092. show_info()
  1093. if grass_gui == "wxpython":
  1094. message(_("Launching <%s> GUI in the background, please wait...") % grass_gui)
  1095. if sh in ['csh', 'tcsh']:
  1096. csh_startup()
  1097. elif sh in ['bash', 'msh', 'cygwin']:
  1098. bash_startup()
  1099. else:
  1100. default_startup()
  1101. clear_screen()
  1102. clean_env()
  1103. clean_temp()
  1104. try_remove(lockfile)
  1105. # Save GISRC
  1106. s = readfile(gisrc)
  1107. if not os.path.exists(grass_config_dir):
  1108. os.mkdir(grass_config_dir)
  1109. writefile(gisrcrc, s)
  1110. cleanup()
  1111. # After this point no more grass modules may be called
  1112. done_message()