本文整理汇总了Python中shutil.copyfileobj函数的典型用法代码示例。如果您正苦于以下问题:Python copyfileobj函数的具体用法?Python copyfileobj怎么用?Python copyfileobj使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copyfileobj函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: imageToCache
def imageToCache(src,name):
response = requests.get(src, stream=True)
target = os.path.join(CACHE_PATH,name)
with open(target, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
return target
示例2: store
def store(self, media_file, file=None, url=None, meta=None):
"""Store the given file or URL and return a unique identifier for it.
:type media_file: :class:`~mediadrop.model.media.MediaFile`
:param media_file: The associated media file object.
:type file: :class:`cgi.FieldStorage` or None
:param file: A freshly uploaded file object.
:type url: unicode or None
:param url: A remote URL string.
:type meta: dict
:param meta: The metadata returned by :meth:`parse`.
:rtype: unicode or None
:returns: The unique ID string. Return None if not generating it here.
"""
file_name = safe_file_name(media_file, file.filename)
file_path = self._get_path(file_name)
temp_file = file.file
temp_file.seek(0)
permanent_file = open(file_path, 'wb')
copyfileobj(temp_file, permanent_file)
temp_file.close()
permanent_file.close()
return file_name
示例3: _downloadAChapter
def _downloadAChapter(self, pathToStore, url, chapter):
pageNumber = 0
baseImageUrl = url + "/" + str(chapter) + "/"
basePathToStore = pathToStore + "/" + str(chapter) + "/"
nextImage = True
downloadSuccess = False
self._log("downloading chapter {} :".format(chapter), "INFO")
while nextImage:
pageNumberFormatted = self._formatPageNumber(pageNumber)
imageUrl = baseImageUrl + pageNumberFormatted + ".jpg"
self._log("try to download {}".format(imageUrl), "DEBUG")
r = requests.get(imageUrl, stream=True)
if r.status_code == 200:
self._log("download page image {} from chapter {}".format(pageNumberFormatted, chapter), "DEBUG")
self.logger.printSameLine("*")
if not downloadSuccess:
downloadSuccess = True
pageNumber += 1
imagePath = basePathToStore + pageNumberFormatted + ".jpg"
with open(imagePath, 'wb') as imageFile:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, imageFile)
else:
if pageNumber == 0:
pageNumber += 1
else:
if downloadSuccess:
self.logger.printSameLine("",True)
nextImage = False
return downloadSuccess
示例4: settings_audio_add
def settings_audio_add(request):
params = request.json_body
transaction.manager.begin()
fileobj = env.file_storage.fileobj(component='compulink_video_producer')
fileobj.persist()
transaction.manager.commit() # ugly hack
transaction.manager.begin()
fileobj.persist()
srcfile, _ = env.file_upload.get_filename(params['id'])
dstfile = env.file_storage.filename(fileobj, makedirs=True)
with open(srcfile, 'r') as fs, open(dstfile, 'w') as fd:
copyfileobj(fs, fd)
vba = VideoBackgroundAudioFile()
vba.file_name = params['name']
vba.file_obj_id = fileobj.id
vba.file_mime_type = params['mime_type']
vba.file_size = params['size']
vba.persist()
transaction.manager.commit()
vba.persist()
return vba.serialize()
示例5: dump_db
def dump_db(db_name, stream, backup_format='zip'):
"""Dump database `db` into file-like object `stream` if stream is None
return a file object with the dump """
_logger.info('DUMP DB: %s format %s', db_name, backup_format)
cmd = ['pg_dump', '--no-owner']
cmd.append(db_name)
if backup_format == 'zip':
with odoo.tools.osutil.tempdir() as dump_dir:
filestore = odoo.tools.config.filestore(db_name)
if os.path.exists(filestore):
shutil.copytree(filestore, os.path.join(dump_dir, 'filestore'))
with open(os.path.join(dump_dir, 'manifest.json'), 'w') as fh:
db = odoo.sql_db.db_connect(db_name)
with db.cursor() as cr:
json.dump(dump_db_manifest(cr), fh, indent=4)
cmd.insert(-1, '--file=' + os.path.join(dump_dir, 'dump.sql'))
odoo.tools.exec_pg_command(*cmd)
if stream:
odoo.tools.osutil.zip_dir(dump_dir, stream, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
else:
t=tempfile.TemporaryFile()
odoo.tools.osutil.zip_dir(dump_dir, t, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
t.seek(0)
return t
else:
cmd.insert(-1, '--format=c')
stdin, stdout = odoo.tools.exec_pg_command_pipe(*cmd)
if stream:
shutil.copyfileobj(stdout, stream)
else:
return stdout
示例6: readEpgDatFile
def readEpgDatFile(self, filename, deleteFile=False):
if not hasattr(self.epgcache, 'load'):
print>>log, "[EPGImport] Cannot load EPG.DAT files on unpatched enigma. Need CrossEPG patch."
return
try:
os.unlink(HDD_EPG_DAT)
except:
pass # ignore...
try:
if filename.endswith('.gz'):
print>>log, "[EPGImport] Uncompressing", filename
import shutil
fd = gzip.open(filename, 'rb')
epgdat = open(HDD_EPG_DAT, 'wb')
shutil.copyfileobj(fd, epgdat)
del fd
epgdat.close()
del epgdat
else:
if filename != HDD_EPG_DAT:
os.symlink(filename, HDD_EPG_DAT)
print>>log, "[EPGImport] Importing", HDD_EPG_DAT
self.epgcache.load()
if deleteFile:
try:
os.unlink(filename)
except:
pass # ignore...
except Exception, e:
print>>log, "[EPGImport] Failed to import %s:" % filename, e
示例7: dlImage
def dlImage(url,logf=None):
if url.startswith(start_img_fail):
if logf:
logf.write("Skipping image in failed s3 bucket.\n")
else:
print "Skipping image in failed s3 bucket."
return None
pos_slash=[pos for pos,c in enumerate(url) if c=="/"]
file_img=url[pos_slash[-1]:]
outpath=os.path.join(tmp_img_dl_dir,file_img)
mkpath(outpath)
#print "Downloading image from {} to {}.".format(url,outpath)
try:
r = requests.get(url, stream=True, timeout=imagedltimeout)
if r.status_code == 200:
with open(outpath, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return outpath
except Exception as inst:
if logf:
logf.write("Download failed for img that should be saved at {} from url {}.\n".format(outpath,url))
else:
print "Download failed for img that should be saved at {} from url {}.".format(outpath,url)
print inst
return None
示例8: salva_audio_ura
def salva_audio_ura(form):
import shutil
try:
caminho_raiz = '/aldeia/etc/asterisk/cliente/'
if not os.path.exists(caminho_raiz):
os.system('sudo mkdir ' + caminho_raiz)
os.system('sudo chown -R www-data:www-data /aldeia/audio/')
os.system('sudo ln -s /aldeia/etc/asterisk/cliente/ /var/www/')
id_edit = request.vars['id_edit']
sql_busca = db(db.f_audios.id == id_edit).select()
if len(sql_busca) > 0:
path_wav = caminho_raiz+ 'wav_'+ sql_busca[0]['nome'].replace(' ','_').replace('.','_').lower() + '.wav'
path_sln = caminho_raiz+ sql_busca[0]['nome'].replace(' ','_').replace('.','_').lower() + '.sln'
if os.path.isfile(path_wav):
os.remove(path_wav)
if os.path.isfile(path_sln):
os.remove(path_sln)
filename='wav_' + request.vars['nome'].replace(' ','_').replace('.','_').lower() + '.wav'
file=request.vars['dados_audio'].file
shutil.copyfileobj(file,open(caminho_raiz+filename,'wb'))
convert_audio = 'sudo sox '+caminho_raiz +'wav_' + request.vars['nome'].replace(' ','_').replace('.','_').lower() + '.wav' +' -r 8000 -c 1 '+caminho_raiz +request.vars['nome'].replace(' ','_').replace('.','_').lower()+'.sln'
os.system(convert_audio)
os.system('sudo chmod +x /aldeia/etc/asterisk/cliente/*')
os.system('sudo chown -R www-data:www-data /aldeia/etc/asterisk/cliente/*')
form.vars['caminho'] = request.vars['nome'].replace(' ','_').replace('.','_').lower() + '.sln'
return 'OK'
except shutil.Error as erro:
return erro
示例9: __init__
def __init__(self, command, src, dst):
"""Execute 'command' with src as stdin and writing to stream
dst. If either stream is not a fileno() stream, temporary files
will be used as required.
Either stream may be None if input or output is not required.
Call the wait() method to wait for the command to finish.
'command' may be a string (passed to os.system) or a list (os.execvp).
"""
if src is not None and not hasattr(src, 'fileno'):
import shutil
new = _Tmp()
src.seek(0)
shutil.copyfileobj(src, new)
src = new
Process.__init__(self)
self.command = command
self.dst = dst
self.src = src
self.tmp_stream = None
self.callback = None
self.killed = 0
self.errors = ""
self.done = False # bool or exception
self.waiting = False
示例10: get_images_from_urls
def get_images_from_urls(id_url_generator, max_count=5, output_directory="images/raw"):
"""Download JPEG images from a generator of image IDs and urls.
Files are saved to `output_directory`, named according to their ID.
Args:
id_url_generator (generator): Pairs of strings (id, url).
max_count (int): The maximum number of pictures to download. This may
not be the same as the number of images actually downloaded, if the
Flickr API returns duplicate images or invalid responses.
output_directory (str): An existing folder to save images to. Does not
include a trailing slash.
"""
ensure_directory(output_directory)
already_downloaded = image_filenames_as_dict(output_directory)
i = 1
with requests.Session() as s:
for uid, url in id_url_generator:
if uid in already_downloaded:
print "{}: Already downloaded {}".format(i, uid)
else:
print "{}: Downloading {}".format(i, url)
response = s.get(url, stream=True)
if response.status_code == 200 and response.headers["Content-Type"] == "image/jpeg":
filename = "{}/{}.jpeg".format(output_directory, uid)
with open(filename, "wb") as out_file:
shutil.copyfileobj(response.raw, out_file)
already_downloaded[uid] = filename
if i < max_count:
i += 1
else:
break
示例11: download
def download(self, url, fileName=None):
if os.path.isfile(fileName) and not self.force_download:
print 'skipping (already downloaded)'
return
def getFileName(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info()['Content-Disposition'].split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
if filename: return filename
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlparse.urlsplit(openUrl.url)[2])
try:
r = urllib2.urlopen(urllib2.Request(url))
except Exception as err:
print 'Download failed > ', url
return
try:
fileName = fileName or getFileName(url,r)
with open(fileName, 'wb') as f:
shutil.copyfileobj(r,f)
finally:
r.close()
示例12: get_matrix_filename
def get_matrix_filename(series_id, platform_id):
filenames = list(matrix_filenames(series_id, platform_id))
mirror_filenames = (os.path.join(conf.SERIES_MATRIX_MIRROR, filename) for filename in filenames)
mirror_filename = first(filename for filename in mirror_filenames if os.path.isfile(filename))
if mirror_filename:
return mirror_filename
for filename in filenames:
print 'Loading URL', conf.SERIES_MATRIX_URL + filename, '...'
try:
res = urllib2.urlopen(conf.SERIES_MATRIX_URL + filename)
except urllib2.URLError:
pass
else:
mirror_filename = os.path.join(conf.SERIES_MATRIX_MIRROR, filename)
print 'Cache to', mirror_filename
directory = os.path.dirname(mirror_filename)
if not os.path.exists(directory):
os.makedirs(directory)
with open(mirror_filename, 'wb') as f:
shutil.copyfileobj(res, f)
return mirror_filename
raise LookupError("Can't find matrix file for series %s, platform %s"
% (series_id, platform_id))
示例13: copyFile
def copyFile(self, newpath):
#Walk the directory tree backwards until "spec" is found
relpath = ''
path = [self.fpath, '']
while True:
path = os.path.split(path[0])
relpath = os.path.join(path[1], relpath)
if "include" in relpath:
break
newpath = os.path.join(newpath, relpath)
#Create the directory if it doesnt exist
if not os.path.exists(newpath):
os.makedirs(newpath)
print "Creating Dir: " + newpath
#Copy the file and fix the filename if its a Tfile
if not self.isT():
#If its a QoS file eg. DomainParticpantQos.hpp then replace it with TEntityQos
if self.isQos():
print "Copying QoS file " + self.fname + " to " + newpath
nf = open(os.path.join(newpath, self.fname), 'w')
shutil.copyfileobj(self.fixQoSFileContents(), nf)
else:
print "Copying plain file " + self.fname + " to " + newpath
shutil.copy(os.path.join(self.fpath, self.fname), os.path.join(newpath, self.fname))
else:
nf = open(os.path.join(newpath, self.fixFilename()), 'w')
shutil.copyfileobj(self.fixFileContents(), nf)
print "Copying T file " + self.fname + " to " + newpath + self.fixFilename()
示例14: download
def download(url, message_id, fileName=None):
def getFileName(url,openUrl):
if not os.path.exists(IMAGE_DIR):
os.makedirs(IMAGE_DIR)
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info()['Content-Disposition'].split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
if filename: return filename
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlparse.urlsplit(openUrl.url)[2])
r = urllib2.urlopen(urllib2.Request(url))
fileName = fileName or getFileName(url,r)
if not os.path.exists(IMAGE_DIR + fileName):
r = urllib2.urlopen(urllib2.Request(url))
try:
with open(IMAGE_DIR + fileName, 'wb') as f:
shutil.copyfileobj(r, f)
finally:
r.close()
downloadedFile = message_id + '_' + fileName
shutil.copyfile(IMAGE_DIR + fileName, IMAGE_DIR + downloadedFile)
return downloadedFile
示例15: install_spec
def install_spec(cli_args, kwargs, abstract_spec, spec):
"""Do the actual installation."""
# handle active environment, if any
def install(spec, kwargs):
env = ev.get_env(cli_args, 'install', required=False)
if env:
env.install(abstract_spec, spec, **kwargs)
env.write()
else:
spec.package.do_install(**kwargs)
try:
if cli_args.things_to_install == 'dependencies':
# Install dependencies as-if they were installed
# for root (explicit=False in the DB)
kwargs['explicit'] = False
for s in spec.dependencies():
install(s, kwargs)
else:
kwargs['explicit'] = True
install(spec, kwargs)
except spack.build_environment.InstallError as e:
if cli_args.show_log_on_error:
e.print_context()
if not os.path.exists(e.pkg.build_log_path):
tty.error("'spack install' created no log.")
else:
sys.stderr.write('Full build log:\n')
with open(e.pkg.build_log_path) as log:
shutil.copyfileobj(log, sys.stderr)
raise