rtcwd.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Create a command to change the current working directory environment variable.
00018 
00019 '''
00020 
00021 
00022 #!/usr/bin/env python
00023 
00024 import os
00025 import rtctree.tree
00026 import rtctree.path
00027 import sys
00028 import traceback
00029 
00030 import path
00031 import rts_exceptions
00032 
00033 if sys.platform == 'win32':
00034     SET_CMD = 'set'
00035     EQUALS = '='
00036     QUOTE = ''
00037 elif 'SHELL' in os.environ and 'csh' in os.environ['SHELL']:
00038     SET_CMD = 'setenv'
00039     EQUALS = ' '
00040     QUOTE = '"'
00041 else:
00042     SET_CMD = 'export'
00043     EQUALS = '='
00044     QUOTE = '"'
00045 
00046 
00047 def make_cmd_line(dest):
00048     return '{0} {1}{2}{3}{4}{3}'.format(SET_CMD, path.ENV_VAR, EQUALS,
00049             QUOTE, dest)
00050 
00051 
00052 def cd(cmd_path, full_path):
00053     path, port = rtctree.path.parse_path(full_path)
00054     if port:
00055         raise rts_exceptions.NotADirectoryError(cmd_path)
00056     if not path[-1]:
00057         # Remove trailing slash part
00058         path = path[:-1]
00059     tree = rtctree.tree.RTCTree(paths=path)
00060     if not tree.has_path(path):
00061         raise rts_exceptions.NotADirectoryError(cmd_path)
00062     if not tree.is_directory(path):
00063         raise rts_exceptions.NotADirectoryError(cmd_path)
00064     return make_cmd_line(full_path)
00065 
00066 
00067 def main(argv=None, tree=None):
00068     if argv:
00069         sys.argv = [sys.argv[0]] + argv
00070     if len(sys.argv) < 2:
00071         # Change to the root dir
00072         print '{0} {1}{3}{2}/{2}'.format(SET_CMD, path.ENV_VAR, QUOTE, EQUALS)
00073         return 0
00074 
00075     # Take the first argument only
00076     cmd_path = sys.argv[1]
00077 
00078     try:
00079         if cmd_path == '.' or cmd_path == './':
00080             # Special case for '.': do nothing
00081             if path.ENV_VAR in os.environ:
00082                 print make_cmd_line(os.environ[path.ENV_VAR])
00083             else:
00084                 print make_cmd_line('/')
00085         elif cmd_path == '..' or cmd_path == '../':
00086             # Special case for '..': go up one directory
00087             if path.ENV_VAR in os.environ and os.environ[path.ENV_VAR] and \
00088                     os.environ[path.ENV_VAR] != '/':
00089                 parent = os.environ[path.ENV_VAR][\
00090                         :os.environ[path.ENV_VAR].rstrip('/').rfind('/')]
00091                 if not parent:
00092                     parent = '/'
00093                 print make_cmd_line(parent)
00094             else:
00095                 print make_cmd_line('/')
00096         else:
00097             full_path = path.cmd_path_to_full_path(cmd_path)
00098             print cd(cmd_path, full_path)
00099     except Exception, e:
00100         print >>sys.stderr, 'rtcwd: {0}'.format(e)
00101         return 1
00102     return 0
00103 
00104 
00105 # vim: tw=79
00106 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12