def fs_rename(self, src, dst): ''' Renames a file. Example: axon.fs_rename('/myfile', '/mycoolerfile') ''' _, srcprops = self.core.getPropNorm('axon:path', src) srcppath, srcfname = srcprops.get('dir'), srcprops.get('base') _, dstprops = self.core.getPropNorm('axon:path', dst) dstppath, dstfname = dstprops.get('dir'), dstprops.get('base') with self.flock: srcfo = self.core.getTufoByProp('axon:path', src) if not srcfo: raise s_common.NoSuchEntity() src_isdir = Axon._fs_isdir(srcfo[1].get('axon:path:st_mode')) psrcfo = self.core.getTufoByProp('axon:path', srcppath) if not (psrcfo and Axon._fs_isdir(psrcfo[1].get('axon:path:st_mode'))): raise s_common.NoSuchDir() pdstfo = self.core.getTufoByProp('axon:path', dstppath) if not (pdstfo and Axon._fs_isdir(pdstfo[1].get('axon:path:st_mode'))): raise s_common.NoSuchDir() # prepare to set dst props to what src props were dstprops = Axon._get_renameprops(srcfo) dstprops.update({'dir': dstppath}) # create or update the dstfo node dstfo = self.core.formTufoByProp('axon:path', dst, **dstprops) dst_isdir = Axon._fs_isdir(dstfo[1].get('axon:path:st_mode')) dst_isemptydir = dstfo[1].get('axon:path:st_nlink', -1) == 2 dstfo_isnew = dstfo[1].get('.new') if dst_isdir and not dst_isemptydir and not dstfo_isnew: raise s_common.NotEmpty() # all pre-checks complete if dstfo_isnew: # if a new file was created, increment its parents link count ?? self.core.incTufoProp(pdstfo, 'st_nlink', 1) else: # Now update dstfo props self.core.setTufoProps(dstfo, **dstprops) # if overwriting a regular file with a dir, remove its st_size if src_isdir: self.core.delRowsByIdProp(dstfo[0], 'axon:path:st_size') self._fs_reroot_kids(src, dst) # Remove src and decrement its parent's link count self.core.delTufo(srcfo) self.core.incTufoProp(psrcfo, 'st_nlink', -1)
def _getDirNode(self, path): node = self.core.getTufoByProp('axon:path', path) if node is None: raise s_common.NoSuchDir() if not Axon._fs_isdir(node[1].get('axon:path:st_mode')): raise s_common.NoSuchDir() return node
def _getDirNode(self, path): ''' Get the axon:path node for a directory. Args: path (str): Path to retrieve Returns: ((str, dict)): axon:path node. Raises: NoSuchDir: If the path does not exist or if the path is a file. ''' node = self.core.getTufoByProp('axon:path', path) if node is None: raise s_common.NoSuchDir() if not Axon._fs_isdir(node[1].get('axon:path:st_mode')): raise s_common.NoSuchDir() return node
def getModsByPath(path, modtree=None): ''' Return a list of (modname,info) tuples for a path entry. Example: for path in sys.path: mods = getModsByPath(path) dostuff(mods) ''' path = os.path.abspath(path) if modtree is None: modtree = [] if not os.path.isdir(path): raise s_common.NoSuchDir(path=path) mods = {} todo = [(path, modtree)] while todo: path, modtree = todo.pop() pkgname = '.'.join(modtree) for name in os.listdir(path): if isSkipName(name): continue subbase = name.rsplit('.')[0] subtree = modtree + [subbase] subpath = os.path.join(path, name) modname = '.'.join(subtree) # check for a pkg dir... if os.path.isdir(subpath): pkgfile = os.path.join(subpath, '__init__.py') if not os.path.isfile(pkgfile): continue # pkg dir found! mods[modname] = s_common.tufo(modname, fmt='src', path=pkgfile, pkg=True) todo.append((subpath, subtree)) continue modinfo = _getModInfo(name) if modinfo is not None: # fmt=None for unhandled module types if not modinfo.get('fmt'): continue mods[modname] = s_common.tufo(modname, path=subpath, **modinfo) continue # add dat files to our pkg moddef pmod = mods.get(pkgname) if pmod is None: continue dats = pmod[1].get('dats') if dats is None: dats = {} pmod[1]['dats'] = dats dats[name] = subpath return mods