Пример #1
0
def Execute(rule, dirObj, pad = None):
    """
    Execute the rule in the given directory
    """

    if pad == 'None':
        pad = ''

    (toProcess, remoteRules,
     optionsToSet, newExportDir) = prepareRule(rule, dirObj)

    # Loop over each directory
    for dirName in toProcess:
        adagio.logInfo(rule, dirObj, 'RECUR: ' + dirName)
        if not os.path.isdir(dirName):
            print i18n.get('not_a_directory').format(dirName)
            sys.exit(1)
        remoteDir = directory.getDirectoryObject(dirName, optionsToSet)

	if remoteDir.rule_list == []:
	    # If rule_list is empty, there is no Properties.dgo in that dir, skip execution
            continue

        # Execute the targets in the remote directory
        remoteDir.Execute(remoteRules, pad + '  ')

    return
Пример #2
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Loop over all the source files
    dstDir = dirObj.getProperty(rule, 'src_dir')
    for datafile in toProcess:

        # If file not found, terminate
        if not os.path.isfile(datafile):
            print i18n.get('file_not_found').format(datafile)
            sys.exit(1)

        # Derive the destination file name
        dstFile = os.path.splitext(os.path.basename(datafile))[0] + \
            '.pdf'
        dstFile = os.path.abspath(os.path.join(dstDir, dstFile))

        if not os.path.exists(dstFile):
            continue

        rules.remove(dstFile)
Пример #3
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Create a list with the suffixes to consider
    suffixes = []
    produceValues = set(dirObj.getProperty(rule, 'produce').split())
    if 'regular' in produceValues:
        # Delete the regular version
        suffixes.append('')
    if 'solution' in produceValues:
        # Delete the solution
        suffixes.append('_solution')
    if 'pguide' in produceValues:
        # Delete the professor guide
        suffixes.append('_pguide')
    if 'submit' in produceValues:
        # Delete the submission file
        suffixes.append('_submit')

    xsltproc.doClean(rule, dirObj, toProcess, suffixes)

    return
Пример #4
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """
    
    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = glob.glob(os.path.join(dirObj.current_dir, '*.xcf'))
    if toProcess == []:
        adagio.logDebug(rule, dirObj, i18n.get('no_file_to_process'))
        return

    # Loop over the source files to see if an execution is needed
    dstFiles = []
    dstDir = dirObj.getProperty(rule, 'src_dir')
    for datafile in toProcess:

        # If file not found, terminate
        if not os.path.isfile(datafile):
            print i18n.get('file_not_found').format(datafile)
            sys.exit(1)

        # Derive the destination file name
        dstFile = os.path.splitext(os.path.basename(datafile))[0] + '.png'
        dstFile = os.path.abspath(os.path.join(dstDir, dstFile))

        if not os.path.exists(dstFile):
            continue

        rules.remove(dstFile)

    return
Пример #5
0
def singleStyleApplication(datafile, styles, styleTransform, styleParams, dstFile, rule, dirObj, dataTree=None):
    """
    Apply a transformation to a file with a dictionary
    """

    # Check for dependencies!
    sources = set(styles + [datafile])
    sources.update(dirObj.option_files)
    dependency.update(dstFile, sources)

    # If the destination file is up to date, skip the execution
    if dependency.isUpToDate(dstFile):
        print i18n.get("file_uptodate").format(os.path.basename(dstFile))
        return dataTree

    # Proceed with the execution of xslt
    print i18n.get("producing").format(os.path.basename(dstFile))

    # Parse the data file if needed
    if dataTree == None:
        adagio.logInfo(rule, dirObj, "Parsing " + datafile)
        dataTree = treecache.findOrAddTree(datafile, True)

    # Apply the transformation
    xsltprocEquivalent(rule, dirObj, styleParams, datafile, dstFile)
    try:
        result = styleTransform(dataTree, **styleParams)
    except etree.XSLTApplyError, e:
        print
        print i18n.get("error_applying_xslt").format("\n".join(styles), datafile, rule)
        print "Error:", str(e)
        sys.exit(1)
Пример #6
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """
    
    adagio.logInfo(rule, dirObj, 'Cleaning')

    prepareExecution(rule, dirObj, 'clean_function')

    return
Пример #7
0
    def __init__(self, path = os.getcwd(), givenOptions = []):
        # Initial values
        self.previous_dir =   os.getcwd()
        self.current_dir =    os.path.abspath(path)
        self.givenOptions =   ''
        self.options =        None
        self.rule_list =      []
        self.alias =          {}
        self.current_rule =   None
        self.executing =      False
        self.executed_rules = set([])
        self.option_files =   set([])

        adagio.logInfo('Directory', None, 
                       'New dir object in ' + self.current_dir)

        configDefaults = adagio.getConfigDefaults(self.current_dir)

        # Compute the project home
        os.chdir(self.current_dir)
        detected_project_home = findProjectDir(configDefaults['project_file'], 
                                               self.current_dir)
        configDefaults['project_home'] = detected_project_home
        os.chdir(self.previous_dir)

        # Compute the relative_basedir (relative path from basedir to
        # project_home
        configDefaults['relative_basedir'] = \
            os.path.relpath(self.current_dir, detected_project_home)

        # Safe parser to store the options, the defaults are loaded here
        self.options = properties.initialConfig(configDefaults)

        #
        # STEP 1: Load the default options from the Rule files
        #
        adagio.LoadDefaults(self.options)

        #
        # STEP 2: Load the ~/.adagiorc if any
        #
        userAdagioConfig = os.path.normpath(os.path.join(os.path.expanduser('~'),
                                                         '.adagiorc'))
        if os.path.isfile(userAdagioConfig):
            # Swallow user file on top of global options, and if trouble, report
            # up
            try:
                (newFiles, b) = properties.loadConfigFile(self.options,
                                                          userAdagioConfig,
                                                          self.alias)
                self.option_files.update(newFiles)
            except ValueError, e:
                print i18n.get('severe_parse_error').format(userAdagioConfig)
                print str(e)
                sys.exit(3)
Пример #8
0
def prepareRule(rule, dirObj):
    """
    Obtain the directories to process, calculate the options to set in the
    remote execution and obtain the remote rules.

    Return:

    (list of dirs to process, remote rules, options to set in the remote exec)
    """

    # Get the directories to process from the files option
    toProcess = []
    for srcDir in dirObj.getProperty(rule, 'files').split():
        newDirs = glob.glob(srcDir)
        if newDirs == []:
            print i18n.get('file_not_found').format(srcDir)
            sys.exit(1)
        toProcess.extend(glob.glob(srcDir))

    # Add the files included in files_included_from
    filesIncluded = \
        obtainXincludes(dirObj.getProperty(rule, 'files_included_from').split())

    # The list of dirs is extended with a set to avoid duplications
    toProcess.extend(set(map(lambda x: os.path.dirname(x), filesIncluded)))

    # If there are no files to process stop
    if toProcess == []:
        adagio.logDebug(rule, dirObj, i18n.get('no_file_to_process'))
        return (toProcess, [], None, '')

    # Translate all paths to absolute paths
    toProcess = map(lambda x: os.path.abspath(x), toProcess)

    # Rules to execute in the remote directory
    remoteRules = dirObj.getProperty(rule, 'rules').split()

    # Create the option dict for the remote directories
    optionsToSet = []
    newExportDir = dirObj.getProperty(rule, 'export_dst')
    if newExportDir != '':
        # If a new dst_dir has been specified, include the options to modify
        # that variable for each of the rules
        if remoteRules != []:
            # If there are some given rules, use them
            optionsToSet = [x + ' dst_dir ' + newExportDir
                            for x in remoteRules if x.startswith('export')]
        else:
            # If no rule is given, leave the option in the export.dst_dir
            # to its default value default
            optionsToSet = ['export dst_dir ' + newExportDir]

    adagio.logInfo(rule, dirObj, 'NEW Options = ' + ', '.join(optionsToSet))

    return (toProcess, remoteRules, optionsToSet, newExportDir)
Пример #9
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, "Cleaning")

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    doClean(rule, dirObj, toProcess)

    return
Пример #10
0
def clean(rule, directory):
    """
    Clean the files produced by this rule
    """
    
    adagio.logInfo(rule, directory, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, directory)
    if toProcess == []:
        return

    print 'Not implemented yet'
    sys.exit(1)

    return
Пример #11
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Loop over all source files to process
    doClean(rule, dirObj, toProcess,
            dirObj.getProperty(rule, 'src_dir'),
            dirObj.getProperty(rule, 'dst_dir'))

    return
Пример #12
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Loop over all source files to process
    dstDir = dirObj.getProperty(rule, 'dst_dir')
    outputFormat = dirObj.getProperty(rule, 'output_format')
    for datafile in toProcess:

        # If file not found, terminate
        if not os.path.isfile(datafile):
            print i18n.get('file_not_found').format(datafile)
            sys.exit(1)

        # Derive the destination file name
        dstPrefix = os.path.splitext(os.path.basename(datafile))[0]
        dstPrefix = os.path.join(dstDir, dstPrefix)

        extensionsToClean = ['out', 'aux', 'log', 'bbl', 'blg', 'idx',
                             'ilg', 'ind', 'lof', 'lot', 'toc']
        if outputFormat != 'dvipdf':
            extensionsToClean.append(outputFormat)
        else:
            extensionsToClean.append('dvi')
            extensionsToClean.append('pdf')

        for fmt in extensionsToClean:
            dstFile = dstPrefix + '.' + fmt

            if not os.path.exists(dstFile):
                continue

            rules.remove(dstFile)

    return
Пример #13
0
def Execute(rule, dirObj, pad = None):
    """
    Execute the rule in the given directory
    """

    if pad == 'None':
        pad = ''

    (toProcess, remoteRules,
     optionsToSet, newExportDir) = prepareRule(rule, dirObj)

    # Loop over each directory
    for dirName in toProcess:
        adagio.logInfo(rule, dirObj, 'RECUR: ' + dirName)
        if not os.path.isdir(dirName):
            print i18n.get('not_a_directory').format(dirName)
            sys.exit(1)
        remoteDir = directory.getDirectoryObject(dirName, optionsToSet)
        remoteDir.Execute(remoteRules, pad + '  ')

    return
Пример #14
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Get geometry
    geometries = dirObj.getProperty(rule, 'geometry').split()
    if geometries == []:
        print i18n.get('no_var_value').format('geometry')
        return

    # Loop over all the source files
    dstDir = dirObj.getProperty(rule, 'dst_dir')
    for datafile in toProcess:

        # If file not found, terminate
        if not os.path.isfile(datafile):
            print i18n.get('file_not_found').format(datafile)
            sys.exit(1)

        # Loop over formats
        for geometry in geometries:
            # Derive the destination file name
            (fn, ext) = os.path.splitext(os.path.basename(datafile))
            dstFile = fn + '_' + geometry + ext
            dstFile = os.path.abspath(os.path.join(dstDir, dstFile))

            if not os.path.exists(dstFile):
                continue

            rules.remove(dstFile)

    return
Пример #15
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    rawFiles = []
    for fname in toProcess:
        # Get the result files
        resultFiles = doGetShuffledFiles(fname)

        # Accumulate the list
        rawFiles.extend(resultFiles)

    suffixes = []
    produceValues = set(dirObj.getProperty(rule, 'produce').split())
    if 'regular' in produceValues:
        # Delete the regular version
        suffixes.append('')
    if 'solution' in produceValues:
        # Delete the solution
        suffixes.append('_solution')
    if 'pguide' in produceValues:
        # Delete the professor guide
        suffixes.append('_pguide')

    xsltproc.doClean(rule, dirObj, rawFiles, suffixes)

    # Clean also the produced files
    map(lambda x: rules.remove(x), rawFiles)

    return
Пример #16
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule. The rule is executed under the same
    conditions explained in the documentation variable.
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Check the condition
    dstDir = dirObj.getProperty(rule, 'dst_dir')
    if dstDir == '':
        return

    # If we are here, the export may proceed!
    filecopy.doClean(rule, dirObj, toProcess,
                 dirObj.getProperty(rule, 'src_dir'), dstDir)

    return
Пример #17
0
def clean(rule, dirObj):
    """
    Clean the files produced by this rule
    """

    adagio.logInfo(rule, dirObj, 'Cleaning')

    # Get the files to process
    toProcess = rules.getFilesToProcess(rule, dirObj)
    if toProcess == []:
        return

    # Get the dstDir
    dstDir = dirObj.getProperty(rule, 'dst_dir')

    # If dist dir not found, terminate
    if not os.path.isdir(dstDir):
        print i18n.get('file_not_found').format(dstDir)
        sys.exit(1)

    # Delete the dst directory
    rules.remove(dstDir)

    return
Пример #18
0
    # Parse the dictionary given in extra_arguments and fold it
    try:
        extraDict = eval("{" + dirObj.getProperty(rule, "extra_arguments") + "}")
        for (k, v) in extraDict.items():
            if hasattr(etree.XSLT, "strparam"):
                # Valid beyond version 2.2 of lxml
                styleParams[k] = etree.XSLT.strparam(str(v))
            else:
                # If v has quotes, too bad...
                styleParams[k] = '"' + str(v) + '"'
    except SyntaxError, e:
        print i18n.get("error_extra_args").format(rule + ".extra_arguments")
        print str(e)
        sys.exit(1)

    adagio.logInfo(rule, dirObj, "StyleParmas: " + str(styleParams))
    return styleParams


def doTransformations(styles, styleTransform, styleParams, toProcess, rule, dirObj, paramDict=None):
    """
    Function that given a style transformation, a set of style parameters, a
    list of pairs (parameter dicitonaries, suffix), and a list of files to
    process, applies the transformation to every file, every local dictionary
    and every language.."""

    if paramDict == None:
        paramDict = [({}, "")]

        # Obtain languages and language prefix/suffix.
    languages = dirObj.getProperty(rule, "languages").split()
Пример #19
0
def clean(rule, dirObj, deepClean = False, pad = None):
    """
    Clean the files produced by this rule
    """

    global module_prefix

    if pad == None:
        pad = ''

    adagio.logInfo(rule, dirObj, 'Cleaning')

    (toProcess, remoteRules,
     optionsToSet, newExportDir) = prepareRule(rule, dirObj)

    # When cleaning, rules should be executed in reversed order
    remoteRules.reverse()

    if deepClean:
        # If the clean is deep, attach .clean suffix to all remote rules
        remoteRules = [x + '.deepclean'
                         for x in remoteRules
                         if not re.match('(.+\.)?help$', x) and \
                             not re.match('(.+\.)?vars$', x)and \
                             not re.match('(.+\.)?clean$', x)]

        # If no rule is obtained, deep clean, means simply execute the clean
        # rule
        if remoteRules == []:
            remoteRules = ['deepclean']
    else:
        # If the clean is not deep, the execution only propagates to those rules
        # of type export and if the newExportDir is this directory (to clean the
        # current directory only). Otherwise, the rule is simply ignored.
        if newExportDir != dirObj.current_dir:
            return

        remoteRules = [x + '.clean'  for x in remoteRules
                         if x.startswith('export')]

    adagio.logInfo(rule, dirObj,
                'Remote Rules = ' + ' '.join(remoteRules))

    # Loop over each directory
    for dirName in toProcess:

        adagio.logInfo(rule, dirObj, 'RECUR: ' + dirName)
        remoteObj = directory.getDirectoryObject(dirName, optionsToSet)

        # If the clean is not deep and there is no given remote rules, we need
        # to select those that start with 'export'
        if (not deepClean) and (remoteRules == []):
            remoteRules = [x + '.clean' for x in remoteObj.rule_list
                             if re.match('^export(\..+)?$',
                                         properties.expandAlias(x,
                                                                remoteObj.alias))]

        # If remoteRules empty, there is nothing to process in the remote
        # directory.
        if remoteRules == []:
            continue

        # Execute the remote rules
        remoteObj.Execute(remoteRules, pad + '  ')

    return
Пример #20
0
    def Execute(self, rules = [], pad = ''):
        """
        properties.ddo has been parsed into a ConfigParse. Loop over the rules
        and execute all of them.
        """

        adagio.logInfo('Directory', self, 'Execute in ' + self.current_dir)

        # Change directory to the current one
        self.previous_dir = os.getcwd()
        os.chdir(self.current_dir)

        # Print a line flagging the start of the execution showing the maximum
        # suffix of the current directory up to 80 chars.
        showCurrentDir = self.current_dir[len(pad) + 3 - 80:]
        if len(self.current_dir) > (77 - len(pad)):
            showCurrentDir = '...' + showCurrentDir[3:]

        print pad + '++ ' + showCurrentDir

        # Make sure no circular execution is produced
        if self.executing:
            print i18n.get('circular_execute_directory').format(self.current_dir)
            sys.exit(2)
        self.executing = True

        # If no rules are given, choose the default ones, that is, ignore:
        # - adagio
        # - clean*
        # - local*
        #
        toExecRules = [x for x in self.rule_list
                         if not re.match('^adagio$', x) and
                         not re.match('^clean(\.?\S+)?$', x) and
                         not re.match('^local(\.?\S+)?$', x) and
                         not re.match('^rsync(\.?\S+)?$', x)]

        # If no rule is given, execute all except the filtered ones
        if rules == []:
            rules = toExecRules

        # If any of the rules is help, vars or clean, expand the current rules
        # to add them that suffix, otherwise simply accumulate
        finalRules = []
        for rule in rules:
            if rule == 'deepclean':
                # Get all the rules
                finalRules.extend([x + '.deepclean' for x in toExecRules])
                finalRules.reverse()
            elif rule == 'clean':
                # Get all the rules except the "gotodir" ones
                finalRules.extend([x + '.clean'
                                     for x in toExecRules])
                finalRules.reverse()
            elif rule == 'help':
                finalRules.append('adagio.help')
                finalRules.extend([x + '.help' for x in toExecRules])
            elif rule == 'vars':
                finalRules.append('adagio.vars')
                finalRules.extend([x + '.vars' for x in toExecRules])
            elif rule == 'local':
                finalRules.extend([x for x in toExecRules
                                     if not x.startswith('gotodir')])
            else:
                finalRules.append(rule)

        adagio.logDebug('Directory', self, '  Rules: ' + str(finalRules))

        # If after all these preparations, finalRules is empty, help is
        # needed, hardwire the rule to adagio.help.
        if finalRules == []:
            finalRules = ['adagio.help']

        adagio.logDebug('Directory', self,
                     ' to execute ' + ' '.join(finalRules))

        # Set the XML resolver
        treecache.setResolver(self.options.get(adagio.module_prefix, 
                                               'xml_paths'))

        # Loop over all the rules and execute them
        for rule_name in finalRules:

            # Check the cache to see if rule has already been executed
            if rule_name in self.executed_rules:
                adagio.logInfo('Directory', self,
                            'Rule HIT: ' + self.current_dir + ': ' + \
                            rule_name)
                continue

            # Execute the rule
            adagio.Execute(rule_name, self, pad)

            # Insert executed rule in cache
            self.executed_rules.add(rule_name)

        self.executing = False
        adagio.logDebug('Directory', self,
                     ' Executed Rules: ' + str(self.executed_rules))

        print pad + '-- ' +  showCurrentDir

        # Change directory to the current one
        os.chdir(self.previous_dir)

        return
Пример #21
0
                                                   adagioPropFile))
        if os.path.exists(propAbsFile):
            try:
                (newFiles, confRules) = properties.loadConfigFile(self.options,
                                                                  propAbsFile,
                                                                  self.alias)
                self.option_files.update(newFiles)
            except ValueError, e:
                print i18n.get('severe_parse_error').format(propAbsFile)
                print str(e)
                sys.exit(3)
                
            self.rule_list = confRules
        else:
            # If there is no rule file, notify
            adagio.logInfo('Directory', None, 'No ' + adagioPropFile + \
                            ' found in ' + self.current_dir)
            print i18n.get('cannot_find_properties').format(adagioPropFile,
                                                            self.current_dir)
            self.rule_list = []

        #
        # STEP 5: Options given from outside the directory
        #
        for assignment in givenOptions:
            # Chop assignment into its three parts
            (sn, on, ov) = assignment.split()

            sn = properties.expandAlias(sn, self.alias)
            # Check first if the option is legal
            if not self.options.has_option(sn, on):
                optionName = sn + '.' + on