| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- p4 source |
|---|
| 3 | # :Creato: Fri Mar 16 23:06:43 PDT 2007 |
|---|
| 4 | # :Autore: Dustin Sallings <dustin@spy.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module implements the source backend for p4. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from vcpx.shwrap import ExternalCommand, PIPE |
|---|
| 15 | from vcpx.config import ConfigurationError |
|---|
| 16 | from vcpx.source import UpdatableSourceWorkingDir, GetUpstreamChangesetsFailure |
|---|
| 17 | from vcpx.source import ChangesetApplicationFailure |
|---|
| 18 | from vcpx.changes import Changeset |
|---|
| 19 | from vcpx.tzinfo import UTC |
|---|
| 20 | |
|---|
| 21 | from datetime import datetime |
|---|
| 22 | import exceptions |
|---|
| 23 | import string |
|---|
| 24 | import time |
|---|
| 25 | import os |
|---|
| 26 | |
|---|
| 27 | import p4lib |
|---|
| 28 | |
|---|
| 29 | P4_DATE_FMT="%Y/%m/%d %H:%M:%S" |
|---|
| 30 | |
|---|
| 31 | class NotForMe(exceptions.Exception): |
|---|
| 32 | def __init__(self, s): |
|---|
| 33 | self.s=s |
|---|
| 34 | def __repr__(self): |
|---|
| 35 | "<NotForMe: " + self.s + ">" |
|---|
| 36 | __str__=__repr__ |
|---|
| 37 | |
|---|
| 38 | class P4SourceWorkingDir(UpdatableSourceWorkingDir): |
|---|
| 39 | def __getP4(self): |
|---|
| 40 | p4=self.repository.EXECUTABLE |
|---|
| 41 | args={} |
|---|
| 42 | if self.repository.p4client is not None: |
|---|
| 43 | args['client']=self.repository.p4client |
|---|
| 44 | if self.repository.p4port is not None: |
|---|
| 45 | args['port']=self.repository.p4port |
|---|
| 46 | return p4lib.P4(p4=p4, **args) |
|---|
| 47 | |
|---|
| 48 | def __getNativeChanges(self, sincerev): |
|---|
| 49 | changes=self.__getP4().changes(self.repository.depo_path + "...") |
|---|
| 50 | changes.reverse() |
|---|
| 51 | # Get rid of changes that are too low |
|---|
| 52 | changes=filter(lambda c: int(c['change']) > sincerev, changes) |
|---|
| 53 | return changes |
|---|
| 54 | |
|---|
| 55 | def __parseDate(self, d): |
|---|
| 56 | return datetime.fromtimestamp(time.mktime( |
|---|
| 57 | time.strptime(d, P4_DATE_FMT)), UTC) |
|---|
| 58 | |
|---|
| 59 | def __adaptChanges(self, changes): |
|---|
| 60 | p4=self.__getP4() |
|---|
| 61 | descrs=[p4.describe(c['change'], shortForm=True) for c in changes] |
|---|
| 62 | return [Changeset(d['change'], self.__parseDate(d['date']), \ |
|---|
| 63 | d['user'], d['description']) for d in descrs] |
|---|
| 64 | |
|---|
| 65 | def _getUpstreamChangesets(self, sincerev): |
|---|
| 66 | return self.__adaptChanges(self.__getNativeChanges(sincerev)) |
|---|
| 67 | |
|---|
| 68 | def __getLocalFilename(self, f, dp=None): |
|---|
| 69 | if dp is None: |
|---|
| 70 | dp=self.repository.depo_path |
|---|
| 71 | trans=string.maketrans(" ", "_") |
|---|
| 72 | fn=f['depotFile'] |
|---|
| 73 | rv=fn |
|---|
| 74 | if fn.startswith(dp): |
|---|
| 75 | rv=fn[len(dp):] |
|---|
| 76 | if rv[0]=='/': |
|---|
| 77 | rv=rv[1:] |
|---|
| 78 | else: |
|---|
| 79 | raise NotForMe(f) |
|---|
| 80 | return rv |
|---|
| 81 | |
|---|
| 82 | def _applyChangeset(self, changeset): |
|---|
| 83 | p4=self.__getP4() |
|---|
| 84 | desc=p4.describe(changeset.revision, shortForm=True) |
|---|
| 85 | p4.sync('@' + str(changeset.revision)) |
|---|
| 86 | for f in desc['files']: |
|---|
| 87 | try: |
|---|
| 88 | e=changeset.addEntry(self.__getLocalFilename(f), |
|---|
| 89 | changeset.revision) |
|---|
| 90 | k=f['action'] |
|---|
| 91 | self.log.debug("action on file: %s", str(f)) |
|---|
| 92 | if k in ['add', 'branch']: |
|---|
| 93 | e.action_kind = e.ADDED |
|---|
| 94 | elif k == 'delete': |
|---|
| 95 | e.action_kind = e.DELETED |
|---|
| 96 | elif k in ['edit', 'integrate']: |
|---|
| 97 | e.action_kind = e.UPDATED |
|---|
| 98 | else: |
|---|
| 99 | assert False |
|---|
| 100 | except NotForMe: |
|---|
| 101 | pass |
|---|
| 102 | return [] |
|---|
| 103 | |
|---|
| 104 | def _checkoutUpstreamRevision(self, revision): |
|---|
| 105 | if revision == 'INITIAL': |
|---|
| 106 | revision = self.__getNativeChanges(-1)[0]['change'] |
|---|
| 107 | p4=self.__getP4() |
|---|
| 108 | desc=p4.describe(revision, shortForm=True) |
|---|
| 109 | |
|---|
| 110 | p4.sync('@' + str(revision)) |
|---|
| 111 | |
|---|
| 112 | ts=self.__parseDate(desc['date']) |
|---|
| 113 | |
|---|
| 114 | return Changeset(revision, ts, desc['user'], desc['description']) |
|---|