def _wrap_exe(relexepath, args=None): snap_dir = common.get_snapdir() exepath = os.path.join(snap_dir, relexepath) wrappath = exepath + ".wrapper" shebang = None # TODO talk to original author if the exception to be captured here is # FileNotFoundError, the original code was a general catch all try: os.remove(wrappath) except FileNotFoundError: pass wrapexec = "$SNAP_APP_PATH/{}".format(relexepath) if not os.path.exists(exepath) and "/" not in relexepath: # If it doesn't exist it might be in the path logger.debug('Checking to see if "{}" is in the $PATH'.format(relexepath)) with tempfile.NamedTemporaryFile("w+") as tempf: script = "#!/bin/sh\n" + "{}\n".format(common.assemble_env()) + 'which "{}"\n'.format(relexepath) tempf.write(script) tempf.flush() common.run(["/bin/sh", tempf.name], cwd=snap_dir) wrapexec = relexepath else: with open(exepath, "rb") as exefile: # If the file has a she-bang, the path might be pointing to # the local 'parts' dir. Extract it so that _write_wrap_exe # will have a chance to rewrite it. if exefile.read(2) == b"#!": shebang = exefile.readline().strip().decode("utf-8") _write_wrap_exe(wrapexec, wrappath, shebang=shebang, args=args) return os.path.relpath(wrappath, snap_dir)
def _write_wrap_exe(wrapexec, wrappath, shebang=None, args=None, cwd=None): args = ' '.join(args) + ' $*' if args else '$*' cwd = 'cd {}'.format(cwd) if cwd else '' snap_dir = common.get_snapdir() assembled_env = common.assemble_env().replace(snap_dir, '$SNAP') replace_path = r'{}/[a-z0-9][a-z0-9+-]*/install'.format( common.get_partsdir()) assembled_env = re.sub(replace_path, '$SNAP', assembled_env) executable = '"{}"'.format(wrapexec) if shebang is not None: new_shebang = re.sub(replace_path, '$SNAP', shebang) if new_shebang != shebang: # If the shebang was pointing to and executable within the # local 'parts' dir, have the wrapper script execute it # directly, since we can't use $SNAP in the shebang itself. executable = '"{}" "{}"'.format(new_shebang, wrapexec) script = ('#!/bin/sh\n' + '{}\n'.format(assembled_env) + '{}\n'.format(cwd) + 'exec {} {}\n'.format(executable, args)) with open(wrappath, 'w+') as f: f.write(script) os.chmod(wrappath, 0o755)
def _write_wrap_exe(wrapexec, wrappath, shebang=None, args=None, cwd=None): args = ' '.join(args) + ' $*' if args else '$*' cwd = 'cd {}'.format(cwd) if cwd else '' snap_dir = common.get_snapdir() assembled_env = common.assemble_env().replace(snap_dir, '$SNAP_APP_PATH') replace_path = r'{}/[a-z0-9][a-z0-9+-]*/install'.format( common.get_partsdir()) assembled_env = re.sub(replace_path, '$SNAP_APP_PATH', assembled_env) executable = '"{}"'.format(wrapexec) if shebang is not None: new_shebang = re.sub(replace_path, '$SNAP_APP_PATH', shebang) if new_shebang != shebang: # If the shebang was pointing to and executable within the # local 'parts' dir, have the wrapper script execute it # directly, since we can't use $SNAP_APP_PATH in the shebang # itself. executable = '"{}" "{}"'.format(new_shebang, wrapexec) script = ('#!/bin/sh\n' + '{}\n'.format(assembled_env) + '{}\n'.format(cwd) + 'exec {} {}\n'.format(executable, args)) with open(wrappath, 'w+') as f: f.write(script) os.chmod(wrappath, 0o755)
def _wrap_exe(relexepath): snap_dir = common.get_snapdir() exepath = os.path.join(snap_dir, relexepath) wrappath = exepath + '.wrapper' # TODO talk to original author if the exception to be captured here is # FileNotFoundError, the original code was a general catch all try: os.remove(wrappath) except FileNotFoundError: pass wrapexec = '$SNAP_APP_PATH/{}'.format(relexepath) if not os.path.exists(exepath) and '/' not in relexepath: # If it doesn't exist it might be in the path logger.debug('Checking to see if "{}" is in the $PATH'.format( relexepath)) with tempfile.NamedTemporaryFile('w+') as tempf: script = ('#!/bin/sh\n' + '{}\n'.format(common.assemble_env()) + 'which "{}"\n'.format(relexepath)) tempf.write(script) tempf.flush() common.run(['/bin/sh', tempf.name], cwd=snap_dir) wrapexec = relexepath _write_wrap_exe(wrapexec, wrappath) return os.path.relpath(wrappath, snap_dir)
def _wrap_exe(relexepath): snap_dir = common.get_snapdir() exepath = os.path.join(snap_dir, relexepath) wrappath = exepath + '.wrapper' # TODO talk to original author if the exception to be captured here is # FileNotFoundError, the original code was a general catch all try: os.remove(wrappath) except FileNotFoundError: pass wrapexec = '$SNAP_APP_PATH/{}'.format(relexepath) if not os.path.exists(exepath) and '/' not in relexepath: # If it doesn't exist it might be in the path logger.debug( 'Checking to see if "{}" is in the $PATH'.format(relexepath)) with tempfile.NamedTemporaryFile('w+') as tempf: script = ('#!/bin/sh\n' + '{}\n'.format(common.assemble_env()) + 'which "{}"\n'.format(relexepath)) tempf.write(script) tempf.flush() common.run(['/bin/sh', tempf.name], cwd=snap_dir) wrapexec = relexepath _write_wrap_exe(wrapexec, wrappath) return os.path.relpath(wrappath, snap_dir)
def _find_bin(binary, basedir): # If it doesn't exist it might be in the path logger.debug('Checking that {!r} is in the $PATH'.format(binary)) script = ('#!/bin/sh\n' + '{}\n'.format(common.assemble_env()) + 'which "{}"\n'.format(binary)) with tempfile.NamedTemporaryFile('w+') as tempf: tempf.write(script) tempf.flush() try: common.run(['/bin/sh', tempf.name], cwd=basedir, stdout=subprocess.DEVNULL) except subprocess.CalledProcessError: raise CommandError(binary)
def _write_wrap_exe(wrapexec, wrappath, args=[], cwd=None): args = ' '.join(args) + ' $*' if args else '$*' cwd = 'cd {}'.format(cwd) if cwd else '' snap_dir = common.get_snapdir() assembled_env = common.assemble_env().replace(snap_dir, '$SNAP_APP_PATH') replace_path = r'{}/.*/install'.format(common.get_partsdir()) assembled_env = re.sub(replace_path, '$SNAP_APP_PATH', assembled_env) script = ('#!/bin/sh\n' + '{}\n'.format(assembled_env) + '{}\n'.format(cwd) + 'exec "{}" {}\n'.format(wrapexec, args)) with open(wrappath, 'w+') as f: f.write(script) os.chmod(wrappath, 0o755)
def _write_wrap_exe(wrapexec, wrappath, shebang=None, args=None, cwd=None): args = " ".join(args) + " $*" if args else "$*" cwd = "cd {}".format(cwd) if cwd else "" snap_dir = common.get_snapdir() assembled_env = common.assemble_env().replace(snap_dir, "$SNAP_APP_PATH") replace_path = r"{}/.*/install".format(common.get_partsdir()) assembled_env = re.sub(replace_path, "$SNAP_APP_PATH", assembled_env) executable = '"{}"'.format(wrapexec) if shebang is not None: new_shebang = re.sub(replace_path, "$SNAP_APP_PATH", shebang) if new_shebang != shebang: # If the shebang was pointing to and executable within the # local 'parts' dir, have the wrapper script execute it # directly, since we can't use $SNAP_APP_PATH in the shebang # itself. executable = '"{}" "{}"'.format(new_shebang, wrapexec) script = "#!/bin/sh\n" + "{}\n".format(assembled_env) + "{}\n".format(cwd) + "exec {} {}\n".format(executable, args) with open(wrappath, "w+") as f: f.write(script) os.chmod(wrappath, 0o755)
def _wrap_exe(command, basename=None): execparts = shlex.split(command) snap_dir = common.get_snapdir() exepath = os.path.join(snap_dir, execparts[0]) if basename: wrappath = os.path.join(snap_dir, basename) + '.wrapper' else: wrappath = exepath + '.wrapper' shebang = None if os.path.exists(wrappath): os.remove(wrappath) wrapexec = '$SNAP/{}'.format(execparts[0]) if not os.path.exists(exepath) and '/' not in execparts[0]: # If it doesn't exist it might be in the path logger.debug('Checking to see if {!r} is in the $PATH'.format( execparts[0])) with tempfile.NamedTemporaryFile('w+') as tempf: script = ('#!/bin/sh\n' + '{}\n'.format(common.assemble_env()) + 'which "{}"\n'.format(execparts[0])) tempf.write(script) tempf.flush() common.run(['/bin/sh', tempf.name], cwd=snap_dir) wrapexec = execparts[0] else: with open(exepath, 'rb') as exefile: # If the file has a she-bang, the path might be pointing to # the local 'parts' dir. Extract it so that _write_wrap_exe # will have a chance to rewrite it. if exefile.read(2) == b'#!': shebang = exefile.readline().strip().decode('utf-8') _write_wrap_exe(wrapexec, wrappath, shebang=shebang, args=execparts[1:]) return os.path.relpath(wrappath, snap_dir)
def _wrap_exe(relexepath, args=None): snap_dir = common.get_snapdir() exepath = os.path.join(snap_dir, relexepath) wrappath = exepath + '.wrapper' shebang = None # TODO talk to original author if the exception to be captured here is # FileNotFoundError, the original code was a general catch all try: os.remove(wrappath) except FileNotFoundError: pass wrapexec = '$SNAP_APP_PATH/{}'.format(relexepath) if not os.path.exists(exepath) and '/' not in relexepath: # If it doesn't exist it might be in the path logger.debug( 'Checking to see if "{}" is in the $PATH'.format(relexepath)) with tempfile.NamedTemporaryFile('w+') as tempf: script = ('#!/bin/sh\n' + '{}\n'.format(common.assemble_env()) + 'which "{}"\n'.format(relexepath)) tempf.write(script) tempf.flush() common.run(['/bin/sh', tempf.name], cwd=snap_dir) wrapexec = relexepath else: with open(exepath, 'rb') as exefile: # If the file has a she-bang, the path might be pointing to # the local 'parts' dir. Extract it so that _write_wrap_exe # will have a chance to rewrite it. if exefile.read(2) == b'#!': shebang = exefile.readline().strip().decode('utf-8') _write_wrap_exe(wrapexec, wrappath, shebang=shebang, args=args) return os.path.relpath(wrappath, snap_dir)