import glob import sys import os import re import time from subprocess import Popen from catalyst import log from catalyst.defaults import valid_config_file_values BASH_BINARY = "/bin/bash" def list_bashify(mylist): if isinstance(mylist, str): mypack=[mylist] else: mypack=mylist[:] for x in range(0,len(mypack)): # surround args with quotes for passing to bash, # allows things like "<" to remain intact mypack[x]="'"+mypack[x]+"'" mypack = ' '.join(mypack) return mypack class CatalystError(Exception): def __init__(self, message, print_traceback=False): if message: log.error('CatalystError: %s', message, exc_info=print_traceback) def cmd(mycmd, myexc="", env=None, debug=False, fail_func=None): if env is None: env = {} log.debug('cmd: %r', mycmd) sys.stdout.flush() args=[BASH_BINARY] if "BASH_ENV" not in env: env = env.copy() env["BASH_ENV"] = "/etc/spork/is/not/valid/profile.env" if debug: args.append("-x") args.append("-c") args.append(mycmd) log.debug('args: %r', args) proc = Popen(args, env=env) if proc.wait() != 0: if fail_func: log.error('CMD(), NON-Zero command return. Running fail_func().') fail_func() raise CatalystError("cmd() NON-zero return value from: %s" % myexc, print_traceback=False) def file_check(filepath): '''Check for the files existence and that only one exists if others are found with various extensions ''' if os.path.isfile(filepath): return filepath # it didn't exist # so check if there are files of that name with an extension files = glob.glob("%s.*" % filepath) # remove any false positive files files = [x for x in files if not x.endswith(".CONTENTS") and not x.endswith(".DIGESTS")] if len(files) is 1: return files[0] elif len(files) > 1: msg = "Ambiguos Filename: %s\nPlease specify the correct extension as well" % filepath raise CatalystError(msg, print_traceback=False) raise CatalystError("File Not Found: %s" % filepath) def file_locate(settings,filelist,expand=1): #if expand=1, non-absolute paths will be accepted and # expanded to os.getcwd()+"/"+localpath if file exists for myfile in filelist: if myfile not in settings: #filenames such as cdtar are optional, so we don't assume the variable is defined. pass else: if len(settings[myfile])==0: raise CatalystError("File variable \"" + myfile + "\" has a length of zero (not specified.)", print_traceback=True) if settings[myfile][0]=="/": if not os.path.exists(settings[myfile]): raise CatalystError("Cannot locate specified " + myfile + ": " + settings[myfile], print_traceback=False) elif expand and os.path.exists(os.getcwd()+"/"+settings[myfile]): settings[myfile]=os.getcwd()+"/"+settings[myfile] else: raise CatalystError("Cannot locate specified " + myfile + ": "+settings[myfile]+" (2nd try)" + """ Spec file format: The spec file format is a very simple and easy-to-use format for storing data. Here's an example file: item1: value1 item2: foo bar oni item3: meep bark gleep moop This file would be interpreted as defining three items: item1, item2 and item3. item1 would contain the string value "value1". Item2 would contain an ordered list [ "foo", "bar", "oni" ]. item3 would contain an ordered list as well: [ "meep", "bark", "gleep", "moop" ]. It's important to note that the order of multiple-value items is preserved, but the order that the items themselves are defined are not preserved. In other words, "foo", "bar", "oni" ordering is preserved but "item1" "item2" "item3" ordering is not, as the item strings are stored in a dictionary (hash). """, print_traceback=True) def parse_makeconf(mylines): mymakeconf={} pos=0 pat=re.compile("([0-9a-zA-Z_]*)=(.*)") while pos>> Waiting %s seconds before starting...\n' '>>> (Control-C to abort)...\n' '%s in: ') % (secs, doing)) ticks=range(secs) ticks.reverse() for sec in ticks: sys.stdout.write(str(sec+1)+" ") sys.stdout.flush() time.sleep(1) sys.stdout.write('\n') def normpath(mypath): TrailingSlash=False if mypath[-1] == "/": TrailingSlash=True newpath = os.path.normpath(mypath) if len(newpath) > 1: if newpath[:2] == "//": newpath = newpath[1:] if TrailingSlash: newpath=newpath+'/' return newpath