swc-windows-installer.py: Look for an R bin directory
authorW. Trevor King <wking@tremily.us>
Mon, 9 Jun 2014 22:17:58 +0000 (15:17 -0700)
committerW. Trevor King <wking@tremily.us>
Mon, 9 Jun 2014 22:17:58 +0000 (15:17 -0700)
On Thu, May 15, 2014 at 08:07:19PM -0700, Ethan White wrote:
> Problem: R is not added to the Windows' path when it is
> installed. This means that any workshops that use R from the command
> line will run into problems (as we did at the first Data Carpentry
> workshop).
>
> Solution: Add R to the path using our Windows' installer.
>
> Challenges: Just to make @wking's life exciting, R's path varies
> depending on the version. The standard path is `C:\Program
> Files\R\R-x.x.x\bin\` where `x.x.x` is the version number of the
> installed version of R.

swc-windows-installer.py

index 9af86e797d1dc00ce73ea40b7d95899ae8a01a40..0c6d0e56350b911d0173ae2ffe233d5dc06e2c1c 100755 (executable)
@@ -9,6 +9,7 @@ The script:
 * Installs sqlite3 and makes it accessible from msysGit
 * Creates ~/nano.rc with links to syntax highlighting configs
 * Provides standard nosetests behavior for msysgit
 * Installs sqlite3 and makes it accessible from msysGit
 * Creates ~/nano.rc with links to syntax highlighting configs
 * Provides standard nosetests behavior for msysgit
+* Add R's bin directory to the path (if we can find it)
 
 To use:
 
 
 To use:
 
@@ -22,6 +23,7 @@ To use:
 
 """
 
 
 """
 
+import glob
 import hashlib
 try:  # Python 3
     from io import BytesIO as _BytesIO
 import hashlib
 try:  # Python 3
     from io import BytesIO as _BytesIO
@@ -175,6 +177,22 @@ def create_nosetests_entry_point(python_scripts_directory):
         f.write(contents)
 
 
         f.write(contents)
 
 
+def get_r_bin_directory():
+    """Locate the R bin directory (if R is installed
+    """
+    pf = _os.environ.get('ProgramFiles', r'c:\ProgramFiles')
+    bin_glob = os.path.join(pf, 'R', 'R-[0-9]*.[0-9]*.[0-9]*', 'bin')
+    version_re = re.compile('^R-(\d+)[.](\d+)[.](\d+)$')
+    paths = {}
+    for path in glob.glob(bin_glob):
+        version_dir = os.path.basename(os.path.dirname(path))
+        version_match = version_re.match(version_dir)
+        if version_match:
+            paths[version_match.groups()] = path
+    version = sorted(paths.keys())[-1]
+    return paths[version]
+
+
 def update_bash_profile(extra_paths=()):
     """Create or append to a .bash_profile for Software Carpentry
 
 def update_bash_profile(extra_paths=()):
     """Create or append to a .bash_profile for Software Carpentry
 
@@ -216,7 +234,11 @@ def main():
     install_nano(install_directory=nano_dir)
     install_nanorc(install_directory=nanorc_dir)
     install_sqlite(install_directory=sqlite_dir)
     install_nano(install_directory=nano_dir)
     install_nanorc(install_directory=nanorc_dir)
     install_sqlite(install_directory=sqlite_dir)
-    update_bash_profile(extra_paths=(nano_dir, sqlite_dir, bin_dir))
+    paths = [nano_dir, sqlite_dir, bin_dir]
+    r_dir = get_r_bin_directory()
+    if r_dir:
+        paths.append(r_dir)
+    update_bash_profile(extra_paths=paths)
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':