| 1 | import __main__ | 
|---|
| 2 | import argparse | 
|---|
| 3 | import os | 
|---|
| 4 | import re | 
|---|
| 5 | import stat | 
|---|
| 6 |  | 
|---|
| 7 | from subprocess import Popen, PIPE, STDOUT | 
|---|
| 8 |  | 
|---|
| 9 | # helper functions to run terminal commands | 
|---|
| 10 | def sh(cmd, dry_run = False, print2stdout = True): | 
|---|
| 11 | if dry_run :    # if this is a dry_run, only print the commands that would be ran | 
|---|
| 12 | print("cmd: %s" % cmd) | 
|---|
| 13 | return 0, None | 
|---|
| 14 | else :                  # otherwise create a pipe and run the desired command | 
|---|
| 15 | proc = Popen(cmd, stdout=None if print2stdout else PIPE, stderr=STDOUT, shell=True) | 
|---|
| 16 | out, err = proc.communicate() | 
|---|
| 17 | return proc.returncode, out | 
|---|
| 18 |  | 
|---|
| 19 | # Remove 1 or more files silently | 
|---|
| 20 | def rm( files, dry_run = False ): | 
|---|
| 21 | try: | 
|---|
| 22 | for file in files: | 
|---|
| 23 | sh("rm -f %s > /dev/null 2>&1" % file, dry_run) | 
|---|
| 24 | except TypeError: | 
|---|
| 25 | sh("rm -f %s > /dev/null 2>&1" % files, dry_run) | 
|---|
| 26 |  | 
|---|
| 27 | def chdir( dest = __main__.__file__ ): | 
|---|
| 28 | abspath = os.path.abspath(dest) | 
|---|
| 29 | dname = os.path.dirname(abspath) | 
|---|
| 30 | os.chdir(dname) | 
|---|
| 31 |  | 
|---|
| 32 | # helper function to replace patterns in a file | 
|---|
| 33 | def file_replace(fname, pat, s_after): | 
|---|
| 34 | # first, see if the pattern is even in the file. | 
|---|
| 35 | with open(fname) as f: | 
|---|
| 36 | if not any(re.search(pat, line) for line in f): | 
|---|
| 37 | return # pattern does not occur in file so we are done. | 
|---|
| 38 |  | 
|---|
| 39 | # pattern is in the file, so perform replace operation. | 
|---|
| 40 | with open(fname) as f: | 
|---|
| 41 | out_fname = fname + ".tmp" | 
|---|
| 42 | out = open(out_fname, "w") | 
|---|
| 43 | for line in f: | 
|---|
| 44 | out.write(re.sub(pat, s_after, line)) | 
|---|
| 45 | out.close() | 
|---|
| 46 | os.rename(out_fname, fname) | 
|---|
| 47 |  | 
|---|
| 48 | # helper function to check if a files contains only a spacific string | 
|---|
| 49 | def fileContainsOnly(file, text) : | 
|---|
| 50 | with open(file) as f: | 
|---|
| 51 | ff = f.read().strip() | 
|---|
| 52 | result = ff == text.strip() | 
|---|
| 53 |  | 
|---|
| 54 | return result; | 
|---|
| 55 |  | 
|---|
| 56 | # check whether or not a file is executable | 
|---|
| 57 | def fileIsExecutable(file) : | 
|---|
| 58 | try : | 
|---|
| 59 | fileinfo = os.stat(file) | 
|---|
| 60 | return bool(fileinfo.st_mode & stat.S_IXUSR) | 
|---|
| 61 | except Exception as inst: | 
|---|
| 62 | print(type(inst))    # the exception instance | 
|---|
| 63 | print(inst.args)     # arguments stored in .args | 
|---|
| 64 | print(inst) | 
|---|
| 65 | return False | 
|---|
| 66 |  | 
|---|
| 67 | # check if arguments is yes or no | 
|---|
| 68 | def yes_no(string): | 
|---|
| 69 | if string == "yes" : | 
|---|
| 70 | return True | 
|---|
| 71 | if string == "no" : | 
|---|
| 72 | return False | 
|---|
| 73 | raise argparse.ArgumentTypeError(msg) | 
|---|
| 74 | return False | 
|---|
| 75 |  | 
|---|
| 76 | # diff two files | 
|---|
| 77 | def diff( lhs, rhs, dry_run ): | 
|---|
| 78 | # diff the output of the files | 
|---|
| 79 | diff_cmd = ("diff --ignore-all-space " | 
|---|
| 80 | "--ignore-blank-lines " | 
|---|
| 81 | "--old-group-format='\t\tmissing lines :\n" | 
|---|
| 82 | "%%<' \\\n" | 
|---|
| 83 | "--new-group-format='\t\tnew lines :\n" | 
|---|
| 84 | "%%>' \\\n" | 
|---|
| 85 | "--unchanged-group-format='%%=' \\" | 
|---|
| 86 | "--changed-group-format='\t\texpected :\n" | 
|---|
| 87 | "%%<" | 
|---|
| 88 | "\t\tgot :\n" | 
|---|
| 89 | "%%>\n' \\\n" | 
|---|
| 90 | "--new-line-format='\t\t%%dn\t%%L' \\\n" | 
|---|
| 91 | "--old-line-format='\t\t%%dn\t%%L' \\\n" | 
|---|
| 92 | "--unchanged-line-format='' \\\n" | 
|---|
| 93 | "%s %s") | 
|---|
| 94 |  | 
|---|
| 95 | # fetch return code and error from the diff command | 
|---|
| 96 | return sh(diff_cmd % (lhs, rhs), dry_run, False) | 
|---|