def ansible_save(hosts, content, dest_filepath): with NamedTemporaryFile() as tempfile: tempfile.write(content) tempfile.flush() module_name = 'copy' module_args = 'src={src} dest={conf_file}'.format( src=tempfile.name, conf_file=dest_filepath, ) results = run(hosts, module_name, module_args, ) return results
def groupvars_edit(self, groupname, FORMAT="JSON"): import tempfile EDITOR = os.environ.get('EDITOR','vi') ''' create tempfile and dump current vars in tempfile ''' with tempfile.NamedTemporaryFile(suffix=".tmp") as tempfile: self.groupvars(groupname, filename=tempfile.name, FORMAT=FORMAT) tempfile.flush() call([EDITOR, tempfile.name]) self.groupvars_update(groupname, filename=tempfile.name, FORMAT=FORMAT)
def edit_in_vim(content): ''' Takes text, writes to a temp file, opens the temp file with vim, you edit file in vim and save. Returns the saved text. ''' import subprocess import tempfile with tempfile.NamedTemporaryFile(suffix='blogpost') as tempfile: tempfile.write(content.encode('utf-8')) tempfile.flush() subprocess.call(['vim', tempfile.name]) text = open(tempfile.name, 'r').read().decode('utf-8') return text
def ansible_save(hosts, content, dest_filepath): with NamedTemporaryFile() as tempfile: tempfile.write(content) tempfile.flush() module_name = 'copy' module_args = 'src={src} dest={conf_file}'.format( src=tempfile.name, conf_file=dest_filepath, ) results = run( hosts, module_name, module_args, ) return results
import tempfile def find_gitroot(filepath_reference): path = filepath_reference path_prev = "" while not os.path.exists(os.path.join(path, ".git")) and path != path_prev: path_prev = path path = os.path.dirname(path) return path doxyfile, sourcefile = sys.argv[-2:] doxyfile = os.path.join(find_gitroot(sourcefile), doxyfile) os.chdir(os.path.dirname(doxyfile)) tempfile = tempfile.NamedTemporaryFile(mode='w+b') doxyfile_tmp = tempfile.name tempfile.write(open(doxyfile, "r+b").read()) tempfile.write(b'\n\n') tempfile.write(b'INPUT=' + os.fsencode(sourcefile) + b'\n') tempfile.flush() subprocess.call(("doxygen", doxyfile_tmp)) del tempfile # Maybe handy, but also annoying? if "--browse" in sys.argv: import webbrowser webbrowser.open("html/files.html")
#parser.add_option("-f", "--fresh", help="configure Unbox from scratch" command_arg = sys.argv[1] # Perform fresh install if command_arg == "fresh": resource_link_dict = core.resource_link_dict core.remove_links(resource_link_dict.keys()) core.ignored_resources = [] remote_resources = core.remote_resources # Write out resources in Dropbox folder in temp file json_obj = dict.fromkeys(remote_resources, " ") tempfile = tempfile.NamedTemporaryFile(delete=False) json.dump(json_obj, tempfile, indent=4, separators=(",", "\t:\t")) tempfile.flush() # Open editor to let user set what should be installed editor = os.environ.get("EDITOR", "vim") return_code = subprocess.call([editor, tempfile.name]) tempfile.seek(0) desired_links = json.load(tempfile) tempfile.close() # Process user's decisions resources_to_ignore = [ resource for resource in desired_links.keys() if resource not in remote_resources ] core.ignored_resources.append(resources_to_ignore) core.forge_links(desired_links)
def _download_and_unpack_file(url): """Downloads the database files created with setup-exfor-db.py as a tarball and unpacks them to the correct folder.""" from tqdm import tqdm from glob import glob import requests import math import tarfile import tempfile import shutil # cleanup for f in [ fullIndexFileName, fullErrorFileName, fullCoupledFileName, fullMonitoredFileName, fullReactionCountFileName, fullDBPath, dbTagFile ]: try: shutil.rmtree(f) except NotADirectoryError: os.remove(f) except FileNotFoundError: pass # Tag files: tag_files = [ f for tag in ['X4-*', 'EXFOR-*'] for f in glob(os.path.join(DATAPATH, tag)) ] for tagfile in tag_files: try: os.remove(tagfile) except FileNotFoundError: pass # Streaming, so we can iterate over the response. r = requests.get(url, stream=True) tarname = os.path.basename(url) # Total size in bytes. total_size = int(r.headers.get('content-length', 0)) block_size = 1024 * 1024 wrote = 0 tempfile = tempfile.TemporaryFile() print('Downloading data file', tarname) for data in tqdm(r.iter_content(block_size), total=math.ceil(total_size // block_size), unit='MB', unit_scale=True): wrote = wrote + len(data) tempfile.write(data) if total_size != 0 and wrote != total_size: raise Exception("ERROR, something went wrong") tempfile.flush() tempfile.seek(0) print('Decompressing archive', tarname) wrote = 0 with tarfile.open(fileobj=tempfile, mode='r') as _tar: total = len(_tar.getmembers()) for member in tqdm(_tar.getmembers(), total=total): wrote = wrote + len(data) _tar.extract(member, DATAPATH) tempfile.close() with open(dbTagFile,'wb') as f: print('Installed database version', dbTagFile) pass