grass.py 41 KB

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