def execute(my): web = my.get_web() keys = web.get_form_keys() file_name = my.kwargs.get("file_name") # process and get the uploaded files dir = Environment.get_upload_dir() license_file = "%s/%s" % (dir, file_name) if not os.path.exists(license_file): raise TacticException("Error retrieving the license file in [%s]" % license_file) std_name = 'tactic-license.xml' head, file_name = os.path.split(license_file) # no restrictions for license file #if file_name != std_name: # raise TacticException("License file name should be named tactic-license.xml. The file given is [%s]" %file_name) license_dir = Environment.get_license_dir() current_license = "%s/%s" % (license_dir, std_name) if os.path.exists(current_license): FileUndo.remove(current_license) FileUndo.move(license_file, current_license) my.add_description('Renewed license file') security = Environment.get_security() security.reread_license()
def execute(my): search = Search(Submission) search.set_show_retired(True) submissions = search.get_sobjects() for submission in submissions: snapshot = Snapshot.get_latest_by_sobject(submission, "publish") paths = snapshot.get_all_lib_paths() bins = submission.get_bins() if not bins: print "Bin for submissin [%s] does not exist" % submission.get_id() continue bin = bins[0] code = bin.get_code() type = bin.get_value("type") label = bin.get_value("label") for path in paths: if not os.path.exists(path): print "WARNING: path '%s' does not exist" % path continue dirname = os.path.dirname(path) basename = os.path.basename(path) new_dirname = "%s/%s/%s/%s" % (dirname,type,label,code) if not os.path.exists(new_dirname): os.makedirs(new_dirname) new_path = "%s/%s" % (new_dirname, basename) print new_path FileUndo.move(path, new_path)
def execute(self): web = self.get_web() keys = web.get_form_keys() file_name = self.kwargs.get("file_name") # process and get the uploaded files dir = Environment.get_upload_dir() license_file = "%s/%s" % (dir, file_name) if not os.path.exists(license_file): raise TacticException("Error retrieving the license file in [%s]"%license_file) std_name = 'tactic-license.xml' head, file_name = os.path.split(license_file) # no restrictions for license file #if file_name != std_name: # raise TacticException("License file name should be named tactic-license.xml. The file given is [%s]" %file_name) license_dir = Environment.get_license_dir() current_license = "%s/%s" %(license_dir, std_name) if os.path.exists(current_license): FileUndo.remove(current_license) FileUndo.move(license_file, current_license) self.add_description('Renewed license file') security = Environment.get_security() security.reread_license()
def execute(self): mode = self.kwargs.get("mode") # modes are either naming based or file object based #mode = "naming" #mode = "file" assert mode in ['naming', 'file'] search_type = self.kwargs.get("search_type") search_keys = self.kwargs.get("search_keys") assert search_type or search_keys if search_type: print "@SOBJECT(%s)" % search_type sobjects = Search.eval("@SOBJECT(%s)" % search_type) else: sobjects = Search.get_by_search_keys(search_keys) if not sobjects: print "No sobjects to process" return snapshots = Search.eval("@SOBJECT(sthpw/snapshot)", sobjects) # set an arbitrary limit for now limit = 1000 base_dir = Environment.get_asset_dir() num_found = 0 errors = [] for i, snapshot in enumerate(snapshots): if i > limit: break print "Processing: %s of %s" % (i, len(snapshots)) file_types = snapshot.get_all_file_types() for file_type in file_types: files = snapshot.get_files_by_type(file_type) # FIXME: not sure why we have this limitation, although it is # a pretty rare occurance if len(files) > 1: raise Exception( "More than one file with type [%s] in snapshot [%s]" % (file_type, snapshot.get_code())) file = files[0] file_name = file.get_value("file_name") # check-in dir is not subject to changes in asset_dir checkin_dir = file.get_value("checkin_dir") old_path = "%s/%s" % (checkin_dir, file_name) try: if mode == "naming": # preallocated path is used to align a file to the # current naming convention # FIXME: # there is a behavior in naming to add "_web" and # "_icon" on the file name. In this case, we don't # want that, so ask for main if file_type in ['icon', 'web']: path = snapshot.get_preallocated_path( 'main', file_name) else: path = snapshot.get_preallocated_path( file_type, file_name) elif mode == "file": # relative_dir is used to align a file to the current # place pointed to by the "file" object relative_dir = file.get_value("relative_dir") path = "%s/%s/%s" % (base_dir, relative_dir, file_name) else: raise Exception("Invalid mode [%s]" % mode) except Exception as e: error = "Snapshot [%s] has an error getting preallocated path: [%s]" % ( snapshot.get_code(), e.message) errors.append(error) continue if old_path == path: continue num_found += 1 print("snapshot: ", snapshot.get_value("code")) print("old: ", old_path) print("new: ", path) print("-" * 20) new_dir = os.path.dirname(path) new_filename = os.path.basename(path) new_relative_dir = new_dir.replace(base_dir, '') new_relative_dir = new_relative_dir.strip("/") xml = snapshot.get_xml_value("snapshot") node = xml.get_node("snapshot/file[@type='%s']" % file_type) Xml.set_attribute(node, "name", new_filename) # update all of the file file.set_value("file_name", new_filename) file.set_value("checkin_dir", new_dir) file.set_value("relative_dir", new_relative_dir) snapshot.set_value("snapshot", xml.to_string()) dirname = os.path.dirname(path) if not os.path.exists(dirname): FileUndo.mkdir(dirname) exists = False if os.path.islink(old_path): exists = os.path.lexists(old_path) else: exists = os.path.exists(old_path) if not exists: print '... old does not exist' continue FileUndo.move(old_path, path) file.commit() snapshot.commit() # try to remove the old folder (if it's empty, it will be removed) dirname = os.path.dirname(old_path) while 1: try: os.rmdir(dirname) dirname = os.path.dirname(dirname) except: break if errors: print "Errors:" for error in errors: print error print "-" * 20 print "Found %s of %s snapshots which have paths different from naming" % ( num_found, len(snapshots))
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)
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)
def execute(my): mode = my.kwargs.get("mode") # modes are either naming based or file object based #mode = "naming" #mode = "file" assert mode in ['naming', 'file'] search_type = my.kwargs.get("search_type") search_keys = my.kwargs.get("search_keys") assert search_type or search_keys if search_type: print "@SOBJECT(%s)" % search_type sobjects = Search.eval("@SOBJECT(%s)" % search_type) else: sobjects = Search.get_by_search_keys(search_keys) if not sobjects: print "No sobjects to process" return snapshots = Search.eval("@SOBJECT(sthpw/snapshot)", sobjects) # set an arbitrary limit for now limit = 1000 base_dir = Environment.get_asset_dir() num_found = 0 errors = [] for i, snapshot in enumerate(snapshots): if i > limit: break print "Processing: %s of %s" % (i, len(snapshots) ) file_types = snapshot.get_all_file_types() for file_type in file_types: files = snapshot.get_files_by_type(file_type) # FIXME: not sure why we have this limitation, although it is # a pretty rare occurance if len(files) > 1: raise Exception("More than one file with type [%s] in snapshot [%s]" % (file_type, snapshot.get_code()) ) file = files[0] file_name = file.get_value("file_name") # check-in dir is not subject to changes in asset_dir checkin_dir = file.get_value("checkin_dir") old_path = "%s/%s" % ( checkin_dir, file_name ) try: if mode == "naming": # preallocated path is used to align a file to the # current naming convention # FIXME: # there is a behavior in naming to add "_web" and # "_icon" on the file name. In this case, we don't # want that, so ask for main if file_type in ['icon', 'web']: path = snapshot.get_preallocated_path('main', file_name) else: path = snapshot.get_preallocated_path(file_type, file_name) elif mode == "file": # relative_dir is used to align a file to the current # place pointed to by the "file" object relative_dir = file.get_value("relative_dir") path = "%s/%s/%s" % ( base_dir, relative_dir, file_name ) else: raise Exception("Invalid mode [%s]" % mode) except Exception, e: error = "Snapshot [%s] has an error getting preallocated path: [%s]" % (snapshot.get_code(), e.message ) errors.append(error) continue if old_path == path: continue num_found += 1 print "snapshot: ", snapshot.get_value("code") print "old: ", old_path print "new: ", path print "-"*20 new_dir = os.path.dirname(path) new_filename = os.path.basename(path) new_relative_dir = new_dir.replace(base_dir, '') new_relative_dir = new_relative_dir.strip("/") xml = snapshot.get_xml_value("snapshot") node = xml.get_node("snapshot/file[@type='%s']" % file_type) Xml.set_attribute(node, "name", new_filename) # update all of the file file.set_value("file_name", new_filename) file.set_value("checkin_dir", new_dir) file.set_value("relative_dir", new_relative_dir) snapshot.set_value("snapshot", xml.to_string() ) dirname = os.path.dirname(path) if not os.path.exists(dirname): FileUndo.mkdir(dirname) exists = False if os.path.islink(old_path): exists = os.path.lexists(old_path) else: exists = os.path.exists(old_path) if not exists: print '... old does not exist' continue FileUndo.move(old_path, path) file.commit() snapshot.commit() # try to remove the old folder (if it's empty, it will be removed) dirname = os.path.dirname(old_path) while 1: try: os.rmdir(dirname) dirname = os.path.dirname(dirname) except: break