def install_packages(packages_dir, quiet=False): """ Install packages. Args: packages_dir (str): Directory containing packages. quiet (bool): Hide packages manager output. """ # Get packages packages_dir = _realpath(packages_dir) packages = [ _join(packages_dir, package_file) for package_file in _listdir(packages_dir) if (_splitext(package_file)[-1].lower() in (".deb", ".rpm") and # Skip "-dev"/"-devel" packages "-dev" not in package_file) ] # Run command if quiet: run_kwargs = dict(stdout=_PIPE, stderr=_PIPE, universal_newlines=True) else: run_kwargs = dict() _run(detect_package_manager() + " ".join(packages), shell=True, **run_kwargs).check_returncode()
def getPy2exeBootLogPath(self) -> Optional[str]: if self.whichNVDA == "installed": executablePath = _locations.findInstalledNVDAPath() # py2exe names this log file after the executable, see py2exe/boot_common.py return _splitext(executablePath)[0] + ".log" elif self.whichNVDA == "source": return None # Py2exe not used for source.
def sign_deb_packages(packages_dir, private_key=None, public_key=None, pass_phrase=None, quiet=False): """ Sign all DEB packages in a directory. Args: packages_dir (str): Directory containing packages. private_key (str): Path to GPG private key to use. If no private key specified, use current GPG configuration. public_key (str): Path to GPG public key to use. pass_phrase (str): GPG key pass phrase. quiet (bool): Hide packages manager output. """ _init_gpg_configuration(private_key) # Sign packages packages = [ package for package in _listdir(packages_dir) if _splitext(package)[1].lower() == '.deb' ] command = ['dpkg-sig'] if pass_phrase: command += ['-g', '--passphrase "%s"' % pass_phrase] _run(command + ['--sign', 'builder'] + packages, quiet=quiet, cwd=packages_dir) # Verify signatures _run(['dpkg-sig', '--verify'] + packages, quiet=quiet, cwd=packages_dir)
def valid_file_name_win(name:str, default_char:str="-", dict_char:dict=None, has_ext:bool=False) -> str: """Toma un string y lo transforma en un nombre valido de archivo en windows. (se asume que el nombre dado no incluye la extension de archivo) name: string a procesar default_char: caracter por el cual los caracteres invalidos seran sustituidos dict_char: diccionario de caracteres invalidos a ser reemplazados por algun caracter en particular, si dicho caracter es invalido, se emplea el default_char En caso de que el nombre resulte vacio despues de todo este proceso, se regresara "archivo" como resultado. Si el nombre resultado es un valor reservado, se añadira el default_char al final o "-" si el mismo es nulo """ if has_ext: name, ext = _splitext(name) return valid_file_name_win(name, default_char, dict_char, False) + ext if default_char in invalid_windows_char: default_char = "-" tabla = {ord(c):default_char for c in invalid_windows_char} if dict_char: for k,v in dict_char.items(): tabla[ord(k)]= default_char if v in invalid_windows_char else v name = name.translate(tabla).strip().rstrip(".") if name.upper() in reserved_win_names: name += default_char or "-" return name if name else "archivo"
def _filesystem_change_event_via(change_type, path, listener): if change_type not in ('file_created', 'file_saved'): def lines(): yield f"Ignoring this change type for now: {change_type!r}" listener('info', 'expression', 'ignoring_change_type', lines) return 0, None # discussed in [#409.3], somewhere in the stack there's either a vendor # or an OS weirdness where the above two get conflated sometimes, so this # is the point at which we munge them into one type. # (there are certainly other change types we could act on, like delete, # but those comprise a tiny fraction of our real-life use case FS events) _, ext = _splitext(path) if '.md' == ext: return None, _DocumentCreatedOrSaved(path) if '.eno' == ext: return None, _NotecardCreatedOrSaved(path) def lines(): yield f"expected '.md' or '.eno' had {ext!r}" listener('error', 'expression', 'unexpected_file_extension', lines) return 123, None
def _output_path_via(output_directory, source_directory_entry): # Output path via from os.path import splitext as _splitext base, ext = _splitext(source_directory_entry) assert '.md' == ext tail = ''.join((base, '.html')) return _path_join(output_directory, 'pages', tail) # [#882.B] pages
def get_python_package_entry_point(package, entry_point): """ Find an CLI entry point from a Python package. Args: package (str): Package name. entry_point (str): Entry point name. Returns: str or None: Path to entry point, or None if nothing found. """ site_packages_path = _dirname(_import_module(package).__path__[0]) # Find package info # Can be a directory ending by ".dist-info" or ".egg-info" with _scandir(site_packages_path) as entries: for entry in entries: if (entry.name.startswith(f'{package}-') and _splitext(entry.name)[1] in ('.dist-info', '.egg-info')): package_info_path = entry.path break else: # Package is not installed or do not have package info return None # Find manifest file # Can be a "RECORD" or a "installed-files.txt" file in package info folder for name in ('RECORD', 'installed-files.txt'): manifest_path = _join(package_info_path, name) if _isfile(manifest_path): break else: # Package do not have manifest file return None # Find entry point relative path in manifest file # Possibles manifest file lines formats: "path\n" or "path,checksum\n" with open(manifest_path, 'rt') as manifest: for line in manifest: entry_point_rel_path = line.strip().split(',', 1)[0] if _basename(entry_point_rel_path) == entry_point: break else: # Entry point is not present in manifest return None # Convert to absolute path # Paths in manifest are relative to site-packages or package info for prefix in (site_packages_path, package_info_path): entry_point_path = _realpath(_join(prefix, entry_point_rel_path)) if _isfile(entry_point_path): return entry_point_path
def via_path(path, use_environ, listener): from os.path import sep base, ext = _splitext(path) assert '.py' == ext mname = base.replace(sep, '.') from importlib import import_module config_mod = import_module(mname) cdef = tuple(config_mod.generation_service_config(use_environ, listener)) if 0 == len(cdef): return return generation_config_via_definition(cdef, filesystem=None)
def _save_averaged_experimental_data(self, avg_data): header = str(avg_data.shape[0]) + '\n' + '# markers: ' + \ np.array2string( np.unique(self.exp_data[:, 6]), formatter={'float_kind': lambda x: "%d" % x} )[1:-1] fmt = 2*['%5d'] + ['%7.1f', '%15.6f', '%7d', '%11.5f'] + 2*['%6d'] fname, fext = _splitext(self.exp_file) avg_file = fname + '_averaged' + fext np.savetxt(avg_file, avg_data, header=header, comments='', fmt=fmt)
def __init__(self,input_file,verbose=True,delimiter="\t",name_field="_Name"): self.input_file = input_file self.verbose = True self.delimiter = delimiter file_name, file_extension = _splitext(input_file) self.file_name = file_name self.file_extension = file_extension if(file_extension not in ['.smi','.smiles','.sdf','.mol2']): raise ValueError("Incorrect file extension") self.mols = [] self.molserr = [] self.nb_mols = None self.mols_ids = [] self.name_field = name_field
def run_command(): """Sign packages""" from os import environ from argparse import ArgumentParser parser = ArgumentParser(prog="sign_packages", description="Sign RPM or DEB packages.") parser.add_argument("packages_dir", help="Input packages directory") parser.add_argument("--quiet", "-q", help="Disable verbosity", action="store_true") args = parser.parse_args() private_key = environ.get("GPG_PRIVATE_KEY", "") if not private_key: parser.exit(1, message="No private key\n") return packages_dir = _realpath(args.packages_dir) for file in _listdir(packages_dir): ext = _splitext(file)[1].lower() if ext == ".rpm": sign = sign_rpm_packages break elif ext == ".deb": sign = sign_deb_packages break else: parser.exit(1, "No package to sign\n") return try: sign( packages_dir=packages_dir, private_key=private_key, public_key=environ.get("GPG_PUBLIC_KEY", ""), pass_phrase=environ.get("GPG_PASS_PHRASE", ""), quiet=args.quiet, ) except _CalledProcessError as exception: parser.exit(exception.returncode, exception.stdout) except RuntimeError as exception: parser.exit(1, str(exception)) if not args.quiet: parser.exit(message="Signature successful\n")
def _build_bitmap_data(): ''' Build an SFrame from 10 saved drawings. ''' from os.path import join as _join, realpath as _realpath from os.path import splitext as _splitext, basename as _basename from os.path import dirname as _dirname drawings_dir = _join(_dirname(_realpath(__file__)), "drawings") sf = _tc.image_analysis.load_images(drawings_dir, with_path=True) sf = sf.rename({"image": "drawing", "path": "label"}) sf["label"] = sf["label"].apply( lambda filepath: _splitext(_basename(filepath))[0][:-1] # Extract the class name from the filename, "check1.png" -> "check" # [:-1] is to get "check" out of "check1" ) return sf
def execute(self, filename=None, metafile=False, compressed=True): """ Creates a PDF or Report Manager Meta File filename=name of report file (defaults to report file name+.pdf/.rpmf) metafile=True to produce Report Manager meta-file or false for PDF. compressed=True to produce commpressed file output. returns None on success or ValueError on error. """ if filename is None: filename, ext = _splitext(self._report_filename) if metafile: filename += ".rpmf" else: filename += ".pdf" rp_execute(self._hreport, filename, metafile, compressed)
def run_command(): """Sign packages""" from argparse import ArgumentParser parser = ArgumentParser(prog='sign_packages', description='Sign RPM or DEB packages.') parser.add_argument('packages_dir', help='Input packages directory') parser.add_argument('--private_key', '-k', help='Private GPG key') parser.add_argument('--public_key', '-b', help='Public GPG key') parser.add_argument('--pass_phrase', '-p', help='GPG key pass phrase') parser.add_argument('--quiet', '-q', help='Disable verbosity', action='store_true') args = parser.parse_args() packages_dir = _realpath(args.packages_dir) if args.private_key is not None and not args.private_key: # Passed and empty value: command called from build script but # not private key is defined. In this case, signature is disabled. return for file in _listdir(packages_dir): ext = _splitext(file)[1].lower() if ext == '.rpm': sign = sign_rpm_packages break elif ext == '.deb': sign = sign_deb_packages break else: parser.exit(1, 'No package to sign.') return try: sign(packages_dir=packages_dir, private_key=args.private_key, public_key=args.public_key, pass_phrase=args.pass_phrase, quiet=args.quiet) except _CalledProcessError as exception: parser.exit(exception.returncode, exception.stdout) except RuntimeError as exception: parser.exit(1, str(exception)) if not args.quiet: parser.exit(message='Signature successful')
def __init__(self, input_file, verbose=True, delimiter="\t", name_field="_Name"): self.input_file = input_file self.verbose = True self.delimiter = delimiter file_name, file_extension = _splitext(input_file) self.file_name = file_name self.file_extension = file_extension if (file_extension not in ['.smi', '.smiles', '.sdf', '.mol2']): raise ValueError("Incorrect file extension") self.mols = [] self.molserr = [] self.nb_mols = None self.mols_ids = [] self.name_field = name_field
def _interesting_parts_via_path(path): # Split long path up in to dirname (discarded) and basename from os.path import basename as _basename, splitext as _splitext basename = _basename(path) # Split basename up into basename head and (discarded) extension basename_head, ext = _splitext(basename) assert '.md' == ext # (in some crazy future this might change and that's okay) # Split basename head up into entity identifier and the rest md = _re.match(r'(?:(\d+(?:\.(?:\d+|[A-Z]))*)[.-])?(.+)', basename_head) if not md: xx(f"regex oops: {basename_head!r}") eid, rest = md.groups() # Split the rest up into "title pieces" title_pieces = tuple(md[0] for md in _re.finditer(r'\w+', rest)) return eid, title_pieces
def unique_name(fpath): """ Make a unique name for the filepath by stripping extension, and adding 1, 2... to the end until a unique name is generated. @param fpath: filepath to make unique name for @type fpath: str @return: str @rtype: str """ if not _exists(fpath): return fpath split_path = _splitext(fpath) i = 1 tmplt = "(%d)".join(split_path) new = tmplt % i while _exists(new): i += 1 new = tmplt % i return new
def install_accelize_drm_library(packages_dir, quiet=False): """ Install Accelize DRM library packages. Args: packages_dir (str): Directory containing packages. quiet (bool): Hide packages manager output. """ # Get packages packages_dir = _realpath(packages_dir) packages = [ _join(packages_dir, package_file) for package_file in _listdir(packages_dir) if (_splitext(package_file)[-1].lower() in ('.deb', '.rpm') and '-dev' not in package_file)] # Run command if quiet: run_kwargs = dict(stdout=_PIPE, stderr=_PIPE, universal_newlines=True) else: run_kwargs = dict() _run(detect_package_manager() + ' '.join(packages), shell=True, **run_kwargs).check_returncode()
def splitext(value): return _splitext(value)
def getFullFilename(path, hint=None): """ Function to get full library path. Figure out what's in the path iteratively, based on 3 common scenarios. @param path: a filepath or filename @type path: str @param hint: the first directory tree in which to search for the file @type hint: str @return: full library path to existing file. @rtype: str Try to find the path by checking for three common cases: 1. filename with extension - base - no base 2. filename with only base 2.1 partially qualified directory name 2.2 fully qualified directory name 3. neither one Build list of folders to search by calling first helper function. Update 1/16/2014- xl nonsense gone I moved the algorithm for executing the search to an inlined dispatch function that receives all the relative args, for the sake of making this function cleaner, but I'm not sure if that level of indirection just makes everything even worse. Having it defined within this function allows it to access path, etc variables without having to explicitly call them. In all, there is much less text in the areas in which the dispatch is called. """ path = path.replace('/', '\\') # Normalize sep type # Was path already good? if _exists(path): return path # Begin process of finding file search_dirs = _lib_path_search_dir_list_builder(hint) base, name = _split(path) ext = _splitext(name)[1] # Most likely- given extension. # no need to check for case of fully qualified basename. # an existing file with a fully qualified base name and extension would # be caught by earlier _exists() if ext: if base: v_print('\nPartially qualified filename \'', path, "\' given, searching for file...") return _get_lib_path_parital_qualname(name, base, search_dirs) # else v_print("\nNo directory given for \'", path, "\', scanning for file...") return _get_lib_path_no_basename(path, search_dirs) # Next, given filename with base, but no extension elif base: drive, _tail = _splitdrive(base) # fully qualified base, just check the dir for matching name if drive: v_print("\nNo file extension given for \'", path, "\', scanning for file...") return _get_lib_path_no_extension(path) # partially qualified base, search dirs. I don't think this works well (at all). # I don't think I managed to get a working unittest for it. else: v_print("\nAttempting to find partially qualified name \'", path, "\' ...") return _get_lib_path_parital_qualname(name, base, search_dirs) # Finally, user gave no context- no base or extension. # Try really hard to find it anyway. else: v_print("\nNo context given for filename, scanning for file.\nIf you give a full filepath, you wouldn't \nhave to wait for the long search.") return _get_lib_path_no_ctxt(path, search_dirs) # noinspection PyUnreachableCode raise SystemExit("Unreachable code reached: fix module olutils")
def sign_rpm_packages(packages_dir, private_key=None, public_key=None, pass_phrase=None, quiet=False): """ Sign all RPM packages in a directory. Args: packages_dir (str): Directory containing packages. private_key (str): Path to GPG private key to use. If no private key specified, use current GPG configuration. public_key (str): Path to GPG public key to use. pass_phrase (str): GPG key pass phrase. quiet (bool): Hide commands output. """ _init_gpg_configuration(private_key) # Import public key if public_key: _run(['rpm', '--import', public_key], quiet=quiet) # Sign packages packages = [ package for package in _listdir(packages_dir) if _splitext(package)[1].lower() == '.rpm' ] gpg_info = _run('gpg --export | gpg --list-packets', shell=True, quiet=True).stdout for line in gpg_info.strip().splitlines(): if ':user ID packet' in line: gpg_user_id = line.rsplit(':', 1)[1].strip().strip('"') break else: raise RuntimeError('Unable to read GPG User ID') macros = [ '_signature gpg', '_gpg_path %s' % _expanduser("~/.gnupg"), '_gpg_name %s' % gpg_user_id ] if pass_phrase: macros += [ '_gpgbin /usr/bin/gpg', ' '.join( ('__gpg_sign_cmd %{__gpg}', 'gpg', '--force-v3-sigs', '--batch', '--verbose', '--no-armor', '--passphrase "%s"' % pass_phrase, '--no-secmem-warning', '-u', '"%{_gpg_name}"', '-sbo', '%{__signature_filename}', '--digest-algo', 'sha256', '%{__plaintext_filename}')) ] define = [] for macro in macros: define.extend(["--define", macro]) _run(['rpm', '--addsign'] + define + packages, quiet=True, cwd=packages_dir) # Verify signatures result = _run(['rpm', '--checksig'] + packages, quiet=True, cwd=packages_dir) for line in result.stdout.splitlines(): line = line.rstrip() if (not line.endswith('gpg OK') and not line.endswith('pgp md5 OK') and not line.endswith('digests signatures OK')): raise RuntimeError('Package signature verification failure: %s' % line)
def sign_rpm_packages(packages_dir, private_key=None, public_key=None, pass_phrase=None, quiet=False): """ Sign all RPM packages in a directory. Args: packages_dir (str): Directory containing packages. private_key (str): Path to GPG private key to use. If no private key specified, use current GPG configuration. public_key (str): Path to GPG public key to use. pass_phrase (str): GPG key pass phrase. quiet (bool): Hide commands output. """ _init_gpg_configuration(private_key) # Import public key if public_key: _run(["rpm", "--import", public_key], quiet=quiet) # Sign packages packages = [ package for package in _listdir(packages_dir) if _splitext(package)[1].lower() == ".rpm" ] gpg_info = _run("gpg --export | gpg --list-packets", shell=True, quiet=True).stdout for line in gpg_info.strip().splitlines(): if ":user ID packet" in line: gpg_user_id = line.rsplit(":", 1)[1].strip().strip('"') break else: raise RuntimeError("Unable to read GPG User ID") macros = [ "_signature gpg", "_gpg_path %s" % _expanduser("~/.gnupg"), "_gpg_name %s" % gpg_user_id, ] if pass_phrase: macros += [ "_gpgbin /usr/bin/gpg", " ".join(( "__gpg_sign_cmd %{__gpg}", "gpg", "--force-v3-sigs", "--batch", "--verbose", "--no-armor", '--passphrase "%s"' % pass_phrase, "--no-secmem-warning", "-u", '"%{_gpg_name}"', "-sbo", "%{__signature_filename}", "--digest-algo", "sha256", "%{__plaintext_filename}", )), ] define = [] for macro in macros: define.extend(["--define", macro]) _run(["rpm", "--addsign"] + define + packages, quiet=True, cwd=packages_dir) # Verify signatures result = _run(["rpm", "--checksig"] + packages, quiet=True, cwd=packages_dir) for line in result.stdout.splitlines(): line = line.rstrip() if (not line.endswith("gpg OK") and not line.endswith("pgp md5 OK") and not line.endswith("digests signatures OK")): raise RuntimeError("Package signature verification failure: %s" % line) if not quiet: print("Signed packages:\n - %s" % "\n- ".join(packages))
def xlBook2(filepath=None, new=False, visible=True, search=False, xl=None): """Get win32com workbook object from filepath. If workbook is open, get active object. If workbook is not open, create a new instance of xl and open the workbook in that instance. If filename is not found, see if user specified default filename error behavior as returning new workbook. If not, raise error. If so, return new workbook Warning: returns error in some circumstances if dialogs or certain areas like formula bar in the desired Excel instance have focus. @param filepath: valid filepath @type filepath: str | None @param visible: xl instance visible to user? turn off to do heavy processing before showing @type visible: bool @param new: open in a new window @type new: bool @return: the newly opened xl workbook instance @rtype: (typehint.ExcelApplication, typehint.ExcelWorkbook) Update 1/15/2014- Lots of refactoring to make it really clean and such. Or so I tried. Update 1/29/2014- this function is now converted to abstract internal function. Interfaced moved to new function with same name. This function still contains logic. Update 1/31/2014- renamed function xlBook2, now public. """ if xl is None: xl = Excel(new, visible) if not filepath: wb = __ensure_wb(xl) return xl, wb _base, name = _split(filepath) no_ext_name, ext = _splitext(name) # First try to see if passed name of open workbook # xl can be a pain, so try with and without ext. wbs = xl.Workbooks possible_names = ( filepath.lstrip("\\/"), no_ext_name, name ) if wbs.Count: for fname in possible_names: try: wb = wbs(fname) except: continue else: v_print("\'%s\' found, returning existing workbook." % filepath) wb.Activate() return xl, wb # Workbook wasn't open, get filepath and open it. # This may take a *long* time. try: if search: v_print("Searching for file...") filepath = getFullFilename(filepath, hint=xl.DefaultFilePath) except FileNotFoundError as e: # cleanup if filepath wasn't found. if new: xl.Quit() else: xl.Visible = True raise xlLibError("Couldn't find path '%s', check that it is correct." % filepath) from e try: wb = wbs.Open(filepath, Notify=False) except: if new: xl.Quit() else: xl.Visible = True else: v_print("Filename \'%s\' found.\nReturning newly opened workbook." % filepath) wb.Activate() return xl, wb raise xlLibError("Unknown error occurred. \nCheck filename: %s " "If the target file is open, ensure\nno dialogs are open." % filepath)
def is_valid_file_name_win(name:str, has_ext:bool=False) -> bool: """Dice si es un nombre valido en windows (se asume que el nombre dado no incluye la extension de archivo)""" return not ( any( c in invalid_windows_char for c in name ) or ( ( _splitext(name)[0] if has_ext else name ).upper() in reserved_win_names) )
from datetime import date as _date from json import load as _load, loads as _loads from os import listdir as _listdir from os.path import dirname as _dirname, join as _join, splitext as _splitext from pybars import Compiler as _Compiler from ssl_config._helpers import HELPERS as _HELPERS from ssl_config._versions import Version as _Version _DATA_DIR = _join(_dirname(__file__), '_data') #: Supported server software SERVERS = tuple( sorted( _splitext(name)[0] for name in _listdir(_join(_DATA_DIR, 'templates')))) with open(_join(_DATA_DIR, 'guidelines.json'), 'rt') as json_file: #: Guidelines information as dict GUIDELINES = _load(json_file) #: Mozilla SSL configuration levels #: #: Modern: #: Services with clients that support TLS 1.3 and don't need #: backward compatibility. #: #: Intermediate #: General-purpose servers with a variety of clients, recommended for #: almost all systems.
def parse_average_locker(measurepath, obsA, opa ): obsA = obsA.replace('HAMILTONIAN','OPERATOR').split('OPERATOR/')[-1].replace('/t_','_')+'_'+opa return _splitext(measurepath)[0]+'-locker/'+obsA
def parse_correlation_locker(measurepath, obsA, opa, obsB, opb ): obsA = obsA.replace('HAMILTONIAN','OPERATOR').split('OPERATOR/')[-1].replace('/t_','_')+'_'+opa obsB = obsB.replace('HAMILTONIAN','OPERATOR').split('OPERATOR/')[-1].replace('/t_','_')+'_'+opb return _splitext(measurepath)[0]+'-locker/'+obsA+'--'+obsB