source: TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/FileUtilities.py @ 3810

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/FileUtilities.py@3810
Revision 3810, 3.2 KB checked in by cbyrom, 11 years ago (diff)

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.

Line 
1import os, sys
2from Logger import Logger
3
4class FileUtilities:
5    '''
6        Various helper methods for setting up and cleaning filesystems
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)
20
21
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))
43
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)
58
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 TracBrowser for help on using the repository browser.