def resolve_path(self, relative_path: str, lax: bool = False) -> Path: """ Resolves paths in the key material store, checking for lax permissions. :param relative_path: Relative path inside the key material store :param lax: Check for lax permissions if `False` :return: Resolved path """ path = PosixPath(self._store_path) / relative_path path.resolve(strict=True) if not lax and stat(path).st_mode & 0o177: raise Exception( f'Key material "{relative_path}" has lax permissions.') return path
def read_file_text(filename: Path): """return all text contents in line-based array of file or 'None' if not a text file""" filename = Path(filename) current_app.logger.info('Reading text from file: %s', filename.resolve()) text_lines = [] try: filename.open() text_lines = filename.read_text().splitlines() current_app.logger.debug('from %s text_lines[0:1]: %s', filename.name, str(text_lines[0:1])) return text_lines except Exception as e: current_app.logger.error('Errors reading from file: %s!', filename.resolve()) return None
def find_storage(self, model, argv=None): if argv is None: argv = sys.argv self.find_default_filename(model) configfile = self.default_filename if self.environ_var and self.environ_var in os.environ: configfile = os.environ[self.environ_var] log.debug("%s defined: %s", self.environ_var, configfile) for i, _ in enumerate(argv): good = [] if self.short_param: good.append(self.short_param) if self.long_param: good.append(self.long_param) if argv[i] in good: if i == len(argv): raise Exception("No value given to {}".format( " or ".join(good))) configfile = argv[i + 1] log.debug("%s defined: %s", " or ".join(good), configfile) break if not configfile: raise ValueError( "Cannot find which configuration file name to search") config_file_path = PosixPath(configfile) log.debug("Configuration file set to: %s", configfile) self.__resolved_config_file = config_file_path.resolve().as_posix() self.__load_bare_config()
def commit_file(filename: str, text: str, trailers: Optional[str] = None) -> str: """ Commits the contents to the given filename and returns the commit hash. """ update_path = PosixPath(filename) if len(update_path.parent.name) > 0: (update_path.parent).mkdir(parents=True, exist_ok=True) update_path.resolve().write_text(text) git('add', filename) msg = f'Updated {filename}' if 'internal' in text: msg = f'[internal] {msg}' if trailers is not None: msg = f'{msg}\n{trailers}' git('commit', '-m', msg) head_commit = git_output('rev-parse', 'HEAD') return head_commit
def test_allow_extra_fields(tmp_path: PosixPath): """Makes sure emmet config can be subclassed without loading issues""" with open(tmp_path / "temp_config.json", "w") as f: json.dump({"sub_class_prop": True}, f) os.environ["EMMET_CONFIG_FILE"] = str(tmp_path.resolve() / "temp_config.json") EmmetSettings()
def test_default_config_path(tmp_path: PosixPath): """Make sure the default config path works""" rand_symprec = random() with open(tmp_path / "temp_config.json", "w") as f: json.dump({"SYMPREC": rand_symprec}, f) os.environ["EMMET_CONFIG_FILE"] = str(tmp_path.resolve() / "temp_config.json") test_config = EmmetSettings() assert test_config.SYMPREC == rand_symprec
def is_a_subfolder(self, path_to_check: PosixPath, parent_folder: PosixPath): """ check if a given path is in parent folder parameters: path_to_check: posixpath, a path to match with base folder parent_folder: posixpath, the base aka parent folder return: boolean """ path = path_to_check.resolve() base = parent_folder.resolve() if path == base: return True if base.stem in path.parts: return True else: return False
def matToWav(filename: pathlib.PosixPath): # convert a MAT-files created by TUCT and containing audiodata to WAV data # resolve path to absolute filename = filename.resolve() mat = scipy.io.loadmat(filename) baseNameMatch = re.search(r".*_A(.*)\.(?=MAT|mat)", filename.as_posix()) baseName = baseNameMatch.group(1) # without extension audioName = re.sub(r"(.*)(?:\.MAT|\.mat)$", r"\1.wav", filename.as_posix()) print(f"input file: {filename!s}") print(f"output file: {audioName!s}") fs = int(mat["TUCT_fs"]) if re.search("_OMNI", baseName): # OMNI suffix = [""] elif re.search("_BIN", baseName): # BINAURAL suffix = ["L", "R"] suffix = ["_" + suff for suff in suffix] elif re.search("_BF", baseName): # B-FORMAT nbrChannels = len(mat) - 1 maxOrder = sqrt(nbrChannel) - 1 print(f"order {maxOrder}") if maxOrder > 0: suffix = ["W", "Y", "Z", "X"] # ACN (order matters !) elif maxOrder > 1: suffix.extend(["V", "T", "R", "S", "U"]) # ACN (order matters !) elif maxOrder == 3: suffix.extend(["Q", "O", "M", "K", "L", "N", "P"]) # ACN (order matters !) else: raise ("wrong number of channels") suffix = ["_" + suff for suff in suffix] varNames = ["h_A" + baseName + suff for suff in suffix] y = np.hstack([np.array(mat[varName][0]) for varName in varNames]) mat.clear() scipy.io.wavfile.write(audioName, fs, y)
def unzip(url:str, dest:PosixPath, chunk_size:int=1024*1024, remove_zip: bool=False): """ Downloads and unzips a zip file parameters: url: str, uri to zip file dest: PosixPath, destination folder chunk_size: int, default 1 MB remove_zip: bool, default False, unlinks zip file after unzip operation returns: tqdm progress bar and typer echo messages """ stream = requests.get(url, stream=True, verify=False, allow_redirects=True) filename = stream.url.split(sep="/")[-1] length = int(stream.headers.get("content-length", -1)) if length < 1: raise Exception(f"content length is less than 1 bytes") if not dest.exists(): raise Exception(f"destination folder does not exist: {dest}") if dest.is_file(): dest = dest.parent dest = dest.resolve() typer.echo("Downloading zip file...") with tqdm.wrapattr( open(dest.joinpath(filename), "wb"), "write", unit='B', unit_scale=True, unit_divisor=1024, miniters=1, desc=filename, total=length) as f: for chunk in stream.iter_content(chunk_size=chunk_size): if chunk: f.write(chunk) f.flush() typer.echo("Extracting zip file...") with zipfile.ZipFile(dest.joinpath(filename)) as zippo: for member in tqdm(zippo.infolist(), desc="Extracting zip file..."): zippo.extract(member, dest) if remove_zip: dest.joinpath(filename).unlink() typer.secho(f"{filename} is removed.", bold=True, fg="red") else: typer.secho(f"{filename} is unzipped in {dest}.", bold=True, fg="green")
def apksign(p_apk: PosixPath, key_path: str, key_alias: str, key_pass: str, ks_pass: str) -> PosixPath: try: signed_apk_name = p_apk.name.replace("-zipaligned.apk", "-signed.apk") psigned_apk = p_apk.parent.joinpath(signed_apk_name) key_cmd = [ '--ks', key_path, '--ks-key-alias', key_alias, '--ks-pass', 'pass:{}'.format(ks_pass), '--key-pass', 'pass:{}'.format(key_pass) ] cmd = [config.apksigner, 'sign'] + key_cmd + [ '--out', str(psigned_apk.resolve()), str(p_apk.resolve()) ] r = check_output(cmd) except Exception as e: print("apk signing error: " + str(e)) return False return psigned_apk
def find_config_storage(self): configfile = self.json_configstorage_default_filename if self.json_configstorage_environ_var_name in os.environ: configfile = os.environ[self.json_configstorage_environ_var_name] log.debug("%s defined: %s", self.json_configstorage_environ_var_name, configfile) for i in range(len(sys.argv)): good = [] if self.json_configstorage_short_param_name: good.append(self.json_configstorage_short_param_name) if self.json_configstorage_long_param_name: good.append(self.json_configstorage_long_param_name) if sys.argv[i] in good: if i == len(sys.argv): raise Exception("No value given to {}".format(" or ".join(good))) configfile = sys.argv[i + 1] log.debug("%s defined: %s", " or ".join(good), configfile) break config_file_path = PosixPath(configfile) log.debug("Configuration file set to: %s", configfile) self.__resolved_config_file = config_file_path.resolve().as_posix() self._load_bare_config()
def zipalign(p_apk: PosixPath) -> PosixPath: try: zipaligned_apk_name = p_apk.name.replace("-unsigned.apk", "") if zipaligned_apk_name.endswith(".apk"): zipaligned_apk_name = zipaligned_apk_name[:-4] zipaligned_apk_name = zipaligned_apk_name + "-zipaligned.apk" p_zipaligned_apk = p_apk.parent.joinpath(zipaligned_apk_name) if p_zipaligned_apk.exists(): p_zipaligned_apk.unlink() cmd = [ config.zipalign, '-v', '-p', '4', str(p_apk.resolve()), str(p_zipaligned_apk.resolve()) ] r = check_output(cmd) except Exception as e: print("apk zipalign error: " + str(e)) return False return p_zipaligned_apk