Python os.path.basename() Examples
The following are 30
code examples of os.path.basename().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os.path
, or try the search function
.
Example #1
Source File: file_watcher.py From incubator-spot with Apache License 2.0 | 6 votes |
def stop(self): ''' Signals the current thread to stop and waits until it terminates. This blocks the calling thread until it terminates -- either normally or through an unhandled exception. :raises RuntimeError: If an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attemps to do so raises the same exception. ''' self._logger.info('Signal {0} thread to stop normally.'.format(str(self))) super(FileWatcher, self).stop() self._logger.info('Wait until the {0} thread terminates...'.format(str(self))) super(FileWatcher, self).join() while not self.is_empty: self._logger.debug('Drop "%s" from the queue.' % basename(self._queue.pop())) assert self.is_empty, 'Failed to clean the queue.'
Example #2
Source File: test_toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_toolchain_standard_build_dir_remapped(self): """ This can either be caused by relative paths or symlinks. Will result in the manually specified build_dir being remapped to its real location """ fake = mkdtemp(self) real = mkdtemp(self) real_base = basename(real) spec = Spec() spec['build_dir'] = join(fake, pardir, real_base) with pretty_logging(stream=StringIO()) as s: with self.assertRaises(NotImplementedError): self.toolchain(spec) self.assertIn("realpath of 'build_dir' resolved to", s.getvalue()) self.assertEqual(spec['build_dir'], real)
Example #3
Source File: test_toolchain.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_transpiler_sourcemap(self): # a kind of silly test but shows concept build_dir = mkdtemp(self) srcdir = mkdtemp(self) js_code = 'var dummy = function() {\n};\n' source = join(srcdir, 'source.js') target = 'target.js' with open(source, 'w') as fd: fd.write(js_code) spec = Spec(build_dir=build_dir, generate_source_map=True) modname = 'dummy' self.toolchain.transpile_modname_source_target( spec, modname, source, target) with open(join(build_dir, target + '.map')) as fd: result = json.load(fd) self.assertEqual(result['mappings'], 'AAAA;AACA;') self.assertEqual(len(result['sources']), 1) self.assertEqual(basename(result['sources'][0]), 'source.js') self.assertEqual(result['file'], target)
Example #4
Source File: artifact.py From calmjs with GNU General Public License v2.0 | 6 votes |
def generate_metadata_entry(self, entry_point, toolchain, spec): """ After the toolchain and spec have been executed, this may be called to generate the artifact export entry for persistence into the metadata file. """ export_target = spec['export_target'] toolchain_bases = trace_toolchain(toolchain) toolchain_bin_path = spec.get(TOOLCHAIN_BIN_PATH) toolchain_bin = ([ basename(toolchain_bin_path), # bin_name get_bin_version_str(toolchain_bin_path), # bin_version ] if toolchain_bin_path else []) return {basename(export_target): { 'toolchain_bases': toolchain_bases, 'toolchain_bin': toolchain_bin, 'builder': '%s:%s' % ( entry_point.module_name, '.'.join(entry_point.attrs)), }}
Example #5
Source File: build.py From alibuild with GNU General Public License v3.0 | 6 votes |
def syncToLocal(self, p, spec): debug("Updating remote store for package %s@%s" % (p, spec["hash"])) cmd = format( "mkdir -p %(tarballHashDir)s\n" "s3cmd sync -s -v --host s3.cern.ch --host-bucket %(b)s.s3.cern.ch s3://%(b)s/%(storePath)s/ %(tarballHashDir)s/ 2>/dev/null || true\n" "for x in `s3cmd ls -s --host s3.cern.ch --host-bucket %(b)s.s3.cern.ch s3://%(b)s/%(linksPath)s/ 2>/dev/null | sed -e 's|.*s3://|s3://|'`; do" " mkdir -p '%(tarballLinkDir)s'; find '%(tarballLinkDir)s' -type l -delete;" " ln -sf `s3cmd get -s --host s3.cern.ch --host-bucket %(b)s.s3.cern.ch $x - 2>/dev/null` %(tarballLinkDir)s/`basename $x` || true\n" "done", b=self.remoteStore, storePath=spec["storePath"], linksPath=spec["linksPath"], tarballHashDir=spec["tarballHashDir"], tarballLinkDir=spec["tarballLinkDir"]) err = execute(cmd) dieOnError(err, "Unable to update from specified store.")
Example #6
Source File: gnupg.py From Authenticator with GNU General Public License v2.0 | 6 votes |
def __on_apply(self, *__): from ...models import BackupJSON try: paraphrase = self.paraphrase_widget.entry.get_text() if not paraphrase: paraphrase = " " output_file = path.join(GLib.get_user_cache_dir(), path.basename(NamedTemporaryFile().name)) status = GPG.get_default().decrypt_json(self._filename, paraphrase, output_file) if status.ok: BackupJSON.import_file(output_file) self.destroy() else: self.__send_notification(_("There was an error during the import of the encrypted file.")) except AttributeError: Logger.error("[GPG] Invalid JSON file.")
Example #7
Source File: app.py From selfmailbot with MIT License | 6 votes |
def send_photo(bot, update: Update, user: User, render): file = update.message.photo[-1].get_file() photo = download(file) subject = 'Photo note to self' text = '' if update.message.caption is not None: text = update.message.caption.strip() if text: subject = 'Photo: {}'.format(get_subject(text)) update.message.reply_text(text=render('photo_is_sent')) tasks.send_file.delay( user_id=user.pk, file=photo, filename=basename(file.file_path), subject=subject, text=text, )
Example #8
Source File: mx_ide_eclipse.py From mx with GNU General Public License v2.0 | 6 votes |
def _get_eclipse_output_path(p, linkedResources=None): """ Gets the Eclipse path attribute value for the output of project `p`. """ outputDirRel = p.output_dir(relative=True) if outputDirRel.startswith('..'): outputDirName = basename(outputDirRel) if linkedResources is not None: linkedResources.append(_eclipse_linked_resource(outputDirName, '2', p.output_dir())) return outputDirName else: return outputDirRel #: Highest Execution Environment defined by most recent Eclipse release. #: https://wiki.eclipse.org/Execution_Environments #: https://git.eclipse.org/c/jdt/eclipse.jdt.debug.git/plain/org.eclipse.jdt.launching/plugin.properties
Example #9
Source File: GitRelease.py From gist-alfred with MIT License | 6 votes |
def upload_asset(self, path, label="", content_type=""): """ :calls: `POST https://<upload_url>/repos/:owner/:repo/releases/:release_id/assets?name=foo.zip <https://developer.github.com/v3/repos/releases/#upload-a-release-asset>`_ :rtype: :class:`github.GitReleaseAsset.GitReleaseAsset` """ assert isinstance(path, (str, unicode)), path assert isinstance(label, (str, unicode)), label post_parameters = { "name": basename(path), "label": label } headers = {} if len(content_type) > 0: headers["Content-Type"] = content_type resp_headers, data = self._requester.requestBlobAndCheck( "POST", self.upload_url.split("{?")[0], parameters=post_parameters, headers=headers, input=path ) return github.GitReleaseAsset.GitReleaseAsset(self._requester, resp_headers, data, completed=True)
Example #10
Source File: purge_checkpoints.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
def purge_checkpoints(log_dir_root, target_dir, verbose): vprint = print if verbose else no_op.NoOp ckpt_dir_glob = Saver.ckpt_dir_for_log_dir(path.join(log_dir_root, '*')) ckpt_dir_matches = sorted(glob.glob(ckpt_dir_glob)) for ckpt_dir in ckpt_dir_matches: log_dir = Saver.log_dir_from_ckpt_dir(ckpt_dir) all_ckpts = Saver.all_ckpts_with_iterations(ckpt_dir) if len(all_ckpts) <= 5: vprint('Skipping {}'.format(log_dir)) continue target_log_dir = path.join(target_dir, path.basename(log_dir)) target_ckpt_dir = Saver.ckpt_dir_for_log_dir(target_log_dir) os.makedirs(target_ckpt_dir, exist_ok=True) ckpts_to_keep = {all_ckpts[2], all_ckpts[len(all_ckpts) // 2], all_ckpts[-1]} ckpts_to_move = set(all_ckpts) - ckpts_to_keep vprint('Moving to {}:'.format(target_ckpt_dir)) for _, ckpt_to_move in ckpts_to_move: # ckpt_to_move is /path/to/dir/ckpt-7000, add a * to match ckpt-7000.data, .meta, .index for ckpt_file in glob.glob(ckpt_to_move + '*'): vprint('- {}'.format(ckpt_file)) shutil.move(ckpt_file, target_ckpt_dir)
Example #11
Source File: base.py From MaskTrack with MIT License | 6 votes |
def __init__(self,path,regex,load_func=None): super(BaseLoader, self).__init__( osp.join(path + '/' + regex),load_func=load_func) # Sequence name self.name = osp.basename(path) # Check sequence name if not self.name in cfg.SEQUENCES: raise Exception("Sequence name \'{}\' not found.".format(self.name)) # Check sequence length if len(self) != cfg.SEQUENCES[self.name].num_frames: raise Exception("Incorrect frames number for sequence" + " \'{}\': found {}, expected {}.".format( self.name,len(self),cfg.SEQUENCES[self.name].num_frames))
Example #12
Source File: mx_unittest.py From mx with GNU General Public License v2.0 | 6 votes |
def _write_cached_testclasses(cachesDir, jar, jdk, testclasses, excludedclasses): """ Writes `testclasses` to a cache file specific to `jar`. :param str cachesDir: directory containing files with cached test lists :param JDKConfig jdk: the JDK for which the cached list of classes must be written :param list testclasses: a list of test class names :param list excludedclasses: a list of excluded class names """ jdkVersion = '.jdk' + str(jdk.javaCompliance) cache = join(cachesDir, basename(jar) + jdkVersion + '.testclasses') exclusions = join(cachesDir, basename(jar) + jdkVersion + '.excludedclasses') try: with open(cache, 'w') as fp: for classname in testclasses: print(classname, file=fp) with open(exclusions, 'w') as fp: if excludedclasses: mx.warn('Unsupported class files listed in ' + exclusions) for classname in excludedclasses: print(classname[1:], file=fp) except IOError as e: mx.warn('Error writing to ' + cache + ': ' + str(e))
Example #13
Source File: get_recipes.py From recipe-box with MIT License | 6 votes |
def get_fn_recipe_links(): letter_links = get_fn_letter_links() recipe_links = {} page_tracker = 0 for page in letter_links: recipe_set = True page_num = 1 lag0 = 0 while recipe_set: t0 = time.time() recipe_set = get_all_recipes_fn(path.basename(page), page_num) lag1 = time.time() - t0 recipe_links[page_tracker] = [] recipe_links[page_tracker].extend(recipe_set) page_num += 1 page_tracker += 1 time.sleep(lag1 * .5 + lag0 * .5) lag0 = lag1 return recipe_links
Example #14
Source File: logdir_helpers.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
def config_paths_from_log_dir(log_dir, base_dirs): log_dir = path.basename(log_dir.strip(path.sep)) # log_dir == {now} {netconfig} {probconfig} [RESTORE@some_dir@XXXX_YYYY], get [netconfig, probconfig] comps = log_dir.split(' ') assert is_log_date(comps[0]), 'Invalid log_dir: {}'.format(log_dir) comps = [c for c in comps[1:] if _RESTORE_PREFIX not in c] assert len(comps) <= len(base_dirs), 'Expected as many config components as base dirs: {}, {}'.format( comps, base_dirs) def get_real_path(base, prepped_p): p_glob = prepped_p.replace('@', path.sep) p_glob = path.join(base, p_glob) # e.g., ae_configs/p_glob glob_matches = glob.glob(p_glob) # We always only replace one character with *, so filter for those. # I.e. lr1e-5 will become lr1e*5, which will match lr1e-5 but also lr1e-4.5 glob_matches_of_same_len = [g for g in glob_matches if len(g) == len(p_glob)] if len(glob_matches_of_same_len) != 1: raise ValueError('Cannot find config on disk: {} (matches: {})'.format(p_glob, glob_matches_of_same_len)) return glob_matches_of_same_len[0] return tuple(get_real_path(base_dir, comp) for base_dir, comp in zip(base_dirs, comps))
Example #15
Source File: file_watcher.py From incubator-spot with Apache License 2.0 | 6 votes |
def detect(self, newfile): ''' Called when a new file is generated under the monitoring directory. :param newfile: Path to file created recently. ''' self._logger.info(' -------- New File Detected! -------- ') filename = basename(newfile) # .............................check whether the filename is in the supported list if any([x.search(filename) for x in self._regexs]) or not self._regexs: self._queue.insert(0, newfile) self._logger.info('File "{0}" added to the queue.'.format(newfile)) return self._logger.warning('Filename "%s" is not supported! Skip file...' % filename)
Example #16
Source File: fileLoaders.py From grlc with MIT License | 6 votes |
def __init__(self, spec_url): """Create a new URLLoader. Keyword arguments: spec_url -- URL where the specification YAML file is located.""" headers = {'Accept' : 'text/yaml'} resp = requests.get(spec_url, headers=headers) if resp.status_code == 200: self.spec = yaml.load(resp.text) self.spec['url'] = spec_url self.spec['files'] = {} for queryUrl in self.spec['queries']: queryNameExt = path.basename(queryUrl) queryName = path.splitext(queryNameExt)[0] # Remove extention item = { 'name': queryName, 'download_url': queryUrl } self.spec['files'][queryNameExt] = item del self.spec['queries'] else: raise Exception(resp.text)
Example #17
Source File: dump_ocsp_response.py From snowflake-connector-python with Apache License 2.0 | 6 votes |
def main(): """Internal Tool: OCSP response dumper.""" def help(): print( "Dump OCSP Response for the URL. ") print(""" Usage: {} <url> [<url> ...] """.format(path.basename(sys.argv[0]))) sys.exit(2) import sys if len(sys.argv) < 2: help() urls = sys.argv[1:] dump_ocsp_response(urls, output_filename=None)
Example #18
Source File: LitTestCommand.py From llvm-zorg with Apache License 2.0 | 5 votes |
def testInfoFinished(self): # We have finished getting information for one test, handle it. if self.lastTestResult: code, name = self.lastTestResult # If the test failed, add a log entry for it (unless we have reached the # max). if code in self.failingCodes and (self.maxLogs is None or self.numLogs < self.maxLogs): # If a verbose log was not provided, just add a one line description. if self.activeVerboseLog is None: self.activeVerboseLog = ['%s: %s' % (code, name)] # Add the log to the build status. # Make the test name short, the qualified test name is in the log anyway. # Otherwise, we run out of the allowed name length on some hosts. name_part = name.rpartition('::') self.step.addCompleteLog( code + ': ' + name_part[0].strip() + name_part[1] + basename(name_part[2]), '\n'.join(self.activeVerboseLog)) self.numLogs += 1 else: if self.activeVerboseLog: self.activeVerboseLog.append( "error: missing test status line, skipping log") # Reset the current state. self.lastTestResult = None self.activeVerboseLog = None
Example #19
Source File: saver.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def is_ckpt_dir(p): return path.basename(p) == _CKPT_DIR_NAME
Example #20
Source File: gftools-fix-cmap.py From gftools with Apache License 2.0 | 5 votes |
def main(): parser = ArgumentParser(description=description) parser.add_argument('fonts', nargs='+') parser.add_argument('--format-4-subtables', '-f4', default=False, action='store_true', help="Convert cmap subtables to format 4") parser.add_argument('--drop-mac-subtable', '-dm', default=False, action='store_true', help='Drop Mac cmap subtables') parser.add_argument('--keep-only-pid-0', '-k0', default=False, action='store_true', help=('Keep only cmap subtables with pid=0' ' and drop the rest.')) args = parser.parse_args() for path in args.fonts: font = TTFont(path) font_filename = basename(path) fixit = False if args.format_4_subtables: print('\nConverting Cmap subtables to format 4...') fixit = convert_cmap_subtables_to_v4(font) if args.keep_only_pid_0: print('\nDropping all Cmap subtables,' ' except the ones with PlatformId = 0...') dropped = keep_only_specific_cmap(font, 0) fixit = fixit or dropped elif args.drop_mac_subtable: print('\nDropping any Cmap Mac subtable...') dropped = remove_cmap_subtable(font, 1, 0) fixit = fixit or dropped if fixit: print('\n\nSaving %s to %s.fix' % (font_filename, path)) font.save(path + '.fix') else: print('\n\nThere were no changes needed on the font file!')
Example #21
Source File: conf_manager.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def conf_file2name(conf_file): conf_name = op.basename(conf_file) if conf_name.endswith(".conf"): conf_name = conf_name[:-5] return conf_name
Example #22
Source File: loader.py From Automatic-Identification-and-Counting-of-Blood-Cells with GNU General Public License v3.0 | 5 votes |
def model_name(file_path): file_name = basename(file_path) ext = str() if '.' in file_name: # exclude extension file_name = file_name.split('.') ext = file_name[-1] file_name = '.'.join(file_name[:-1]) if ext == str() or ext == 'meta': # ckpt file file_name = file_name.split('-') num = int(file_name[-1]) return '-'.join(file_name[:-1]) if ext == 'weights': return file_name
Example #23
Source File: dired.py From dired with MIT License | 5 votes |
def run(self, edit): parent = dirname(self.path.rstrip(os.sep)) + os.sep if parent == self.path: return view_id = (self.view.id() if reuse_view() else None) show(self.view.window(), parent, view_id, goto=basename(self.path.rstrip(os.sep)))
Example #24
Source File: saver.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def get_itr_from_ckpt_p(self, ckpt_p): file_name = os.path.splitext(os.path.basename(ckpt_p))[0] assert self.ckpt_prefix in file_name itr_part = file_name.replace(self.ckpt_prefix, '') itr_part_digits_only = int(''.join(c for c in itr_part if c.isdigit())) return itr_part_digits_only
Example #25
Source File: framework.py From Automatic-Identification-and-Counting-of-Blood-Cells with GNU General Public License v3.0 | 5 votes |
def __init__(self, meta, FLAGS): model = basename(meta['model']) model = '.'.join(model.split('.')[:-1]) meta['name'] = model self.constructor(meta, FLAGS)
Example #26
Source File: logdir_helpers.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def log_date_from_log_dir(log_dir): # extract {log_date} from LOG_DIR/{log_date} {netconfig} {probconfig} possible_log_date = os.path.basename(log_dir).split(' ')[0] if not is_log_date(possible_log_date): raise ValueError('Invalid log dir: {}'.format(log_dir)) return possible_log_date
Example #27
Source File: images_iterator.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def iter_imgs(self, pad): """ :param pad: :yield: (img_name, padded img_content, shape = CHW) """ if self.cached_image_loader is None or self.cached_image_loader.pad != pad: print('Creating CachedImageLoader...') self.cached_image_loader = ImagesIterator.CachedImageLoader(self.images_paths, pad, self.cache_max_mb) return zip(map(path.basename, self.images_paths), self.cached_image_loader)
Example #28
Source File: val_files.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def job_id_from_out_dir(out_dir): base = path.basename(out_dir) # should be {log_date} {dataset_name} return logdir_helpers.log_date_from_log_dir(base) # may raise ValueError
Example #29
Source File: logdir_helpers.py From L3C-PyTorch with GNU General Public License v3.0 | 5 votes |
def parse_log_dir(log_dir, configs_dir, base_dirs, append_ext=''): """ Given a log_dir produced by `create_unique_log_dir`, return the full paths of all configs used. The log dir has thus the following format {now} {netconfig} {probconfig} [r@XXXX_YYYY] [{postfix} {postfix}] :param log_dir: the log dir to parse :param configs_dir: the root config dir, where all the configs live :param base_dirs: Prefixed to the paths of the configs, e.g., ['ae', 'pc'] :return: all config paths, as well as the postfix if one was given """ base_dirs = [path.join(configs_dir, base_dir) for base_dir in base_dirs] log_dir = path.basename(log_dir.strip(path.sep)) comps = log_dir.split(' ') assert is_log_date(comps[0]), 'Invalid log_dir: {}'.format(log_dir) assert len(comps) > len(base_dirs), 'Expected a base dir for every component, got {} and {}'.format( comps, base_dirs) config_components = comps[1:(1+len(base_dirs))] has_restore = any(_RESTORE_PREFIX in c for c in comps) postfix = comps[1+len(base_dirs)+has_restore:] def get_real_path(base, prepped_p): p_glob = prepped_p.replace('@', path.sep) p_glob = path.join(base, p_glob) + append_ext # e.g., ae_configs/p_glob.cf glob_matches = glob.glob(p_glob) # We always only replace one character with *, so filter for those. # I.e. lr1e-5 will become lr1e*5, which will match lr1e-5 but also lr1e-4.5 glob_matches_of_same_len = [g for g in glob_matches if len(g) == len(p_glob)] if len(glob_matches_of_same_len) != 1: raise ValueError('Cannot find config on disk: {} (matches: {})'.format(p_glob, glob_matches_of_same_len)) return glob_matches_of_same_len[0] return LogDirComps( config_paths=tuple(get_real_path(base_dir, comp) for base_dir, comp in zip(base_dirs, config_components)), postfix=tuple(postfix) if postfix else None) # ------------------------------------------------------------------------------
Example #30
Source File: logdir_helpers.py From imgcomp-cvpr with GNU General Public License v3.0 | 5 votes |
def log_date_from_log_dir(log_dir): # extract {log_date} from LOG_DIR/{log_date} {netconfig} {probconfig} possible_log_date = os.path.basename(log_dir).split(' ')[0] if not is_log_date(possible_log_date): raise ValueError('Invalid log dir: {}'.format(log_dir)) return possible_log_date