Ignore:
Timestamp:
21/04/08 13:10:30 (13 years ago)
Author:
cbyrom
Message:

Create Logger class to standardise output - and allow this to be
switched on/off with a verbose flag.
Implement the logger in several other classes + extend available logging in these.
Also, add a 'usage' method to oai_ingest_new to show proper usage of script.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/FileUtilities.py

    r3800 r3810  
    1 import os 
    2 import sys 
     1import os, sys 
     2from Logger import Logger 
     3 
    34class FileUtilities: 
    4         ''' 
     5    ''' 
    56        Various helper methods for setting up and cleaning filesystems  
    6         C Byrom Apr 08 
    7         ''' 
    8  
    9 def makepath(path): 
    10     """ creates missing directories for the given path and 
    11         returns a normalized absolute version of the path. 
    12  
    13     - if the given path already exists in the filesystem 
    14       the filesystem is not modified. 
    15  
    16     - otherwise makepath creates directories along the given path 
    17       using the dirname() of the path. You may append 
    18       a '/' to the path if you want it to be a directory path. 
    19  
    20     from holger@trillke.net 2002/03/18 
    21     """ 
    22     from os import makedirs 
    23     from os.path import normpath,dirname,exists,abspath 
    24  
    25     print "INFO: Creating dir: %s" %path  
    26     dpath = normpath(dirname(path)) 
    27     if not exists(dpath): 
    28         makedirs(dpath) 
    29     return normpath(abspath(path)) 
    30  
    31 def setUpDir(dir): 
    32         ''' 
    33         Clean out specified directory - or create this, if it doesn't already exist 
    34         @param dir: directory to set up/clean 
    35         ''' 
    36         print "Setting up directory, %s" %dir 
    37         if os.path.isdir(dir): 
    38                 cleanDir(dir) 
    39         else: 
    40                 try: 
    41                         makepath(dir) 
    42                 except: 
    43                         sys.exit("Failed at setting up directory, %s" %dir) 
     7    C Byrom Apr 08 
     8    ''' 
     9    logger = None 
     10    def __init__(self, inLogger): 
     11        ''' 
     12        Constructor - to set up the utils with a default logger 
     13        @param logger: logger to use; if none specified, use default logger.  NB, logger must  
     14                support a 'printOutput' method 
     15        ''' 
     16        if inLogger is not None: 
     17            self.logger = inLogger 
     18        else: 
     19            self.logger = Logger(True) 
    4420 
    4521 
    46 def cleanDir(dir): 
    47         ''' 
    48         Remove all files from the specified directory 
    49         @param dir: directory to clean  
    50         ''' 
    51         print "INFO: Cleaning dir %s" %dir 
    52         commandline = "ls -1 " + dir + " | xargs -i rm " + dir + "{\}" 
    53         print "INFO: Executing : " + commandline 
    54         status = os.system(commandline) 
    55         if status !=0: 
    56             sys.exit("Failed at cleaning out directory, %s" %dir) 
     22    def makepath(self, path): 
     23        ''' creates missing directories for the given path and 
     24            returns a normalized absolute version of the path. 
     25     
     26        - if the given path already exists in the filesystem 
     27          the filesystem is not modified. 
     28     
     29        - otherwise makepath creates directories along the given path 
     30          using the dirname() of the path. You may append 
     31          a '/' to the path if you want it to be a directory path. 
     32     
     33        from holger@trillke.net 2002/03/18 
     34        ''' 
     35        from os import makedirs 
     36        from os.path import normpath,dirname,exists,abspath 
     37     
     38        self.logger.printOutput("INFO: Creating dir: " + path)  
     39        dpath = normpath(dirname(path)) 
     40        if not exists(dpath): 
     41            makedirs(dpath) 
     42        return normpath(abspath(path)) 
    5743 
    58          
    59 def makeBackUp(original_dir, backup_dir): 
    60         ''' 
    61         Copy contents of original dir into backup dir 
    62         @param original_dir: dir to backup 
    63         @param backup_dir: dir to backup to   
    64         ''' 
    65         print "Creating backup directory (%s --> %s)" %original_dir %backup_dir 
    66         commandline = "mkdir " + backup_dir 
    67         print "INFO: Executing : " + commandline 
    68         status = os.system(commandline) 
    69         if status !=0: 
    70                 sys.exit("Failed at creating backup directory %s" %backup_dir) 
     44     
     45    def setUpDir(self, dir): 
     46        ''' 
     47        Clean out specified directory - or create this, if it doesn't already exist 
     48        @param dir: directory to set up/clean 
     49        ''' 
     50        self.logger.printOutput("Setting up directory, " + dir) 
     51        if os.path.isdir(dir): 
     52            self.cleanDir(dir) 
     53        else: 
     54            try: 
     55                makepath(dir) 
     56            except: 
     57                sys.exit("Failed at setting up directory, %s" %dir) 
    7158 
    72                 commandline = "ls -1 " + original_dir + " | xargs -i cp " + original_dir + "{\} " + backup_dir 
    73                 print "INFO: Executing : " + commandline 
    74                 status = os.system(commandline) 
    75                 if status !=0: 
    76                         sys.exit("Failed at copying to backup directory %s" %backup_dir) 
    77          
     59 
     60    def cleanDir(self, dir): 
     61        ''' 
     62        Remove all files from the specified directory 
     63        @param dir: directory to clean  
     64        ''' 
     65        self.logger.printOutput("INFO: Cleaning dir " + dir) 
     66        commandline = "ls -1 " + dir + " | xargs -i rm " + dir + "{\}" 
     67        self.logger.printOutput("INFO: Executing : " + commandline) 
     68         
     69        status = os.system(commandline) 
     70        if status !=0: 
     71            sys.exit("Failed at cleaning out directory, %s" %dir) 
     72 
     73         
     74    def makeBackUp(self, original_dir, backup_dir): 
     75        ''' 
     76        Copy contents of original dir into backup dir 
     77        @param original_dir: dir to backup 
     78        @param backup_dir: dir to backup to   
     79        ''' 
     80        self.logger.printOutput("Creating backup directory (" + original_dir + \ 
     81                                " --> " + backup_dir + ")") 
     82        commandline = "mkdir " + backup_dir 
     83        self.logger.printOutput("INFO: Executing : " + commandline) 
     84        status = os.system(commandline) 
     85        if status !=0: 
     86            sys.exit("Failed at creating backup directory %s" %backup_dir) 
     87     
     88            commandline = "ls -1 " + original_dir + " | xargs -i cp " + original_dir + "{\} " + backup_dir 
     89            self.logger.printOutput("INFO: Executing : " + commandline) 
     90            status = os.system(commandline) 
     91            if status !=0: 
     92                sys.exit("Failed at copying to backup directory %s" %backup_dir) 
     93     
Note: See TracChangeset for help on using the changeset viewer.