def check_paths(file_path, file_range): ''' check existence of files. this expects a FileRange object''' expanded = FileGroup.expand_paths(file_path, file_range) for expand in expanded: if not System().exists(expand): raise FileException("File '%s' does not exist!" % expand) return expanded
def get_render_dir(my): ticket = Environment.get_security().get_ticket_key() tmpdir = Environment.get_tmp_dir() render_dir = "%s/temp/%s" % (tmpdir, ticket) System().makedirs(render_dir) return render_dir
def download(my, url, to_dir="", skip_if_exists=False): import urllib, urllib2 filename = os.path.basename(url) # download to the current project if not to_dir: to_dir = my.app.get_workspace_dir() # for now, put everything in scenes to_dir = "%s/scenes" % to_dir # make sure the directory exists System().makedirs(to_dir) to_path = "%s/%s" % (to_dir, filename) # check if this file is already downloaded. if so, skip if skip_if_exists and os.path.exists(to_path): print "skipping '%s', already exists" % to_path return to_path file = open(to_path, "wb") req = urllib2.Request(url) try: resp = urllib2.urlopen(req) file.write(resp.read()) file.close() except urllib2.URLError, e: raise Exception('%s - %s' % (e, url))
def get_upload_dir(my, ticket): '''simple function that returns a temporary path that files can be copied to''' tmp_dir = Environment.get_tmp_dir() upload_dir = "%s/upload/%s" % (tmp_dir, ticket) # TODO: upload_dir is not System().makedirs(upload_dir) return upload_dir
def makedirs(dir, mode=None): '''wrapper to mkdirs in case it ever needs to be overridden''' print "DEPRECATED: use System().makedirs()" return System().makedirs(dir, mode)
def handle_system_commands(my, snapshot, files, file_objects, mode, md5s, source_paths=[], file_sizes=[]): '''move the tmp files in the appropriate directory''' # if mode is local then nothing happens here if mode == 'local': return sobject = snapshot.get_sobject() # inplace mode does not move the file. It just registers the file # object if mode == 'inplace': for i, file in enumerate(files): file_object = file_objects[i] to_name = file_object.get_full_file_name() to_path = file # This is handled in create_file_types #file_type = snapshot.get_type_by_file_name(to_name) #file_object.set_value('type', file_type) if not os.path.isdir(to_path): md5_checksum = None if md5s: md5_checksum = md5s[i] if not md5_checksum: md5_checksum = File.get_md5(to_path) if md5_checksum: file_object.set_value("md5", md5_checksum) file_object.commit(triggers=False) return for i, file in enumerate(files): file_object = file_objects[i] to_name = file_object.get_full_file_name() file_type = snapshot.get_type_by_file_name(to_name) lib_dir = snapshot.get_lib_dir(file_type=file_type, file_object=file_object) # it should have been created in postprocess_snapshot System().makedirs(lib_dir) to_path = "%s/%s" % (lib_dir, to_name) #print "path: ", i, files[i] #print to_path, os.path.exists(to_path) # first make sure that the to path does not exist, if so, just skip if os.path.exists(to_path) and mode not in [ 'inplace', 'preallocate' ]: raise CheckinException('This path [%s] already exists' % to_path) # add the file try: # inplace undo used to not touch the file, # now it will be moved to cache on undo io_action = True if mode in ['preallocate']: io_action = False if mode == 'move': FileUndo.move(source_paths[i], to_path) #elif mode == 'copy': # was free_copy #FileUndo.create( source_paths[i], to_path, io_action=io_action ) # make it look like the files was created in the repository else: # mode ='create' md5 = file_object.get_value("md5") st_size = file_object.get_value("st_size") rel_dir = file_object.get_value("relative_dir") if mode == 'copy': io_action = 'copy' src_path = source_paths[i] else: src_path = files[i] file_name = to_name rel_path = "%s/%s" % (rel_dir, file_name) FileUndo.create(src_path, to_path, io_action=io_action, extra={ "md5": md5, "st_size": st_size, "rel_path": rel_path }) except IOError, e: raise CheckinException('IO Error occurred. %s' % e.__str__()) # check to see that the file exists. if not os.path.exists(to_path): if mode in ["inplace", "preallocate"]: raise CheckinException("File not found in repo at [%s]" % to_path) else: raise CheckinException("Failed move [%s] to [%s]" % \ (files[i], to_path) ) file_object.set_value('type', file_type) if not os.path.isdir(to_path): md5_checksum = None if md5s: md5_checksum = md5s[i] if not md5_checksum: md5_checksum = File.get_md5(to_path) if md5_checksum: file_object.set_value("md5", md5_checksum) file_object.commit(triggers=False)