Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _win32_dir(path, star=''):
"""
Using the windows cmd shell to get information about a directory
"""
from ubelt import util_cmd
import re
wrapper = 'cmd /S /C "{}"' # the /S will preserve all inner quotes
command = 'dir /-C "{}"{}'.format(path, star)
wrapped = wrapper.format(command)
info = util_cmd.cmd(wrapped, shell=True)
if info['ret'] != 0:
from ubelt import util_format
print('Failed command:')
print(info['command'])
print(util_format.repr2(info, nl=1))
raise OSError(str(info))
# parse the output of dir to get some info
# Remove header and footer
lines = info['out'].split('\n')[5:-3]
splitter = re.compile('( +)')
for line in lines:
parts = splitter.split(line)
date, sep, time, sep, ampm, sep, type_or_size, sep = parts[:8]
name = ''.join(parts[8:])
# if type is a junction then name will also contain the linked loc
if name == '.' or name == '..':
>>> ub.touch(fpath1)
>>> proc = ub.startfile(fpath1)
"""
from ubelt import util_cmd
if verbose:
print('[ubelt] startfile("{}")'.format(fpath))
fpath = normpath(fpath)
if not exists(fpath):
raise Exception('Cannot start nonexistant file: %r' % fpath)
if not WIN32:
import pipes
fpath = pipes.quote(fpath)
if LINUX:
info = util_cmd.cmd(('xdg-open', fpath), detach=True, verbose=verbose)
elif DARWIN:
info = util_cmd.cmd(('open', fpath), detach=True, verbose=verbose)
elif WIN32:
os.startfile(fpath)
info = None
else:
raise RuntimeError('Unknown Platform')
if info is not None:
if not info['proc']:
raise Exception('startfile failed')
# directory symbolic link
if verbose:
print('... as directory symlink')
command = 'mklink /D "{}" "{}"'.format(link, path)
# Using the win32 API seems to result in privilege errors
# but using shell commands does not have this problem. Weird.
# jwfs.symlink(path, link, target_is_directory=True)
# TODO: what do we need to do to use the windows api instead of shell?
else:
# file symbolic link
if verbose:
print('... as file symlink')
command = 'mklink "{}" "{}"'.format(link, path)
if command is not None:
info = util_cmd.cmd(command, shell=True)
if info['ret'] != 0:
from ubelt import util_format
permission_msg = 'You do not have sufficient privledges'
if permission_msg not in info['err']:
print('Failed command:')
print(info['command'])
print(util_format.repr2(info, nl=1))
raise OSError(str(info))
return link
# TODO: what is the windows api for this?
command = 'mklink /J "{}" "{}"'.format(link, path)
else:
# try using a hard link
if verbose:
print('... as hard link')
# command = 'mklink /H "{}" "{}"'.format(link, path)
try:
jwfs.link(path, link) # this seems to be allowed
except Exception:
print('Failed to hardlink link={} to path={}'.format(link, path))
raise
command = None
if command is not None:
info = util_cmd.cmd(command, shell=True)
if info['ret'] != 0:
from ubelt import util_format
print('Failed command:')
print(info['command'])
print(util_format.repr2(info, nl=1))
raise OSError(str(info))
return link
>>> base = ub.ensure_app_cache_dir('ubelt')
>>> fpath1 = join(base, 'test_open.txt')
>>> ub.touch(fpath1)
>>> proc = ub.startfile(fpath1)
"""
from ubelt import util_cmd
if verbose:
print('[ubelt] startfile("{}")'.format(fpath))
fpath = normpath(fpath)
if not exists(fpath):
raise Exception('Cannot start nonexistant file: %r' % fpath)
if not WIN32:
import pipes
fpath = pipes.quote(fpath)
if LINUX:
info = util_cmd.cmd(('xdg-open', fpath), detach=True, verbose=verbose)
elif DARWIN:
info = util_cmd.cmd(('open', fpath), detach=True, verbose=verbose)
elif WIN32:
os.startfile(fpath)
info = None
else:
raise RuntimeError('Unknown Platform')
if info is not None:
if not info['proc']:
raise Exception('startfile failed')