OLD | NEW |
1 """ | 1 """ |
2 DO NOT import this file directly - import client/bin/utils.py, | 2 DO NOT import this file directly - import client/bin/utils.py, |
3 which will mix this in | 3 which will mix this in |
4 | 4 |
5 Convenience functions for use by tests or whomever. | 5 Convenience functions for use by tests or whomever. |
6 | 6 |
7 Note that this file is mixed in by utils.py - note very carefully the | 7 Note that this file is mixed in by utils.py - note very carefully the |
8 precedence order defined there | 8 precedence order defined there |
9 """ | 9 """ |
10 import os, shutil, sys, signal, commands, pickle, glob, statvfs | 10 import os, shutil, sys, signal, commands, pickle, glob, statvfs |
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 """ | 741 """ |
742 'pgrep name' misses all python processes and also long process names. | 742 'pgrep name' misses all python processes and also long process names. |
743 'pgrep -f name' gets all shell commands with name in args. | 743 'pgrep -f name' gets all shell commands with name in args. |
744 So look only for command whose initial pathname ends with name. | 744 So look only for command whose initial pathname ends with name. |
745 Name itself is an egrep pattern, so it can use | etc for variations. | 745 Name itself is an egrep pattern, so it can use | etc for variations. |
746 """ | 746 """ |
747 return utils.system("pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern, | 747 return utils.system("pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern, |
748 ignore_status=True) == 0 | 748 ignore_status=True) == 0 |
749 | 749 |
750 | 750 |
| 751 def get_hwclock_seconds(utc=True): |
| 752 """ |
| 753 Return the hardware clock in seconds as a floating point value. |
| 754 Use Coordinated Universal Time if utc is True, local time otherwise. |
| 755 Raise a ValueError if unable to read the hardware clock. |
| 756 """ |
| 757 cmd = '/sbin/hwclock --debug' |
| 758 if utc: |
| 759 cmd += ' --utc' |
| 760 hwclock_output = utils.system_output(cmd, ignore_status=True) |
| 761 match = re.search(r'= ([0-9]+) seconds since .+ (-?[0-9.]+) seconds$', |
| 762 hwclock_output, re.DOTALL) |
| 763 if match: |
| 764 seconds = int(match.group(1)) + float(match.group(2)) |
| 765 logging.debug('hwclock seconds = %f' % seconds) |
| 766 return seconds |
| 767 |
| 768 raise ValueError('Unable to read the hardware clock -- ' + |
| 769 hwclock_output) |
| 770 |
| 771 |
| 772 def set_wake_alarm(alarm_time): |
| 773 """ |
| 774 Set the hardware RTC-based wake alarm to 'alarm_time'. |
| 775 """ |
| 776 utils.write_one_line('/sys/class/rtc/rtc0/wakealarm', str(alarm_time)) |
| 777 |
| 778 |
| 779 def set_power_state(state): |
| 780 """ |
| 781 Set the system power state to 'state'. |
| 782 """ |
| 783 utils.write_one_line('/sys/power/state', state) |
| 784 |
| 785 |
| 786 def standby(): |
| 787 """ |
| 788 Power-on suspend (S1) |
| 789 """ |
| 790 set_power_state('standby') |
| 791 |
| 792 |
| 793 def suspend_to_ram(): |
| 794 """ |
| 795 Suspend the system to RAM (S3) |
| 796 """ |
| 797 set_power_state('mem') |
| 798 |
| 799 |
| 800 def suspend_to_disk(): |
| 801 """ |
| 802 Suspend the system to disk (S4) |
| 803 """ |
| 804 set_power_state('disk') |
OLD | NEW |