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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 | # Requirements from Barcelon sprint with Ubuntu Server Team:
# Run with "importer-command <package>"
# Needs access to:
# Launchpad git push to ~usd-import-team, unless --no-push is
# specified
# Local git cache (maybe optional for a prototype).
# Must be idempotent.
# Must pick up any (zero or more) intermediate uploads since the previous
# import. Use Launchpad publishing history perhaps?
# Import new Debian versions automatically
# Verification: second changelog entry must match version tag in debian
# tracking branch.
# Import new Ubuntu versions automatically
# Verification: second changelog entry must match version tag in current
# Ubuntu tracking branch, OR match a newer parent version tag in debian
# tracking branch.
# Import will use the verified parent as the parent commit.
# If verification fails, then import anyway, but log and use an orphan commit.
#
# Results in a set of branches (git heads):
# - One per distribution/series/pocket
# + note in LP web UI that series is called target
# - For a given import, the parent(s) of that commit are:
# + the previous published version in distribution/series/pocket
# + the last imported version from debian/changelog
import argparse
import atexit
import copy
import getpass
import logging
import os
import re
import shutil
from subprocess import CalledProcessError
import sys
import tempfile
from usd.cache import CACHE_PATH
from usd.git_repository import USDGitRepository, lpusip_orphan_tag, lpusip_applied_tag, lpusip_import_tag, dsc_branch_name, pristine_tar_branch_name, lpusip_upstream_tag
from usd.run import decode_binary, run, runq
from usd.source_information import USDSourceInformation, NoPublicationHistoryException, SourceExtractionException
from usd.version import VERSION
try:
pkg = 'python3-pygit2'
import pygit2
except ImportError:
logging.error('Is %s installed?', pkg)
sys.exit(1)
def dsc_to_tree_hash(repo, dsc_path):
'''Convert a dsc file into a git tree in the given repo
repo: USDGitRepository object
dsc_path: string path to dsc file
Returns: tree hash as a hex string
'''
with tempfile.TemporaryDirectory() as temp_dir:
extracted_dir = os.path.join(temp_dir, 'x')
run(['dpkg-source',
'-x',
'--skip-patches',
dsc_path,
extracted_dir,
]
)
if not os.path.exists(extracted_dir):
logging.exception('No source extracted?')
raise SourceExtractionException
index_env = {'GIT_INDEX_FILE': os.path.join(temp_dir, 'index')}
repo.git_run(['--work-tree', extracted_dir, 'add', '-f', '-A'], env=index_env)
cp = repo.git_run(['--work-tree', extracted_dir, 'write-tree'], env=index_env)
import_tree_hash = decode_binary(cp.stdout).strip()
return import_tree_hash
class USDImport:
def __init__(self):
self.parent_overrides = None
def parse_args(self, subparsers=None, base_subparsers=None):
kwargs = dict(description='Update a launchpad git tree based upon '
'the state of the Ubuntu and Debian archives',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
if base_subparsers:
kwargs['parents'] = base_subparsers
if subparsers:
parser = subparsers.add_parser('import', **kwargs)
parser.set_defaults(func=self.main)
else:
parser = argparse.ArgumentParser(**kwargs)
parser.add_argument('package', type=str,
help='Which package to update in the git tree')
parser.add_argument('-o', '--lp-owner', type=str,
help='Which launchpad user\'s tree to update',
default='usd-import-team')
parser.add_argument('-l', '--lp-user', type=str,
help='Specify the Launchpad user to use',
default=getpass.getuser())
parser.add_argument('--dl-cache', type=str,
help=('Cache directory for downloads. Default is to '
'put downloads in <directory>/.git/%s' %
CACHE_PATH
),
default=argparse.SUPPRESS)
parser.add_argument('--no-push', action='store_true',
help='Do not push to the remote')
parser.add_argument('--no-clean', action='store_true',
help='Do not clean the temporary directory')
parser.add_argument('-d', '--directory', type=str,
help='Use git repository at specified location rather '
'than a temporary directory (implies --no-clean). '
'Will create the local repository if needed.',
default=argparse.SUPPRESS
)
parser.add_argument('--patches-applied', action='store_true',
help='Also import patches-applied history '
'the specified source package')
if not subparsers:
return parser.parse_args()
return 'import - %s' % kwargs['description']
def cleanup(self, no_clean, local_dir):
"""Recursively remove local_dir if no_clean is not True"""
if not no_clean:
shutil.rmtree(local_dir)
else:
print('Leaving %s as directed' % local_dir)
def get_changelog_for_commit(self, tree_hash, publish_parent_commit, changelog_parent_commit):
"""Extract changes to debian/changelog in this publish
LP: #1633114 -- favor the changelog parent's diff
"""
raw_clog_entry = b''
if changelog_parent_commit is not None:
cmd = ['diff-tree', '-p', changelog_parent_commit,
tree_hash, '--', 'debian/changelog']
raw_clog_entry = self.local_repo.git_run(cmd).stdout
elif publish_parent_commit is not None:
cmd = ['diff-tree', '-p', publish_parent_commit,
tree_hash, '--', 'debian/changelog']
raw_clog_entry = self.local_repo.git_run(cmd).stdout
changelog_entry = b''
changelog_entry_found = False
i = 0
for line in raw_clog_entry.split(b'\n'):
# skip the header lines
if i < 4:
i += 1
continue
if not line.startswith(b'+'):
# in case there are stray changelog changes, just take
# the first complete section of changelog additions
if changelog_entry_found:
break
continue
line = re.sub(b'^[+]', b'', line)
if not line.startswith(b' '):
continue
if not changelog_entry_found:
changelog_entry = b'\n\nNew changelog entries:\n'
changelog_entry += line + b'\n'
changelog_entry_found = True
return changelog_entry
def _commit_import(self, spi, tree_hash, publish_parent_commit, changelog_parent_commit, upload_parent_commit, unapplied_parent_commit):
"""Commit a tree object into the repository with the specified
parents
The importer algorithm works by 'staging' the import of a
publication as a tree, then using that tree to determine the
appropriate parents from the publishing history and
debian/changelog.
Arguments:
spi - A SourcePackageInformation instance for this publish
tree_hash - SHA1 from git-dsc-commit --tree-only of this publish
publish_parent_commit = SHA1 of publishing parent
changelog_parent_commit = SHA1 of changelog parent
upload_parent_commit = SHA1 of upload parent
unapplied_parent_commit = SHA1 of unapplied-patches import parent
"""
tag = None
if unapplied_parent_commit:
import_type = 'patches-applied'
target_head_name = spi.lpusip_applied_head_name
if self.local_repo.get_applied_tag(spi.version) is None:
# Not imported before
tag = lpusip_applied_tag(spi.version)
else:
import_type = 'patches-unapplied'
target_head_name = spi.lpusip_head_name
if self.local_repo.get_import_tag(spi.version) is None:
# Not imported before
tag = lpusip_import_tag(spi.version)
# Do not show importer/ namespace to user
_, _, pretty_head_name = target_head_name.partition('lpusip/')
changelog_entry = self.get_changelog_for_commit(
tree_hash,
publish_parent_commit,
changelog_parent_commit
)
msg = (b'Import %s version %b to %b\n\nImported using usd-importer.' %
(import_type.encode(), spi.version.encode(), pretty_head_name.encode())
)
parents = []
if publish_parent_commit is None and \
changelog_parent_commit is None and \
upload_parent_commit is None and \
unapplied_parent_commit is None and \
target_head_name in self.local_repo.listall_branches():
# No parents and not creating a new branch
tag = lpusip_orphan_tag(spi.version)
else:
msg += b'\n'
if publish_parent_commit is not None:
parents.append(publish_parent_commit)
msg += b'\nPublish parent: %b' % publish_parent_commit.encode()
if changelog_parent_commit is not None:
parents.append(changelog_parent_commit)
msg += b'\nChangelog parent: %b' % changelog_parent_commit.encode()
if upload_parent_commit is not None:
parents.append('-p', upload_parent_commit)
msg += b'\nUpload parent: %b' % upload_parent_commit.encode()
if unapplied_parent_commit is not None:
parents.append(unapplied_parent_commit)
msg += b'\nUnapplied parent: %b' % unapplied_parent_commit.encode()
msg += b'%b' % changelog_entry
commit_hash = local_repo.commit_tree_hash(
tree_hash,
parents,
msg,
environment_spi=spi
)
self.local_repo.update_head_to_commit(target_head_name, commit_hash)
logging.info('Committed %s import of %s as %s in %s',
import_type, spi.version, commit_hash, pretty_head_name
)
if tag is not None:
# should be annotated to use create_tag API
logging.info('Creating tag %s pointing to %s', tag, commit_hash)
self.local_repo.git_run(['tag', '-a', '-m', 'usd import v%s' % VERSION,
tag, commit_hash
]
)
def commit_unapplied_patches_import(self, spi, import_tree_hash, publish_parent_commit, changelog_parent_commit, upload_parent_commit):
self._commit_import(
spi,
import_tree_hash,
publish_parent_commit,
changelog_parent_commit,
upload_parent_commit,
# unapplied trees do not have a distinct unapplied parent
None
)
def commit_applied_patches_import(self, spi, import_tree_hash, publish_parent_commit, changelog_parent_commit, unapplied_parent_commit):
self._commit_import(
spi,
import_tree_hash,
publish_parent_commit,
changelog_parent_commit,
# uploads will be unapplied trees currently, so applied trees
# can not have them as direct parents
None,
unapplied_parent_commit
)
def import_dsc(self, spi):
"""Imports the dsc file to importer/{debian,ubuntu}/dsc
Arguments:
spi - A USDSourcePackageInformation instance
"""
# Add DSC file to the appropriate branch
runq(['git', 'checkout', dsc_branch_name(spi)], env=self.local_repo.env)
# It is possible the same-named DSC is published multiple times
# with different contents, e.g. on epoch bumps
local_dir = os.path.join(self.local_repo.local_dir, str(spi.version))
os.makedirs(local_dir, exist_ok=True)
shutil.copy(spi.dsc_pathname,
local_dir)
self.local_repo.git_run(['add', self.local_repo.local_dir])
try:
# Anything to commit? (this will be 0 if, for instance, the
# same dsc is being imported again)
runq(['git', 'diff', '--exit-code', 'HEAD'], env=self.local_repo.env)
except:
self.local_repo.git_run(['commit', '-m', 'DSC file for %s' % spi.version])
self.local_repo.clean_repository_state()
def import_orig(self, spi):
"""Imports the orig-tarball using gbp import-org --pristine-tar
Arguments:
spi - A USDSourcePackageInformation instance
"""
# adapted from ubuntutools.SourcePackage.verify_orig
orig_tarball = None
orig_re = re.compile(r'.*\.orig(-[^.]+)?\.tar\.[^.]+$')
for entry in spi.dsc['Files']:
if orig_re.match(entry['name']):
if not orig_tarball:
orig_tarball = entry['name']
else:
logging.exception('Multiple orig tarballs in DSC '
'for version %s, currently '
'unsupported',
spi.version
)
sys.exit(1)
# gbp fails if the tag already exist
if (orig_tarball and
not self.local_repo.get_upstream_tag(spi.upstream_version)
):
# going to be deleted
self.local_repo.git_run(['checkout', 'do-not-push'])
# update the branch name so that gbp is happy
self.local_repo.git_run(['branch', '-M',
pristine_tar_branch_name(spi),
'pristine-tar'
]
)
# gbp does not support running from arbitrary git trees or
# working directories
# https://github.com/agx/git-buildpackage/pull/16
oldcwd = os.getcwd()
os.chdir(self.local_repo.local_dir)
try:
# make this non-fatal if upstream already exist as tagged?
run(['gbp', 'import-orig',
'--no-merge',
'--upstream-branch', 'do-not-push',
'--pristine-tar',
'--no-interactive',
'--upstream-tag=lpusip/upstream/%(version)s',
os.path.join(spi.workdir, orig_tarball)
],
env=self.local_repo.env
)
finally:
os.chdir(oldcwd)
# update the branch name so that the importer is happy
self.local_repo.git_run(['branch', '-M',
'pristine-tar',
pristine_tar_branch_name(spi),
]
)
self.local_repo.clean_repository_state()
def import_patches_unapplied_tree(self, spi):
"""Imports the patches-unapplied source package and writes the
corresponding working tree for a given srcpkg
Arguments:
spi - A USDSourcePackageInformation instance
"""
import_tree_hash = dsc_to_tree_hash(
os.path.join(oldcwd, spi.dsc_pathname)
)
self.local_repo.clean_repository_state()
return import_tree_hash
def import_patches_applied_tree(self, spi):
"""Imports the patches-applied source package and writes the
corresponding working tree for a given srcpkg, patch by patch
Arguments:
spi - A USDSourcePackageInformation instance
"""
oldcwd = os.getcwd()
extract_dir = tempfile.mkdtemp()
os.chdir(extract_dir)
run(['dpkg-source', '-x',
'--skip-patches',
os.path.join(oldcwd, spi.dsc_pathname)
]
)
extracted_dir = None
for path in os.listdir(extract_dir):
if os.path.isdir(path):
extracted_dir = os.path.join(extract_dir, path)
break
if extracted_dir is None:
logging.exception('No source extracted?')
raise SourceExtractionException
if os.path.isdir(os.path.join(extracted_dir, '.pc')):
self.local_repo.git_run(['--work-tree', extracted_dir, 'add', '-f', '-A'])
self.local_repo.git_run(['--work-tree', extracted_dir, 'rm', '.pc'])
cp = self.local_repo.git_run(['--work-tree', extracted_dir, 'write-tree'])
import_tree_hash = decode_binary(cp.stdout).strip()
yield (import_tree_hash, None, 'Remove .pc directory from source package')
try:
try:
cp = run(['dpkg-source', '--print-format', extracted_dir])
fmt = decode_binary(cp.stdout).strip()
if '3.0 (quilt)' not in fmt:
raise StopIteration
except CalledProcessError as e:
try:
with open('debian/source/format', 'r') as f:
for line in f:
if re.match(r'3.0 (.*)', line):
break
else:
raise StopIteration
# `man dpkg-source` indicates no d/s/format implies 1.0
except OSError:
raise StopIteration
while True:
try:
os.chdir(extracted_dir)
run(['quilt', 'push'])
cp = run(['quilt', 'top'])
patch_name = decode_binary(cp.stdout).strip()
cp = run(['quilt', 'header'])
header = decode_binary(cp.stdout).strip()
patch_desc = None
for regex in (r'Subject:\s*(.*?)$',
r'Description:\s*(.*?)$'
):
try:
m = re.search(regex, header, re.MULTILINE)
patch_desc = m.group(1)
except AttributeError:
pass
if patch_desc is None:
logging.info('Unable to find Subject or Description in %s' % patch_name)
self.local_repo.git_run(['--work-tree', extracted_dir, 'add', '-f', '-A'])
self.local_repo.git_run(['--work-tree', extracted_dir, 'reset', 'HEAD', '--', '.pc'])
cp = self.local_repo.git_run(['--work-tree', extracted_dir, 'write-tree'])
import_tree_hash = decode_binary(cp.stdout).strip()
yield (import_tree_hash, patch_name, patch_desc)
except CalledProcessError as e:
# quilt returns 2 when done pushing
if e.returncode != 2:
raise
raise StopIteration
finally:
os.chdir(oldcwd)
shutil.rmtree(extract_dir)
self.local_repo.clean_repository_state()
def parse_parentfile(self, pkgname, parentfile):
"""Extract parent overrides from a file
The parent overrides file specifies parent-child relationship(s),
where the importer may not be able to determine them automatically.
The format of the file is:
<pkgname> <child version> <publish parent version> <changelog parent version>
with one override per line.
<pkgname> is the name of the source package to which this override
applies.
<child version> is the published version to which this override
applies.
The <publish parent version> is the prior version published in the
same series/pocket, or the immediately preceding series/pocket, if
this is the first publish in a series/pocket.
The <changelog parent version> is the prior version in the
child publish's debian/changelog.
Keyword Arguments:
parentfile -- Path to parent overrides file
"""
if self.parent_overrides:
return
parent_overrides = dict()
try:
with open(parentfile) as f:
for line in f:
if line.startswith('#'):
continue
m = re.match(
r'(?P<pkgname>\S*)\s*(?P<childversion>\S*)\s*(?P<parent1version>\S*)\s*(?P<parent2version>\S*)',
line
)
if m is None:
continue
if m.group('pkgname') != pkgname:
continue
parent_overrides[m.group('childversion')] = {
'publish_parent':m.group('parent1version'),
'changelog_parent':m.group('parent2version')
}
except FileNotFoundError:
pass
self.parent_overrides = parent_overrides
def override_parents(self, spi):
unapplied_publish_parent_commit = None
unapplied_changelog_parent_commit = None
applied_publish_parent_commit = None
applied_changelog_parent_commit = None
unapplied_publish_parent_tag = self.local_repo.get_import_tag(
self.parent_overrides[spi.version]['publish_parent']
)
applied_publish_parent_tag = self.local_repo.get_applied_tag(
self.parent_overrides[spi.version]['publish_parent']
)
if unapplied_publish_parent_tag is not None:
# sanity check that version from d/changelog of the
# tagged commit matches ours
parent_publish_version, _ = \
self.local_repo.get_changelog_versions_from_treeish(
str(unapplied_publish_parent_tag.peel().id),
)
if parent_publish_version != self.parent_overrides[spi.version]['publish_parent']:
logging.error('Found a tag corresponding to '
'publish parent override '
'version %s, but d/changelog '
'version (%s) differs. Will '
'orphan commit.' % (
self.parent_overrides[spi.version]['publish_parent'],
parent_publish_version
)
)
else:
unapplied_publish_parent_commit = str(unapplied_publish_parent_tag.peel().id)
if applied_publish_parent_tag is not None:
applied_publish_parent_commit = str(applied_publish_parent_tag.peel().id)
logging.info('Overriding publish parent (tag) to %s',
self.local_repo.tag_to_pretty_name(unapplied_publish_parent_tag)
)
else:
if self.parent_overrides[spi.version]['publish_parent'] == '-':
logging.info('Not setting publish parent as '
'specified in override file.'
)
else:
logging.error('Specified publish parent override '
'(%s) for version (%s) not found in tags. '
'Unable to proceed.' % (
self.parent_overrides[spi.version]['publish_parent'],
spi.version
)
)
sys.exit(1)
unapplied_changelog_parent_tag = self.local_repo.get_import_tag(
self.parent_overrides[spi.version]['changelog_parent']
)
applied_changelog_parent_tag = self.local_repo.get_applied_tag(
self.parent_overrides[spi.version]['changelog_parent']
)
if unapplied_changelog_parent_tag is not None:
# sanity check that version from d/changelog of the
# tagged commit matches ours
parent_changelog_version, _ = \
self.local_repo.get_changelog_versions_from_treeish(
str(unapplied_changelog_parent_tag.peel().id),
)
if parent_changelog_version != self.parent_overrides[spi.version]['changelog_parent']:
logging.error('Found a tag corresponding to '
'changelog parent override '
'version %s, but d/changelog '
'version (%s) differs. Will '
'orphan commit.' % (
self.parent_overrides[spi.version]['changelog_parent'],
parent_changelog_version
)
)
else:
unapplied_changelog_parent_commit = str(unapplied_changelog_parent_tag.peel().id)
if applied_changelog_parent_tag is not None:
applied_changelog_parent_commit = str(applied_changelog_parent_tag.peel().id)
logging.info('Overriding changelog parent (tag) to %s',
self.local_repo.tag_to_pretty_name(unapplied_changelog_parent_tag)
)
else:
if self.parent_overrides[spi.version]['changelog_parent'] == '-':
logging.info('Not setting changelog parent as '
'specified in override file.'
)
else:
logging.error('Specified changelog parent override '
'(%s) for version (%s) not found in tags. '
'Unable to proceed.' % (
self.parent_overrides[spi.version]['changelog_parent'],
spi.version
)
)
sys.exit(1)
return (unapplied_publish_parent_commit,
unapplied_changelog_parent_commit,
applied_publish_parent_commit,
applied_changelog_parent_commit)
# imports a package based upon source package information
def import_spi(self, spi):
"""Imports a source package from Launchpad into the git
repository
Arguments:
spi - a SourcePackageInformation instance
"""
pretty_head_name = spi.pretty_head_name
print('Importing %s to %s' % (spi.version, pretty_head_name))
unapplied_tip_head = self.local_repo.get_head_by_name(spi.lpusip_head_name)
applied_tip_head = self.local_repo.get_head_by_name(spi.lpusip_applied_head_name)
spi.pull()
self.local_repo.clean_repository_state()
self.import_dsc(spi)
try:
self.import_orig(spi)
except:
logging.error('Unable to import orig tarball for %s' %
spi.version
)
unapplied_import_tree_hash = self.import_patches_unapplied_tree(spi)
logging.info('Imported patches-unapplied version %s as tree %s',
spi.version, unapplied_import_tree_hash
)
import_tree_versions = self.local_repo.get_all_changelog_versions_from_treeish(
unapplied_import_tree_hash
)
changelog_version = import_tree_versions[0]
logging.info(
'Found changelog version %s in newly imported tree' %
changelog_version
)
# sanity check on spph and d/changelog agreeing on the latest version
if spi.version not in self.parent_overrides:
assert spi.version == changelog_version, (
'source pkg version: {} != changelog version: {}'.format(
spi.version, changelog_version))
unapplied_tip_version = None
# check if the version to import has already been imported to
# this head
if unapplied_tip_head is not None:
if self.local_repo.treeishs_identical(
unapplied_import_tree_hash, str(unapplied_tip_head.peel().id)
):
logging.warn('%s is identical to %s',
pretty_head_name, spi.version
)
return
unapplied_tip_version, _ = self.local_repo.get_changelog_versions_from_treeish(
str(unapplied_tip_head.peel().id),
)
logging.info('Tip version is %s', unapplied_tip_version)
unapplied_publish_parent_commit = None
unapplied_changelog_parent_commit = None
upload_parent_commit = None
applied_publish_parent_commit = None
applied_changelog_parent_commit = None
unapplied_parent_commit = None
if spi.version in self.parent_overrides:
logging.info('%s is specified in the parent override file.' %
spi.version
)
(unapplied_publish_parent_commit,
unapplied_changelog_parent_commit,
applied_publish_parent_commit,
applied_changelog_parent_commit) = self.override_parents(spi)
else:
# Get parent from publishing history (which is the last
# published version in the corresponding series)
try:
unapplied_publish_parent_commit = str(unapplied_tip_head.peel().id)
except AttributeError:
try:
unapplied_publish_parent_head = self.local_repo.get_head_by_name(spi.lpusip_parent_head_name)
unapplied_publish_parent_commit = str(unapplied_publish_parent_head.peel().id)
except AttributeError:
pass
finally:
try:
unapplied_publish_parent_tag = self.local_repo.nearest_import_tag(unapplied_publish_parent_commit)
logging.info('Publishing parent (tag) is %s',
self.local_repo.tag_to_pretty_name(unapplied_publish_parent_tag)
)
except AttributeError:
pass
try:
applied_publish_parent_commit = str(applied_tip_head.peel().id)
except AttributeError:
try:
applied_publish_parent_head = self.local_repo.get_head_by_name(spi.lpusip_applied_parent_head_name)
applied_publish_parent_head_commit = str(applied_publish_parent_head.peel().id)
except AttributeError:
pass
if self.local_repo.version_compare(str(spi.version), unapplied_tip_version) <= 0:
logging.warn('Version to import (%s) is not after %s tip (%s)',
spi.version, pretty_head_name, unapplied_tip_version
)
# Walk changelog backwards until we find an imported version
for version in import_tree_versions:
unapplied_changelog_parent_tag = self.local_repo.get_import_tag(version)
applied_changelog_parent_tag = self.local_repo.get_applied_tag(version)
if unapplied_changelog_parent_tag is not None:
# sanity check that version from d/changelog of the
# tagged parent matches ours
parent_changelog_version, _ = \
self.local_repo.get_changelog_versions_from_treeish(
str(unapplied_changelog_parent_tag.peel(pygit2.Tree).id),
)
# if the parent was imported via a parent override
# itself, assume it is valid without checking its
# tree's debian/changelog, as it wasn't used
if (version not in self.parent_overrides and
parent_changelog_version != version
):
logging.error('Found a tag corresponding to '
'parent version %s, but d/changelog '
'version (%s) differs. Will '
'orphan commit.' % (
version,
parent_changelog_version
)
)
else:
unapplied_changelog_parent_commit = str(unapplied_changelog_parent_tag.peel().id)
if applied_changelog_parent_tag:
applied_changelog_parent_commit = str(applied_changelog_parent_tag.peel().id)
logging.info('Changelog parent (tag) is %s',
self.local_repo.tag_to_pretty_name(unapplied_changelog_parent_tag)
)
break
# If the two parents are tree-identical, then favor publication
# history. This deals with copy-forwards between series and with
# syncs.
if self.local_repo.treeishs_identical(unapplied_publish_parent_commit, unapplied_changelog_parent_commit):
unapplied_changelog_parent_commit = None
if self.local_repo.treeishs_identical(applied_publish_parent_commit, applied_changelog_parent_commit):
applied_changelog_parent_commit = None
# check if the version to import has already been uploaded to
# this head -- we leverage the above code to perform a 'dry-run'
# of the import tree, to obtain the (up to) two parents
upload_tag = self.local_repo.get_upload_tag(spi.version)
import_tag = self.local_repo.get_import_tag(spi.version)
if upload_tag and not import_tag:
if str(upload_tag.peel().tree.id) != unapplied_import_tree_hash:
logging.warn('Found upload tag %s, '
'but the corresponding tree '
'does not match the published '
'version. Will import %s as '
'normal, ignoring the upload tag.',
self.local_repo.tag_to_pretty_name(upload_tag),
spi.version
)
else:
upload_parent_commit = str(upload_tag.peel().id)
if unapplied_publish_parent_commit is not None:
try:
self.local_repo.git_run(['merge-base', '--is-ancestor',
unapplied_publish_parent_commit,
upload_parent_commit
]
)
unapplied_publish_parent_commit = None
except CalledProcessError as e:
if e.returncode != 1:
raise
if unapplied_changelog_parent_commit is not None:
try:
self.local_repo.git_run(['merge-base', '--is-ancestor',
unapplied_changelog_parent_commit,
upload_parent_commit
]
)
unapplied_changelog_parent_commit = None
except CalledProcessError as e:
if e.returncode != 1:
raise
self.commit_unapplied_patches_import(
spi,
unapplied_import_tree_hash,
unapplied_publish_parent_commit,
unapplied_changelog_parent_commit,
upload_parent_commit,
)
# only import patches-applied publishes if explicitly specified
if not self.patches_applied:
return
# Could also obtain this from the above
unapplied_parent_commit = str(self.local_repo.get_import_tag(spi.version).peel().id)
# Assume no patches to apply
applied_import_tree_hash = unapplied_import_tree_hash
for (applied_import_tree_hash, patch_name, patch_desc) in self.import_patches_applied_tree(spi):
# special case for .pc removal
if patch_name is None:
msg = b'%b.' % (patch_desc.encode())
else:
if patch_desc is None:
patch_desc = '%s\n\nNo DEP3 Subject or Description header found' % patch_name
msg = b'%b\n\nGbp-Pq: %b.' % (patch_desc.encode(), patch_name.encode())
commit_tree = ['git', 'commit-tree', applied_import_tree_hash,
'-p', unapplied_parent_commit
]
commit_env = copy.copy(self.local_repo.env)
commit_env.update(
self.local_repo.get_commit_environment(applied_import_tree_hash, spi)
)
with tempfile.NamedTemporaryFile() as fp:
fp.write(msg)
fp.flush()
commit_tree += ['-F', fp.name]
cp = run(commit_tree, env=commit_env)
unapplied_parent_commit = decode_binary(cp.stdout).strip()
logging.info('Committed patch-application of %s as %s',
patch_name, unapplied_parent_commit
)
self.commit_applied_patches_import(
spi,
applied_import_tree_hash,
applied_publish_parent_commit,
applied_changelog_parent_commit,
unapplied_parent_commit
)
def main(self, args):
pkgname = args.package
owner = args.lp_owner
user = args.lp_user
no_clean = args.no_clean
try:
directory = args.directory
no_clean = True
except AttributeError:
directory = None
no_push = args.no_push
try:
dl_cache = args.dl_cache
except AttributeError:
dl_cache = None
self.patches_applied = args.patches_applied
print('Ubuntu Server Team importer v%s' % VERSION)
self.local_repo = USDGitRepository(directory,
args.proto
)
self.local_repo.ensure_importer_branches_exist()
atexit.register(self.cleanup, no_clean, self.local_repo.local_dir)
if owner != 'usd-import-team':
self.local_repo.add_and_fetch_remote(pkgname, owner, owner, user)
else:
self.local_repo.add_and_fetch_lpusip(pkgname, user)
debian_source_information = USDSourceInformation(
'debian',
pkgname,
os.path.abspath(args.pullfile),
args.retries,
args.retry_backoffs
)
ubuntu_source_information = USDSourceInformation(
'ubuntu',
pkgname,
os.path.abspath(args.pullfile),
args.retries,
args.retry_backoffs
)
debian_head_versions = self.local_repo.get_imported_heads_and_versions('debian')
for head_name in sorted(debian_head_versions):
_, _, pretty_head_name = head_name.partition('lpusip/')
logging.info('Last imported %s version is %s',
pretty_head_name,
debian_head_versions[head_name]['version']
)
ubuntu_head_versions = self.local_repo.get_imported_heads_and_versions('ubuntu')
for head_name in sorted(ubuntu_head_versions):
_, _, pretty_head_name = head_name.partition('lpusip/')
logging.info('Last imported %s version is %s',
pretty_head_name,
ubuntu_head_versions[head_name]['version']
)
oldcwd = os.getcwd()
os.chdir(self.local_repo.local_dir)
if dl_cache is None:
workdir = os.path.join(self.local_repo.git_dir, CACHE_PATH)
else:
workdir = dl_cache
os.makedirs(workdir, exist_ok=True)
self.parse_parentfile(pkgname, args.parentfile)
history_found = []
for distname, versions, dist_sinfo in (
("debian", debian_head_versions, debian_source_information),
("ubuntu", ubuntu_head_versions, ubuntu_source_information)):
try:
for srcpkg_information in \
dist_sinfo.launchpad_versions_published_after(versions,
workdir=workdir):
self.import_spi(srcpkg_information)
history_found.append(distname)
except NoPublicationHistoryException:
logging.warning("No publication history found for %s in %s. ",
pkgname, distname)
if len(history_found) == 0:
logging.error("No publication history for '%s' in debian or ubuntu."
" Wrong source package name?", pkgname)
sys.exit(1)
devel_hash = None
devel_name = None
ubuntu_head_versions = self.local_repo.get_imported_heads_and_versions('ubuntu')
for rel in ubuntu_source_information.active_series_list:
series_devel_hash = None
series_devel_name = None
series_devel_version = None
# Find highest version publish in these pockets
for suff in ("-proposed", "-updates", "-security", ""):
name = "lpusip/ubuntu/%s%s" % (rel, suff)
if name in ubuntu_head_versions and \
self.local_repo.version_compare(
ubuntu_head_versions[name]['version'],
series_devel_version
) > 0:
series_devel_name = name
series_devel_version = ubuntu_head_versions[name]['version']
series_devel_hash = ubuntu_head_versions[name]['head'].peel().id
if series_devel_hash:
if not devel_hash:
devel_hash = series_devel_hash
devel_name = series_devel_name
logging.debug("Setting lpusip/ubuntu/%s-devel branch to '%s' (%s)",
rel, series_devel_name, series_devel_hash)
self.local_repo.update_head_to_commit("lpusip/ubuntu/%s-devel"
% rel, series_devel_hash)
if devel_hash is None:
logging.warn("Package '%s' does not appear to have been published "
"in Ubuntu. No lpusip/ubuntu/devel branch created.",
pkgname)
else:
logging.debug("Setting lpusip/ubuntu/devel branch to '%s' (%s)",
devel_name, devel_hash)
self.local_repo.update_head_to_commit("lpusip/ubuntu/devel", devel_hash)
os.chdir(oldcwd)
self.local_repo.garbage_collect()
if no_push:
print('Not pushing to remote as specified')
else:
if owner != 'usd-import-team':
self.local_repo.push(owner)
else:
self.local_repo.push_lpusip()
|