-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_dsc.py
1463 lines (1314 loc) · 63.5 KB
/
import_dsc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# import_dsc.py -- Import a series of .dsc files.
# Copyright (C) 2007 James Westby <[email protected]>
# (C) 2008 Canonical Ltd.
#
# Code is also taken from bzrtools, which is
# (C) 2005, 2006, 2007 Aaron Bentley <[email protected]>
# (C) 2005, 2006 Canonical Limited.
# (C) 2006 Michael Ellerman.
# and distributed under the GPL, version 2 or later.
#
# This file is part of bzr-builddeb.
#
# bzr-builddeb is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# bzr-builddeb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bzr-builddeb; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import calendar
from contextlib import contextmanager, ExitStack
import os
import stat
import tempfile
import tarfile
from typing import Optional
from debian import deb822
from debian.changelog import Version, Changelog, VersionError
from debmutate.versions import mangle_version_for_git
from ...branch import Branch
from ... import (
controldir,
)
from ...config import ConfigObj
from ...errors import (
BzrError,
AlreadyBranchError,
NotBranchError,
NoRoundtrippingSupport,
NoSuchTag,
NoWorkingTree,
UnrelatedBranches,
)
from ...revision import NULL_REVISION, RevisionID
from ...trace import warning, mutter
from ...transport import (
get_transport,
)
from ...tree import Tree
from .bzrtools_import import import_dir
from .errors import (
MultipleUpstreamTarballsNotSupported,
)
from .extract import extract
from .util import (
export_with_nested,
extract_orig_tarball,
get_commit_info_from_changelog,
md5sum_filename,
open_file_via_transport,
open_transport,
)
from .upstream import (
PackageVersionNotPresent,
)
from .upstream.branch import (
PreviousVersionTagMissing,
)
from .upstream.pristinetar import (
get_pristine_tar_source,
)
class CorruptUpstreamSourceFile(BzrError):
_fmt = 'Corrupt upstream source file %(filename)s: %(reason)s'
class UpstreamBranchAlreadyMerged(BzrError):
_fmt = 'That revision of the upstream branch has already been merged.'
class UpstreamAlreadyImported(BzrError):
_fmt = 'Upstream version "%(version)s" has already been imported.'
def __init__(self, version):
BzrError.__init__(self, version=str(version))
class VersionAlreadyImported(BzrError):
_fmt = "Debian version %(version)s has already been imported."
def __init__(self, version, tag_name):
BzrError.__init__(self, version=str(version), tag_name=tag_name)
class DscCache:
def __init__(self, transport=None):
self.cache = {}
self.transport_cache = {}
self.transport = transport
def get_dsc(self, name):
if name in self.cache:
dsc1 = self.cache[name]
else:
# Obtain the dsc file, following any redirects as needed.
filename, transport = open_transport(name)
with open_file_via_transport(filename, transport) as f1:
dsc1 = deb822.Dsc(f1)
self.cache[name] = dsc1
self.transport_cache[name] = transport
return dsc1
def get_transport(self, name):
return self.transport_cache[name]
class DscComp:
def __init__(self, cache):
self.cache = cache
def key(self, dscname):
dsc = self.cache.get_dsc(dscname)
return Version(dsc['Version'])
class DistributionBranchSet:
"""A collection of DistributionBranches with an ordering.
A DistributionBranchSet collects a group of DistributionBranches
and an order, and then can provide the branches with information
about their place in the relationship with other branches.
"""
def __init__(self):
"""Create a DistributionBranchSet."""
self._branch_list = []
def add_branch(self, branch):
"""Adds a DistributionBranch to the end of the list.
Appends the passed distribution branch to the end of the list
that this DistributionBranchSet represents. It also provides
the distribution branch with a way to get the branches that
are before and after it in the list.
It will call branch.set_get_lesser_branches_callback() and
branch.set_get_greater_branches_callback(), passing it methods
that the DistributionBranch can call to get the list of branches
before it in the list and after it in the list respectively.
The passed methods take no arguments and return a list (possibly
empty) of the desired branches.
:param branch: the DistributionBranch to add.
"""
self._branch_list.append(branch)
lesser_callback = self._make_lesser_callback(branch)
branch.set_get_lesser_branches_callback(lesser_callback)
greater_callback = self._make_greater_callback(branch)
branch.set_get_greater_branches_callback(greater_callback)
def _make_lesser_callback(self, branch):
return lambda: self.get_lesser_branches(branch)
def _make_greater_callback(self, branch):
return lambda: self.get_greater_branches(branch)
def get_lesser_branches(self, branch):
"""Return the list of branches less than the argument.
:param branch: The branch that all branches returned must be less
than.
:return: a (possibly empty) list of all the branches that are
less than the argument. The list is sorted starting with the
least element.
"""
index = self._branch_list.index(branch)
return self._branch_list[:index]
def get_greater_branches(self, branch):
"""Return the list of branches greater than the argument.
:param branch: The branch that all branches returned must be greater
than.
:return: a (possibly empty) list of all the branches that are
greater than the argument. The list is sorted starting with the
least element.
"""
index = self._branch_list.index(branch)
return self._branch_list[index+1:]
def checkout_upstream_version(tree, package, version, revisions):
"""Checkout an upstream version from the pristine tar source.
"""
main_revid, main_subpath = revisions[None]
if main_subpath:
raise Exception("subpaths not yet supported")
tree.update(revision=main_revid)
parent_ids = []
for component in sorted(revisions.keys()):
revid, subpath = revisions[component]
if component is not None:
component_tree = tree.branch.repository.revision_tree(revid)
export_with_nested(
component_tree, os.path.join(tree.basedir, component),
format='dir', subdir=subpath)
parent_ids.append(revid)
tree.set_parent_ids(parent_ids)
class DistributionBranch:
"""A DistributionBranch is a representation of one line of development.
It is a branch that is linked to a line of development, such as Debian
unstable. It also has associated branches, some of which are "lesser"
and some are "greater". A lesser branch is one that this branch
derives from. A greater branch is one that derives from this. For
instance Debian experimental would have unstable as a lesser branch,
and vice-versa. It is assumed that a group of DistributionBranches will
have a total ordering with respect to these relationships.
"""
def __init__(self, branch, pristine_upstream_branch, tree=None,
pristine_upstream_tree=None):
"""Create a distribution branch.
You can only import packages on to the DistributionBranch
if both tree and pristine_upstream_tree are provided.
:param branch: the Branch for the packaging part.
:param pristine_upstream_branch: the Branch for the pristine tar part,
if any.
:param tree: an optional tree for the branch.
:param pristine_upstream_tree: an optional tree for the
pristine_upstream_branch.
"""
self.branch = branch
self.tree = tree
self.pristine_upstream_branch = pristine_upstream_branch
self.pristine_upstream_tree = pristine_upstream_tree
if pristine_upstream_branch is not None:
self.pristine_upstream_source = get_pristine_tar_source(
tree, pristine_upstream_branch)
else:
self.pristine_upstream_source = None
self.get_lesser_branches = None
self.get_greater_branches = None
def set_get_lesser_branches_callback(self, callback):
"""Set the callback to get the branches "lesser" than this.
The function passed to this method will be used to get the
list of branches that are "lesser" than this one. It is
expected to require no arguments, and to return the desired
(possibly empty) list of branches. The returned list should
be sorted starting with the least element.
:param callback: a function that is called to get the desired list
of branches.
"""
self.get_lesser_branches = callback
def set_get_greater_branches_callback(self, callback):
"""Set the callback to get the branches "greater" than this.
The function passed to this method will be used to get the
list of branches that are "greater" than this one. It is
expected to require no arguments, and to return the desired
(possibly empty) list of branches. The returned list should
be sorted starting with the least element.
:param callback: a function that is called to get the desired list
of branches.
"""
self.get_greater_branches = callback
def get_other_branches(self):
"""Return all the other branches in this set.
The returned list will be ordered, and will not contain this
branch.
:return: a list of all the other branches in this set (if any).
"""
return self.get_lesser_branches() + self.get_greater_branches()
def tag_name(self, version: Version, vendor: Optional[str]) -> str:
"""Gets the name of the tag that is used for the version.
:param version: the Version object that the tag should refer to.
:return: a String with the name of the tag.
"""
if vendor is not None and version.debian_revision:
return '{}/{}'.format(vendor, mangle_version(self.branch, version))
else:
return mangle_version(self.branch, version)
def has_version(
self, version: Version, md5: Optional[str] = None,
vendor: Optional[str] = None) -> bool:
"""Whether this branch contains the package version specified.
The version must be judged present by having the appropriate tag
in the branch. If the md5 argument is not None then the string
passed must the the md5sum that is associated with the revision
pointed to by the tag.
:param version: a Version object to look for in this branch.
:param md5: a string with the md5sum that if not None must be
associated with the revision.
:return: True if this branch contains the specified version of the
package. False otherwise.
"""
for tag in self.possible_tags(version):
if branch_has_debian_version(self.branch, tag, md5=md5):
return True
return False
def contained_versions(
self,
versions: list[Version]) -> tuple[list[Version], list[Version]]:
"""Splits a list of versions depending on presence in the branch.
Partitions the input list of versions depending on whether they
are present in the branch or not.
The two output lists will be sorted in the same order as the input
list.
:param versions: a list of Version objects to look for in the
branch. May be an empty list.
:return: A tuple of two lists. The first list is the list of those
items from the input list that are present in the branch. The
second list is the list of those items from the input list that
are not present in the branch. The two lists will be disjoint
and cover the input list. Either list may be empty, or both if
the input list is empty.
"""
# FIXME: should probably do an ancestory check to find all
# merged revisions. This will avoid adding an extra parent when say
# experimental 1-1~rc1
# unstable 1-1 1-1~rc1
# Ubuntu 1-1ubuntu1 1-1 1-1~rc1
# where only the first in each list is actually uploaded.
contained = []
not_contained = []
for version in versions:
if self.has_version(version):
contained.append(version)
else:
not_contained.append(version)
return contained, not_contained
def missing_versions(self, versions: list[Version]) -> list[Version]:
"""Returns the versions from the list that the branch does not have.
Looks at all the versions specified and returns a list of the ones
that are earlier in the list that the last version that is
contained in this branch.
:param versions: a list of Version objects to look for in the branch.
May be an empty list.
:return: The subset of versions from the list that are not present
in this branch. May be an empty list.
"""
last_contained = self.last_contained_version(versions)
if last_contained is None:
return versions
index = versions.index(last_contained)
return versions[:index]
def last_contained_version(
self, versions: list[Version]) -> Optional[Version]:
"""Returns the highest version from the list present in this branch.
It assumes that the input list of versions is sorted with the
highest version first.
:param versions: a list of Version objects to look for in the branch.
Must be sorted with the highest version first. May be an empty
list.
:return: the highest version that is contained in this branch, or
None if none of the versions are contained within the branch.
"""
for version in versions:
if self.has_version(version):
return version
return None
def possible_tags(self, version: Version, vendor: Optional[str] = None):
if vendor:
if version.debian_revision:
yield from ["{}-{}".format(vendor, version),
"{}/{}".format(vendor, version)]
else:
yield str(version)
else:
version = mangle_version(self.branch, version)
yield str(version)
yield from ["debian-%s" % version, "debian/%s" % version]
yield from ["ubuntu-%s" % version, "ubuntu/%s" % version]
yield from ["v%s" % version]
def revid_of_version(self, version: Version) -> RevisionID:
"""Returns the revision id corresponding to that version.
:param version: the Version object that you wish to retrieve the
revision id of. The Version must be present in the branch.
:return: the revision id corresponding to that version
"""
tag = self.tag_of_version(version)
if tag is None:
raise NoSuchTag(version)
return self.branch.tags.lookup_tag(tag)
def tag_of_version(self, version: Version,
vendor: Optional[str] = None) -> RevisionID:
"""Returns the revision id corresponding to that version.
:param version: the Version object that you wish to retrieve the
revision id of. The Version must be present in the branch.
:return: the tag corresponding to that version
"""
for tag in self.possible_tags(version, vendor):
if branch_has_debian_version(self.branch, tag):
return tag
return None
def tag_version(self, version: Version, revid: Optional[RevisionID] = None,
vendor: Optional[str] = None) -> str:
"""Tags the branch's last revision with the given version.
Sets a tag on the last revision of the branch with a tag that refers
to the version provided.
:param version: the Version object to derive the tag name from.
:param revid: the revid to associate the tag with, or None for the
tip of self.branch.
:return: Name of the tag set
"""
tag_name = self.tag_name(version, vendor)
if revid is None:
revid = self.branch.last_revision()
self.branch.tags.set_tag(tag_name, revid)
return tag_name
def is_version_native(self, version: Version) -> bool:
"""Determines whether the given version is native.
:param version: the Version object to test. Must be present in
the branch.
:return: True if the version is was recorded as native when
imported, False otherwise.
"""
revid = self.revid_of_version(version)
rev_tree = self.branch.repository.revision_tree(revid)
(config_fileid, config_path,
current_config) = _default_config_for_tree(rev_tree)
rev = self.branch.repository.get_revision(revid)
try:
prop = rev.properties["deb-native"]
return prop == "True"
except KeyError:
return False
def can_pull_from_branch(self, branch, version, md5):
if not branch.has_version(version, md5=md5):
return False
# Check that they haven't diverged
with branch.branch.lock_read():
graph = branch.branch.repository.get_graph(
self.branch.repository)
return graph.is_ancestor(
self.branch.last_revision(), branch.revid_of_version(version))
def branch_to_pull_version_from(self, version, md5):
"""Checks whether this upload is a pull from a lesser branch.
Looks in all the lesser branches for the given version/md5 pair
in a branch that has not diverged from this.
If it is present in another branch that has not diverged this
method will return the greatest branch that it is present in,
otherwise it will return None. If it returns a branch then it
indicates that a pull should be done from that branch, rather
than importing the version as a new revision in this branch.
:param version: the Version object to look for in the lesser
branches.
:param md5: a String containing the md5 associateed with the
version.
:return: a DistributionBranch object to pull from if that is
what should be done, otherwise None.
"""
assert md5 is not None, \
("It's not a good idea to use branch_to_pull_version_from with "
"md5 == None, as you may pull the wrong revision.")
with self.branch.lock_read():
for branch in reversed(self.get_lesser_branches()):
if self.can_pull_from_branch(branch, version, md5):
return branch
for branch in self.get_greater_branches():
if self.can_pull_from_branch(branch, version, md5):
return branch
return None
def can_pull_upstream_from_branch(self, branch, package, version,
upstream_tarballs=None):
"""Check if a version can be pulled from another branch into this one.
:param branch: Branch with upstream version
:param package: Package name
:param version: Package version
:param upstream_tarballs: Required upstream tarballs (optional)
"""
if not branch.pristine_upstream_source.has_version(
package, version, tarballs=upstream_tarballs):
return False
up_branch = self.pristine_upstream_branch
with up_branch.lock_read():
# Check that they haven't diverged
other_up_branch = branch.pristine_upstream_branch
with other_up_branch.lock_read():
graph = other_up_branch.repository.get_graph(
up_branch.repository)
pristine_upstream_revids = branch.pristine_upstream_source\
.version_as_revisions(
package, version, tarballs=upstream_tarballs)
for (pristine_upstream_revid, pristine_upstream_subpath) in (
pristine_upstream_revids.values()):
if not graph.is_ancestor(
up_branch.last_revision(), pristine_upstream_revid):
return False
return True
def branch_to_pull_upstream_from(self, package, version,
upstream_tarballs):
"""Checks whether this upstream is a pull from a lesser branch.
Looks in all the other upstream branches for the given
version/md5 pair in a branch that has not diverged from this.
If it is present in a lower branch this method will return the
greatest branch that it is present in that has not diverged,
otherwise it will return None. If it returns a branch then it
indicates that a pull should be done from that branch, rather
than importing the upstream as a new revision in this branch.
:param version: the upstream version to use when searching in the
lesser branches.
:return: a DistributionBranch object to pull the upstream from
if that is what should be done, otherwise None.
"""
assert isinstance(version, str)
for branch in reversed(self.get_lesser_branches()):
if self.can_pull_upstream_from_branch(
branch, package, version, upstream_tarballs):
return branch
for branch in self.get_greater_branches():
if self.can_pull_upstream_from_branch(
branch, package, version, upstream_tarballs):
return branch
return None
def get_parents(self, versions: list[Version]):
"""Return the list of parents for a specific version.
This method returns the list of revision ids that should be parents
for importing a specific package version. The specific package version
is the first element of the list of versions passed.
The parents are determined by looking at the other versions in the
passed list and examining which of the branches (if any) they are
already present in.
You should probably use get_parents_with_upstream rather than
this method.
:param versions: a list of Version objects, the first item of
which is the version of the package that is currently being
imported.
:return: a list of tuples of (DistributionBranch, version,
revision id). The revision ids should all be parents of the
revision that imports the specified version of the package.
The versions are the versions that correspond to that revision
id. The DistributionBranch is the branch that contains that
version.
"""
assert len(versions) > 0, "Need a version to import"
mutter("Getting parents of %s", versions)
missing_versions = self.missing_versions(versions)
mutter("Versions we don't have are %s", missing_versions)
last_contained_version = self.last_contained_version(versions)
parents = []
if last_contained_version is not None:
assert last_contained_version != versions[0], \
"Reupload of a version?"
mutter("The last versions we do have is %s",
str(last_contained_version))
parents = [
(self, last_contained_version,
self.revid_of_version(last_contained_version),
"")]
else:
mutter("We don't have any of those versions")
for branch in (list(reversed(self.get_lesser_branches()))
+ self.get_greater_branches()):
merged, missing_versions = \
branch.contained_versions(missing_versions)
if merged:
revid = branch.revid_of_version(merged[0])
parents.append((branch, merged[0], revid, ""))
mutter("Adding merge from related branch of %s for version %s",
revid, str(merged[0]))
# FIXME: should this really be here?
self._fetch_from_branch(branch, revid)
return parents
def pull_upstream_from_branch(self, pull_branch, package, version):
"""Pulls an upstream version from a branch.
Given a DistributionBranch and a version number this method
will pull the upstream part of the given version from the
branch in to this. The upstream version must be present
in the DistributionBranch, and it is assumed that the md5
matches.
It sets the necessary tags so that the pulled version is
recognised as being part of this branch.
:param pull_branch: the DistributionBranch to pull from.
:param version: the upstream version string
"""
assert isinstance(version, str)
pull_revisions = (
pull_branch.pristine_upstream_source.version_as_revisions(
package, version))
for (component,
(pull_revision, _pull_subpath)) in pull_revisions.items():
mutter("Fetching upstream part %s of %s from revision %s",
component, version, pull_revision)
assert self.pristine_upstream_tree is not None, \
"Can't pull upstream with no tree"
self.pristine_upstream_branch.pull(
pull_branch.pristine_upstream_branch,
stop_revision=pull_revision)
self.pristine_upstream_source.tag_version(version, pull_revision)
self.branch.fetch(self.pristine_upstream_branch, pull_revision)
self.pristine_upstream_branch.tags.merge_to(self.branch.tags)
checkout_upstream_version(
self.pristine_upstream_tree, package, version, pull_revisions)
def pull_version_from_branch(self, pull_branch, package, version,
native=False):
"""Pull a version from a particular branch.
Given a DistributionBranch and a version number this method
will pull the given version from the branch in to this. The
version must be present in the DistributionBranch, and it
is assumed that the md5 matches.
It will also pull in any upstream part that is needed to
the upstream branch. It is assumed that the md5 matches
here as well. If the upstream version must be present in
at least one of the upstream branches.
It sets the necessary tags on the revisions so they are
recongnised in this branch as well.
:param pull_branch: the DistributionBranch to pull from.
:param version: the Version to pull.
:param native: whether it is a native version that is being
imported.
"""
pull_revision = pull_branch.revid_of_version(version)
mutter("already has version %s so pulling from revision %s",
str(version), pull_revision)
assert self.tree is not None, "Can't pull branch with no tree"
self.tree.pull(pull_branch.branch, stop_revision=pull_revision)
tag_name = self.tag_version(version, revid=pull_revision)
if not native and not self.pristine_upstream_source.has_version(
package, version.upstream_version):
if pull_branch.pristine_upstream_source.has_version(
package, version.upstream_version):
self.pull_upstream_from_branch(
pull_branch, package, version.upstream_version)
else:
raise AssertionError(
"Can't find the needed upstream part for version %s" %
version)
if (native
and self.pristine_upstream_branch.last_revision()
== NULL_REVISION
and pull_branch.pristine_upstream_branch.last_revision()
!= NULL_REVISION):
# in case the package wasn't native before then we pull
# the upstream. These checks may be a bit restrictive.
self.pristine_upstream_tree.pull(
pull_branch.pristine_upstream_branch)
pull_branch.pristine_upstream_branch.tags.merge_to(
self.pristine_upstream_branch.tags)
elif native:
mutter("Not checking for upstream as it is a native package")
else:
mutter("Not importing the upstream part as it is already "
"present in the upstream branch")
return tag_name
def get_parents_with_upstream(self, package, version, versions,
tarballs, force_upstream_parent=False):
"""Get the list of parents including any upstream parents.
Further to get_parents this method includes any upstream parents
that are needed. An upstream parent is needed if none of
the other parents include the upstream version. The needed
upstream must already present in the upstream branch before
calling this method.
If force_upstream_parent is True then the upstream parent will
be included, even if another parent is already using that
upstream. This is for use in cases where the .orig.tar.gz
is different in two distributions.
:param version: the Version that we are currently importing.
:param versions: the list of Versions that are ancestors of
version, including version itself. Sorted with the latest
versions first, so version must be the first entry.
:param force_upstream_parent: if True then an upstream parent
will be added as the first parent, regardless of what the
other parents are.
:return: a list of revision ids that should be the parents when
importing the specified revision.
"""
assert version == versions[0], \
"version is not the first entry of versions"
parents = self.get_parents(versions)
need_upstream_parent = True
if not force_upstream_parent:
for parent_pair in parents:
if parent_pair[1].upstream_version == version.upstream_version:
need_upstream_parent = False
break
real_parents = [(p[2], p[3]) for p in parents]
if need_upstream_parent:
upstream_revids = self.pristine_upstream_source\
.version_as_revisions(
package, version.upstream_version, tarballs)
def key(a):
if a is None:
return ""
return a
for component in sorted(upstream_revids.keys(), key=key):
if len(real_parents) > 0:
real_parents.insert(1, upstream_revids[component])
else:
real_parents = [upstream_revids[component]]
return real_parents
def _fetch_upstream_to_branch(self, imported_revids):
"""Fetch the revision from the upstream branch in to the packaging one.
"""
# Make sure we see any revisions added by the upstream branch
# since self.tree was locked.
self.branch.repository.refresh_data()
for (component, tag, revid, pristine_tar_imported,
subpath) in imported_revids:
self.branch.fetch(self.pristine_upstream_branch, revid)
self.pristine_upstream_branch.tags.merge_to(self.branch.tags)
def import_upstream(self, upstream_part: str, package: str, version: str,
upstream_parents, upstream_tarballs,
upstream_branch=None, upstream_revisions=None,
timestamp=None, author=None, file_ids_from=None,
force_pristine_tar=False, committer=None,
files_excluded=None):
"""Import an upstream part on to the upstream branch.
This imports the upstream part of the code and places it on to
the upstream branch, setting the necessary tags.
:param upstream_part: the path of a directory containing the
unpacked upstream part of the source package.
:param version: upstream version that is being imported
:param upstream_parents: the parents to give the upstream revision
:param timestamp: a tuple of (timestamp, timezone) to use for
the commit, or None to use the current time.
:return: list with
(component, tag, revid, pristine_tar_imported, subpath) tuples
"""
# Should we just dump the upstream part on whatever is currently
# there, or try and pull all of the other upstream versions
# from lesser branches first? For now we'll just dump it on.
# TODO: this method needs a lot of work for when we will make
# the branches writeable by others.
mutter("Importing upstream version %s from %s with parents %r",
version, upstream_part, upstream_parents)
assert self.pristine_upstream_tree is not None, \
"Can't import upstream with no tree"
other_branches = self.get_other_branches()
ret = []
for (tarball, component, md5) in upstream_tarballs:
parents = upstream_parents.get(component, [])
if upstream_revisions is not None:
revid, subpath = upstream_revisions[component]
else:
revid = None
subpath = ""
if subpath:
raise Exception('subpaths are not yet supported')
upstream_trees = [
o.pristine_upstream_branch.basis_tree()
for o in other_branches]
target_tree = None
if upstream_branch is not None:
if revid is None:
# FIXME: This is wrong for component tarballs
revid = upstream_branch.last_revision()
try:
self.pristine_upstream_branch.fetch(
upstream_branch, revid)
except NoRoundtrippingSupport:
fetch_result = self.pristine_upstream_branch.fetch(
upstream_branch, revid, lossy=True)
revid = fetch_result.revidmap[revid]
upstream_branch.tags.merge_to(
self.pristine_upstream_branch.tags)
parents.append((revid, ""))
target_tree = (
self.pristine_upstream_branch.repository.revision_tree(
revid))
if file_ids_from is not None:
upstream_trees = file_ids_from + upstream_trees
if self.tree:
self_tree = self.tree
self_tree.lock_write()
else:
self_tree = self.branch.basis_tree()
self_tree.lock_read()
if len(parents) > 0:
parent_revid, parent_subpath = parents[0]
else:
parent_revid = NULL_REVISION
parent_subpath = None
if parent_subpath:
raise Exception('subpaths are not supported yet')
self.pristine_upstream_tree.pull(
self.pristine_upstream_tree.branch,
overwrite=True, stop_revision=parent_revid)
if component is None:
path = upstream_part
else:
path = os.path.join(upstream_part, component)
try:
import_dir(
self.pristine_upstream_tree, path,
file_ids_from=[self_tree] + upstream_trees,
target_tree=target_tree)
finally:
self_tree.unlock()
if component is None:
exclude = [
tb[1] for tb in upstream_tarballs if tb[1] is not None]
else:
exclude = []
(tag, revid, pristine_tar_imported) = (
self.pristine_upstream_source.import_component_tarball(
package, version, self.pristine_upstream_tree, parents,
component, md5, tarball, author=author,
timestamp=timestamp, exclude=exclude,
force_pristine_tar=force_pristine_tar,
committer=committer, files_excluded=files_excluded,
reuse_existing=True))
self.pristine_upstream_branch.generate_revision_history(revid)
ret.append((component, tag, revid, pristine_tar_imported, subpath))
self.branch.fetch(self.pristine_upstream_branch)
self.branch.tags.set_tag(tag, revid)
return ret
def import_upstream_tarballs(self, tarballs, package, version, parents,
upstream_branch=None, upstream_revisions=None,
force_pristine_tar=False, committer=None,
files_excluded=None):
"""Import an upstream part to the upstream branch.
Args:
tarballs: List of tarballs / components to extract
version: The upstream version to import.
parents: The tarball-branch parents to use for the import.
If an upstream branch is supplied, its automatically added to
parents.
upstream_branch: An upstream branch to associate with the
tarball.
upstream_revisions: Upstream revision ids dictionary
md5sum: hex digest of the md5sum of the tarball, if known.
Returns:
list with (component, tag, revid, pristine_tar_imported, subpath)
tuples
"""
with _extract_tarballs_to_tempdir(tarballs) as tarball_dir:
return self.import_upstream(
tarball_dir, package, version, parents, tarballs,
upstream_branch=upstream_branch,
upstream_revisions=upstream_revisions,
force_pristine_tar=force_pristine_tar,
committer=committer, files_excluded=files_excluded)
def import_debian(self, debian_part, version, parents, md5, *,
native: bool = False, timestamp=None,
file_ids_from: Optional[Tree] = None):
"""Import the debian part of a source package.
:param debian_part: the path of a directory containing the unpacked
source package.
:param version: the Version of the source package.
:param parents: a list of revision ids that should be the
parents of the imported revision.
:param md5: the md5 sum reported by the .dsc for
the .diff.gz part of this source package.
:param native: whether the package is native.
:param timestamp: a tuple of (timestamp, timezone) to use for
the commit, or None to use the current values.
"""
mutter("Importing debian part for version %s from %s, with parents "
"%s", str(version), debian_part, str(parents))
assert self.tree is not None, "Can't import with no tree"
# First we move the branch to the first parent
if parents:
if self.branch.last_revision() == NULL_REVISION:
parent_revid, parent_subpath = parents[0]
if parent_subpath:
raise Exception('subpaths are not yet supported')
self.tree.pull(
self.tree.branch, overwrite=True,
stop_revision=parent_revid)
elif parents[0][0] != self.branch.last_revision():
mutter("Adding current tip as parent: %s",
self.branch.last_revision())
parents.insert(0, (self.branch.last_revision(), ""))
elif self.branch.last_revision() != NULL_REVISION:
# We were told to import with no parents. That's not
# right, so import with the current parent. Should
# perhaps be fixed in the methods to determine the parents.
mutter("Told to import with no parents. Adding current tip "
"as the single parent")
parents = [(self.branch.last_revision(), "")]
other_branches = self.get_other_branches()
debian_trees = [o.branch.basis_tree() for o in other_branches]
parent_trees = []
if file_ids_from is not None:
parent_trees = file_ids_from[:]
for parent_revid, parent_subpath in parents:
parent_trees.append(
self.branch.repository.revision_tree(parent_revid))
import_dir(
self.tree, debian_part,
file_ids_from=parent_trees + debian_trees)
rules_path = os.path.join(self.tree.basedir, 'debian', 'rules')
if os.path.isfile(rules_path):
os.chmod(rules_path,
(stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP |
stat.S_IROTH | stat.S_IXOTH))
self.tree.set_parent_ids(
[parent_revid for (parent_revid, parent_subpath) in parents])
changelog_path = os.path.join(
self.tree.basedir, 'debian', 'changelog')
if os.path.exists(changelog_path):
changelog = get_changelog_from_source(
self.tree.basedir, max_blocks=1)
message, authors, thanks, bugs = \
get_commit_info_from_changelog(changelog, self.branch)
if message is None:
message = 'Import packaging changes for version %s' % \
(str(version),)
supports_revprops = (
self.tree.branch.repository._format.
supports_custom_revision_properties)
revprops = {}
if authors:
revprops['authors'] = "\n".join(authors)
if supports_revprops:
revprops["deb-md5"] = md5