Exemplo n.º 1
0
def rename_backup(srcfile, optiondict, bak_ext='.bak', debug=False):
    fpath, fext = os.path.splitext(srcfile)
    if bak_ext[:1] != '.':
        bak_ext = '.' + bak_ext
    fpath_bak = os.path.join(fpath + bak_ext)
    if os.path.isfile(fpath_bak):
        logger.info('remove_filename:%s', fpath_bak)
        if not optiondict['emulate']:
            kvutil.remove_filename(fpath_bak)
    logger.info('rename:%s:to:%s', srcfile, fpath_bak)
    if not optiondict['emulate']:
        os.rename(srcfile, fpath_bak)
Exemplo n.º 2
0
def rename_file( orig_filename, rename_ext='bak', rename_filename=None, debug=False ):
    if not rename_ext:
        rename_ext = 'bak'
    if not rename_filename:
        # filename with no extension
        rename_filename = os.path.splitext(orig_filename)[0]

        # Add the extension
        if not rename_ext.startswith('.'):
            rename_file += '.'
        rename_filename += rename_ext

    if os.path.exists(rename_filename):
        # remove file
        kvutil.remove_filename(rename_filename)

    # move the file to the rename filename
    os.rename(orig_filename, rename_filename)
Exemplo n.º 3
0
 def test_readxlslist_findheader_f02_xlsx_unique_columns_test(self):
     logger.debug('STARTUP')
     header = list(records[0].keys())
     dupkey = header[0]
     header.append(dupkey)
     aref = []
     for rec in records:
         aref.append(list(rec.values()) + [rec[dupkey]])
     kvxls.writelist2xls(filenamexlsx3,
                         aref,
                         optiondict={'aref_result': True},
                         debug=False)
     with self.assertRaises(Exception) as context:
         kvxls.readxls2list_findheader(filenamexlsx3,
                                       req_cols,
                                       optiondict={'unique_column': True},
                                       debug=False)
     kvutil.remove_filename(filenamexlsx3, kvutil.functionName())
Exemplo n.º 4
0
 def test_readxlslist_findheader_f01_xlsx_maxrows_exceeded_in_header_search(
         self):
     logger.debug('STARTUP')
     # create a list of values that are used to create the xls - we have 6 blank lines at the top
     aref = [[''], [''], [''], [''], ['']]
     aref.append(list(records[0].keys()))
     for rec in records:
         aref.append(list(rec.values()))
     kvxls.writelist2xls(filenamexlsx3,
                         aref,
                         optiondict={'no_header': True},
                         debug=False)
     with self.assertRaises(Exception) as context:
         kvxls.readxls2list_findheader(filenamexlsx3,
                                       req_cols,
                                       optiondict={'maxrows': 2},
                                       debug=False)
     kvutil.remove_filename(filenamexlsx3, kvutil.functionName())
Exemplo n.º 5
0
 def test_readxlslist_findheader_p11_xlsx_simple_blankReqCols_aref_result_starting_blank_lines(
         self):
     logger.debug('STARTUP')
     # create a list of values that are used to create the xls - we have 6 blank lines at the top
     aref = [[''], [''], [''], [''], ['']]
     aref.append(list(records[0].keys()))
     for rec in records:
         aref.append(list(rec.values()))
     kvxls.writelist2xls(filenamexlsx3,
                         aref,
                         optiondict={'aref_result': True},
                         debug=False)
     # now read in the file
     result = kvxls.readxls2list_findheader(
         filenamexlsx3, [], optiondict={'aref_result': True}, debug=False)
     self.assertEqual(result[0], [None, None, None, None, None, None, None])
     self.assertEqual(result[6], list(records[0].values()))
     kvutil.remove_filename(filenamexlsx3, kvutil.functionName())
Exemplo n.º 6
0
def update_file_version(filename, major_update=False, test=False, debug=False):
    file_tmp = kvutil.filename_create(filename, filename_ext='tmp')
    file_bak = kvutil.filename_create(filename, filename_ext='bak')

    if debug:
        print('update_file_version:start')
        print('update_file_version:filename:', filename)
        print('update_file_version:file_bak:', file_bak)
        print('update_file_version:file_tmp:', file_tmp)
        print('update_file_version:major_update:', major_update)
        print('update_file_version:test:', test)

    app_ver = ''
    new_app_ver = ''
    version_found = False
    opt_ver_found = False

    version_changed = False

    with open(file_tmp, 'w') as fpOut:
        with open(filename, 'r') as fpIn:
            filelines = fpIn.readlines()

        for line in filelines:
            if version_found:
                if opt_ver_found:
                    # clear the flag no matter what
                    opt_ver_found = False
                    # this next line should be the value line to test for it
                    if reOptValueLine.search(line):
                        version_changed = True
                        fpOut.write(fmtOptValue.format(new_app_ver))
                        continue

                if reOptVerLine.search(line):
                    opt_ver_found = True
                elif reAppVerLine.search(line):
                    if debug:
                        print('AppVerLine')
                        print('line:', line)
                        print('now.:', fmtAppVerLine.format(new_app_ver))
                    version_changed = True
                    fpOut.write(fmtAppVerLine.format(new_app_ver))
                    continue
                elif rePgmVerLine.search(line):
                    if debug:
                        print('PgmVerLine')
                        print('line:', line)
                        print('now.:', fmtPgmVerLine.format(new_app_ver))
                    version_changed = True
                    fpOut.write(fmtPgmVerLine.format(new_app_ver))
                    continue
                elif reVerLine.search(line):
                    if debug:
                        print('VerLine')
                        print('line:', line)
                        print('now.:', VerLine.format(new_app_ver))
                    version_changed = True
                    fpOut.write(fmtVerLine.format(new_app_ver))
                    continue
            else:
                # find the first version defintion and use it to set the version
                for srch_idx, reSearchRegex in enumerate(reSearchList):
                    m = reSearchRegex.search(line)
                    if m:
                        # app_ver = m.group(1)
                        major_ver = int(m.group(1))
                        minor_ver = int(m.group(2))
                        app_ver = '{}.{}'.format(m.group(1), m.group(2))

                        if debug:
                            print('matching line:', line)
                            print('major_ver:', major_ver)
                            print('minor_ver:', minor_ver)

                        if major_update:
                            new_major_ver = major_ver + 1
                            new_minor_ver = 1
                        else:
                            new_major_ver = major_ver
                            new_minor_ver = minor_ver + 1

                        if new_minor_ver < 100:
                            new_app_ver = fmtVersion2d.format(new_major_ver, new_minor_ver)
                        else:
                            new_app_ver = fmtVersion3d.format(new_major_ver, new_minor_ver)

                        if debug:
                            print('major_ver-updt:', new_major_ver)
                            print('minor_ver-updt:', new_minor_ver)
                            print('new_app_ver:', new_app_ver)
                            print('srch_idx:', srch_idx)
                            print('line:', line)
                            print('new.:', fmtSearchList[srch_idx].format(new_app_ver))

                        version_found = True
                        version_changed = True
                        fpOut.write(fmtSearchList[srch_idx].format(new_app_ver))
                        break

                if version_found:
                    continue

            fpOut.write(line)

    if debug:
        print('update_file_version:version_found:', version_found)
        print('update_file_version:version_changed:', version_changed)
        print('update_file_version:test:', test)

    # create temp file determine if we take existing file
    # convert to bak and make tmp the current file
    if version_found and version_changed:
        if not test:
            if os.path.exists(file_bak):
                kvutil.remove_filename(file_bak)
            os.rename(filename, file_bak)
            os.rename(file_tmp, filename)
        return app_ver, new_app_ver, filename, file_bak
    else:
        return app_ver, None, None, None
Exemplo n.º 7
0
 def tearDownClass(cls):
     if 1:
         kvutil.remove_filename(filenamexls, kvutil.functionName())
         kvutil.remove_filename(filenamexlsx, kvutil.functionName())
         kvutil.remove_filename(filenamexls2, kvutil.functionName())
         kvutil.remove_filename(filenamexlsx2, kvutil.functionName())