Esempio n. 1
0
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)
Esempio n. 2
0
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))
Esempio n. 3
0
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))
Esempio n. 4
0
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))
Esempio n. 5
0
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))
Esempio n. 6
0
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
Esempio n. 7
0
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))
Esempio n. 8
0
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))
Esempio n. 9
0
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))
Esempio n. 10
0
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))
Esempio n. 11
0
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))
Esempio n. 12
0
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))