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

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

Fix a few problems - including referencing the xquery libraries; these
have now been added to the ndgUtils egg and are extracted locally and
referenced directly. Also add functionality to deal with the moles -> other
transforms + add new utility methods and tidy up code and add more logging.

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            sys.exit("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        return normpath(abspath(path))
47
48   
49    def setUpDir(self, dir):
50        '''
51        Clean out specified directory - or create this, if it doesn't already exist
52        @param dir: directory to set up/clean
53        '''
54        logging.info("Setting up directory, " + dir)
55        if os.path.isdir(dir):
56            self.cleanDir(dir)
57        else:
58            try:
59                self.makepath(dir)
60            except:
61                sys.exit("Failed at setting up directory, %s" %dir)
62
63
64    def cleanDir(self, dir):
65        '''
66        Remove all files from the specified directory
67        @param dir: directory to clean
68        '''
69        logging.info("Cleaning dir " + dir)
70        commandline = "find " + dir + "  -type f -print | xargs -i rm \{\}"
71        logging.debug("Executing : " + commandline)
72
73        status = os.system(commandline)
74        if status !=0:
75            sys.exit("Failed at cleaning out directory, %s" %dir)
76
77       
78    def makeBackUp(self, original_dir, backup_dir):
79        '''
80        Copy contents of original dir into backup dir
81        @param original_dir: dir to backup
82        @param backup_dir: dir to backup to 
83        '''
84        logging.info("Creating backup directory (" + original_dir + \
85                                " --> " + backup_dir + ")")
86        commandline = "mkdir " + backup_dir
87        logging.debug("Executing : " + commandline)
88        status = os.system(commandline)
89        if status !=0:
90            sys.exit("Failed at creating backup directory %s" %backup_dir)
91   
92#            commandline = "ls -1 " + original_dir + " | xargs -i cp " + original_dir + "{\} " + backup_dir
93            commandline = "find " + original_dir + " -type f -print | xargs -i cp \{\} " + backup_dir
94            logging.info("Executing : " + commandline)
95            status = os.system(commandline)
96            if status !=0:
97                sys.exit("Failed at copying to backup directory %s" %backup_dir)
98   
Note: See TracBrowser for help on using the repository browser.