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

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

Improve the FileUtilities? - using 'find' to get the various required filenames in a more
effective manner + add logging support and improve output.

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