def X_db_file_exists(cnx, in_dict): # , vol_id): gd = GetD(in_dict) gd['vol_id'] = in_dict['vol_id'].encode('utf8') select_query = ( "select 1 from files " "where vol_id = %(vol_id)r and folder_id = %(folder_id)s " "and file_name = %(file_name)r and file_mod_date = %(file_mod_date)r " ) # mysql.connector.errors.ProgrammingError: 1064 (42000): # You have an error in your SQL syntax; check the manual that corresponds # to your MySQL server version for the right syntax to use # near ''vol0003' and folder_id = 2 and # file_name = 'TV Show\xe2\x80\x94single' and file' at line 1 cursor = cnx.cursor() GPR.print_it(select_query % gd, 4) cursor.execute( select_query % gd ) r = [z for z in cursor] cursor.close() if r == [(1,)]: return True elif r == []: return False else: print "db_file_exists", r raise TypeError , ("db_file_exists says %r" % r)
def db_select_vol_idz(cnx, d): gd = GetD(d) select_query = ( "select vol_id from files " "where folder_id = 1 " "and file_name = %(file_name)s and file_create_date = %(file_create_date)s " ) cursor = cnx.cursor() GPR.print_it(select_query % gd, 4) cursor.execute( select_query , gd ) r = [z for z in cursor] if r == []: vol_id = None # print "vol_id is:", vol_id else: vol_id = r[0][0] # could have multiple results cursor.close() return vol_id
def vol_id_gen(cnx, in_gen): """processor for to find, store and then add vol_id to each item as it goes by""" # try: # select vol_id from volumes_uuid (based on volume uuid) # select vol_id from files (based on volume name and create date) # do a "test" insert of the volume entry just to get the autocreated vol_id back. (then act like it didn't happen :) local_vol_id = None for in_dict in in_gen: if local_vol_id == None: local_vol_id = db_get_vol_id(cnx, in_dict, local_vol_id) GPR.print_it2("vol_id_gen", "volume = %s, vol_id = %r" % (in_dict[NSURLNameKey], local_vol_id), verbose_level_threshold=2) in_dict['vol_id'] = local_vol_id yield in_dict GPR.print_it("end vol_id_gen.", verbose_level_threshold=3)
def Z_db_query_folder(cnx, item_dict): """get database contents of item as folder.""" vol_id = item_dict['vol_id'].encode('utf8') this_folder_id = item_dict['NSFileSystemFileNumber'] # in sql format "%r" is good because it provides quotes but will also insert u'vol0001' # so either: str() the text (or utf8 encode it?) or format it like '%s' (but this is dumb quotes) sql = "select vol_id, folder_id, file_name, file_id, file_mod_date from files "+\ "where vol_id = %r and folder_id = %d " data = (vol_id, this_folder_id ) cur = cnx.cursor(cursor_class=MySQLCursorDict) GPR.print_it( sql % data, verbose_level_threshold=2) cur.execute( sql % data ) cur.set_rel_name(in_rel_name="files_del") # need name at relation init time (ie, inside cursor fetch) r = cur.fetchall() cur.close() return r
def do_args(args, options): """do_args is the high-level, self-contained routine most like the command-line invocation""" cnx = db_connect() required_fields = ['vol_id', 'folder_id', 'file_name', 'file_id', 'file_size', 'file_create_date', 'file_mod_date', 'file_uti' ] try: for basepath in args: for arg_dict in do_arg_gen(basepath, cnx, options): # depth 0 should be a fully-realized level if (arg_dict['depth'] < 0): if not ('directory_is_up_to_date' in arg_dict) or not arg_dict['directory_is_up_to_date']: sql_dict = GetDR(arg_dict, required_fields) sql_dict['file_mod_date'] = "'1970-01-01 00:00:00'" # args are escaped and quoted at this point add_file_sql = ("insert into files " "(vol_id, folder_id, file_name, file_id, file_size, file_create_date, file_mod_date, file_uti) " "values " "( %(vol_id)s, %(folder_id)s, %(file_name)s, %(file_id)s, %(file_size)s, %(file_create_date)s, " "%(file_mod_date)s, %(file_uti)s ) " ) # execute_update_query(cnx, add_file_sql , sql_dict, label='(depth < 0)', verbose_level_threshold=3 ) db_execute_sql(cnx, add_file_sql % sql_dict, label='(depth < 0)', verbose_level_threshold=2) GPR.pr7z( arg_dict ) elif 'sql_action' in arg_dict: if arg_dict['sql_action'] in ["update_directory", "insert"]: # technically, we are updating (ie, completing) the directory # before we do the directory entries? consistency problem if we fail? add_file_sql = ("insert into files " "(vol_id, folder_id, file_name, file_id, file_size, file_create_date, file_mod_date, file_uti) " "values " "( %(vol_id)s, %(folder_id)s, %(file_name)s, %(file_id)s, %(file_size)s, %(file_create_date)s, " "%(file_mod_date)s, %(file_uti)s ) " ) # execute_update_query(cnx, add_file_sql , sql_dict, label=arg_dict['sql_action'], verbose_level_threshold=2) # sql and dict are "%"'ed inside function db_execute(cnx, add_file_sql, arg_dict, required_fields, label="do_args" + arg_dict['sql_action'], verbose_level_threshold=2) else: sql_dict = GetDR(arg_dict, required_fields) GPR.print_it(add_file_sql % sql_dict, 3) GPR.pr7z( arg_dict ) elif (arg_dict['depth'] == 0): GPR.pr7z( arg_dict , verbose_level_threshold=1) else: GPR.pr7z( arg_dict , verbose_level_threshold=2) except MyError, err: print err.description
def files_generator(basepath, options): """a generator which yields all files (as file_dicts) including volume, superfolder(s), basepath, and then all subfiles (subject to depth_limit and enumerator options). """ GPR.print_it25("files_generator", basepath, 2) superfolders_list = [] basepath_url = NSURL.fileURLWithPath_(basepath) # begin loop going upwards url = NSURL.fileURLWithPath_(basepath) while True: d1 = GetURLValues(url, enumeratorURLKeys) superfolders_list.insert(0,d1) if d1[NSURLIsVolumeKey]: break # go "upwards" one level (towards volume) url = url.URLByDeletingLastPathComponent() GPR.print_superfolders_list("volume, superfolder(s)", superfolders_list, 4) # now go back down, yielding dict objects at each step: n = len(superfolders_list) for i, superfolder_dict in enumerate(superfolders_list): superfolder_dict['depth'] = i+1-n yield superfolder_dict # last dict in superfolder list is the basepath_dict basepath_dict = superfolder_dict item_is_package = is_item_a_package(basepath_url) if basepath_dict[NSURLIsDirectoryKey] and item_is_package and not options.scan_packages: GPR.print_it("\nbasepath is a directory and a package but we're not scanning packages.\n", 1) return # we've yielded basepath above, don't enumerate if basepath is not a directory (or package and we want packages) if basepath_dict[NSURLIsDirectoryKey]: enumeratorOptionKeys = 0L if not options.scan_packages: enumeratorOptionKeys |= NSDirectoryEnumerationSkipsPackageDescendants if not options.scan_hidden_files: enumeratorOptionKeys |= NSDirectoryEnumerationSkipsHiddenFiles enumerator = sharedFM.enumeratorAtURL_includingPropertiesForKeys_options_errorHandler_( basepath_url, enumeratorURLKeys, enumeratorOptionKeys, error_handler_for_enumerator ) for url in enumerator: item_dict = GetURLValues(url, enumeratorURLKeys) depth = enumerator.level() item_dict['depth'] = depth if options.depth_limit and (depth >= options.depth_limit-1): enumerator.skipDescendants() yield item_dict GPR.print_it2("end files_generator", basepath, verbose_level_threshold=3)