def upload(file): if path.isdir(file): print '--- %s ---' % file for each in filesfirst( [path.join(file, unsorted) for unsorted in listdir(file)]): upload(each) return if sub('file -b --mime-type %s' % re.escape(file), shell=True, stdout=PIPE).stdout.read().strip() not in types: return tinypicSource = sub('curl -s tinypic.com', shell=True, stdout=PIPE).stdout.read() try: formValues = [field.search(tinypicSource).group(1) for field in form] except: print '-- form error --', sys.exit(2) try: result = resultDetails.search( sub('curl -s -L -F UPLOAD_IDENTIFIER="%s" -F upk="%s" -F domain_lang="en" -F action="upload" -F MAX_FILE_SIZE="500000000" -F shareopt="true" -F the_file=@%s -F file_type="image" -F dimension="1600" "http://s%s.tinypic.com/upload.php" -H "Expect:" -e "http://tinypic.com"' % (formValues[1], formValues[2], re.escape(file), formValues[0]), shell=True, stdout=PIPE).stdout.read()).group(1) except: print '-- upload error --', sys.exit(3) if cli: print 'http://s%s.tinypic.com/%s\t%s' % (formValues[0], result, file) else: print 'http://s%s.tinypic.com/%s' % (formValues[0], result)
def File( name, mode, content = None, ensure = None, path = None): if( ensure == "present" ): if(os.path.exists(path + '/' + name)): print("> The file '" + name + "' already exists.") return 0 else: #Uses the sub env variable imported earlier. file = open(path + "/" + name, w) file.write(content) sub(["sudo","chmod",mode,path + '/' + name]) file.close() print("> The file " + name + " has been created in " + path + ".") return 0 if( ensure == "absent" ): if os.path.exists(path + '/' + name): os.remove(path + "/" + name) print("> The file " + name + " has been deleted.") return 0
def whois(): form = WhoisForm() domain = None if form.validate_on_submit(): domain = urlparse(form.domain.data).netloc whoisout = sub(['whois ' + domain], shell=True).replace('\n', '<br />') return render_template('whois.html', form=form, output=whoisout, domain=domain) else: return render_template('whois.html', form=form, domain=domain)
def dig(): form = DigForm() domain = None if form.validate_on_submit(): domain = urlparse(form.domain.data).netloc digout = sub(['dig -t ANY ' + domain], shell=True).replace('\n', '<br />') return render_template('dig.html', form=form, output=digout, domain=domain) else: return render_template('dig.html', form=form, domain=domain)
def host(): form = HostForm() domain = None if form.validate_on_submit(): domain = urlparse(form.domain.data).netloc pingout = sub(['host ' + domain], shell=True).replace('\n', '<br />') return render_template('host.html', form=form, output=pingout, domain=domain) else: return render_template('host.html', form=form)
def traceroute(): form = TracerouteForm() domain = None if form.validate_on_submit(): domain = urlparse(form.domain.data).netloc traceout = sub(['traceroute ' + domain], shell=True).replace('\n', '<br />') return render_template('traceroute.html', form=form, output=traceout, domain=domain) else: return render_template('traceroute.html', form=form, domain=domain)
def passphrase(): form = PassphraseForm() length = None entropy = 77.55 if form.validate_on_submit(): length = form.length.data entropy = 12.925 * length passphrase=sub(['diceware -n ' + str(length)], shell=True) return render_template('ppgen.html', passphrase=passphrase, entropy=entropy, form=form) else: passphrase = ppgen() return render_template('ppgen.html', passphrase=passphrase, entropy=entropy, form=form)
def install_module(module, install=True, conda_or_pip="pip", print_terminal=True, verbose=False): """Allows installation of a module directly from a notebook or script. Improves reproducibility of scripting/ notebook usage, where installed modules may differ between users. Arguments: module -- string. The name of the module intended to be installed. install -- bool. Install the module (True) or check if already installed (False) conda_or_pip -- string. Only required if install = True. Must be either 'conda' or 'pip'. print_terminal -- bool. Print the terminal output resulting from installing module, or not. If verbose = False, print_terminal is overridden. Returns: "True" if module is available for import, "False" if not. """ # Import libraries: # importlib.import_module to allow import via a string argument. # subprocess.getstatusoutput to allow printing of terminal output and to check zero or non-zero exit status. # logging for... yep, logging from importlib import import_module as im from subprocess import getstatusoutput as sub import logging # Set inital state for progress and success/failure messaging. state = 0 # Set out to default, in case no terminal output to display out = {1: "No terminal output to display"} # Create inverse of pip or conda for messaging if conda_or_pip == "conda": inv_conda_or_pip = "pip" elif conda_or_pip == "pip": inv_conda_or_pip = "conda" # Check whether module is available for import and adjust messaging state try: im(module) except (ImportError): state = 1 if install == True: assert conda_or_pip in [ "conda", "pip" ], 'the attribute conda_or_pip needs to be one of "conda" or "pip"' if verbose: print(module, "not installed") print("automatically installing", module, "using", conda_or_pip, "...please wait.") # Install module using conda if conda_or_pip == "conda": out = sub("conda install " + module + " -y") # Install module using pip elif conda_or_pip == "pip": out = sub("pip install " + module) # If non-zero exit, adjust state if out[0] != 0: state = 2 # If zero exit, reset state if out[0] == 0: state = 0 # If module still can't be imported, adjust state try: im(module) except (ImportError): state = 3 # Messaging based on state if state == 0: if verbose: print(module + " is installed and available for import.") elif state == 1: logging.error( module + " not installed. Please install using pip or conda before importing. \n" + "(Run this function again with the 'conda_or_pip' attribute set to either 'conda' or 'pip' and 'install' set to 'True')." ) elif state == 2: logging.error(module + " could not be installed using " + conda_or_pip + ". Try using " + inv_conda_or_pip + ". If both fail, check module spelling.") elif state == 3: logging.error(module + " is installed but not importable. Install using " + inv_conda_or_pip + " and try again.") # Print terminal output if verbose: if print_terminal: print("-" * 18 + "\n Terminal output \n" + "-" * 18) print(out[1]) # Set exit status if state == 0: return True else: return False
def User( name, comment = "", ensure = "absent", uid = "", gid = "", groups = [], password = "" ): if(ensure == "present"): sub(['adduser',name,'-p',password]) return 0
def Cron( name, user, command, minute, hour, day, month ): cron = minute + ' ' + hour + ' ' + day + ' ' + month + ' ' + user + ' ' + command sub(["sudo","crontab","<",cron]) print("> Cron job " + name + " has been added to " + user + "'s crontab.") return 0
def Package( name, distro, ensure = "absent" ): #Install Packages. if( ensure == "present" and distro == "debian" ): #Subcommand calls apt-get for use within Debian Distros. sub(["sudo","apt-get","install","-y",name]) print("> " + name + " has been installed.") return 0 if( ensure == "present" and distro == "redhat" ): #Subcommand calls apt-get for use within RedHat Distros. sub(["sudo","yum","install","-y",name]) print("> " + name + " has been installed.") return 0 if( ensure == "present" and distro == "fedora" ): #Subcommand calls apt-get for use within Fedora. sub(["sudo","dnf","install","-y",name]) print("> " + name + " has been installed.") return 0 #Remove Packages. if( ensure == "absent" and distro == "debian" ): #Subcommand calls apt-get for use within Debian Distros. sub(["sudo","apt","remove","-y",name]) return 0 if( ensure == "absent" and distro == "debian" ): #Subcommand calls apt-get for use within Debian Distros. sub(["sudo","apt","remove","-y",name]) return 0 if( ensure == "absent" and distro == "debian" ): #Subcommand calls apt-get for use within Debian Distros. sub(["sudo","apt","remove","-y",name]) return 0 else: return 0
#!/usr/bin/python # tinypic uploader :: 4.0 # gnome-look.org :: magicmarkers :: 2010-05-18 import sys, re from subprocess import Popen as sub, PIPE from os import path, getenv, listdir cli = getenv('NAUTILUS_SCRIPT_WINDOW_GEOMETRY') is None if cli: selected = sys.argv[1:] else: zenity = sub( 'zenity --title="Uploading to tinypic.com" --text-info --width=500 --height=300', shell=True, stdin=PIPE) sys.stdout, sys.stderr = zenity.stdin, zenity.stdin selected = getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS', '').splitlines() if not selected: print "-- nothing selected --", sys.exit(1) types = [ 'image/jpeg', 'image/gif', 'image/png', 'image/x-ms-bmp', 'image/x-pcx', 'image/tiff', 'image/vnd.adobe.photoshop' ] form = [ re.compile('form action="http://s(.)'), re.compile('id="uid" value="([^"]+)'), re.compile('name="upk" value="([^"]+)') ] resultDetails = re.compile('name="pic" value="([^"]+)') def filesfirst(list): return sorted([f for f in list if path.isfile(f)]) + sorted( [d for d in list if path.isdir(d)])