Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2.7
- import os
- import sys
- import fnmatch
- import subprocess
- import traceback
- # whichever directory is running on.
- cwd = os.getcwd()
- def which(program):
- """
- Emulates which command. Checks if exacutable does exist in PATH
- """
- def is_exe(fpath):
- return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
- fpath, fname = os.path.split(program)
- if fpath:
- if is_exe(program):
- return program
- else:
- for path in os.environ["PATH"].split(os.pathsep):
- path = path.strip('"')
- exe_file = os.path.join(path, program)
- if is_exe(exe_file):
- return exe_file
- return None
- def format_files(file_list, overwrite=False):
- """
- Not sure
- """
- if len(file_list) > 0:
- if overwrite is True:
- for file in file_list:
- cmd = ['xmllint', '--format']
- cmd.append(file)
- print "[Formatting] " + file
- cmd = " ".join(cmd)
- p = subprocess.Popen(
- cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- (output, err) = p.communicate()
- p_status = p.wait()
- f = open(file, 'w')
- f.truncate()
- f.write(output)
- f.close()
- else:
- for file in file_list:
- newfile = file.replace('.xml', '-formatted.xml')
- cmd = ['xmllint', '--format']
- cmd.append(file)
- cmd.append('>>')
- cmd.append(newfile)
- print "[Formatting] " + file
- subprocess.call(" ".join(cmd), shell=True)
- else:
- print "No valid files to format, Quit!"
- def main():
- """
- Module
- """
- if which('xmllint') is None:
- print "XMLlint can not be found. Quit!"
- sys.exit(1)
- print 'Working on ' + cwd
- try:
- matches = []
- for root, dirnames, filenames in os.walk(cwd):
- for filename in fnmatch.filter(filenames, '*.xml'):
- matches.append(os.path.join(root, filename))
- print "\n".join(matches)
- confirm = raw_input("Listed files will be formatted, yes? 5$ pls!\n")
- overwrite = raw_input("Overwrite files, yes?\n")
- if confirm in ['y', 'yes']:
- if overwrite in ['y', 'yes']:
- format_files(matches, True)
- else:
- format_files(matches)
- except KeyboardInterrupt:
- print "Shutdown requested...exiting"
- except Exception:
- traceback.print_exc(file=sys.stdout)
- sys.exit(0)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement