Mercurial > hg > buttercup
annotate buttercup/source.py @ 52:b717de8b384f
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 03 Nov 2020 07:51:52 -0800 |
parents | 048e391423a1 |
children |
rev | line source |
---|---|
30
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
1 """ |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
2 VCS sources for the flower project |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
3 """ |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
4 |
18
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 import os |
52 | 6 from subprocess import check_call as call |
7 | |
18
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class Source(object): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 """abstract base class for VCS source""" |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 def __init__(self, uri, srcdir=None): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 self.uri = uri |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 self.srcdir = srcdir or os.getcwd() |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 @classmethod |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 def directory_name(cls, uri): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 """return relative directory name from self.uri""" |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 return uri.rstrip('/').split('/')[-1] |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 def directory(self): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 return os.path.join(self.srcdir, self.directory_name(self.uri)) |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
26 | 23 def update(self): |
24 raise NotImplementedError("`Source` is an abstract base class") | |
30
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
25 |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
26 def __call__(self): |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
27 """convenience wrapper to update""" |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
28 self.update() |
44 | 29 |
26 | 30 |
18
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 class HgSource(Source): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 """mercurial source""" |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 def update(self): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 """updates a checkout or does one if it does not exist""" |
20
60ddc49acd5f
add partial checkout method to hg
Jeff Hammel <jhammel@mozilla.com>
parents:
19
diff
changeset
|
36 if os.path.exists(self.directory()): |
35 | 37 hgdir = os.path.join(self.directory(), '.hg') |
34 | 38 assert os.path.exists(hgdir), "Not an hg directory: %s" % hgdir |
40 | 39 try: |
40 call(['hg', 'pull'], cwd=self.directory()) | |
41 call(['hg', 'update'], cwd=self.directory()) | |
42 except: | |
52 | 43 print('Directory: %s' % self.directory()) |
40 | 44 raise |
20
60ddc49acd5f
add partial checkout method to hg
Jeff Hammel <jhammel@mozilla.com>
parents:
19
diff
changeset
|
45 else: |
21 | 46 if not os.path.exists(self.srcdir): |
47 os.makedirs(self.srcdir) | |
48 call(['hg', 'clone', self.uri], cwd=self.srcdir) | |
44 | 49 |
50 # add a more intelligent .hg/hgrc | |
51 # TODO | |
21 | 52 |
18
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 class GitSource(Source): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 @classmethod |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 def directory_name(cls, uri): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 ext = '.git' |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 if uri.endswith(uri): |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 uri = uri[:-len(ext)] |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 return Source.directory_name(uri) |
151862a0a711
begin restructuring to something more modular and extensible
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 |
22 | 63 def update(self): |
64 """updates a checkout or does one if it does not exist""" | |
65 if os.path.exists(self.directory()): | |
66 assert os.path.exists(os.path.join(self.directory(), '.git')) | |
67 call(['git', 'pull'], cwd=self.directory()) | |
68 else: | |
69 if not os.path.exists(self.srcdir): | |
70 os.makedirs(self.srcdir) | |
71 call(['git', 'clone', self.uri], cwd=self.srcdir) | |
45 | 72 # TODO: add a more intelligent .git/config, e.g. |
73 # [core] | |
74 # repositoryformatversion = 0 | |
75 # filemode = true | |
76 # bare = false | |
77 # logallrefupdates = true | |
78 # [remote "origin"] | |
79 # fetch = +refs/heads/*:refs/remotes/origin/* | |
80 # url = git://github.com/mozilla/toolbox.git | |
81 # pushurl = git@github.com:mozilla/toolbox.git | |
82 # [branch "master"] | |
83 # remote = origin | |
84 # merge = refs/heads/master | |
85 # see: http://git.661346.n2.nabble.com/Separate-default-push-pull-td4555821.html | |
23
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
86 |
25
a54543838686
add test for source demunging
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
87 def sources(source_dict, **kwargs): |
30
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
88 """ |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
89 return source objects from a dict: |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
90 {'hg': ['http://k0s.org/hg/pyloader'], |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
91 'git': ['git://github.com/mozilla/toolbox.git']} |
6089d6ec745a
should now have a working flower; will test when i get internets again
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
92 """ |
23
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
93 retval = [] |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
94 for repo_type, sources in source_dict.items(): |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
95 if repo_type == 'hg': |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
96 retval.extend([HgSource(source, **kwargs) |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
97 for source in sources]) |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
98 elif repo_type == 'git': |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
99 retval.extend([GitSource(source, **kwargs) |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
100 for source in sources]) |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
101 else: |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
102 raise AssertionError("Source type unknown: %s" % repo_type) |
425a3c73ac1d
add a convenience method for VCS sources
Jeff Hammel <jhammel@mozilla.com>
parents:
22
diff
changeset
|
103 return retval |