1 # ============================================================================ #
4 # Copyright (C) 2012 Jose A. Rivera <jarrpa@redhat.com>
6 # $Date: 2013-01-22 22:49:58 -0600$
8 # ---------------------------------------------------------------------------- #
10 # Description: Mostly-git-related utility functions.
12 # ---------------------------------------------------------------------------- #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
29 # ---------------------------------------------------------------------------- #
33 # ============================================================================ #
40 from datetime import datetime, timedelta, tzinfo
43 def git(args, interactive=False, input=None, cwd=None ):
47 for i in range(len(tmp)):
51 tmp2 += tmp[i].split()
54 out = None if interactive else subprocess.PIPE
55 inp = None if interactive else subprocess.PIPE
56 git = subprocess.Popen( args,
57 stdout=out, stdin=inp, stderr=subprocess.STDOUT,
61 details = git.communicate(input)[0]
62 details = details.strip()
69 details = git(['config', '%s' % (key)])
76 if git(['rev-parse', '--is-bare-repository']) == 'true':
77 return os.path.basename(os.getcwd())
79 return os.path.basename(os.path.dirname(os.getcwd()))
81 def git_parse_date( datestr, fmt=False ):
82 '''Parse a given date string into a Python datetime object. Formats allowed
83 for the date string are the same as those outlined in 'git help commit' under
86 datestr - A properly formatted date string.
87 fmt - A boolean to indicate if the discovered format of the string should
88 also be returned. See the Output section.
90 Output: If fmt is False, return a Python datetime object. If fmt is True,
91 return a tuple (<date>, <format>) where <date> is a datetime object
92 and <format> is a format string indicating the discovered format of
93 datestr. If datestr can not me parsed, return either None (if
94 fmt=False) or a tuple of (None,'') (if fmt=True).
96 # Define acceptable formats.
97 fmtstrs = { 'gittime' : '%s %z', # NOTE: %s may not be cross-platform.
98 'rfc2822' : '%a, %d %b %Y %H:%M:%S %z',
99 'isodate1' : '%Y-%m-%d',
100 'isodate2' : '%Y.%m.%d',
101 'isodate3' : '%m/%d/%Y',
102 'isodate4' : '%d.%m.%Y',
105 'isotime' : '%H:%M:%S',
111 # Done. Return results.
115 results = ( dtm, fmtstr )
119 # A class capturing the platform's idea of local time.
120 # Taken from http://docs.python.org/library/datetime.html#tzinfo-objects
125 STDOFFSET = timedelta(seconds = -_time.timezone)
127 DSTOFFSET = timedelta(seconds = -_time.altzone)
129 DSTOFFSET = STDOFFSET
131 DSTDIFF = DSTOFFSET - STDOFFSET
133 class LocalTimezone(tzinfo):
135 def utcoffset(self, dt):
147 def tzname(self, dt):
148 return _time.tzname[self._isdst(dt)]
150 def _isdst(self, dt):
151 tt = (dt.year, dt.month, dt.day,
152 dt.hour, dt.minute, dt.second,
154 stamp = _time.mktime(tt)
155 tt = _time.localtime(stamp)
156 return tt.tm_isdst > 0
158 LocalTZ = LocalTimezone()
160 # ============================================================================ #