示例#1
0
def del_log(*args, **kwargs):
    """Delete each of the log files listed in "*arg" list.
    Errors are printed to "makelog" log file."""

    # metadata.settings should not be part of argument defaults so that they
    # can be overwritten by make_log.set_option
    if 'makelog' in kwargs.keys():
        makelog = kwargs['makelog']
    else:
        makelog = metadata.settings['makelog_file']

    if not (metadata.makelog_started and os.path.isfile(makelog)):
        raise CritError(messages.crit_error_nomakelog % makelog)

    print("\nDelete log file(s)")

    makelog = re.sub('\\\\', '/', makelog)
    try:
        LOGFILE = open(makelog, 'a')
    except Exception as errmsg:
        raise CritError((messages.crit_error_log % makelog) + '\n' +
                        str(errmsg))

    try:
        for log in args:
            log = re.sub('\\\\', '/', log)
            if os.path.isfile(log):
                os.remove(log)
            else:
                print(messages.note_nofile % log, file=LOGFILE)
    except:
        print_error(LOGFILE)

    LOGFILE.flush()
    LOGFILE.close()
示例#2
0
def list_directory(top, makelog='@DEFAULTVALUE@'):
    """List all non-hidden sub-directories of "top" and their content from top
    down. Write their names, modified times and sizes in bytes to "makelog" log
    file"""

    # metadata.settings should not be part of argument defaults so that they
    # can be overwritten by make_log.set_option
    if makelog == '@DEFAULTVALUE@':
        makelog = metadata.settings['makelog_file']

    print("\nList all files in directory", top)

    # To print numbers (file sizes) with thousand separator
    locale.setlocale(locale.LC_ALL, '')

    makelog = re.sub('\\\\', '/', makelog)
    try:
        LOGFILE = open(makelog, 'a')
    except Exception as errmsg:
        print(errmsg)
        raise CritError(messages.crit_error_log % makelog)

    print('\n', file=LOGFILE)
    print('List of all files in sub-directories in', top, file=LOGFILE)

    try:
        if os.path.isdir(top):
            for root, dirs, files in os.walk(top, topdown=True):
                # Ignore non-hidden sub-directories
                dirs_to_keep = []
                for dirname in dirs:
                    if not dirname.startswith('.'):
                        dirs_to_keep.append(dirname)
                dirs[:] = dirs_to_keep

                # print out the sub-directory and its time stamp
                created = os.stat(root).st_mtime
                asciiTime = time.asctime(time.localtime(created))
                print(root, file=LOGFILE)
                print('created/modified', asciiTime, file=LOGFILE)

                # print out all the files in the sub-directories
                for name in files:
                    full_name = os.path.join(root, name)
                    created = os.path.getmtime(full_name)
                    size = os.path.getsize(full_name)
                    asciiTime = time.asctime(time.localtime(created))
                    print('%50s' % name,
                          '--- created/modified',
                          asciiTime,
                          '(',
                          locale.format('%d', size, 1),
                          'bytes )',
                          file=LOGFILE)
    except:
        print_error(LOGFILE)

    LOGFILE.write('\n')
    LOGFILE.close()
示例#3
0
def check_manifest(manifestlog='@DEFAULTVALUE@',
                   output_dir='@DEFAULTVALUE@',
                   makelog='@DEFAULTVALUE@'):
    """Produce an error if there are any .dta files in "output_dir" and all
    non-hidden sub-directories that are not in the manifest file "manifestlog",
    and produce a warning if there are .txt or .csv files not in the manifest
    along with a list of these files. All log is printed to "makelog" log
    file."""

    # metadata.settings should not be part of argument defaults so that they
    # can be overwritten by make_log.set_option
    if manifestlog == '@DEFAULTVALUE@':
        manifestlog = metadata.settings['manifest_file']
    if output_dir == '@DEFAULTVALUE@':
        output_dir = metadata.settings['output_dir']
    if makelog == '@DEFAULTVALUE@':
        makelog = metadata.settings['makelog_file']

    print("\nCheck manifest log file", manifestlog)

    # Open main log file
    try:
        LOGFILE = open(makelog, 'a')
    except Exception as errmsg:
        print(errmsg)
        raise CritError(messages.crit_error_log % makelog)

    try:
        # Open manifest log file
        try:
            MANIFESTFILE = open(manifestlog, 'r')
        except Exception as errmsg:
            print(errmsg)
            raise CritError(messages.crit_error_log % manifestlog)
        manifest_lines = MANIFESTFILE.readlines()
        MANIFESTFILE.close()

        # Get file list
        try:
            file_list = []
            for i in range(len(manifest_lines)):
                if manifest_lines[i].startswith('File: '):
                    filepath = os.path.abspath(manifest_lines[i][6:].rstrip())
                    ext = os.path.splitext(filepath)[1]
                    if ext == '':
                        filepath = filepath + '.dta'
                    file_list.append(filepath)
        except Exception as errmsg:
            print(errmsg)
            raise SyntaxError(messages.syn_error_manifest % manifestlog)

        if not os.path.isdir(output_dir):
            raise CritError(messages.crit_error_no_directory % (output_dir))

        # Loop over all levels of sub-directories of output_dir
        for root, dirs, files in os.walk(output_dir, topdown=True):
            # Ignore non-hidden sub-directories
            dirs_to_keep = []
            for dirname in dirs:
                if not dirname.startswith('.'):
                    dirs_to_keep.append(dirname)
            dirs[:] = dirs_to_keep

            # Check each file
            for filename in files:
                ext = os.path.splitext(filename)[1]
                fullpath = os.path.abspath(os.path.join(root, filename))
                # non-hidden .dta file: error
                if (not filename.startswith('.')) and (ext == '.dta'):
                    print('Checking: ', fullpath)
                    if not (fullpath in file_list):
                        raise CritError(messages.crit_error_no_dta_file %
                                        (filename, manifestlog))
                # non-hidden .csv file: warning
                if (not filename.startswith('.')) and (ext == '.csv'):
                    print('Checking: ', fullpath)
                    if not (fullpath in file_list):
                        print(messages.note_no_csv_file %
                              (filename, manifestlog))
                        LOGFILE.write(messages.note_no_csv_file %
                                      (filename, manifestlog))
                # non-hidden .txt file: warning
                if (not filename.startswith('.')) and (ext == '.txt'):
                    print('Checking: ', fullpath)
                    if not (fullpath in file_list):
                        print(messages.note_no_txt_file %
                              (filename, manifestlog))
                        LOGFILE.write(messages.note_no_txt_file %
                                      (filename, manifestlog))
    except:
        print_error(LOGFILE)

    LOGFILE.close()
示例#4
0
 def issue_sys_command(self, logfile, quiet):
     for link in self.linkdirectives_list:
         try:
             link.issue_sys_command(logfile, quiet)
         except:
             print_error(logfile)