def __main(self, __bckconf): '''The main for the CheckFileList class''' if 'sha512' in __bckconf and __bckconf['sha512'] != None: __hashtype = 'sha512' with open(__bckconf['files_list'], 'rb') as __conf: __realhash = get_hash(__conf, __hashtype) if __realhash != __bckconf['sha512']: print('The list of files {} should have a {} hash sum of {}. Current value: {}'.format(__bckconf['files_list'], __hashtype, __bckconf['sha512'], __realhash)) sys.exit(1)
def _get_list_hash(self, __listpath): '''Get the hash sum of the list of files''' try: with open(__listpath, 'rb') as __file: __listhash = get_hash(__file, 'sha512') except (OSError, IOError) as __msg: print(__msg) sys.exit(1) return __listhash
def __main(self, __bckconf): '''The main for the CheckFileList class''' if 'sha512' in __bckconf and __bckconf['sha512'] != None: __hashtype = 'sha512' with open(__bckconf['files_list'], 'rb') as __conf: __realhash = get_hash(__conf, __hashtype) if __realhash != __bckconf['sha512']: print( 'The list of files {} should have a {} hash sum of {}. Current value: {}' .format(__bckconf['files_list'], __hashtype, __bckconf['sha512'], __realhash)) sys.exit(1)
def __init__(self, __genparams): '''The constructor for the GenerateListForBzip2 class''' __arcpath = __genparams['arcpath'] __delimiter = __genparams['delimiter'] self._genfull = __genparams['genfull'] self.__listoutput = __genparams['listoutput'] self.__confoutput = __genparams['confoutput'] self.__fulloutput = __genparams['fulloutput'] self.__getallhashes = __genparams['getallhashes'] self.__hashtype = __genparams['hashtype'] self.__parsingexceptions = __genparams['parsingexceptions'] self.__confname = __genparams['confname'] __listoffiles = ['[files]\n'] __filetype = 'f' __filehash = get_hash(bz2.BZ2File(__arcpath, 'r'), 'md5') if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} type{delimiter}{value} md5{delimiter}{value}\n'.format( value='{}', delimiter=__delimiter) else: __onelinewithhash = '{value}{delimiter} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self.__hashtype, delimiter=__delimiter) __listoffiles.append( __onelinewithhash.format( os.path.split(__arcpath)[-1][:-4], __filetype, __filehash)) else: if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch( os.path.split(__arcpath)[-1][:-4], __file): __filehash = get_hash(bz2.BZ2File(__arcpath, 'r'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self.__parsingexceptions[__file], delimiter=__delimiter) __listoffiles.append( __onelinewithhash.format( os.path.split(__arcpath)[-1][:-4], __filetype, __filehash)) else: __onelinewithouthash = '{value}{delimiter} type{delimiter}{value}\n'.format( value='{}', delimiter=__delimiter) __listoffiles.append( __onelinewithouthash.format( os.path.split(__arcpath)[-1][:-4], __filetype)) else: __onelinewithouthash = '{value}{delimiter} type{delimiter}{value}\n'.format( value='{}', delimiter=__delimiter) __listoffiles.append( __onelinewithouthash.format( os.path.split(__arcpath)[-1][:-4], __filetype)) # define the flexible file list path __arcwithext = os.path.split(''.join([__arcpath[:-3], 'list']))[1] if self.__listoutput: if self.__confname: # --gen-list and --output-list-dir and --configuration-name __arclistpath = os.path.join( self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-dir __arclistpath = os.path.join(self.__listoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-list and --output-conf-and-list-dir and --configuration-name __arclistpath = os.path.join( self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-conf-and-list-dir __arclistpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-list only if self.__confname: __arc = os.path.dirname(__arcpath) __arclistpath = os.path.join( __arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([__arcpath[:-3], 'list']) # call the method to write information in a file __listconfinfo = { 'arclistpath': __arclistpath, 'listoffiles': __listoffiles } self.__lci = __listconfinfo self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) # define the flexible configuration file path __arcwithext = os.path.split(''.join([__arcpath[:-3], 'conf']))[1] if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join( self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-full and --output-conf-and-list-dir and --configuration-name __arcconfpath = os.path.join( self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-and-list-dir __arcconfpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-full only if self.__confname: __arc = os.path.dirname(__arcpath) __arcconfpath = os.path.join( __arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = ''.join([__arcpath[:-3], 'conf']) # user-define name of the archive if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(__arcpath[:-4]) __confinfo = { 'arcname': __arcname, 'arcpath': __arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __listconfinfo['arclistpath'], 'arctype': 'archive', 'sha512': __listhashsum } self.__ci = __confinfo self._generate_conf(__confinfo)
def __main(self, __zip): '''Main of the GenerateListForZip class''' __arcstat = os.stat(self.__arcpath) __listoffiles = [ '[archive]\nmtime{} {}\n\n[files]\n'.format( self.__delimiter, __arcstat.st_mtime) ] __oneline = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} md5{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) else: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self.__hashtype, delimiter=self.__delimiter) else: __onelinewithouthash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) __onelinenoexternalattr = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mtime{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) __crcerror = __zip.testzip() if __crcerror: logging.warning('{} has at least one file corrupted:{}'.format( self.__arcpath, __crcerror)) else: __zipinfo = __zip.infolist() for __fileinfo in __zipinfo: __fileinfo.filename = self._normalize_path(__fileinfo.filename) __uid, __gid = self.__extract_uid_gid(__fileinfo) # check if external_attr is available if __fileinfo.external_attr != 0: __type = self.__translate_type( __fileinfo.external_attr >> 16) __mode = oct(stat.S_IMODE( (__fileinfo.external_attr >> 16))).split('o')[-1] # Prepare a timestamp for the ctime object __dt = __fileinfo.date_time try: __mtime = float( datetime.datetime(__dt[0], __dt[1], __dt[2], __dt[3], __dt[4], __dt[5]).timestamp()) except ValueError as __msg: __warn = 'Issue with timestamp while controlling {} in {}'.format( _fileinfo.filename, _cfgvalues['path']) logging.warning(__warn) if __fileinfo.external_attr != 0 and __type == 'f': if self.__getallhashes: if not self.__hashtype: __hash = get_hash( __zip.open(__fileinfo.filename, 'r'), 'md5') else: __hash = get_hash( __zip.open(__fileinfo.filename, 'r'), self.__hashtype) __listoffiles.append( __onelinewithhash.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime, __hash)) else: # check if there are exceptions while parsing if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch(__fileinfo.filename, __file): __hash = get_hash( __zip.open(__fileinfo.filename, 'r'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self. __parsingexceptions[__file], delimiter=self.__delimiter) __listoffiles.append( __onelinewithhash.format( __fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime, __hash)) else: # we use exceptions-file option but the file is not concerned by an exception __listoffiles.append( __onelinewithouthash.format( __fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) else: # we don't use the --exceptions-file option __listoffiles.append( __onelinewithouthash.format( __fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) elif __fileinfo.external_attr != 0 and __type == 'd': __listoffiles.append( __oneline.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) else: __listoffiles.append( __onelinenoexternalattr.format( __fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mtime)) # define the flexible file list path __arcwithext = os.path.split(''.join([self.__arcpath[:-3], 'list']))[1] if self.__listoutput: if self.__confname: # --gen-list and --output-list-dir and --configuration-name __arclistpath = os.path.join( self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-dir __arclistpath = os.path.join(self.__listoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-list and --output-conf-and-list-dir and --configuration-name __arclistpath = os.path.join( self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-conf-and-list-dir __arclistpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-list if self.__confname: __arc = os.path.dirname(self.__arcpath) __arclistpath = os.path.join( __arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([self.__arcpath[:-3], 'list']) __listconfinfo = { 'arclistpath': __arclistpath, 'listoffiles': __listoffiles } # call the method to write the list of files inside the archive self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) # define the flexible configuration file path __arcwithext = os.path.split(''.join([self.__arcpath[:-3], 'conf']))[1] if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join( self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-full and --output-conf-and-list-dir and --configuration-name __arcconfpath = os.path.join( self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-and-list-dir __arcconfpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-full only if self.__confname: __arc = os.path.dirname(self.__arcpath) __arcconfpath = os.path.join( __arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = ''.join([self.__arcpath[:-3], 'conf']) # name of the archive in the configuration file if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(self.__arcpath[:-4]) __confinfo = { 'arcname': __arcname, 'arcpath': self.__arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __listconfinfo['arclistpath'], 'arctype': 'archive', 'sha512': __listhashsum } self._generate_conf(__confinfo)
def __init__(self, __genparams): '''The constructor for the GenerateListForTree class''' __arcpath = __genparams['arcpath'] __delimiter = __genparams['delimiter'] self._genfull = __genparams['genfull'] self.__listoutput = __genparams['listoutput'] self.__confoutput = __genparams['confoutput'] self.__fulloutput = __genparams['fulloutput'] self.__getallhashes = __genparams['getallhashes'] self.__hashtype = __genparams['hashtype'] self.__parsingexceptions = __genparams['parsingexceptions'] self.__confname = __genparams['confname'] __listoffiles = ['[files]\n'] __oneline = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} md5{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) else: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__hashtype, delimiter=__delimiter) else: __onelinewithouthash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) # we also need parameters for symbolic links __onelinewithtarget = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} target{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) for __dirpath, __dirnames, __filenames, in os.walk(__arcpath): # ignoring the uppest directory if os.path.relpath(__dirpath, __arcpath) != '.': # studying directories __dirinfo = os.lstat(__dirpath) __dirmode = oct(stat.S_IMODE(__dirinfo.st_mode)).split('o')[-1] # translate file type in backupchecker intern file type __type = self.__translate_type(__dirinfo.st_mode) # extract file data __listoffiles.append(__oneline.format( os.path.relpath(__dirpath, __arcpath), str(__dirinfo.st_size), str(__dirinfo.st_uid), str(__dirinfo.st_gid), pwd.getpwuid(__dirinfo.st_uid).pw_name, grp.getgrgid(__dirinfo.st_gid).gr_name, __dirmode, __type, str(__dirinfo.st_mtime))) # studying files for __filename in __filenames: __filepath = os.path.join(__dirpath, __filename) __filepath = self._normalize_path(__filepath) self.__fileinfo = os.lstat(__filepath) __filemode = oct(stat.S_IMODE(self.__fileinfo.st_mode)).split('o')[-1] __type = self.__translate_type(self.__fileinfo.st_mode) if __type == 'f': if self.__getallhashes: if not self.__hashtype: # extract hash sum of the file inside the archive __hash = get_hash(open(__filepath, 'rb'), 'md5') else: # extract hash sum of the file inside the archive __hash = get_hash(open(__filepath, 'rb'), self.__hashtype) # extract file data and prepare data __listoffiles.append(__onelinewithhash.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime), __hash)) else: # check if there are exceptions while parsing if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch(os.path.relpath(__filepath, __arcpath), __file): __hash = get_hash(open(__filepath, 'rb'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__parsingexceptions[__file], delimiter=__delimiter) __listoffiles.append(__onelinewithhash.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime), __hash)) else: # we use exceptions-file option but the file is not concerned by an exception __listoffiles.append(__onelinewithouthash.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime))) else: # we don't use the --exceptions-file option __listoffiles.append(__onelinewithouthash.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime))) elif __type == 's': # extract hash sum of the file inside the archive # extract file data and prepare data __listoffiles.append(__onelinewithtarget.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime), os.readlink(__filepath))) else: # if file is not regular file, ignoring its hash sum __listoffiles.append(__oneline.format( os.path.relpath(__filepath, __arcpath), str(self.__fileinfo.st_size), str(self.__fileinfo.st_uid), str(self.__fileinfo.st_gid), pwd.getpwuid(self.__fileinfo.st_uid).pw_name, grp.getgrgid(self.__fileinfo.st_gid).gr_name, __filemode, __type, str(self.__fileinfo.st_mtime))) # include custom paths for output conf files __reparc = os.path.split(__arcpath)[-1] if self.__listoutput: # --gen-list and --output-list-dir and --configuration-name if self.__confname: __arclistpath = os.path.join(self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-dir __arclistpath = os.path.join(self.__listoutput, '.'.join([__reparc, 'list'])) elif self.__fulloutput: if self.__confname: # --gen-list and --output-list-and-conf-dir and --configuration-name __arclistpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-and-conf-dir __arclistpath = os.path.join(self.__fulloutput, '.'.join([__reparc, 'list'])) else: # --gen-list only if self.__confname: __arc = os.path.dirname(__arcpath) __arclistpath = os.path.join(__arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([__arcpath, '.list']) __listconfinfo = {'arclistpath': __arclistpath, 'listoffiles': __listoffiles} # call the method to write information in a file self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, '.'.join([__reparc, 'conf'])) elif self.__fulloutput: if self.__confname: # --gen-full and --output-list-and-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-list-and-conf-dir __arcconfpath = os.path.join(self.__fulloutput, '.'.join([__reparc, 'conf'])) else: if self.__confname: # --gen-full and --configuration-name __arc = os.path.dirname(__arcpath) __arc = os.path.join(__arc, self.__confname) __arcconfpath = '.'.join([__arc, 'conf']) else: # --gen-full only if self.__confname: __arc = os.path.dirname(__arcpath) __arcconfpath = os.path.join(__arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = '.'.join([__arcpath, 'conf']) # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) __arcname = os.path.basename(__arcpath) if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(__arcpath) # include custom paths for output conf files __confinfo = {'arcname': __arcname, 'arcpath': __arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __arclistpath, 'arctype': 'tree', 'sha512': __listhashsum} self._generate_conf(__confinfo)
def __init__(self, __genparams): '''The constructor for the GenerateListForBzip2 class''' __arcpath = __genparams['arcpath'] __delimiter = __genparams['delimiter'] self._genfull = __genparams['genfull'] self.__listoutput = __genparams['listoutput'] self.__confoutput = __genparams['confoutput'] self.__fulloutput = __genparams['fulloutput'] self.__getallhashes = __genparams['getallhashes'] self.__hashtype = __genparams['hashtype'] self.__parsingexceptions = __genparams['parsingexceptions'] self.__confname = __genparams['confname'] __listoffiles = ['[files]\n'] __filetype = 'f' __filehash = get_hash(bz2.BZ2File(__arcpath, 'r'), 'md5') if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} type{delimiter}{value} md5{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) else: __onelinewithhash = '{value}{delimiter} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__hashtype, delimiter=__delimiter) __listoffiles.append(__onelinewithhash.format( os.path.split(__arcpath)[-1][:-4], __filetype, __filehash)) else: if self.__parsingexceptions : for __file in self.__parsingexceptions: if fnmatch.fnmatch(os.path.split(__arcpath)[-1][:-4], __file): __filehash = get_hash(bz2.BZ2File(__arcpath, 'r'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__parsingexceptions[__file], delimiter=__delimiter) __listoffiles.append(__onelinewithhash.format( os.path.split(__arcpath)[-1][:-4], __filetype, __filehash)) else: __onelinewithouthash = '{value}{delimiter} type{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) __listoffiles.append(__onelinewithouthash.format( os.path.split(__arcpath)[-1][:-4], __filetype)) else: __onelinewithouthash = '{value}{delimiter} type{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) __listoffiles.append(__onelinewithouthash.format( os.path.split(__arcpath)[-1][:-4], __filetype)) # define the flexible file list path __arcwithext = os.path.split(''.join([__arcpath[:-3], 'list']))[1] if self.__listoutput: if self.__confname: # --gen-list and --output-list-dir and --configuration-name __arclistpath = os.path.join(self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-dir __arclistpath = os.path.join(self.__listoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-list and --output-conf-and-list-dir and --configuration-name __arclistpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-conf-and-list-dir __arclistpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-list only if self.__confname: __arc = os.path.dirname(__arcpath) __arclistpath = os.path.join(__arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([__arcpath[:-3], 'list']) # call the method to write information in a file __listconfinfo = {'arclistpath': __arclistpath, 'listoffiles': __listoffiles} self.__lci = __listconfinfo self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) # define the flexible configuration file path __arcwithext = os.path.split(''.join([__arcpath[:-3], 'conf']))[1] if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-full and --output-conf-and-list-dir and --configuration-name __arcconfpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-and-list-dir __arcconfpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-full only if self.__confname: __arc = os.path.dirname(__arcpath) __arcconfpath = os.path.join(__arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = ''.join([__arcpath[:-3], 'conf']) # user-define name of the archive if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(__arcpath[:-4]) __confinfo = {'arcname': __arcname, 'arcpath': __arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __listconfinfo['arclistpath'], 'arctype': 'archive', 'sha512': __listhashsum} self.__ci = __confinfo self._generate_conf(__confinfo)
def __init__(self, __genparams): '''The constructor for the GenerateListForGzip class''' __arcpath = __genparams['arcpath'] __delimiter = __genparams['delimiter'] self._genfull = __genparams['genfull'] self.__confoutput = __genparams['confoutput'] self.__listoutput = __genparams['listoutput'] self.__fulloutput = __genparams['fulloutput'] self.__getallhashes = __genparams['getallhashes'] self.__hashtype = __genparams['hashtype'] self.__parsingexceptions = __genparams['parsingexceptions'] self.__confname = __genparams['confname'] __arcstat = os.stat(__arcpath) __listoffiles = ['[archive]\nmtime{} {}\n\n[files]\n'.format(__delimiter,__arcstat.st_mtime)] __fileinfo = os.lstat(__arcpath) __filetype = 'f' if not self.__hashtype: __filehash = get_hash(gzip.open(__arcpath, 'rb'), 'md5') else: __filehash = get_hash(gzip.open(__arcpath, 'rb'), self.__hashtype) with open(__arcpath, 'rb') as __gzip: __filesize = self.__extract_size(__gzip) __filename = self.__extract_initial_filename(__gzip, os.path.split(__arcpath)[-1][:-2]) if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} type{delimiter}{value} md5{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) else: __onelinewithhash = '{value}{delimiter} ={value} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__hashtype, delimiter=__delimiter) __listoffiles.append(__onelinewithhash.format( __filename, str(__filesize), __filetype, __filehash)) else: if self.__parsingexceptions : for __file in self.__parsingexceptions: if fnmatch.fnmatch(__filename, __file): __filehash = get_hash(gzip.open(__arcpath, 'rb'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} type{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__parsingexceptions[__file], delimiter=__delimiter) __listoffiles.append(__onelinewithhash.format( __filename, str(__filesize), __filetype, __filehash)) else: __onelinewithouthash = '{value}{delimiter} ={value} type{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) __listoffiles.append(__onelinewithouthash.format( __filename, str(__filesize), __filetype)) else: __onelinewithouthash = '{value}{delimiter} ={value} type{delimiter}{value}\n'.format(value='{}', delimiter=__delimiter) __listoffiles.append(__onelinewithouthash.format( __filename, str(__filesize), __filetype)) # define the flexible file list path __arcwithext = os.path.split(''.join([__arcpath[:-2], 'list']))[1] if self.__listoutput: if self.__confname: # --gen-list and --list-output-dir and --configuration-name __arclistpath = os.path.join(self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --list-output-dir __arclistpath = os.path.join(self.__listoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-list and --output-list-and-conf-dir and --configuration-name __arclistpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-and-conf-dir __arclistpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-list only if self.__confname: __arc = os.path.dirname(__arcpath) __arclistpath = os.path.join(__arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([__arcpath[:-2], 'list']) # call the method to write information in a file __listconfinfo = {'arclistpath': __arclistpath, 'listoffiles': __listoffiles} self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) # define the flexible configuration file path __arcwithext = os.path.split(''.join([__arcpath[:-2], 'conf']))[1] if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-full and --output-list-and-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-list-and-conf-dir __arcconfpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-full only if self.__confname: __arc = os.path.dirname(__arcpath) __arcconfpath = os.path.join(__arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = ''.join([__arcpath[:-2], 'conf']) # the name of the backup inside the configuration file if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(__arcpath[:-3]) __confinfo = {'arcname': __arcname, 'arcpath': __arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __listconfinfo['arclistpath'], 'arctype': 'archive', 'sha512': __listhashsum} self._generate_conf(__confinfo)
def __main(self, __tar): '''Main for the GenerateListForTar class''' # extract mtime of the archive if not self.__isastream: __arcstat = os.stat(self.__arcpath) __listoffiles = ['[archive]\nmtime{} {}\n\n[files]\n'.format(self.__delimiter,__arcstat.st_mtime)] else: __listoffiles = ['[files]\n'] __oneline = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) if self.__getallhashes: # we get all the hash sums of files inside the backup if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} md5{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) else: # we switch the default hash sum __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__hashtype, delimiter=self.__delimiter) else: __onelinewithouthash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) __onelinewithtarget = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} target{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) for __tarinfo in __tar: # Pick up tar information __tarinfo.name = self._normalize_path(__tarinfo.name) __type = self.__translate_type(__tarinfo.type) __mode = oct(__tarinfo.mode).split('o')[-1] # if the file has no right, need to manipulate the output - solving #15 if __mode == '0': __mode = '000' if __type == 'f': if self.__getallhashes: # extract all hash sums from the archive if not self.__hashtype: # extract hash sum of the file inside the archive __hash = get_hash(__tar.extractfile(__tarinfo.name), 'md5') else: # switch the default hash sum type __hash = get_hash(__tar.extractfile(__tarinfo.name), self.__hashtype) # format the retrieved information __listoffiles.append(__onelinewithhash.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __hash, __tarinfo.linkname)) else: # check if there are exceptions while parsing if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch(__tarinfo.name, __file): __hash = get_hash(__tar.extractfile(__tarinfo.name), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__parsingexceptions[__file], delimiter=self.__delimiter) __listoffiles.append(__onelinewithhash.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __hash, __tarinfo.linkname)) else: # we use exceptions-file option but the file is not concerned by an exception __listoffiles.append(__onelinewithouthash.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) else: # we don't use the --exceptions-file option __listoffiles.append(__onelinewithouthash.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) elif __type == 'l' or __type == 's': # format the retrieved information __listoffiles.append(__onelinewithtarget.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) else: # if file is not regular file, ignoring its hash sum __listoffiles.append(__oneline.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime))) # Compose the name of the generated list ### for tar archive if self.__arcpath.lower().endswith('.tar'): self.__make_conf_and_list_paths('.tar') ### for tar.gz archive elif self.__arcpath.lower().endswith('.tar.gz'): self.__make_conf_and_list_paths('.tar.gz') ### for tar.bz2 archive elif self.__arcpath.lower().endswith('.tar.bz2'): self.__make_conf_and_list_paths('.tar.bz2') ### for tar.xz archive elif self.__arcpath.lower().endswith('.tar.xz'): self.__make_conf_and_list_paths('.tar.xz') ### for tgz archive elif self.__arcpath.lower().endswith('.tgz'): self.__make_conf_and_list_paths('.tgz') ### for tbz archive elif self.__arcpath.lower().endswith('.tbz'): self.__make_conf_and_list_paths('.tbz') ### for tbz2 archive elif self.__arcpath.lower().endswith('.tbz2'): self.__make_conf_and_list_paths('.tbz2') ### for tar stream elif self.__isastream: #if self._genfull: # self.__arcname = self.__tarstreamname #if self.__confname: # self.__arcname = self.__confname # self.__arcconfpath = ''.join([self.__confname, '.conf']) # self.__arclistpath = ''.join([self.__confname, '.list']) #else: # self.__arcconfpath = ''.join([self.__streampath, '.conf']) # self.__arclistpath = ''.join([self.__streampath, '.list']) self.__make_conf_and_list_paths('') # call the method to write information in a file __listconfinfo = {'arclistpath': self.__arclistpath, 'listoffiles':__listoffiles} self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) if self.__isastream: __confinfo = {'arcname':self.__arcname, 'arcconfpath': self.__arcconfpath, 'arclistpath': self.__arclistpath, 'arctype': 'archive', 'sha512': __listhashsum} else: __confinfo = {'arcname':self.__arcname, 'arcpath':self.__arcpath, 'arcconfpath': self.__arcconfpath, 'arclistpath': self.__arclistpath, 'arctype': 'archive', 'sha512': __listhashsum} self._generate_conf(__confinfo,self.__isastream)
def __main(self, __zip): '''Main of the GenerateListForZip class''' __arcstat = os.stat(self.__arcpath) __listoffiles = ['[archive]\nmtime{} {}\n\n[files]\n'.format(self.__delimiter,__arcstat.st_mtime)] __oneline = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) if self.__getallhashes: if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} md5{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) else: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__hashtype, delimiter=self.__delimiter) else: __onelinewithouthash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) __onelinenoexternalattr = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mtime{delimiter}{value}\n'.format(value='{}', delimiter=self.__delimiter) __crcerror = __zip.testzip() if __crcerror: logging.warning('{} has at least one file corrupted:{}'.format(self.__arcpath, __crcerror)) else: __zipinfo = __zip.infolist() for __fileinfo in __zipinfo: __fileinfo.filename = self._normalize_path(__fileinfo.filename) __uid, __gid = self.__extract_uid_gid(__fileinfo) # check if external_attr is available if __fileinfo.external_attr != 0: __type = self.__translate_type(__fileinfo.external_attr >> 16) __mode = oct(stat.S_IMODE((__fileinfo.external_attr >> 16))).split('o')[-1] # Prepare a timestamp for the ctime object __dt = __fileinfo.date_time try: __mtime = float(datetime.datetime(__dt[0], __dt[1], __dt[2], __dt[3], __dt[4], __dt[5]).timestamp()) except ValueError as __msg: __warn = 'Issue with timestamp while controlling {} in {}'.format(_fileinfo.filename,_cfgvalues['path']) logging.warning(__warn) if __fileinfo.external_attr != 0 and __type == 'f': if self.__getallhashes: if not self.__hashtype: __hash = get_hash(__zip.open(__fileinfo.filename, 'r'), 'md5') else: __hash = get_hash(__zip.open(__fileinfo.filename, 'r'), self.__hashtype) __listoffiles.append(__onelinewithhash.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime, __hash)) else: # check if there are exceptions while parsing if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch(__fileinfo.filename, __file): __hash = get_hash(__zip.open(__fileinfo.filename, 'r'), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format(value='{}', hashtype=self.__parsingexceptions[__file], delimiter=self.__delimiter) __listoffiles.append(__onelinewithhash.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime, __hash)) else: # we use exceptions-file option but the file is not concerned by an exception __listoffiles.append(__onelinewithouthash.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) else: # we don't use the --exceptions-file option __listoffiles.append(__onelinewithouthash.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) elif __fileinfo.external_attr != 0 and __type == 'd': __listoffiles.append(__oneline.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mode, __type, __mtime)) else: __listoffiles.append(__onelinenoexternalattr.format(__fileinfo.filename, str(__fileinfo.file_size), str(__uid), str(__gid), __mtime)) # define the flexible file list path __arcwithext = os.path.split(''.join([self.__arcpath[:-3], 'list']))[1] if self.__listoutput: if self.__confname: # --gen-list and --output-list-dir and --configuration-name __arclistpath = os.path.join(self.__listoutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-list-dir __arclistpath = os.path.join(self.__listoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-list and --output-conf-and-list-dir and --configuration-name __arclistpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'list'])) else: # --gen-list and --output-conf-and-list-dir __arclistpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-list if self.__confname: __arc = os.path.dirname(self.__arcpath) __arclistpath = os.path.join(__arc, '.'.join([self.__confname, 'list'])) else: __arclistpath = ''.join([self.__arcpath[:-3], 'list']) __listconfinfo = {'arclistpath': __arclistpath, 'listoffiles': __listoffiles} # call the method to write the list of files inside the archive self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) # define the flexible configuration file path __arcwithext = os.path.split(''.join([self.__arcpath[:-3], 'conf']))[1] if self.__confoutput: if self.__confname: # --gen-full and --output-conf-dir and --configuration-name __arcconfpath = os.path.join(self.__confoutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-dir __arcconfpath = os.path.join(self.__confoutput, __arcwithext) elif self.__fulloutput: if self.__confname: # --gen-full and --output-conf-and-list-dir and --configuration-name __arcconfpath = os.path.join(self.__fulloutput, '.'.join([self.__confname, 'conf'])) else: # --gen-full and --output-conf-and-list-dir __arcconfpath = os.path.join(self.__fulloutput, __arcwithext) else: # --gen-full only if self.__confname: __arc = os.path.dirname(self.__arcpath) __arcconfpath = os.path.join(__arc, '.'.join([self.__confname, 'conf'])) else: __arcconfpath = ''.join([self.__arcpath[:-3], 'conf']) # name of the archive in the configuration file if self.__confname: __arcname = self.__confname else: __arcname = os.path.basename(self.__arcpath[:-4]) __confinfo = {'arcname': __arcname, 'arcpath': self.__arcpath, 'arcconfpath': __arcconfpath, 'arclistpath': __listconfinfo['arclistpath'], 'arctype': 'archive', 'sha512': __listhashsum} self._generate_conf(__confinfo)
def __main(self, __tar): '''Main for the GenerateListForTar class''' # extract mtime of the archive if not self.__isastream: __arcstat = os.stat(self.__arcpath) __listoffiles = [ '[archive]\nmtime{} {}\n\n[files]\n'.format( self.__delimiter, __arcstat.st_mtime) ] else: __listoffiles = ['[files]\n'] __oneline = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) if self.__getallhashes: # we get all the hash sums of files inside the backup if not self.__hashtype: __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} md5{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) else: # we switch the default hash sum __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self.__hashtype, delimiter=self.__delimiter) else: __onelinewithouthash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) __onelinewithtarget = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} target{delimiter}{value}\n'.format( value='{}', delimiter=self.__delimiter) for __tarinfo in __tar: # Pick up tar information __tarinfo.name = self._normalize_path(__tarinfo.name) __type = self.__translate_type(__tarinfo.type) __mode = oct(__tarinfo.mode).split('o')[-1] # if the file has no right, need to manipulate the output - solving #15 if __mode == '0': __mode = '000' if __type == 'f': if self.__getallhashes: # extract all hash sums from the archive if not self.__hashtype: # extract hash sum of the file inside the archive __hash = get_hash(__tar.extractfile(__tarinfo.name), 'md5') else: # switch the default hash sum type __hash = get_hash(__tar.extractfile(__tarinfo.name), self.__hashtype) # format the retrieved information __listoffiles.append( __onelinewithhash.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __hash, __tarinfo.linkname)) else: # check if there are exceptions while parsing if self.__parsingexceptions: for __file in self.__parsingexceptions: if fnmatch.fnmatch(__tarinfo.name, __file): __hash = get_hash( __tar.extractfile(__tarinfo.name), self.__parsingexceptions[__file]) __onelinewithhash = '{value}{delimiter} ={value} uid{delimiter}{value} gid{delimiter}{value} owner{delimiter}{value} group{delimiter}{value} mode{delimiter}{value} type{delimiter}{value} mtime{delimiter}{value} {hashtype}{delimiter}{value}\n'.format( value='{}', hashtype=self.__parsingexceptions[__file], delimiter=self.__delimiter) __listoffiles.append( __onelinewithhash.format( __tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __hash, __tarinfo.linkname)) else: # we use exceptions-file option but the file is not concerned by an exception __listoffiles.append( __onelinewithouthash.format( __tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) else: # we don't use the --exceptions-file option __listoffiles.append( __onelinewithouthash.format( __tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) elif __type == 'l' or __type == 's': # format the retrieved information __listoffiles.append( __onelinewithtarget.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime), __tarinfo.linkname)) else: # if file is not regular file, ignoring its hash sum __listoffiles.append( __oneline.format(__tarinfo.name, str(__tarinfo.size), str(__tarinfo.uid), str(__tarinfo.gid), str(__tarinfo.uname), str(__tarinfo.gname), __mode, __type, float(__tarinfo.mtime))) # Compose the name of the generated list ### for tar archive if self.__arcpath.lower().endswith('.tar'): self.__make_conf_and_list_paths('.tar') ### for tar.gz archive elif self.__arcpath.lower().endswith('.tar.gz'): self.__make_conf_and_list_paths('.tar.gz') ### for tar.bz2 archive elif self.__arcpath.lower().endswith('.tar.bz2'): self.__make_conf_and_list_paths('.tar.bz2') ### for tar.xz archive elif self.__arcpath.lower().endswith('.tar.xz'): self.__make_conf_and_list_paths('.tar.xz') ### for tgz archive elif self.__arcpath.lower().endswith('.tgz'): self.__make_conf_and_list_paths('.tgz') ### for tbz archive elif self.__arcpath.lower().endswith('.tbz'): self.__make_conf_and_list_paths('.tbz') ### for tbz2 archive elif self.__arcpath.lower().endswith('.tbz2'): self.__make_conf_and_list_paths('.tbz2') ### for tar stream elif self.__isastream: #if self._genfull: # self.__arcname = self.__tarstreamname #if self.__confname: # self.__arcname = self.__confname # self.__arcconfpath = ''.join([self.__confname, '.conf']) # self.__arclistpath = ''.join([self.__confname, '.list']) #else: # self.__arcconfpath = ''.join([self.__streampath, '.conf']) # self.__arclistpath = ''.join([self.__streampath, '.list']) self.__make_conf_and_list_paths('') # call the method to write information in a file __listconfinfo = { 'arclistpath': self.__arclistpath, 'listoffiles': __listoffiles } self._generate_list(__listconfinfo) # call the method to write the configuration file if --gen-full was required if self._genfull: # generate the hash sum of the list of files __listhashsum = self._get_list_hash(__listconfinfo['arclistpath']) if self.__isastream: __confinfo = { 'arcname': self.__arcname, 'arcconfpath': self.__arcconfpath, 'arclistpath': self.__arclistpath, 'arctype': 'archive', 'sha512': __listhashsum } else: __confinfo = { 'arcname': self.__arcname, 'arcpath': self.__arcpath, 'arcconfpath': self.__arcconfpath, 'arclistpath': self.__arclistpath, 'arctype': 'archive', 'sha512': __listhashsum } self._generate_conf(__confinfo, self.__isastream)