def doman(*sourceFiles): '''inserts the man pages in the list of files into /usr/share/man/''' '''example call: pisilinuxtools.doman("man.1", "pardus.*")''' manDIR = join_path(get.installDIR(), get.manDIR()) if not can_access_directory(manDIR): makedirs(manDIR) for sourceFile in sourceFiles: sourceFileGlob = glob.glob(sourceFile) if len(sourceFileGlob) == 0: raise FileError(_("No file matched pattern \"%s\"") % sourceFile) for source in sourceFileGlob: compressed = source.endswith("gz") and source if compressed: source = source[:-3] try: pageName, pageDirectory = source[:source.rindex('.')], \ source[source.rindex('.')+1:] except ValueError: error(_('ActionsAPI [doman]: Wrong man page file: %s') % (source)) manPDIR = join_path(manDIR, '/man%s' % pageDirectory) makedirs(manPDIR) if not compressed: system('install -m0644 %s %s' % (source, manPDIR)) else: uncompress(compressed, targetDir=manPDIR)
def echo(destionationFile, content): try: f = open(destionationFile, 'a') f.write('%s\n' % content) f.close() except IOError: error(_('ActionsAPI [echo]: Can\'t append to file %s.') % (destionationFile))
def makedirs(destinationDirectory): '''recursive directory creation function''' try: if not os.access(destinationDirectory, os.F_OK): os.makedirs(destinationDirectory) except OSError: error(_('Cannot create directory %s') % destinationDirectory)
def system(command): command = string.join(string.split(command)) retValue = run_logged(command) #if return value is different than 0, it means error, raise exception if retValue != 0: error(_("Command \"%s\" failed, return value was %d.") % (command, retValue)) return retValue
def dosym(sourceFile, destinationFile): '''creates soft link between sourceFile and destinationFile''' ''' example call: pisilinuxtools.dosym("/usr/bin/bash", "/bin/bash")''' makedirs(join_path(get.installDIR(), os.path.dirname(destinationFile))) try: os.symlink(sourceFile, join_path(get.installDIR() ,destinationFile)) except OSError: error(_('ActionsAPI [dosym]: File already exists: %s') % (destinationFile))
def unlinkDir(sourceDirectory): '''delete an entire directory tree''' if isDirectory(sourceDirectory) or isLink(sourceDirectory): try: shutil.rmtree(sourceDirectory) except OSError: error(_('ActionsAPI [unlinkDir]: Operation not permitted: %s') % (sourceDirectory)) elif isFile(sourceDirectory): pass else: error(_('ActionsAPI [unlinkDir]: Directory %s doesn\'t exists.') % (sourceDirectory))
def rename(sourceFile, destinationFile): ''' renames sourceFile as destinationFile''' ''' example call: pisilinuxtools.rename("/usr/bin/bash", "bash.old") ''' ''' the result of the previous example would be "/usr/bin/bash.old" ''' baseDir = os.path.dirname(sourceFile) try: os.rename(join_path(get.installDIR(), sourceFile), join_path(get.installDIR(), baseDir, destinationFile)) except OSError as e: error(_('ActionsAPI [rename]: %s: %s') % (e, sourceFile))
def chmod(filePath, mode = 0o755): '''change the mode of filePath to the mode''' filePathGlob = glob.glob(filePath) if len(filePathGlob) == 0: error(_("ActionsAPI [chmod]: No file matched pattern \"%s\".") % filePath) for fileName in filePathGlob: if can_access_file(fileName): try: os.chmod(fileName, mode) except OSError: ctx.ui.error(_('ActionsAPI [chmod]: Operation not permitted: %s (mode: 0%o)') \ % (fileName, mode)) else: ctx.ui.error(_('ActionsAPI [chmod]: File %s doesn\'t exists.') % (fileName))
def touch(filePath): '''changes the access time of the 'filePath', or creates it if it does not exist''' filePathGlob = glob.glob(filePath) if filePathGlob: if len(filePathGlob) == 0: error(_("ActionsAPI [touch]: No file matched pattern \"%s\".") % filePath) for f in filePathGlob: os.utime(f, None) else: try: f = open(filePath, 'w') f.close() except IOError: error(_('ActionsAPI [touch]: Permission denied: %s') % (filePath))
def copytree(source, destination, sym = True): '''recursively copy an entire directory tree rooted at source''' if isDirectory(source): if os.path.exists(destination): if isDirectory(destination): copytree(source, join_path(destination, os.path.basename(source.strip('/')))) return else: copytree(source, join_path(destination, os.path.basename(source))) return try: shutil.copytree(source, destination, sym) except OSError as e: error(_('ActionsAPI [copytree] %s to %s: %s') % (source, destination, e)) else: error(_('ActionsAPI [copytree]: Directory %s doesn\'t exists.') % (source))
def copy(source, destination, sym = True): '''recursively copy a "source" file or directory to "destination"''' sourceGlob = glob.glob(source) if len(sourceGlob) == 0: error(_("ActionsAPI [copy]: No file matched pattern \"%s\".") % source) for filePath in sourceGlob: if isFile(filePath) and not isLink(filePath): try: shutil.copy(filePath, destination) except IOError: error(_('ActionsAPI [copy]: Permission denied: %s to %s') % (filePath, destination)) elif isLink(filePath) and sym: if isDirectory(destination): os.symlink(os.readlink(filePath), join_path(destination, os.path.basename(filePath))) else: if isFile(destination): os.remove(destination) os.symlink(os.readlink(filePath), destination) elif isLink(filePath) and not sym: if isDirectory(filePath): copytree(filePath, destination) else: shutil.copy(filePath, destination) elif isDirectory(filePath): copytree(filePath, destination, sym) else: error(_('ActionsAPI [copy]: File %s does not exist.') % filePath)
def move(source, destination): '''recursively move a "source" file or directory to "destination"''' sourceGlob = glob.glob(source) if len(sourceGlob) == 0: error(_("ActionsAPI [move]: No file matched pattern \"%s\".") % source) for filePath in sourceGlob: if isFile(filePath) or isLink(filePath) or isDirectory(filePath): try: shutil.move(filePath, destination) except OSError: error(_('ActionsAPI [move]: Permission denied: %s to %s') % (filePath, destination)) else: error(_('ActionsAPI [move]: File %s doesn\'t exists.') % (filePath))