def _pythonpath():
items = os.environ.get('PYTHONPATH', '').split(os.pathsep)
return filter(None, items)
python类pathsep()的实例源码
def which(program):
"""Determines whether program exists
"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
raise
def site_config_dirs(appname):
"""Return a list of potential user-shared config dirs for this application.
"appname" is the name of application.
Typical user config directories are:
macOS: /Library/Application Support/<AppName>/
Unix: /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in
$XDG_CONFIG_DIRS
Win XP: C:\Documents and Settings\All Users\Application ...
...Data\<AppName>\
Vista: (Fail! "C:\ProgramData" is a hidden *system* directory
on Vista.)
Win 7: Hidden, but writeable on Win 7:
C:\ProgramData\<AppName>\
"""
if WINDOWS:
path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
pathlist = [os.path.join(path, appname)]
elif sys.platform == 'darwin':
pathlist = [os.path.join('/Library/Application Support', appname)]
else:
# try looking in $XDG_CONFIG_DIRS
xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
if xdg_config_dirs:
pathlist = [
os.path.join(expanduser(x), appname)
for x in xdg_config_dirs.split(os.pathsep)
]
else:
pathlist = []
# always look in /etc directly as well
pathlist.append('/etc')
return pathlist
# -- Windows support functions --
def finalize_options(self):
# This might be confusing: both build-clib and build-temp default
# to build-temp as defined by the "build" command. This is because
# I think that C libraries are really just temporary build
# by-products, at least from the point of view of building Python
# extensions -- but I want to keep my options open.
self.set_undefined_options('build',
('build_temp', 'build_clib'),
('build_temp', 'build_temp'),
('compiler', 'compiler'),
('debug', 'debug'),
('force', 'force'))
self.libraries = self.distribution.libraries
if self.libraries:
self.check_library_list(self.libraries)
if self.include_dirs is None:
self.include_dirs = self.distribution.include_dirs or []
if isinstance(self.include_dirs, str):
self.include_dirs = self.include_dirs.split(os.pathsep)
# XXX same as for build_ext -- what about 'self.define' and
# 'self.undef' ?
def find_executable(executable, path=None):
"""Tries to find 'executable' in the directories listed in 'path'.
A string listing directories separated by 'os.pathsep'; defaults to
os.environ['PATH']. Returns the complete filename or None if not found.
"""
if path is None:
path = os.environ['PATH']
paths = path.split(os.pathsep)
base, ext = os.path.splitext(executable)
if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
executable = executable + '.exe'
if not os.path.isfile(executable):
for p in paths:
f = os.path.join(p, executable)
if os.path.isfile(f):
# the file exists, we have a shot at spawn working
return f
return None
else:
return executable
def _iscommand(cmd):
"""Return True if cmd is executable or can be found on the executable
search path."""
if _isexecutable(cmd):
return True
path = os.environ.get("PATH")
if not path:
return False
for d in path.split(os.pathsep):
exe = os.path.join(d, cmd)
if _isexecutable(exe):
return True
return False
# General parent classes
def which(program):
# type: (str) -> Optional[str]
program = exename(program)
fpath, _ = os.path.split(program)
if fpath:
if is_executable(program):
return program
else:
for path in [os.path.abspath(os.curdir)] + os.environ['PATH'].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(unifilename(path), unifilename(program))
if is_executable(exe_file):
return exe_file
return None
# ----------------------------------------------------------------------
def find_command(cmd, paths=None, pathext=None):
"""Searches the PATH for the given command and returns its path"""
if paths is None:
paths = os.environ.get('PATH', '').split(os.pathsep)
if isinstance(paths, string_types):
paths = [paths]
# check if there are funny path extensions for executables, e.g. Windows
if pathext is None:
pathext = get_pathext()
pathext = [ext for ext in pathext.lower().split(os.pathsep) if len(ext)]
# don't use extensions if the command ends with one of them
if os.path.splitext(cmd)[1].lower() in pathext:
pathext = ['']
# check if we find the command on PATH
for path in paths:
# try without extension first
cmd_path = os.path.join(path, cmd)
for ext in pathext:
# then including the extension
cmd_path_ext = cmd_path + ext
if os.path.isfile(cmd_path_ext):
return cmd_path_ext
if os.path.isfile(cmd_path):
return cmd_path
raise BadCommand('Cannot find command %r' % cmd)
def find_library_nt(name):
# modified from ctypes.util
# ctypes.util.find_library just returns first result he found
# but we want to try them all
# because on Windows, users may have both 32bit and 64bit version installed
results = []
for directory in os.environ['PATH'].split(os.pathsep):
fname = os.path.join(directory, name)
if os.path.isfile(fname):
results.append(fname)
if fname.lower().endswith(".dll"):
continue
fname = fname + ".dll"
if os.path.isfile(fname):
results.append(fname)
return results
def run(self):
env = self.env
bld = self.generator.bld
wd = bld.bldnode.abspath()
#add src node + bld node (for generated java code)
srcpath = self.generator.path.abspath() + os.sep + self.generator.srcdir
srcpath += os.pathsep
srcpath += self.generator.path.get_bld().abspath() + os.sep + self.generator.srcdir
classpath = env.CLASSPATH
classpath += os.pathsep
classpath += os.pathsep.join(self.classpath)
classpath = "".join(classpath)
self.last_cmd = lst = []
lst.extend(Utils.to_list(env['JAVADOC']))
lst.extend(['-d', self.generator.javadoc_output.abspath()])
lst.extend(['-sourcepath', srcpath])
lst.extend(['-classpath', classpath])
lst.extend(['-subpackages'])
lst.extend(self.generator.javadoc_package)
lst = [x for x in lst if x]
self.generator.bld.cmd_and_log(lst, cwd=wd, env=env.env or None, quiet=0)
def configure(self):
"""
Detect the javac, java and jar programs
"""
# If JAVA_PATH is set, we prepend it to the path list
java_path = self.environ['PATH'].split(os.pathsep)
v = self.env
if 'JAVA_HOME' in self.environ:
java_path = [os.path.join(self.environ['JAVA_HOME'], 'bin')] + java_path
self.env['JAVA_HOME'] = [self.environ['JAVA_HOME']]
for x in 'javac java jar javadoc'.split():
self.find_program(x, var=x.upper(), path_list=java_path)
if 'CLASSPATH' in self.environ:
v['CLASSPATH'] = self.environ['CLASSPATH']
if not v['JAR']: self.fatal('jar is required for making java packages')
if not v['JAVAC']: self.fatal('javac is required for compiling java classes')
v['JARCREATE'] = 'cf' # can use cvf
v['JAVACFLAGS'] = []
def configure(self):
"""
Detect the scalac program
"""
# If SCALA_HOME is set, we prepend it to the path list
java_path = self.environ['PATH'].split(os.pathsep)
v = self.env
if 'SCALA_HOME' in self.environ:
java_path = [os.path.join(self.environ['SCALA_HOME'], 'bin')] + java_path
self.env['SCALA_HOME'] = [self.environ['SCALA_HOME']]
for x in 'scalac scala'.split():
self.find_program(x, var=x.upper(), path_list=java_path)
if 'CLASSPATH' in self.environ:
v['CLASSPATH'] = self.environ['CLASSPATH']
v.SCALACFLAGS = ['-verbose']
if not v['SCALAC']: self.fatal('scalac is required for compiling scala classes')
def run(task):
command = 'SAS'
fun = sas_fun
node = task.inputs[0]
logfilenode = node.change_ext('.log')
lstfilenode = node.change_ext('.lst')
# set the cwd
task.cwd = task.inputs[0].parent.get_src().abspath()
Logs.debug('runner: %s on %s' % (command, node.abspath))
SASINPUTS = node.parent.get_bld().abspath() + os.pathsep + node.parent.get_src().abspath() + os.pathsep
task.env.env = {'SASINPUTS': SASINPUTS}
task.env.SRCFILE = node.abspath()
task.env.LOGFILE = logfilenode.abspath()
task.env.LSTFILE = lstfilenode.abspath()
ret = fun(task)
if ret:
Logs.error('Running %s on %r returned a non-zero exit' % (command, node))
Logs.error('SRCFILE = %r' % node)
Logs.error('LOGFILE = %r' % logfilenode)
Logs.error('LSTFILE = %r' % lstfilenode)
return ret
def configure(self):
"""
Detect the javac, java and jar programs
"""
# If JAVA_PATH is set, we prepend it to the path list
java_path = self.environ['PATH'].split(os.pathsep)
v = self.env
if 'JAVA_HOME' in self.environ:
java_path = [os.path.join(self.environ['JAVA_HOME'], 'bin')] + java_path
self.env['JAVA_HOME'] = [self.environ['JAVA_HOME']]
for x in 'javac java jar javadoc'.split():
self.find_program(x, var=x.upper(), path_list=java_path)
if 'CLASSPATH' in self.environ:
v['CLASSPATH'] = self.environ['CLASSPATH']
if not v['JAR']: self.fatal('jar is required for making java packages')
if not v['JAVAC']: self.fatal('javac is required for compiling java classes')
v['JARCREATE'] = 'cf' # can use cvf
v['JAVACFLAGS'] = []
def configure(self):
"""
Detect the scalac program
"""
# If SCALA_HOME is set, we prepend it to the path list
java_path = self.environ['PATH'].split(os.pathsep)
v = self.env
if 'SCALA_HOME' in self.environ:
java_path = [os.path.join(self.environ['SCALA_HOME'], 'bin')] + java_path
self.env['SCALA_HOME'] = [self.environ['SCALA_HOME']]
for x in 'scalac scala'.split():
self.find_program(x, var=x.upper(), path_list=java_path)
if 'CLASSPATH' in self.environ:
v['CLASSPATH'] = self.environ['CLASSPATH']
v.SCALACFLAGS = ['-verbose']
if not v['SCALAC']: self.fatal('scalac is required for compiling scala classes')
def run(task):
command = 'SAS'
fun = sas_fun
node = task.inputs[0]
logfilenode = node.change_ext('.log')
lstfilenode = node.change_ext('.lst')
# set the cwd
task.cwd = task.inputs[0].parent.get_src().abspath()
Logs.debug('runner: %s on %s' % (command, node.abspath))
SASINPUTS = node.parent.get_bld().abspath() + os.pathsep + node.parent.get_src().abspath() + os.pathsep
task.env.env = {'SASINPUTS': SASINPUTS}
task.env.SRCFILE = node.abspath()
task.env.LOGFILE = logfilenode.abspath()
task.env.LSTFILE = lstfilenode.abspath()
ret = fun(task)
if ret:
Logs.error('Running %s on %r returned a non-zero exit' % (command, node))
Logs.error('SRCFILE = %r' % node)
Logs.error('LOGFILE = %r' % logfilenode)
Logs.error('LSTFILE = %r' % lstfilenode)
return ret
def check_exe(name, env=None):
"""
Ensure that a program exists
:type name: string
:param name: name or path to program
:return: path of the program or None
"""
if not name:
raise ValueError('Cannot execute an empty string!')
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(name)
if fpath and is_exe(name):
return os.path.abspath(name)
else:
env = env or os.environ
for path in env["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, name)
if is_exe(exe_file):
return os.path.abspath(exe_file)
return None
def run(self):
env = self.env
bld = self.generator.bld
wd = bld.bldnode.abspath()
#add src node + bld node (for generated java code)
srcpath = self.generator.path.abspath() + os.sep + self.generator.srcdir
srcpath += os.pathsep
srcpath += self.generator.path.get_bld().abspath() + os.sep + self.generator.srcdir
classpath = env.CLASSPATH
classpath += os.pathsep
classpath += os.pathsep.join(self.classpath)
classpath = "".join(classpath)
self.last_cmd = lst = []
lst.extend(Utils.to_list(env['JAVADOC']))
lst.extend(['-d', self.generator.javadoc_output.abspath()])
lst.extend(['-sourcepath', srcpath])
lst.extend(['-classpath', classpath])
lst.extend(['-subpackages'])
lst.extend(self.generator.javadoc_package)
lst = [x for x in lst if x]
self.generator.bld.cmd_and_log(lst, cwd=wd, env=env.env or None, quiet=0)
def configure(self):
"""
Detect the javac, java and jar programs
"""
# If JAVA_PATH is set, we prepend it to the path list
java_path = self.environ['PATH'].split(os.pathsep)
v = self.env
if 'JAVA_HOME' in self.environ:
java_path = [os.path.join(self.environ['JAVA_HOME'], 'bin')] + java_path
self.env['JAVA_HOME'] = [self.environ['JAVA_HOME']]
for x in 'javac java jar javadoc'.split():
self.find_program(x, var=x.upper(), path_list=java_path)
if 'CLASSPATH' in self.environ:
v['CLASSPATH'] = self.environ['CLASSPATH']
if not v['JAR']: self.fatal('jar is required for making java packages')
if not v['JAVAC']: self.fatal('javac is required for compiling java classes')
v['JARCREATE'] = 'cf' # can use cvf
v['JAVACFLAGS'] = []
def check_exe(name, env=None):
"""
Ensure that a program exists
:type name: string
:param name: name or path to program
:return: path of the program or None
"""
if not name:
raise ValueError('Cannot execute an empty string!')
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(name)
if fpath and is_exe(name):
return os.path.abspath(name)
else:
env = env or os.environ
for path in env["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, name)
if is_exe(exe_file):
return os.path.abspath(exe_file)
return None
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
#source: https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file
def find_executable(executable):
"""
Return the path to the given executable, or None if not found.
create_environment is used to augment PATH before searching
for the executable.
"""
env = create_environment()
for base in env.get('PATH', '').split(os.pathsep):
path = os.path.join(os.path.expanduser(base), executable)
# On Windows, if path does not have an extension, try .exe, .cmd, .bat
if sublime.platform() == 'windows' and not os.path.splitext(path)[1]:
for extension in ('.exe', '.cmd', '.bat'):
path_ext = path + extension
if can_exec(path_ext):
return path_ext
elif can_exec(path):
return path
return None
def find_command(cmd, path=None, pathext=None):
if path is None:
path = os.environ.get('PATH', '').split(os.pathsep)
if isinstance(path, six.string_types):
path = [path]
# check if there are funny path extensions for executables, e.g. Windows
if pathext is None:
pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD').split(os.pathsep)
# don't use extensions if the command ends with one of them
for ext in pathext:
if cmd.endswith(ext):
pathext = ['']
break
# check if we find the command on PATH
for p in path:
f = os.path.join(p, cmd)
if os.path.isfile(f):
return f
for ext in pathext:
fext = f + ext
if os.path.isfile(fext):
return fext
return None
def which1(program, pathstr=None):
if pathstr is None:
pathstr = os.environ["PATH"]
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in pathstr.split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def loadProjectFile(path):
global project_data, project_path
try:
with open(path) as json_file: project_data = json.load(json_file)
project_path=os.path.dirname(path) + os.sep
com_path = project_path + "project" + os.sep + "core" + os.sep + "com"
sys.path.append(com_path)
except Exception as e:
print("LOAD ERROR")
print(str(e))
env_path = os.getenv("PYTHONPATH")
if env_path:
if not com_path in env_path:
os.environ["PYTHONPATH"] += os.pathsep + com_path
else:
os.environ["PYTHONPATH"] = com_path
def find_command(cmd, path=None, pathext=None):
if path is None:
path = os.environ.get('PATH', '').split(os.pathsep)
if isinstance(path, six.string_types):
path = [path]
# check if there are funny path extensions for executables, e.g. Windows
if pathext is None:
pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD').split(os.pathsep)
# don't use extensions if the command ends with one of them
for ext in pathext:
if cmd.endswith(ext):
pathext = ['']
break
# check if we find the command on PATH
for p in path:
f = os.path.join(p, cmd)
if os.path.isfile(f):
return f
for ext in pathext:
fext = f + ext
if os.path.isfile(fext):
return fext
return None
def is_executable_available(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath = os.path.dirname(program)
if fpath:
if is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return False
def is_executable_available(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath = os.path.dirname(program)
if fpath:
if is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return False
def is_executable_available(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath = os.path.dirname(program)
if fpath:
if is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return False
def find_command(cmd, paths=None, pathext=None):
"""Searches the PATH for the given command and returns its path"""
if paths is None:
paths = os.environ.get('PATH', '').split(os.pathsep)
if isinstance(paths, string_types):
paths = [paths]
# check if there are funny path extensions for executables, e.g. Windows
if pathext is None:
pathext = get_pathext()
pathext = [ext for ext in pathext.lower().split(os.pathsep) if len(ext)]
# don't use extensions if the command ends with one of them
if os.path.splitext(cmd)[1].lower() in pathext:
pathext = ['']
# check if we find the command on PATH
for path in paths:
# try without extension first
cmd_path = os.path.join(path, cmd)
for ext in pathext:
# then including the extension
cmd_path_ext = cmd_path + ext
if os.path.isfile(cmd_path_ext):
return cmd_path_ext
if os.path.isfile(cmd_path):
return cmd_path
raise BadCommand('Cannot find command %r' % cmd)