Beispiel #1
0
def test_run_command():
    """ test run_command """
    cmd = 'echo "HELLO"'
    out = run_command(cmd, do_popen=True, single_line=True).strip()
    assert out == b'HELLO'

    out = run_command(cmd, turn_on_commands=False)
    assert out == cmd
Beispiel #2
0
def cleanup_pickle():
    """ remove temporary files """
    if os.path.exists('temp.pkl.gz'):
        os.remove('temp.pkl.gz')
    for testf in glob.glob('%s/run/cache/test.*' % CURDIR):
        if os.path.exists(testf):
            os.remove(testf)
    if os.path.exists('json_test'):
        for testf in glob.glob('%s/json_test/*' % CURDIR):
            os.remove(testf)
        run_command('rm -rf json_test')
    if os.path.exists('html'):
        run_command('rm -rf html')
Beispiel #3
0
def get_md5(fname):
    """ md5 function using cli """
    if not os.path.exists(fname):
        return None
    output = run_command('md5sum "%s"' % fname,
                         do_popen=True,
                         single_line=True).split()[0]
    return output.decode()
Beispiel #4
0
 def process_files(_, dirname, names):
     """ callback function for os.walk """
     for fn_ in names:
         fname = '%s/%s' % (dirname, fn_)
         if not sync_cache:
             if os.path.isdir(fname) or \
                     ('garmin.pkl' in fn_) or \
                     ('garmin.list' in fn_) or \
                     ('.pkl.gz' in fn_) or \
                     ('.avro' in fn_):
                 continue
         cmd = 'md5sum %s' % fname
         md5sum = run_command(cmd, do_popen=True,
                              single_line=True).split()[0]
         md5sum = md5sum.decode()
         if fn_ not in local_file_chksum:
             local_file_chksum[fn_] = md5sum
Beispiel #5
0
def convert_gmn_to_gpx(gmn_filename):
    """ create temporary gpx file from gmn or tcx files """
    if '.txt' in gmn_filename.lower():
        return None
    with NamedTemporaryFile(prefix='temp', suffix='.gpx', delete=False) as fn_:
        if '.fit' in gmn_filename.lower():
            tcx_filename = convert_fit_to_tcx(gmn_filename)
            run_command('gpsbabel -i gtrnctr -f %s -o gpx -F %s ' %
                        (tcx_filename, fn_.name))
            os.remove(tcx_filename)
        elif '.tcx' in gmn_filename.lower():
            run_command('gpsbabel -i gtrnctr -f %s -o gpx -F %s' %
                        (gmn_filename, fn_.name))
        else:
            run_command('garmin_gpx %s > %s' % (gmn_filename, fn_.name))
        return fn_.name
Beispiel #6
0
def convert_gmn_to_xml(gmn_filename):
    """
        create temporary xml file from gmn,
    """
    if any([
            a in gmn_filename
            for a in ('.tcx', '.TCX', '.fit', '.FIT', '.xml', '.txt')
    ]):
        return gmn_filename
    with NamedTemporaryFile(prefix='temp',
                            suffix='.xml',
                            delete=False,
                            mode='wt') as xml_file:
        xml_file.write('<root>\n')
        with run_command('garmin_dump %s' % gmn_filename, do_popen=True) as \
                pop_:
            for line in pop_:
                if hasattr(line, 'decode'):
                    line = line.decode()
                xml_file.write(line)
        xml_file.write('</root>\n')
        return xml_file.name
Beispiel #7
0
def convert_fit_to_tcx(fit_filename):
    """ fit files to tcx files """
    with NamedTemporaryFile(prefix='temp',
                            suffix='.tcx',
                            delete=False,
                            mode='wt') as fn_:
        if '.fit' in fit_filename.lower():
            if os.path.exists('/usr/bin/fit2tcx'):
                run_command('/usr/bin/fit2tcx -i %s ' % fit_filename +
                            '-o %s 2>&1 > /dev/null' % fn_.name)
            elif os.path.exists('%s/bin/fit2tcx' % os.getenv('HOME')):
                run_command('fit2tcx %s > %s' % (fit_filename, fn_.name))
            elif os.path.exists('./bin/fit2tcx'):
                run_command('./bin/fit2tcx %s > %s' % (fit_filename, fn_.name))
            return fn_.name
        else:
            os.remove(fn_.name)
            return None
Beispiel #8
0
def garmin_parse_arg_list(args, options=None, msg_q=None):
    """ parse command line arguments """
    from garmin_app.garmin_cache import GarminCache
    from garmin_app.garmin_cache_sql import write_postgresql_table
    from garmin_app.garmin_corrections import list_of_corrected_laps
    from garmin_app.garmin_corrections_sql import write_corrections_table

    if options is None:
        options = {'cache_dir': CACHEDIR}
    cache_dir = options['cache_dir']

    s3_files = get_list_of_keys()

    gdir = set()
    for arg in args:
        if arg == 'build':
            if msg_q is not None:
                return
            options['build'] = True
        elif arg == 'backup':
            if msg_q is not None:
                return
            fname = '%s/garmin_data_%s.tar.gz'\
                    % (cache_dir, datetime.date.today().strftime('%Y%m%d'))
            run_command('cd %s/run/ ; ' % cache_dir +
                        'tar zcvf %s gps_tracks/ ' % fname +
                        'garmin_corrections.json')
            if os.path.exists('%s/public_html/backup' % os.getenv('HOME')):
                run_command('cp %s %s/public_html/backup/garmin_data.tar.gz' %
                            (fname, os.getenv('HOME')))
            if os.path.exists('%s/public_html/garmin/tar' % os.getenv('HOME')):
                run_command('mv %s %s/public_html/garmin/tar' %
                            (fname, os.getenv('HOME')))

            pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
            cache_dir_ = '%s/run/cache' % cache_dir
            corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)

            write_corrections_table(corr_list_,
                                    do_tunnel=options.get('do_tunnel', False))

            cache_ = GarminCache(pickle_file=pickle_file_,
                                 cache_directory=cache_dir_,
                                 corr_list=corr_list_,
                                 check_md5=True)
            summary_list_ = cache_.cache_read_fn()
            # backup garmin.pkl.gz info to postgresql database
            write_postgresql_table(summary_list_,
                                   do_tunnel=options.get('do_tunnel', False))

            return
        elif arg == 'occur':
            options['occur'] = True
        elif os.path.isfile(arg):
            gdir.add(arg)
        elif arg != 'run' and os.path.isdir(arg):
            gdir.add(arg)
        elif arg != 'run' and os.path.isdir('%s/run/%s' % (cache_dir, arg)):
            gdir.add('%s/run/%s' % (cache_dir, arg))
        elif arg == 'correction':
            add_correction(' '.join(args[1:]),
                           json_path='%s/run' % cache_dir,
                           options=options)
            return
        elif arg in options:
            options[arg] = True
        elif 'do_%s' % arg in options:
            options['do_%s' % arg] = True
        else:
            spts = [x for x in SPORT_TYPES if arg in x]
            if len(spts) > 0:
                options['do_sport'] = spts[0]
            elif arg == 'bike':
                options['do_sport'] = 'biking'
            elif '-' in arg or arg in (
                    '%4d' % _ for _ in range(2008,
                                             datetime.date.today().year + 1)):
                gdir.update(find_gps_tracks(arg, cache_dir, s3_files=s3_files))
            elif '.gmn' in arg or 'T' in arg:
                files = glob.glob('%s/run/gps_tracks/%s' % (cache_dir, arg))
                gdir.update(files)
            else:
                print('unhandled argument:', arg)
    if not gdir:
        gdir.add('%s/run/gps_tracks' % cache_dir)

    gdir = sorted(gdir)

    if len(gdir) == 1 and os.path.isfile(gdir[0]):
        return read_garmin_file(gdir[0],
                                msg_q=msg_q,
                                options=options,
                                s3_files=s3_files)
    return do_summary(gdir, msg_q=msg_q, options=options)
Beispiel #9
0
def md5_command(command):
    """ convenience function """
    md5 = run_command(command, single_line=True, do_popen=True).split()[0]
    if hasattr(md5, 'decode'):
        md5 = md5.decode()
    return md5