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

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

Adjust logging in oai_document_ingester + adjust exception handling,
avoiding use of sys.exit() to allow processing workflow to continue
with other files + general tidy up and small fixes of several files.

Line 
1import os, sys, logging
2from os import makedirs
3from os.path import normpath,dirname,exists,abspath
4
5class FileUtilities:
6    '''
7        Various helper methods for setting up and cleaning filesystems
8    C Byrom Apr 08
9    '''
10    def __init__(self):
11        '''
12        Constructor
13        '''
14
15    def createFile(self, fileName, content):
16        '''
17        Create a file with the specified name and content
18        @param fileName: name of file to create
19        @param content: content of file to create 
20        '''
21        try:
22            f = open(fileName,'w')
23            f.write(content)
24            f.close()
25        except:
26            raise SystemError, "ERROR: Problem encountered when creating file, %s" %fileName
27
28
29    def makepath(self, path):
30        ''' creates missing directories for the given path and
31            returns a normalized absolute version of the path.
32   
33        - if the given path already exists in the filesystem
34          the filesystem is not modified.
35   
36        - otherwise makepath creates directories along the given path
37          using the dirname() of the path. You may append
38          a '/' to the path if you want it to be a directory path.
39   
40        from holger@trillke.net 2002/03/18
41        '''
42        logging.info("Creating dir: " + path) 
43        dpath = normpath(dirname(path))
44        if not exists(dpath):
45            makedirs(dpath)
46        status = normpath(abspath(path))
47       
48        if status == 0:
49            logging.info("Directory created successfully")
50        return status
51
52   
53    def setUpDir(self, dir):
54        '''
55        Clean out specified directory - or create this, if it doesn't already exist
56        @param dir: directory to set up/clean
57        '''
58        logging.info("Setting up directory, " + dir)
59        if os.path.isdir(dir):
60            self.cleanDir(dir)
61        else:
62            try:
63                self.makepath(dir)
64            except:
65                sys.exit("Failed at setting up directory, %s" %dir)
66
67        logging.info("Directory set up successfully")
68
69    def cleanDir(self, dir):
70        '''
71        Remove all files from the specified directory
72        @param dir: directory to clean
73        '''
74        logging.info("Cleaning dir " + dir)
75        commandline = "find " + dir + "  -type f -print | xargs -i rm \{\}"
76        logging.debug("Executing : " + commandline)
77
78        status = os.system(commandline)
79        if status != 0:
80            sys.exit("Failed at cleaning out directory, %s" %dir)
81       
82        logging.info("Directory cleaned successfully")
83
84       
85    def makeBackUp(self, original_dir, backup_dir):
86        '''
87        Copy contents of original dir into backup dir
88        @param original_dir: dir to backup
89        @param backup_dir: dir to backup to 
90        '''
91        logging.info("Creating backup directory (" + original_dir + \
92                                " --> " + backup_dir + ")")
93        self.makepath(backup_dir)
94   
95        commandline = "find " + original_dir + " -type f -print | xargs -i cp \{\} " + backup_dir
96        logging.info("Executing : " + commandline)
97        status = os.system(commandline)
98        if status !=0:
99            sys.exit("Failed at copying to backup directory %s" %backup_dir)
100           
101        logging.info("Backup completed successfully")
102   
Note: See TracBrowser for help on using the repository browser.