Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. This program builds shell scripts that define ISIS3 environment variables during
  4. conda environment activation and deactivation, and creates some directories.
  5. """
  6.  
  7. import argparse
  8. import os
  9. import sys
  10.  
  11. ##########################################################################################################
  12. #
  13. # This work is free and unencumbered software released into the public domain.
  14. # In jurisdictions that recognize copyright laws, the author or authors
  15. # of this software dedicate any and all copyright interest in the
  16. # software to the public domain.
  17. #
  18. #
  19. # Description: This program builds the shell scripts that define the
  20. # ISISROOT/ISIS3DATA/ISIS3TESTDATA environment variables for the user
  21. # when the ISIS3 conda environment is activated, and clean up when it is
  22. # deactivated.
  23. #
  24. # The data directory and test directory are optional command line arguments.
  25. # If the user chooses not to set them, they will both be created in the
  26. # $ISISROOT directory.
  27. #
  28. # History:
  29. # Author: Tyler Wilson, USGS
  30. # Date: 2018-10-05
  31. # Description: Initial commit.
  32. #
  33. # Author: Tyler Wilson, USGS
  34. # Date: 2018-11-01
  35. # Description: Removed a pair of lines which were causing output errors on Mac OS X and were not
  36. # required anyway.
  37. #
  38. # Author: Ross Beyer
  39. # Date: 2018-11-19
  40. # Description: Streamlined the program, improved documentation, and made the directory and
  41. # file creation more `pythonic' rather than using system calls.
  42. #
  43. # Author: Jesse Mapel
  44. # Date: 2019-03-25
  45. # Description: Added C-Shell support.
  46. #
  47. #
  48. ##########################################################################################################
  49.  
  50. # There are still a lot of Python 2 installations out there, and if people don't have
  51. # their conda environment set up properly, the error message they'll get will be hard
  52. # to decipher. This might help:
  53.  
  54. assert sys.version_info >= (3, 2) # Must be using Python 3.2 or later
  55.  
  56.  
  57. # This just wraps and reports on the directory creation:
  58. def mkdir(p):
  59. if os.path.exists(p):
  60. print("Tried to create " + p + ", but it already exists.")
  61. else:
  62. os.makedirs(p)
  63. print("Created " + p)
  64. return
  65.  
  66.  
  67. # Set up and then parse the command line:
  68. parser = argparse.ArgumentParser(description=__doc__)
  69.  
  70. parser.add_argument(
  71. "-d",
  72. "--data-dir",
  73. default=os.environ["CONDA_PREFIX"] + "/data",
  74. help="ISIS3 Data Directory, default: %(default)s",
  75. )
  76. parser.add_argument(
  77. "-t",
  78. "--test-dir",
  79. default=os.environ["CONDA_PREFIX"] + "/testData",
  80. help="ISIS3 Test Data Directory, default: %(default)s",
  81. )
  82. args = parser.parse_args()
  83.  
  84.  
  85. # Create the data directories:
  86. mkdir(args.data_dir)
  87. mkdir(args.test_dir)
  88.  
  89. # Create the conda activation and deactivation directories:
  90. activate_dir = os.environ["CONDA_PREFIX"] + "/etc/conda/activate.d"
  91. deactivate_dir = os.environ["CONDA_PREFIX"] + "/etc/conda/deactivate.d"
  92.  
  93. mkdir(activate_dir)
  94. mkdir(deactivate_dir)
  95.  
  96. # Write the files that manage the ISIS3 environments:
  97. activate_vars_sh = activate_dir + "/env_vars.sh"
  98. deactivate_vars_sh = deactivate_dir + "/env_vars.sh"
  99. activate_vars_csh = activate_dir + "/env_vars.csh"
  100. deactivate_vars_csh = deactivate_dir + "/env_vars.csh"
  101. activate_vars_fish = activate_dir + "/env_vars.fish"
  102. deactivate_vars_fish = deactivate_dir + "/env_vars.fish"
  103.  
  104. # bash/zsh
  105. with open(activate_vars_sh, mode="w") as a:
  106. script = """#!/bin/sh
  107. export ISISROOT={}
  108. export ISIS3DATA={}
  109. export ISIS3TESTDATA={}
  110. """.format(
  111. os.environ["CONDA_PREFIX"], args.data_dir, args.test_dir
  112. )
  113. a.write(script)
  114. print("Wrote " + activate_vars_sh)
  115.  
  116. with open(deactivate_vars_sh, mode="w") as d:
  117. script = """#!/bin/sh
  118. unset ISISROOT
  119. unset ISIS3DATA
  120. unset ISIS3TESTDATA
  121. """
  122. d.write(script)
  123. print("Wrote " + deactivate_vars_sh)
  124.  
  125. # csh
  126. with open(activate_vars_csh, mode="w") as a:
  127. script = """#!/bin/csh
  128. setenv ISISROOT {}
  129. setenv ISIS3DATA {}
  130. setenv ISIS3TESTDATA {}
  131.  
  132. source $CONDA_PREFIX/scripts/tabCompletion.csh
  133. """.format(
  134. os.environ["CONDA_PREFIX"], args.data_dir, args.test_dir
  135. )
  136. a.write(script)
  137. print("Wrote " + activate_vars_csh)
  138.  
  139. with open(deactivate_vars_csh, mode="w") as d:
  140. script = """#!/bin/sh
  141. unsetenv ISISROOT
  142. unsetenv ISIS3DATA
  143. unsetenv ISIS3TESTDATA
  144. """
  145. d.write(script)
  146. print("Wrote " + deactivate_vars_csh)
  147.  
  148. # fish
  149. with open(activate_vars_fish, mode="w") as a:
  150. script = """#!/usr/bin/env fish
  151. set -gx ISISROOT {}
  152. set -gx ISIS3DATA {}
  153. set -gx ISIS3TESTDATA {}
  154. """.format(
  155. os.environ["CONDA_PREFIX"], args.data_dir, args.test_dir
  156. )
  157. a.write(script)
  158. print("Wrote " + activate_vars_fish)
  159.  
  160. with open(deactivate_vars_csh, mode="w") as d:
  161. script = """#!/bin/sh
  162. set -e ISISROOT
  163. set -e ISIS3DATA
  164. set -e ISIS3TESTDATA
  165. """
  166. d.write(script)
  167. print("Wrote " + deactivate_vars_fish)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement