def doman(*sourceFiles): """inserts the man pages in the list of files into /usr/share/man/""" '''example call: inarytools.doman("man.1", "sulin.*")''' 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 \"{}\"").format(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: \"{}\"'). format(source)) manPDIR = join_path(manDIR, '/man{}'.format(pageDirectory)) makedirs(manPDIR) if not compressed: system('install -m 0644 {0} {1}'.format(source, manPDIR)) else: uncompress(compressed, targetDir=manPDIR)
def echo(destionationFile, content): try: f = open(destionationFile, 'a') f.write('{}\n'.format(content)) f.close() except IOError: error( _('ActionsAPI [echo]: Can\'t append to file \"{}\"').format( destionationFile))
def makedirs(destinationDirectory): """recursive directory creation function""" try: if not os.access(destinationDirectory, os.F_OK): os.makedirs(destinationDirectory) except OSError: error( _('ActionsAPI [makedirs]: Cannot create directory \"{}\"').format( destinationDirectory))
def dosym(sourceFile, destinationFile): """creates soft link between sourceFile and destinationFile""" ''' example call: inarytools.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: \"{}\"').format( destinationFile))
def rename(sourceFile, destinationFile): """ renames sourceFile as destinationFile""" ''' example call: inarytools.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]: \"{0}\": \"{1}\"').format(e, sourceFile))
def system(command): # command an list but should be an str sys.stdout.write( colorize(_("[Running Command]: "), 'brightwhite') + command + "\n") # command = str.join(str.split(command)) retValue = run_logged(command) # if return value is different than 0, it means error, raise exception if retValue != 0: error( _("ActionsAPI [system]: Command \'{0}\' failed, return value was {1}." ).format(command, retValue)) return retValue
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: \"{}\"'). format(sourceDirectory)) elif isFile(sourceDirectory): pass else: error( _('ActionsAPI [unlinkDir]: Directory \"{}\" doesn\'t exists.'). format(sourceDirectory))
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 \"{}\"").format( 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: {0} (mode: 0{1})' ).format(fileName, mode)) else: ctx.ui.error( _('ActionsAPI [chmod]: File \"{}\" doesn\'t exists.').format( 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 \"{}\"."). format(filePath)) for f in filePathGlob: os.utime(f, None) else: try: f = open(filePath, 'w') f.close() except IOError: error( _('ActionsAPI [touch]: Permission denied: \"{}\"').format( filePath))
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 \"{}\".").format( source)) for filePath in sourceGlob: if isFile(filePath) and not isLink(filePath): try: shutil.copy(filePath, destination) except IOError: error( _('ActionsAPI [copy]: Permission denied: \"{0}\" to \"{1}\"' ).format(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 \"{}\" does not exist.').format( 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] \"{0}\" to \"{1}\": {2}').format( source, destination, e)) else: error( _('ActionsAPI [copytree]: Directory \"{}\" doesn\'t exists.'). format(source))
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 \"{}\".").format( 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: \"{0}\" to \"{1}\"' ).format(filePath, destination)) else: error( _('ActionsAPI [move]: File \"{}\" doesn\'t exists.').format( filePath))