source: ndgCommon/trunk/ndg/common/src/lib/fileutilities.py @ 4793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/lib/fileutilities.py@4991
Revision 4793, 2.8 KB checked in by cbyrom, 11 years ago (diff)

Checking in initial codebase for ndgUtils restructure.

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