[Git][NTPsec/ntpsec][master] calc_tickadj: Translate to Python.
Eric S. Raymond
gitlab at mg.gitlab.com
Fri Sep 9 13:30:57 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
f6a1255d by Eric S. Raymond at 2016-09-09T09:30:44-04:00
calc_tickadj: Translate to Python.
- - - - -
1 changed file:
- util/calc_tickadj/calc_tickadj
Changes:
=====================================
util/calc_tickadj/calc_tickadj
=====================================
--- a/util/calc_tickadj/calc_tickadj
+++ b/util/calc_tickadj/calc_tickadj
@@ -1,115 +1,92 @@
-#!/usr/bin/env perl
+#!/usr/bin/env python
#
# drift of 104.8576 -> +1 tick. Base of 10000 ticks.
#
# 970306 HMS Deal with nanoseconds. Fix sign of adjustments.
+#
+# Translated from a very old Perl script - the comment above is a clue
+# to how old.
+#
+'''
+calc_tickadj - Calculates "optimal" value for tick given ntp drift file.
-use warnings;
-
-package calc_tickadj;
-use strict;
-
-exit run(@ARGV) unless caller;
+USAGE: calc_tickadj [-t tick] [-d drift-file]
-sub run {
- my $opts;
- if (!processOptions(\@_, $opts)) {
- usage(1);
- };
- my $drift_file = $opts->{'drift-file'};
- my $tick = $opts->{'tick'};
+ -d, --drift-file=str Ntp drift file to use
+ -t, --tick=num Tick value of this host
+ -h, --help Display usage message and exit
- if (!$tick) {
- my ($fl) = `ntpfrob -A`;
- if (defined $fl && $fl =~ /(\d+)/) {
- $tick = $1;
- }
- else {
- die "Could not get tick value, try manually with -t/--tick\n";
- }
- }
+Options are specified by doubled hyphens and their name or by a single
+hyphen and the flag character.
+'''
+#SPDX-License-Identifier: BSD-2-Clause
+
+from __future__ import print_function, division
+
+import sys, getopt, re
+
+if __name__ == '__main__':
+ try:
+ (options, arguments) = getopt.getopt(
+ sys.argv[1:], "d:t:h", ["drift-file=", "tick=", "--help"])
+ except getopt.GetoptError as err:
+ sys.stderr.write(str(err) + "\n")
+ raise SystemExit(1)
+ tick = 0
+ drift_file = "/etc/ntp/drift"
+ for (switch, val) in options:
+ if switch == "-d" or switch == "--drift-file":
+ drift_file = val
+ elif switch == "-t" or switch == "--tick":
+ tick = int(val)
+ elif switch == "-h" or switch == "--help":
+ print(__doc__)
+ sys.exit(0)
+
+ if tick == 0:
+ try:
+ with sys.popen("ntpfrob -A") as rp:
+ response = rp.read()
+ m = re.search("[0-9]+", response)
+ if m:
+ tick = int(m.group(0))
+ except:
+ pass
+
+ if tick == 0:
+ sys.stderr.write("Could not get tick value, try manually with -t/--tick\n\n")
+ sys.exit(1)
# Drift file is in PPM where Million is actually 2**20
- my $cvt = (2 ** 20) / $tick;
- my $drift = 0.;
-
- open my $dfh, $drift_file or die "Could not open $drift_file: $!\n";
-
- $drift = <$dfh>;
-
- close $dfh;
- die "Invalid drift file value <$drift>" if $drift !~ /[+-]?\d+\.?[0-9]+/;
-
- while ($drift < 0) {
- $drift += $cvt;
- $tick--;
- }
-
- while ($drift > $cvt) {
- $drift -= $cvt;
- $tick++;
- }
+ cvt = (2 ** 20) / tick
+ drift = 0.0
- printf "%.3f (drift)\n", $drift;
- printf "%d usec; %d nsec\n", $tick, ($tick + ($drift/$cvt)) * 1000;
+ try:
+ with open(drift_file) as dp:
+ drift = dp.read()
+ except OSError:
+ sys.stderr.write("Could not open drift file: %s\n" % drift_file)
+ sys.exit(1)
- return 0;
-}
+ m = re.match("[+-]?\d+\.?[0-9]+", drift)
+ if not m:
+ sys.stderr.write("Invalid drift file value '%s'\n" % drift)
+ sys.exit(1)
+ else:
+ drift = float(drift)
-use Getopt::Long qw(GetOptionsFromArray);
-Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case_always));
+ while drift < 0:
+ drift += cvt
+ tick -= 1
-my $usage;
+ while drift > cvt:
+ drift -= cvt
+ tick += 1
-sub usage {
- my ($ret) = @_;
- print STDERR $usage;
- exit $ret;
-}
-
-sub paged_usage {
- my ($ret) = @_;
- my $pager = $ENV{PAGER} || '(less || more)';
-
- open STDOUT, "| $pager" or die "Can't fork a pager: $!";
- print $usage;
-
- exit $ret;
-}
-
-sub processOptions {
- my $args = shift;
-
- my $opts = {
- 'drift-file' => '/etc/ntp/drift',
- 'tick' => '',
- 'help' => '', 'more-help' => ''
- };
- my $argument = '';
- my $ret = GetOptionsFromArray($args, $opts, (
- 'drift-file|d=s', 'tick|t=i',
- 'help|?', 'more-help'));
-
- $usage = <<'USAGE';
-calc_tickadj - Calculates "optimal" value for tick given ntp drift file. - Ver. 4.2.7p467
-USAGE: calc_tickadj [ -<flag> [<val>] | --<name>[{=| }<val>] ]...
-
- -d, --drift-file=str Ntp drift file to use
- -t, --tick=num Tick value of this host
- -?, --help Display usage information and exit
- --more-help Pass the extended usage text through a pager
-
-Options are specified by doubled hyphens and their name or by a single
-hyphen and the flag character.
-USAGE
+ print("%.3f (drift)" % drift)
+ print("%d usec; %d nsec" % (tick, (tick + (drift/cvt)) * 1000))
- usage(0) if $opts->{'help'};
- paged_usage(0) if $opts->{'more-help'};
- $_[0] = $opts;
- return $ret;
-}
+ sys.exit(0)
-END { close STDOUT };
+# end
-1;
-__END__
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/f6a1255df1c53dedb845b7cea9efbf46152278d1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20160909/1ad4c125/attachment.html>
More information about the vc
mailing list